Skip to content

Commit a5390a0

Browse files
committed
Replace util compare method with JsonPath
1 parent c6204a9 commit a5390a0

1 file changed

Lines changed: 105 additions & 100 deletions

File tree

JSONArrayTest.java

Lines changed: 105 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
import static org.junit.Assert.assertTrue;
44

5-
import java.util.ArrayList;
6-
import java.util.Collection;
7-
import java.util.Collections;
8-
import java.util.HashMap;
9-
import java.util.Iterator;
10-
import java.util.Map;
5+
import java.util.*;
116

127
import org.json.JSONArray;
138
import org.json.JSONException;
149
import org.json.JSONObject;
1510
import org.junit.Test;
1611

12+
import com.jayway.jsonpath.*;
13+
1714

1815
/**
1916
* Tests for JSON-Java JSONArray.java
@@ -306,41 +303,41 @@ public void failedGetArrayValues() {
306303
/**
307304
* Exercise JSONArray.join() by converting a JSONArray into a
308305
* comma-separated string. Since this is very nearly a JSON document,
309-
* array braces are added to the beginning and end, and it is reconverted
310-
* back to a JSONArray for comparison.
306+
* array braces are added to the beginning and end prior to validation.
311307
*/
312308
@Test
313309
public void join() {
314-
String expectedStr =
315-
"["+
316-
"true,"+
317-
"false,"+
318-
"\"true\","+
319-
"\"false\","+
320-
"\"hello\","+
321-
"0.002345,"+
322-
"\"23.45\","+
323-
"42,"+
324-
"\"43\","+
325-
"["+
326-
"\"world\""+
327-
"],"+
328-
"{"+
329-
"\"key1\":\"value1\","+
330-
"\"key2\":\"value2\","+
331-
"\"key3\":\"value3\","+
332-
"\"key4\":\"value4\""+
333-
"},"+
334-
"0,"+
335-
"\"-1\""+
336-
"]";
337-
338310
JSONArray jsonArray = new JSONArray(arrayStr);
339311
String joinStr = jsonArray.join(",");
340-
JSONArray finalJsonArray = new JSONArray("["+joinStr+"]");
341-
JSONArray expectedJsonArray = new JSONArray(expectedStr);
342-
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
343-
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
312+
313+
// validate JSON
314+
/**
315+
* Don't need to remake the JSONArray to perform the parsing
316+
*/
317+
Object doc = Configuration.defaultConfiguration().jsonProvider()
318+
.parse("["+joinStr+"]");
319+
List<?> docList = JsonPath.read(doc, "$");
320+
assertTrue("expected 13 items in top level object", docList.size() == 13);
321+
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
322+
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
323+
assertTrue("expected \"true\"", "true".equals(JsonPath.read(doc, "$[2]")));
324+
assertTrue("expected \"false\"", "false".equals(JsonPath.read(doc, "$[3]")));
325+
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[4]")));
326+
assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(JsonPath.read(doc, "$[5]")));
327+
assertTrue("expected \"23.45\"", "23.45".equals(JsonPath.read(doc, "$[6]")));
328+
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[7]")));
329+
assertTrue("expected \"43\"", "43".equals(JsonPath.read(doc, "$[8]")));
330+
docList = JsonPath.read(doc, "$[9]");
331+
assertTrue("expected 1 array item", docList.size() == 1);
332+
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]")));
333+
Map<?,?> docMap = JsonPath.read(doc, "$[10]");
334+
assertTrue("expected 4 object items", docMap.size() == 4);
335+
assertTrue("expected value1", "value1".equals(JsonPath.read(doc, "$[10].key1")));
336+
assertTrue("expected value2", "value2".equals(JsonPath.read(doc, "$[10].key2")));
337+
assertTrue("expected value3", "value3".equals(JsonPath.read(doc, "$[10].key3")));
338+
assertTrue("expected value4", "value4".equals(JsonPath.read(doc, "$[10].key4")));
339+
assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$[11]")));
340+
assertTrue("expected \"-1\"", "-1".equals(JsonPath.read(doc, "$[12]")));
344341
}
345342

