11/*
22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33 *
4- * Copyright (c) 2013 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
@@ -67,52 +67,66 @@ public class ContentNegotiationTest extends JerseyTest {
6767
6868 @ Path ("persons" )
6969 public static class MyResource {
70+ private static final Person [] LIST = new Person [] {
71+ new Person ("Penny" , 1 ),
72+ new Person ("Howard" , 2 ),
73+ new Person ("Sheldon" , 3 )
74+ };
75+
7076 @ GET
7177 @ Produces ({"application/xml;qs=0.75" , "application/json;qs=1.0" })
7278 public Person [] getList () {
73- Person [] list = new Person [3 ];
74- list [0 ] = new Person ("Penny" , 1 );
75- list [1 ] = new Person ("Howard" , 2 );
76- list [2 ] = new Person ("Sheldon" , 3 );
77-
78- return list ;
79+ return LIST ;
7980 }
8081
8182 @ GET
8283 @ Produces ({"application/json;qs=1" , "application/xml;qs=0.75" })
8384 @ Path ("reordered" )
8485 public Person [] getListReordered () {
85- Person [] list = new Person [3 ];
86- list [0 ] = new Person ("Penny" , 1 );
87- list [1 ] = new Person ("Howard" , 2 );
88- list [2 ] = new Person ("Sheldon" , 3 );
89-
90- return list ;
86+ return LIST ;
9187 }
9288
9389 @ GET
9490 @ Produces ({"application/json;qs=0.75" , "application/xml;qs=1" })
9591 @ Path ("inverted" )
9692 public Person [] getListInverted () {
97- Person [] list = new Person [3 ];
98- list [0 ] = new Person ("Penny" , 1 );
99- list [1 ] = new Person ("Howard" , 2 );
100- list [2 ] = new Person ("Sheldon" , 3 );
101-
102- return list ;
93+ return LIST ;
10394 }
10495
10596
10697 @ GET
10798 @ Produces ({"application/xml;qs=0.75" , "application/json;qs=0.9" , "unknown/hello;qs=1.0" })
10899 @ Path ("unkownMT" )
109100 public Person [] getListWithUnkownType () {
110- Person [] list = new Person [3 ];
111- list [0 ] = new Person ("Penny" , 1 );
112- list [1 ] = new Person ("Howard" , 2 );
113- list [2 ] = new Person ("Sheldon" , 3 );
101+ return LIST ;
102+ }
103+
104+ @ GET
105+ @ Produces ({"application/json" , "application/xml" , "text/plain" })
106+ @ Path ("shouldPickFirstJson" )
107+ public Person [] getJsonArrayUnlessOtherwiseSpecified () {
108+ return LIST ;
109+ }
114110
115- return list ;
111+ @ GET
112+ @ Produces ({"application/xml" , "text/plain" , "application/json" })
113+ @ Path ("shouldPickFirstXml" )
114+ public Person [] getXmlUnlessOtherwiseSpecified () {
115+ return LIST ;
116+ }
117+
118+ @ GET
119+ @ Produces ("application/json;qs=0.75" )
120+ @ Path ("twoMethodsOneEndpoint" )
121+ public Person [] getJsonArray () {
122+ return LIST ;
123+ }
124+
125+ @ GET
126+ @ Produces ("application/xml;qs=1" )
127+ @ Path ("twoMethodsOneEndpoint" )
128+ public Person [] getXml () {
129+ return LIST ;
116130 }
117131 }
118132
@@ -173,6 +187,30 @@ public void testWithoutDefinedRequestedMediaType() {
173187 Assert .assertEquals (MediaType .APPLICATION_JSON_TYPE , response .getMediaType ());
174188 }
175189
190+ @ Test
191+ public void testWithoutDefinedRequestedMediaTypeAndTwoMethods () {
192+ //We can not rely on method declaration ordering:
193+ //From Class javadoc: "The elements in the returned array are not sorted and are not in any particular order."
194+ //If there are same endpoints it is necessary to use quality parameter to ensure ordering.
195+ Response response = target ().path ("/persons/twoMethodsOneEndpoint" ).request ().get ();
196+ Assert .assertEquals (200 , response .getStatus ());
197+ Assert .assertEquals (MediaType .APPLICATION_XML_TYPE , response .getMediaType ());
198+ }
199+
200+ @ Test
201+ public void testWithoutDefinedRequestedMediaTypeOrQualityModifiersJson () {
202+ Response response = target ().path ("/persons/shouldPickFirstJson" ).request ().get ();
203+ Assert .assertEquals (200 , response .getStatus ());
204+ Assert .assertEquals (MediaType .APPLICATION_JSON_TYPE , response .getMediaType ());
205+ }
206+
207+ @ Test
208+ public void testWithoutDefinedRequestedMediaTypeOrQualityModifiersXml () {
209+ Response response = target ().path ("/persons/shouldPickFirstXml" ).request ().get ();
210+ Assert .assertEquals (200 , response .getStatus ());
211+ Assert .assertEquals (MediaType .APPLICATION_XML_TYPE , response .getMediaType ());
212+ }
213+
176214 @ Test
177215 public void test () {
178216 WebTarget target = target ().path ("/persons" );
@@ -190,7 +228,7 @@ public void testInverted() {
190228 }
191229
192230 @ Test
193- public void testInvertedWithJSONPrefferedByClient () {
231+ public void testInvertedWithJSONPreferredByClient () {
194232 WebTarget target = target ().path ("/persons/inverted" );
195233 Response response = target .request ("application/json;q=1.0" , "application/xml;q=0.8" ).get ();
196234 Assert .assertEquals (200 , response .getStatus ());
@@ -210,7 +248,7 @@ public void testReordered() {
210248 * this type is ignored and "application/xml" is chosen (because it is the second preferred type by the client).
211249 */
212250 @ Test
213- public void testWithUnkownTypePreferedByClient () {
251+ public void testWithUnknownTypePreferredByClient () {
214252 WebTarget target = target ().path ("/persons/reordered" );
215253 Response response = target .request ("application/json;q=0.8" , "application/xml;q=0.9" ,
216254 "unknown/hello;qs=1.0" ).get ();
0 commit comments