1818
1919import java .net .URI ;
2020
21- import javax .ws .rs .ApplicationPath ;
2221import javax .ws .rs .GET ;
2322import javax .ws .rs .Path ;
2423import javax .ws .rs .PathParam ;
2524
2625import io .micrometer .core .instrument .MeterRegistry ;
26+ import io .micrometer .core .instrument .Tag ;
2727import io .micrometer .core .instrument .Timer ;
28+ import io .micrometer .jersey2 .server .DefaultJerseyTagsProvider ;
29+ import io .micrometer .jersey2 .server .JerseyTagsProvider ;
2830import io .micrometer .jersey2 .server .MetricsApplicationEventListener ;
2931import org .glassfish .jersey .server .ResourceConfig ;
32+ import org .glassfish .jersey .server .monitoring .RequestEvent ;
3033import org .junit .Test ;
3134
3235import org .springframework .boot .actuate .autoconfigure .metrics .MetricsAutoConfiguration ;
3336import org .springframework .boot .actuate .autoconfigure .metrics .export .simple .SimpleMetricsExportAutoConfiguration ;
37+ import org .springframework .boot .actuate .autoconfigure .metrics .test .MetricsRun ;
3438import org .springframework .boot .autoconfigure .AutoConfigurations ;
3539import org .springframework .boot .autoconfigure .jersey .JerseyAutoConfiguration ;
3640import org .springframework .boot .autoconfigure .jersey .ResourceConfigCustomizer ;
3741import org .springframework .boot .autoconfigure .web .servlet .ServletWebServerFactoryAutoConfiguration ;
3842import org .springframework .boot .test .context .FilteredClassLoader ;
3943import org .springframework .boot .test .context .assertj .AssertableWebApplicationContext ;
44+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
4045import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
4146import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
4247import org .springframework .context .annotation .Bean ;
43- import org .springframework .context .annotation .Configuration ;
4448import org .springframework .web .client .RestTemplate ;
4549
4650import static org .assertj .core .api .Assertions .assertThat ;
4953 * Tests for {@link JerseyServerMetricsAutoConfiguration}.
5054 *
5155 * @author Michael Weirauch
56+ * @author Michael Simons
5257 */
5358public class JerseyServerMetricsAutoConfigurationTests {
5459
55- private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner (
60+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
61+ .with (MetricsRun .simple ()).withConfiguration (
62+ AutoConfigurations .of (JerseyServerMetricsAutoConfiguration .class ));
63+
64+ private final WebApplicationContextRunner webContextRunner = new WebApplicationContextRunner (
5665 AnnotationConfigServletWebServerApplicationContext ::new )
5766 .withConfiguration (
5867 AutoConfigurations .of (JerseyAutoConfiguration .class ,
@@ -63,9 +72,30 @@ public class JerseyServerMetricsAutoConfigurationTests {
6372 .withUserConfiguration (ResourceConfiguration .class )
6473 .withPropertyValues ("server.port:0" );
6574
75+ @ Test
76+ public void shouldOnlyBeActiveInWebApplicationContext () {
77+ this .contextRunner .run ((context ) -> assertThat (context )
78+ .doesNotHaveBean (ResourceConfigCustomizer .class ));
79+ }
80+
81+ @ Test
82+ public void shouldProvideAllNecessaryBeans () {
83+ this .webContextRunner .run ((context ) -> assertThat (context )
84+ .hasSingleBean (DefaultJerseyTagsProvider .class )
85+ .hasSingleBean (ResourceConfigCustomizer .class ));
86+ }
87+
88+ @ Test
89+ public void shouldHonorExistingTagProvider () {
90+ this .webContextRunner
91+ .withUserConfiguration (CustomJerseyTagsProviderConfiguration .class )
92+ .run ((context ) -> assertThat (context )
93+ .hasSingleBean (CustomJerseyTagsProvider .class ));
94+ }
95+
6696 @ Test
6797 public void httpRequestsAreTimed () {
68- this .contextRunner .run ((context ) -> {
98+ this .webContextRunner .run ((context ) -> {
6999 doRequest (context );
70100
71101 MeterRegistry registry = context .getBean (MeterRegistry .class );
@@ -77,7 +107,7 @@ public void httpRequestsAreTimed() {
77107
78108 @ Test
79109 public void noHttpRequestsTimedWhenJerseyInstrumentationMissingFromClasspath () {
80- this .contextRunner
110+ this .webContextRunner
81111 .withClassLoader (
82112 new FilteredClassLoader (MetricsApplicationEventListener .class ))
83113 .run ((context ) -> {
@@ -98,18 +128,11 @@ private static void doRequest(AssertableWebApplicationContext context) {
98128 String .class );
99129 }
100130
101- @ Configuration
102- @ ApplicationPath ("/" )
103131 static class ResourceConfiguration {
104132
105133 @ Bean
106134 ResourceConfig resourceConfig () {
107- return new ResourceConfig ();
108- }
109-
110- @ Bean
111- ResourceConfigCustomizer resourceConfigCustomizer () {
112- return (config ) -> config .register (new TestResource ());
135+ return new ResourceConfig ().register (new TestResource ());
113136 }
114137
115138 @ Path ("/users" )
@@ -125,4 +148,27 @@ public String getUser(@PathParam("id") String id) {
125148
126149 }
127150
151+ static class CustomJerseyTagsProviderConfiguration {
152+
153+ @ Bean
154+ JerseyTagsProvider customJerseyTagsProvider () {
155+ return new CustomJerseyTagsProvider ();
156+ }
157+
158+ }
159+
160+ static class CustomJerseyTagsProvider implements JerseyTagsProvider {
161+
162+ @ Override
163+ public Iterable <Tag > httpRequestTags (RequestEvent event ) {
164+ return null ;
165+ }
166+
167+ @ Override
168+ public Iterable <Tag > httpLongRequestTags (RequestEvent event ) {
169+ return null ;
170+ }
171+
172+ }
173+
128174}
0 commit comments