346343
/**
@@ -419,33 +416,7 @@ public void opt() {
419416
*/
420417
@Test
421418
public void put() {
422-
String expectedStr =
423-
"["+
424-
"true,"+
425-
"false,"+
426-
"["+
427-
"hello,"+
428-
"world"+
429-
"],"+
430-
"2.5,"+
431-
"1,"+
432-
"45,"+
433-
"\"objectPut\","+
434-
"{"+
435-
"\"key10\":\"val10\","+
436-
"\"key20\":\"val20\","+
437-
"\"key30\":\"val30\""+
438-
"},"+
439-
"{"+
440-
"\"k1\":\"v1\""+
441-
"},"+
442-
"["+
443-
"1,"+
444-
"2"+
445-
"]"+
446-
"]";
447419
JSONArray jsonArray = new JSONArray();
448-
JSONArray expectedJsonArray = new JSONArray(expectedStr);
449420

450421
// index 0
451422
jsonArray.put(true);
@@ -488,9 +459,36 @@ public void put() {
488459
Collection<Object> collection = new ArrayList<Object>();
489460
collection.add(1);
490461
collection.add(2);
491-
// 9
462+
// 9
492463
jsonArray.put(collection);
493-
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
464+
465+
// validate JSON
466+
Object doc = Configuration.defaultConfiguration().jsonProvider()
467+
.parse(jsonArray.toString());
468+
List<?> docList = JsonPath.read(doc, "$");
469+
assertTrue("expected 10 items in top level object", docList.size() == 10);
470+
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
471+
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
472+
docList = JsonPath.read(doc, "$[2]");
473+
assertTrue("expected 2 items in array", docList.size() == 2);
474+
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
475+
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
476+
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
477+
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
478+
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
479+
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
480+
Map<?,?> docMap = JsonPath.read(doc, "$[7]");
481+
assertTrue("expected 3 items in object", docMap.size() == 3);
482+
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[7].key10")));
483+
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[7].key20")));
484+
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[7].key30")));
485+
docMap = JsonPath.read(doc, "$[8]");
486+
assertTrue("expected 1 item in object", docMap.size() == 1);
487+
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[8].k1")));
488+
docList = JsonPath.read(doc, "$[9]");
489+
assertTrue("expected 2 items in array", docList.size() == 2);
490+
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
491+
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
494492
}
495493

496494
/**
@@ -499,34 +497,7 @@ public void put() {
499497
*/
500498
@Test
501499
public void putIndex() {
502-
String expectedStr =
503-
"["+
504-
"true,"+
505-
"false,"+
506-
"["+
507-
"hello,"+
508-
"world"+
509-
"],"+
510-
"2.5,"+
511-
"1,"+
512-
"45,"+
513-
"\"objectPut\","+
514-
"null,"+
515-
"{"+
516-
"\"key10\":\"val10\","+
517-
"\"key20\":\"val20\","+
518-
"\"key30\":\"val30\""+
519-
"},"+
520-
"["+
521-
"1,"+
522-
"2"+
523-
"],"+
524-
"{"+
525-
"\"k1\":\"v1\""+
526-
"},"+
527-
"]";
528500
JSONArray jsonArray = new JSONArray();
529-
JSONArray expectedJsonArray = new JSONArray(expectedStr);
530501

531502
// 1
532503
jsonArray.put(1, false);
@@ -573,7 +544,35 @@ public void putIndex() {
573544
jsonArray.put(-1, "abc");
574545
assertTrue("put index < 0 should have thrown exception", false);
575546
} catch(Exception ignored) {}
576-
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
547+
548+
// validate JSON
549+
Object doc = Configuration.defaultConfiguration().jsonProvider()
550+
.parse(jsonArray.toString());
551+
List<?> docList = JsonPath.read(doc, "$");
552+
assertTrue("expected 11 items in top level object", docList.size() == 11);
553+
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
554+
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
555+
docList = JsonPath.read(doc, "$[2]");
556+
assertTrue("expected 2 items in array", docList.size() == 2);
557+
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
558+
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
559+
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
560+
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
561+
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
562+
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
563+
assertTrue("expected null", null == JsonPath.read(doc, "$[7]"));
564+
Map<?,?> docMap = JsonPath.read(doc, "$[8]");
565+
assertTrue("expected 3 items in object", docMap.size() == 3);
566+
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[8].key10")));
567+
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[8].key20")));
568+
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[8].key30")));
569+
docList = JsonPath.read(doc, "$[9]");
570+
assertTrue("expected 2 items in array", docList.size() == 2);
571+
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
572+
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
573+
docMap = JsonPath.read(doc, "$[10]");
574+
assertTrue("expected 1 item in object", docMap.size() == 1);
575+
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[10].k1")));
577576
}
578577

579578
/**
@@ -587,10 +586,9 @@ public void remove() {
587586
"1"+
588587
"]";
589588
JSONArray jsonArray = new JSONArray(arrayStr);
590-
JSONArray expectedJsonArray = new JSONArray();
591589
jsonArray.remove(0);
592590
assertTrue("array should be empty", null == jsonArray.remove(5));
593-
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
591+
assertTrue("jsonArray should be empty", jsonArray.length() == 0);
594592
}
595593

596594
/**
@@ -648,15 +646,22 @@ public void toJSONObject() {
648646
*/
649647
@Test
650648
public void objectArrayVsIsArray() {
651-
String expectedStr =
652-
"["+
653-
"1,2,3,4,5,6,7"+
654-
"]";
655649
int[] myInts = { 1, 2, 3, 4, 5, 6, 7 };
656650
Object myObject = myInts;
657651
JSONArray jsonArray = new JSONArray(myObject);
658-
JSONArray expectedJsonArray = new JSONArray(expectedStr);
659-
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
652+
653+
// validate JSON
654+
Object doc = Configuration.defaultConfiguration().jsonProvider()
655+
.parse(jsonArray.toString());
656+
List<?> docList = JsonPath.read(doc, "$");
657+
assertTrue("expected 7 items in top level object", docList.size() == 7);
658+
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
659+
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
660+
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
661+
assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$[3]")));
662+
assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$[4]")));
663+
assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$[5]")));
664+
assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$[6]")));
660665
}
661666

662667
/**

0 commit comments

Comments
 (0)