11/*
22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33 *
4- * Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
4+ * Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
55 *
66 * The contents of this file are subject to the terms of either the GNU
77 * General Public License Version 2 only ("GPL") or the Common Development
4444
4545import javax .ws .rs .GET ;
4646import javax .ws .rs .Path ;
47+ import javax .ws .rs .Produces ;
4748import javax .ws .rs .core .Application ;
4849import javax .ws .rs .core .Context ;
50+ import javax .ws .rs .core .MediaType ;
4951import javax .ws .rs .core .Response ;
5052
5153import javax .inject .Provider ;
5254
5355import org .glassfish .jersey .server .ResourceConfig ;
5456import org .glassfish .jersey .server .ServerProperties ;
57+ import org .glassfish .jersey .server .model .Resource ;
5558import org .glassfish .jersey .server .model .ResourceMethod ;
5659import org .glassfish .jersey .server .monitoring .MonitoringStatistics ;
5760import org .glassfish .jersey .server .monitoring .ResourceMethodStatistics ;
5861import org .glassfish .jersey .server .monitoring .ResourceStatistics ;
62+ import org .glassfish .jersey .server .wadl .processor .WadlModelProcessor ;
5963import org .glassfish .jersey .test .JerseyTest ;
6064
6165import org .junit .Assert ;
66+ import org .junit .Ignore ;
6267import org .junit .Test ;
6368
6469/**
6873 * {@link ResourceMethodStatistics}.
6974 *
7075 * @author Miroslav Fuksa (miroslav.fuksa at oracle.com)
76+ * @author Libor Kramolis (libor.kramolis at oracle.com)
7177 */
7278public class MonitoringStatisticsLocatorTest extends JerseyTest {
7379
7480 @ Override
7581 protected Application configure () {
76- final ResourceConfig resourceConfig = new ResourceConfig (StatisticsResource .class );
82+ final ResourceConfig resourceConfig = new ResourceConfig (StatisticsResource .class , AnotherResource . class );
7783 resourceConfig .property (ServerProperties .MONITORING_STATISTICS_ENABLED , true );
7884 resourceConfig .property (ServerProperties .APPLICATION_NAME , "testApp" );
7985 return resourceConfig ;
@@ -116,7 +122,6 @@ public String getUriStats() throws InterruptedException {
116122
117123 String resp = "" ;
118124
119-
120125 for (Map .Entry <ResourceMethod , ResourceMethodStatistics > entry
121126 : resourceStatistics .getResourceMethodStatistics ().entrySet ()) {
122127 if (entry .getKey ().getHttpMethod ().equals ("GET" )) {
@@ -131,6 +136,132 @@ public String getUriStats() throws InterruptedException {
131136 public SubResource locator () {
132137 return new SubResource ();
133138 }
139+
140+ @ Path ("hello" )
141+ @ GET
142+ @ Produces ("text/plain" )
143+ public String hello () {
144+ return "Hello!" ;
145+ }
146+
147+ @ GET
148+ @ Path ("resourceClassStatisticsWadlOptionsTest" )
149+ public String getResourceClassStatisticsWadlOptionsTest () {
150+ return getResourceClassStatisticsTest (WadlModelProcessor .OptionsHandler .class .getName ());
151+ }
152+
153+ @ GET
154+ @ Path ("resourceClassStatisticsGenericOptionsTest" )
155+ public String getResourceClassStatisticsGenericOptionsTest () {
156+ return getResourceClassStatisticsTest ("org.glassfish.jersey.server.wadl.processor.OptionsMethodProcessor$GenericOptionsInflector" );
157+ }
158+
159+ @ GET
160+ @ Path ("resourceClassStatisticsPlainTextOptionsTest" )
161+ public String getResourceClassStatisticsPlainTestOptionsTest () {
162+ return getResourceClassStatisticsTest ("org.glassfish.jersey.server.wadl.processor.OptionsMethodProcessor$PlainTextOptionsInflector" );
163+ }
164+
165+ private String getResourceClassStatisticsTest (String resourceClassName ) {
166+ ResourceStatistics resourceMethodStatistics = findResourceClassStatistics (statistics .get (), resourceClassName );
167+
168+ boolean resourceHelloOptions = false ;
169+ boolean anotherHelloOptions = false ;
170+ boolean anotherXmlOptions = false ;
171+ for (final Map .Entry <ResourceMethod , ResourceMethodStatistics > entry : resourceMethodStatistics .getResourceMethodStatistics ().entrySet ()) {
172+ final ResourceMethod resourceMethod = entry .getKey ();
173+ final String fullPath = getFullPath (resourceMethod );
174+ if ("/resource/hello" .equals (fullPath )) {
175+ resourceHelloOptions = true ;
176+ } else if ("/another/hello" .equals (fullPath )) {
177+ anotherHelloOptions = true ;
178+ } else if ("/another/xml" .equals (fullPath )) {
179+ anotherXmlOptions = true ;
180+ }
181+ }
182+ if (resourceHelloOptions && anotherHelloOptions && anotherXmlOptions ) {
183+ return "OK" ;
184+ } else {
185+ return "FAIL: /resource/hello=" + resourceHelloOptions + "; /another/hello=" + anotherHelloOptions +
186+ "; /another/xml=" + anotherXmlOptions ;
187+ }
188+ }
189+
190+ @ GET
191+ @ Path ("uriStatisticsResourceHelloTest" )
192+ public String getUriStatisticsResourceHelloTest () {
193+ return getUriStatisticsTest ("/resource/hello" );
194+ }
195+
196+ @ GET
197+ @ Path ("uriStatisticsAnotherHelloTest" )
198+ public String getUriStatisticsAnotherHelloTest () {
199+ return getUriStatisticsTest ("/another/hello" );
200+ }
201+
202+ @ GET
203+ @ Path ("uriStatisticsAnotherXmlTest" )
204+ public String getUriStatisticsAnotherXmlTest () {
205+ return getUriStatisticsTest ("/another/xml" );
206+ }
207+
208+ private String getUriStatisticsTest (String uri ) {
209+ boolean plainTextOptions = false ;
210+ boolean wadlOptions = false ;
211+ boolean genericOptions = false ;
212+ final ResourceStatistics resourceStatistics = statistics .get ().getUriStatistics ().get (uri );
213+ for (Map .Entry <ResourceMethod , ResourceMethodStatistics > entry : resourceStatistics .getResourceMethodStatistics ().entrySet ()) {
214+ if (entry .getKey ().getHttpMethod ().equals ("OPTIONS" )) {
215+ final ResourceMethod resourceMethod = entry .getKey ();
216+ final String producedTypes = resourceMethod .getProducedTypes ().toString ();
217+ if ("[text/plain]" .equals (producedTypes )) {
218+ plainTextOptions = true ;
219+ } else if ("[application/vnd.sun.wadl+xml]" .equals (producedTypes )) {
220+ wadlOptions = true ;
221+ } else if ("[*/*]" .equals (producedTypes )) {
222+ genericOptions = true ;
223+ }
224+ }
225+ }
226+ if (plainTextOptions && wadlOptions && genericOptions ) {
227+ return "OK" ;
228+ } else {
229+ return "FAIL: [text/plain]=" + plainTextOptions + "; [application/vnd.sun.wadl+xml]=" + wadlOptions +
230+ "; [*/*]=" + genericOptions ;
231+ }
232+ }
233+
234+ private ResourceStatistics findResourceClassStatistics (MonitoringStatistics monitoringStatistics , String resourceClassName ) {
235+ for (final Map .Entry <Class <?>, ResourceStatistics > entry : monitoringStatistics .getResourceClassStatistics ().entrySet ()) {
236+ final Class <?> key = entry .getKey ();
237+ final String clazz = key .getName ();
238+
239+ if (clazz .equals (resourceClassName )) {
240+ return entry .getValue ();
241+ }
242+ }
243+ return null ;
244+ }
245+
246+ private static String getFullPath (final ResourceMethod resourceMethod ) {
247+ final StringBuilder fullPath = new StringBuilder ();
248+ if (resourceMethod != null ) {
249+ prefixPath (fullPath , resourceMethod .getParent ());
250+ }
251+ return fullPath .toString ();
252+ }
253+
254+ private static void prefixPath (final StringBuilder fullPath , final Resource parent ) {
255+ if (parent != null ) {
256+ String path = parent .getPath ();
257+ if (path .startsWith ("/" )) {
258+ path = path .substring (1 );
259+ }
260+ fullPath .insert (0 , "/" + path );
261+ prefixPath (fullPath , parent .getParent ());
262+ }
263+ }
264+
134265 }
135266
136267 public static class SubResource {
@@ -146,6 +277,23 @@ public SubResource subLocator() {
146277
147278 }
148279
280+ @ Path ("/another" )
281+ public static class AnotherResource {
282+ @ Path ("hello" )
283+ @ GET
284+ @ Produces ("text/plain" )
285+ public String sayHello () {
286+ return "Hello, again." ;
287+ }
288+
289+ @ Path ("xml" )
290+ @ GET
291+ @ Produces (MediaType .TEXT_XML )
292+ public String sayXMLHello () {
293+ return "<?xml version=\" 1.0\" ?><hello>World!</hello>" ;
294+ }
295+ }
296+
149297 @ Test
150298 public void test () throws InterruptedException {
151299 Response response = target ().path ("resource" ).request ().get ();
@@ -156,16 +304,26 @@ public void test() throws InterruptedException {
156304 Assert .assertEquals (200 , response .getStatus ());
157305 Assert .assertEquals ("get" , response .readEntity (String .class ));
158306
159-
160307 response = target ().path ("resource/resource-locator" ).request ().get ();
161308 Assert .assertEquals (200 , response .getStatus ());
162309 Assert .assertEquals ("get" , response .readEntity (String .class ));
163310
164-
165311 response = target ().path ("resource/resource-locator/sub" ).request ().get ();
166312 Assert .assertEquals (200 , response .getStatus ());
167313 Assert .assertEquals ("get" , response .readEntity (String .class ));
168314
315+ response = target ().path ("resource/hello" ).request ().get ();
316+ Assert .assertEquals (200 , response .getStatus ());
317+ Assert .assertEquals ("Hello!" , response .readEntity (String .class ));
318+
319+ response = target ().path ("another/hello" ).request ().get ();
320+ Assert .assertEquals (200 , response .getStatus ());
321+ Assert .assertEquals ("Hello, again." , response .readEntity (String .class ));
322+
323+ response = target ().path ("another/xml" ).request ().get ();
324+ Assert .assertEquals (200 , response .getStatus ());
325+ Assert .assertEquals ("<?xml version=\" 1.0\" ?><hello>World!</hello>" , response .readEntity (String .class ));
326+
169327 Thread .sleep (600 );
170328
171329 response = target ().path ("resource" ).request ().get ();
@@ -175,6 +333,69 @@ public void test() throws InterruptedException {
175333 response = target ().path ("resource/uri" ).request ().get ();
176334 Assert .assertEquals (200 , response .getStatus ());
177335 Assert .assertEquals ("getFound" , response .readEntity (String .class ));
336+ }
337+
338+ @ Test
339+ @ Ignore //J-396 reproducer
340+ public void testResourceClassStatisticsWadlOptions () {
341+ Response response = target ().path ("resource/resourceClassStatisticsWadlOptionsTest" ).request ().get ();
342+ Assert .assertEquals (200 , response .getStatus ());
343+ Assert .assertEquals ("OK" , response .readEntity (String .class ));
344+ }
345+
346+ @ Test
347+ @ Ignore //J-396 reproducer
348+ public void testResourceClassStatisticsGenericOptions () {
349+ Response response = target ().path ("resource/resourceClassStatisticsGenericOptionsTest" ).request ().get ();
350+ Assert .assertEquals (200 , response .getStatus ());
351+ Assert .assertEquals ("OK" , response .readEntity (String .class ));
352+ }
353+
354+ @ Test
355+ @ Ignore //J-396 reproducer
356+ public void testResourceClassStatisticsPlainTextOptions () {
357+ Response response = target ().path ("resource/resourceClassStatisticsPlainTextOptionsTest" ).request ().get ();
358+ Assert .assertEquals (200 , response .getStatus ());
359+ Assert .assertEquals ("OK" , response .readEntity (String .class ));
360+ }
361+
362+ @ Test
363+ public void testUriStatisticsResourceHello () throws InterruptedException {
364+ Response response = target ().path ("resource/hello" ).request ().get ();
365+ Assert .assertEquals (200 , response .getStatus ());
366+ Assert .assertEquals ("Hello!" , response .readEntity (String .class ));
367+
368+ Thread .sleep (600 );
369+
370+ response = target ().path ("resource/uriStatisticsResourceHelloTest" ).request ().get ();
371+ Assert .assertEquals (200 , response .getStatus ());
372+ Assert .assertEquals ("OK" , response .readEntity (String .class ));
373+ }
374+
375+ @ Test
376+ public void testUriStatisticsAnotherHello () throws InterruptedException {
377+ Response response = target ().path ("another/hello" ).request ().get ();
378+ Assert .assertEquals (200 , response .getStatus ());
379+ Assert .assertEquals ("Hello, again." , response .readEntity (String .class ));
178380
381+ Thread .sleep (600 );
382+
383+ response = target ().path ("resource/uriStatisticsAnotherHelloTest" ).request ().get ();
384+ Assert .assertEquals (200 , response .getStatus ());
385+ Assert .assertEquals ("OK" , response .readEntity (String .class ));
386+ }
387+
388+ @ Test
389+ public void testUriStatisticsAnotherXml () throws InterruptedException {
390+ Response response = target ().path ("another/xml" ).request ().get ();
391+ Assert .assertEquals (200 , response .getStatus ());
392+ Assert .assertEquals ("<?xml version=\" 1.0\" ?><hello>World!</hello>" , response .readEntity (String .class ));
393+
394+ Thread .sleep (600 );
395+
396+ response = target ().path ("resource/uriStatisticsAnotherXmlTest" ).request ().get ();
397+ Assert .assertEquals (200 , response .getStatus ());
398+ Assert .assertEquals ("OK" , response .readEntity (String .class ));
179399 }
400+
180401}
0 commit comments