22
33import static org .junit .Assert .*;
44
5+ import java .util .*;
6+
57import org .json .*;
68import org .junit .Test ;
79
10+ import com .jayway .jsonpath .*;
11+
812
913/**
1014 * Tests for JSON-Java JSONStringer.
@@ -175,16 +179,6 @@ public void exceedNestDepthException() {
175179 */
176180 @ Test
177181 public void simpleObjectString () {
178- String expectedStr =
179- "{" +
180- "\" trueValue\" :true," +
181- "\" falseValue\" :false," +
182- "\" nullValue\" :null," +
183- "\" stringValue\" :\" hello world!\" ," +
184- "\" complexStringValue\" :\" h\b e\t llo w\u1234 orld!\" ," +
185- "\" intValue\" :42," +
186- "\" doubleValue\" :-23.45e67" +
187- "}" ;
188182 JSONStringer jsonStringer = new JSONStringer ();
189183 jsonStringer .object ();
190184 jsonStringer .key ("trueValue" ).value (true );
@@ -197,8 +191,16 @@ public void simpleObjectString() {
197191 jsonStringer .endObject ();
198192 String str = jsonStringer .toString ();
199193 JSONObject jsonObject = new JSONObject (str );
200- JSONObject expectedJsonObject = new JSONObject (expectedStr );
201- Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
194+
195+ Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
196+ assertTrue ("expected 7 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 7 );
197+ assertTrue ("expected true" , Boolean .TRUE .equals (JsonPath .read (doc , "$.trueValue" )));
198+ assertTrue ("expected false" , Boolean .FALSE .equals (JsonPath .read (doc , "$.falseValue" )));
199+ assertTrue ("expected null" , null == JsonPath .read (doc , "$.nullValue" ));
200+ assertTrue ("expected hello world!" , "hello world!" .equals (JsonPath .read (doc , "$.stringValue" )));
201+ assertTrue ("expected h\b e\t llo w\u1234 orld!" , "h\b e\t llo w\u1234 orld!" .equals (JsonPath .read (doc , "$.complexStringValue" )));
202+ assertTrue ("expected 42" , Integer .valueOf (42 ).equals (JsonPath .read (doc , "$.intValue" )));
203+ assertTrue ("expected -23.45e67" , Double .valueOf (-23.45e67 ).equals (JsonPath .read (doc , "$.doubleValue" )));
202204 }
203205
204206 /**
@@ -207,15 +209,6 @@ public void simpleObjectString() {
207209 */
208210 @ Test
209211 public void simpleArrayString () {
210- String expectedStr =
211- "[" +
212- "true," +
213- "false," +
214- "null," +
215- "\" hello world!\" ," +
216- "42," +
217- "-23.45e67" +
218- "]" ;
219212 JSONStringer jsonStringer = new JSONStringer ();
220213 jsonStringer .array ();
221214 jsonStringer .value (true );
@@ -227,8 +220,15 @@ public void simpleArrayString() {
227220 jsonStringer .endArray ();
228221 String str = jsonStringer .toString ();
229222 JSONArray jsonArray = new JSONArray (str );
230- JSONArray expectedJsonArray = new JSONArray (expectedStr );
231- Util .compareActualVsExpectedJsonArrays (jsonArray , expectedJsonArray );
223+
224+ Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonArray .toString ());
225+ assertTrue ("expected 6 top level items" , ((List <?>)(JsonPath .read (doc , "$" ))).size () == 6 );
226+ assertTrue ("expected true" , Boolean .TRUE .equals (JsonPath .read (doc , "$[0]" )));
227+ assertTrue ("expected false" , Boolean .FALSE .equals (JsonPath .read (doc , "$[1]" )));
228+ assertTrue ("expected null" , null == JsonPath .read (doc , "$[2]" ));
229+ assertTrue ("expected hello world!" , "hello world!" .equals (JsonPath .read (doc , "$[3]" )));
230+ assertTrue ("expected 42" , Integer .valueOf (42 ).equals (JsonPath .read (doc , "$[4]" )));
231+ assertTrue ("expected -23.45e67" , Double .valueOf (-23.45e67 ).equals (JsonPath .read (doc , "$[5]" )));
232232 }
233233
234234 /**
@@ -237,38 +237,6 @@ public void simpleArrayString() {
237237 */
238238 @ Test
239239 public void complexObjectString () {
240- String expectedStr =
241- "{" +
242- "\" trueValue\" :true," +
243- "\" falseValue\" :false," +
244- "\" nullValue\" :null," +
245- "\" stringValue\" :\" hello world!\" ," +
246- "\" object2\" :{" +
247- "\" k1\" :\" v1\" ," +
248- "\" k2\" :\" v2\" ," +
249- "\" k3\" :\" v3\" ," +
250- "\" array1\" :[" +
251- "1," +
252- "2," +
253- "{" +
254- "\" k4\" :\" v4\" ," +
255- "\" k5\" :\" v5\" ," +
256- "\" k6\" :\" v6\" ," +
257- "\" array2\" :[" +
258- "5," +
259- "6," +
260- "7," +
261- "8" +
262- "]" +
263- "}," +
264- "3," +
265- "4" +
266- "]" +
267- "}," +
268- "\" complexStringValue\" :\" h\b e\t llo w\u1234 orld!\" ," +
269- "\" intValue\" :42," +
270- "\" doubleValue\" :-23.45e67" +
271- "}" ;
272240 JSONStringer jsonStringer = new JSONStringer ();
273241 jsonStringer .object ();
274242 jsonStringer .key ("trueValue" ).value (true );
@@ -303,8 +271,34 @@ public void complexObjectString() {
303271 jsonStringer .endObject ();
304272 String str = jsonStringer .toString ();
305273 JSONObject jsonObject = new JSONObject (str );
306- JSONObject expectedJsonObject = new JSONObject (expectedStr );
307- Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
274+
275+ Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
276+ assertTrue ("expected 8 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 8 );
277+ assertTrue ("expected 4 object2 items" , ((Map <?,?>)(JsonPath .read (doc , "$.object2" ))).size () == 4 );
278+ assertTrue ("expected 5 array1 items" , ((List <?>)(JsonPath .read (doc , "$.object2.array1" ))).size () == 5 );
279+ assertTrue ("expected 4 array[2] items" , ((Map <?,?>)(JsonPath .read (doc , "$.object2.array1[2]" ))).size () == 4 );
280+ assertTrue ("expected 4 array1[2].array2 items" , ((List <?>)(JsonPath .read (doc , "$.object2.array1[2].array2" ))).size () == 4 );
281+ assertTrue ("expected true" , Boolean .TRUE .equals (JsonPath .read (doc , "$.trueValue" )));
282+ assertTrue ("expected false" , Boolean .FALSE .equals (JsonPath .read (doc , "$.falseValue" )));
283+ assertTrue ("expected null" , null == JsonPath .read (doc , "$.nullValue" ));
284+ assertTrue ("expected hello world!" , "hello world!" .equals (JsonPath .read (doc , "$.stringValue" )));
285+ assertTrue ("expected 42" , Integer .valueOf (42 ).equals (JsonPath .read (doc , "$.intValue" )));
286+ assertTrue ("expected -23.45e67" , Double .valueOf (-23.45e67 ).equals (JsonPath .read (doc , "$.doubleValue" )));
287+ assertTrue ("expected h\b e\t llo w\u1234 orld!" , "h\b e\t llo w\u1234 orld!" .equals (JsonPath .read (doc , "$.complexStringValue" )));
288+ assertTrue ("expected v1" , "v1" .equals (JsonPath .read (doc , "$.object2.k1" )));
289+ assertTrue ("expected v2" , "v2" .equals (JsonPath .read (doc , "$.object2.k2" )));
290+ assertTrue ("expected v3" , "v3" .equals (JsonPath .read (doc , "$.object2.k3" )));
291+ assertTrue ("expected 1" , Integer .valueOf (1 ).equals (JsonPath .read (doc , "$.object2.array1[0]" )));
292+ assertTrue ("expected 2" , Integer .valueOf (2 ).equals (JsonPath .read (doc , "$.object2.array1[1]" )));
293+ assertTrue ("expected v4" , "v4" .equals (JsonPath .read (doc , "$.object2.array1[2].k4" )));
294+ assertTrue ("expected v5" , "v5" .equals (JsonPath .read (doc , "$.object2.array1[2].k5" )));
295+ assertTrue ("expected v6" , "v6" .equals (JsonPath .read (doc , "$.object2.array1[2].k6" )));
296+ assertTrue ("expected 5" , Integer .valueOf (5 ).equals (JsonPath .read (doc , "$.object2.array1[2].array2[0]" )));
297+ assertTrue ("expected 6" , Integer .valueOf (6 ).equals (JsonPath .read (doc , "$.object2.array1[2].array2[1]" )));
298+ assertTrue ("expected 7" , Integer .valueOf (7 ).equals (JsonPath .read (doc , "$.object2.array1[2].array2[2]" )));
299+ assertTrue ("expected 8" , Integer .valueOf (8 ).equals (JsonPath .read (doc , "$.object2.array1[2].array2[3]" )));
300+ assertTrue ("expected 3" , Integer .valueOf (3 ).equals (JsonPath .read (doc , "$.object2.array1[3]" )));
301+ assertTrue ("expected 4" , Integer .valueOf (4 ).equals (JsonPath .read (doc , "$.object2.array1[4]" )));
308302 }
309303
310304}
0 commit comments