5353
5454import javax .json .Json ;
5555import javax .json .JsonArray ;
56+ import javax .json .JsonNumber ;
5657import javax .json .JsonObject ;
58+ import javax .json .JsonString ;
5759import javax .json .JsonStructure ;
60+ import javax .json .JsonValue ;
5861
5962import org .glassfish .jersey .jsonp .JsonProcessingFeature ;
6063import org .glassfish .jersey .server .JSONP ;
6164import org .glassfish .jersey .server .ResourceConfig ;
6265import org .glassfish .jersey .server .ServerProperties ;
6366import org .glassfish .jersey .test .JerseyTest ;
6467import org .glassfish .jersey .test .TestProperties ;
65- import org .glassfish .jersey .test .util .runner .ConcurrentRunner ;
6668
6769import org .junit .Test ;
68- import org .junit .runner .RunWith ;
6970import static org .hamcrest .CoreMatchers .is ;
7071import static org .junit .Assert .assertEquals ;
7172import static org .junit .Assert .assertThat ;
7273
7374/**
7475 * @author Michal Gajdos
7576 */
76- @ RunWith (ConcurrentRunner .class )
77+ // @RunWith(ConcurrentRunner.class)
7778public class JsonProcessingTest extends JerseyTest {
7879
79- private static final String JSON_OBJECT_STR = "{\" foo\" :\" bar\" }" ;
80- private static final String JSON_ARRAY_STR = "[" + JSON_OBJECT_STR + "," + JSON_OBJECT_STR + "]" ;
80+ private static final String JSON_OBJECT_STR1 = "{\" foo\" :\" bar\" }" ;
81+ private static final String JSON_OBJECT_STR2 = "{\" foo\" : 12345}" ;
82+ private static final String JSON_ARRAY_STR1 = "[" + JSON_OBJECT_STR1 + "," + JSON_OBJECT_STR1 + "]" ;
83+ private static final String JSON_ARRAY_STR2 = "[" + JSON_OBJECT_STR2 + "," + JSON_OBJECT_STR2 + "]" ;
8184 private static final String JSON_ARRAY_VALUE_STR = "[null]" ;
8285
83- private static final JsonObject JSON_OBJECT = Json .createReader (new StringReader (JSON_OBJECT_STR )).readObject ();
84- private static final JsonArray JSON_ARRAY = Json .createReader (new StringReader (JSON_ARRAY_STR )).readArray ();
85- private static final JsonArray JSON_ARRAY_VALUE = Json .createReader (new StringReader (JSON_ARRAY_VALUE_STR )).readArray ();
86+ private static final JsonObject JSON_OBJECT = Json .createReader (new StringReader (JSON_OBJECT_STR1 )).readObject ();
87+ private static final JsonArray JSON_ARRAY = Json .createReader (new StringReader (JSON_ARRAY_STR1 )).readArray ();
88+ private static final JsonArray JSON_ARRAY_VALUE = Json .createReader (new StringReader (JSON_ARRAY_VALUE_STR ))
89+ .readArray ();
90+
91+ private static final JsonValue JSON_VALUE_BOOL = JsonValue .TRUE ;
92+ private static final JsonString JSON_VALUE_STRING = Json .createReader (
93+ new StringReader (JSON_ARRAY_STR1 )).readArray ().getJsonObject (0 ).getJsonString ("foo" );
94+ private static final JsonNumber JSON_VALUE_NUMBER = Json .createReader (
95+ new StringReader (JSON_ARRAY_STR2 )).readArray ().getJsonObject (0 ).getJsonNumber ("foo" );
8696
8797 @ Path ("/" )
8898 public static class Resource {
@@ -105,6 +115,24 @@ public JsonArray postJsonArray(final JsonArray jsonArray) {
105115 return jsonArray ;
106116 }
107117
118+ @ POST
119+ @ Path ("jsonValue" )
120+ public JsonValue postJsonValue (final JsonValue jsonValue ) {
121+ return jsonValue ;
122+ }
123+
124+ @ POST
125+ @ Path ("jsonString" )
126+ public JsonString postJsonString (final JsonString jsonString ) {
127+ return jsonString ;
128+ }
129+
130+ @ POST
131+ @ Path ("jsonNumber" )
132+ public JsonValue postJsonNumber (final JsonNumber jsonNumber ) {
133+ return jsonNumber ;
134+ }
135+
108136 @ GET
109137 @ JSONP
110138 @ Path ("jsonObjectWithPadding" )
@@ -134,7 +162,8 @@ public void testJsonObject() throws Exception {
134162
135163 @ Test
136164 public void testJsonObjectAsString () throws Exception {
137- final Response response = target ("jsonObject" ).request (MediaType .APPLICATION_JSON ).post (Entity .json (JSON_OBJECT_STR ));
165+ final Response response = target ("jsonObject" ).request (MediaType .APPLICATION_JSON )
166+ .post (Entity .json (JSON_OBJECT_STR1 ));
138167
139168 assertEquals (JSON_OBJECT , response .readEntity (JsonObject .class ));
140169 }
@@ -148,7 +177,8 @@ public void testJsonObjectPlus() throws Exception {
148177
149178 @ Test
150179 public void testJsonObjectAsStringPlus () throws Exception {
151- final Response response = target ("jsonObject" ).request ("application/foo+json" ).post (Entity .json (JSON_OBJECT_STR ));
180+ final Response response = target ("jsonObject" ).request ("application/foo+json" )
181+ .post (Entity .json (JSON_OBJECT_STR1 ));
152182
153183 assertEquals (JSON_OBJECT , response .readEntity (JsonObject .class ));
154184 }
@@ -162,7 +192,8 @@ public void testJsonObjectWrongTarget() throws Exception {
162192
163193 @ Test
164194 public void testJsonObjectAsStringWrongTarget () throws Exception {
165- final Response response = target ("jsonArray" ).request (MediaType .APPLICATION_JSON ).post (Entity .json (JSON_OBJECT_STR ));
195+ final Response response = target ("jsonArray" ).request (MediaType .APPLICATION_JSON )
196+ .post (Entity .json (JSON_OBJECT_STR1 ));
166197
167198 assertEquals (500 , response .getStatus ());
168199 }
@@ -176,7 +207,8 @@ public void testJsonObjectWrongEntity() throws Exception {
176207
177208 @ Test
178209 public void testJsonObjectAsStringWrongEntity () throws Exception {
179- final Response response = target ("jsonObject" ).request (MediaType .APPLICATION_JSON ).post (Entity .json (JSON_ARRAY_STR ));
210+ final Response response = target ("jsonObject" ).request (MediaType .APPLICATION_JSON )
211+ .post (Entity .json (JSON_ARRAY_STR1 ));
180212
181213 assertEquals (500 , response .getStatus ());
182214 }
@@ -191,7 +223,7 @@ public void testJsonObjectWrongMediaType() throws Exception {
191223 @ Test
192224 public void testJsonObjectAsStringWrongMediaType () throws Exception {
193225 final Response response = target ("jsonObject" ).request (MediaType .APPLICATION_OCTET_STREAM )
194- .post (Entity .json (JSON_OBJECT_STR ));
226+ .post (Entity .json (JSON_OBJECT_STR1 ));
195227
196228 assertEquals (500 , response .getStatus ());
197229 }
@@ -205,7 +237,8 @@ public void testJsonArray() throws Exception {
205237
206238 @ Test
207239 public void testJsonArrayAsString () throws Exception {
208- final Response response = target ("jsonArray" ).request (MediaType .APPLICATION_JSON ).post (Entity .json (JSON_ARRAY_STR ));
240+ final Response response = target ("jsonArray" ).request (MediaType .APPLICATION_JSON )
241+ .post (Entity .json (JSON_ARRAY_STR1 ));
209242
210243 assertEquals (JSON_ARRAY , response .readEntity (JsonArray .class ));
211244 }
@@ -219,7 +252,8 @@ public void testJsonArrayPlus() throws Exception {
219252
220253 @ Test
221254 public void testJsonArrayAsStringPlus () throws Exception {
222- final Response response = target ("jsonArray" ).request ("application/foo+json" ).post (Entity .json (JSON_ARRAY_STR ));
255+ final Response response = target ("jsonArray" ).request ("application/foo+json" )
256+ .post (Entity .json (JSON_ARRAY_STR1 ));
223257
224258 assertEquals (JSON_ARRAY , response .readEntity (JsonArray .class ));
225259 }
@@ -233,7 +267,8 @@ public void testJsonArrayWrongTarget() throws Exception {
233267
234268 @ Test
235269 public void testJsonArrayAsStringWrongTarget () throws Exception {
236- final Response response = target ("jsonObject" ).request (MediaType .APPLICATION_JSON ).post (Entity .json (JSON_ARRAY_STR ));
270+ final Response response = target ("jsonObject" ).request (MediaType .APPLICATION_JSON )
271+ .post (Entity .json (JSON_ARRAY_STR1 ));
237272
238273 assertEquals (500 , response .getStatus ());
239274 }
@@ -247,7 +282,8 @@ public void testJsonArrayWrongEntity() throws Exception {
247282
248283 @ Test
249284 public void testJsonArrayAsStringWrongEntity () throws Exception {
250- final Response response = target ("jsonArray" ).request (MediaType .APPLICATION_JSON ).post (Entity .json (JSON_OBJECT_STR ));
285+ final Response response = target ("jsonArray" ).request (MediaType .APPLICATION_JSON )
286+ .post (Entity .json (JSON_OBJECT_STR1 ));
251287
252288 assertEquals (500 , response .getStatus ());
253289 }
@@ -262,7 +298,7 @@ public void testJsonArrayWrongMediaType() throws Exception {
262298 @ Test
263299 public void testJsonArraytAsStringWrongMediaType () throws Exception {
264300 final Response response = target ("jsonArray" ).request (MediaType .APPLICATION_OCTET_STREAM )
265- .post (Entity .json (JSON_ARRAY_STR ));
301+ .post (Entity .json (JSON_ARRAY_STR1 ));
266302
267303 assertEquals (500 , response .getStatus ());
268304 }
@@ -288,11 +324,59 @@ public void testJsonStructureObject() throws Exception {
288324 assertEquals (JSON_OBJECT , response .readEntity (JsonStructure .class ));
289325 }
290326
327+ @ Test
328+ public void testJsonValueBool () throws Exception {
329+ final Response response = target ("jsonValue" ).request (MediaType .APPLICATION_JSON )
330+ .post (Entity .json (JSON_VALUE_BOOL ));
331+
332+ assertEquals (JSON_VALUE_BOOL , response .readEntity (JsonValue .class ));
333+ }
334+
335+ @ Test
336+ public void testJsonValueString () throws Exception {
337+ final Response response = target ("jsonString" ).request (MediaType .APPLICATION_JSON )
338+ .post (Entity .json (JSON_VALUE_STRING ));
339+
340+ assertEquals (JSON_VALUE_STRING , response .readEntity (JsonString .class ));
341+ }
342+
343+ @ Test
344+ public void testJsonValueStringAsValue () throws Exception {
345+ final Response response = target ("jsonValue" ).request (MediaType .APPLICATION_JSON )
346+ .post (Entity .json (JSON_VALUE_STRING ));
347+
348+ assertEquals (JSON_VALUE_STRING , response .readEntity (JsonString .class ));
349+ }
350+
351+ @ Test
352+ public void testJsonValueStringAsString () throws Exception {
353+ final Response response = target ("jsonValue" ).request (MediaType .APPLICATION_JSON )
354+ .post (Entity .json ("\" Red 5\" " ));
355+
356+ assertEquals ("Red 5" , response .readEntity (JsonString .class ).getString ());
357+ }
358+
359+ @ Test
360+ public void testJsonValueNumber () throws Exception {
361+ final Response response = target ("jsonNumber" ).request (MediaType .APPLICATION_JSON )
362+ .post (Entity .json (JSON_VALUE_NUMBER ));
363+
364+ assertEquals (JSON_VALUE_NUMBER , response .readEntity (JsonNumber .class ));
365+ }
366+
367+ @ Test
368+ public void testJsonValueNumberAsValue () throws Exception {
369+ final Response response = target ("jsonValue" ).request (MediaType .APPLICATION_JSON )
370+ .post (Entity .json (JSON_VALUE_NUMBER ));
371+
372+ assertEquals (JSON_VALUE_NUMBER , response .readEntity (JsonNumber .class ));
373+ }
374+
291375 @ Test
292376 public void testJsonObjectWithPadding () throws Exception {
293377 final Response response = target ("jsonObjectWithPadding" ).request ("application/javascript" ).get ();
294378
295379 assertThat (response .getStatus (), is (200 ));
296- assertThat (response .readEntity (String .class ), is (JSONP .DEFAULT_CALLBACK + "(" + JSON_OBJECT_STR + ")" ));
380+ assertThat (response .readEntity (String .class ), is (JSONP .DEFAULT_CALLBACK + "(" + JSON_OBJECT_STR1 + ")" ));
297381 }
298382}
0 commit comments