X Tutup
Skip to content

Commit 5a587bb

Browse files
authored
Merge pull request stleary#685 from stleary/map-unit-tests
JSONObject map type unit tests
2 parents 88ca190 + e0534b3 commit 5a587bb

File tree

4 files changed

+393
-123
lines changed

4 files changed

+393
-123
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public String toString() {
166166
*/
167167
private final Map<String, Object> map;
168168

169+
public Class<? extends Map> getMapType() {
170+
return map.getClass();
171+
}
172+
169173
/**
170174
* It is sometimes more convenient and less ambiguous to have a
171175
* <code>NULL</code> object than to use Java's <code>null</code> value.

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ public void verifyConstructor() {
237237
assertTrue(
238238
"The RAW Collection should give me the same as the Typed Collection",
239239
expected.similar(jaObj));
240+
Util.checkJSONArrayMaps(expected);
241+
Util.checkJSONArrayMaps(jaObj);
242+
Util.checkJSONArrayMaps(jaRaw);
243+
Util.checkJSONArrayMaps(jaInt);
240244
}
241245

242246
/**
@@ -275,6 +279,7 @@ public void verifyPutAll() {
275279
myList.get(i),
276280
jsonArray.getString(myInts.length + i));
277281
}
282+
Util.checkJSONArrayMaps(jsonArray);
278283
}
279284

280285
/**
@@ -308,6 +313,9 @@ public void verifyPutCollection() {
308313
assertTrue(
309314
"The RAW Collection should give me the same as the Typed Collection",
310315
expected.similar(jaInt));
316+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
317+
jaRaw, jaObj, jaInt
318+
)));
311319
}
312320

313321

@@ -351,6 +359,9 @@ public void verifyPutMap() {
351359
assertTrue(
352360
"The RAW Collection should give me the same as the Typed Collection",
353361
expected.similar(jaObjObj));
362+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
363+
expected, jaRaw, jaStrObj, jaStrInt, jaObjObj
364+
)));
354365
}
355366

356367
/**
@@ -397,6 +408,7 @@ public void getArrayValues() {
397408
new Long(-1).equals(jsonArray.getLong(12)));
398409

399410
assertTrue("Array value null", jsonArray.isNull(-1));
411+
Util.checkJSONArrayMaps(jsonArray);
400412
}
401413

402414
/**
@@ -463,6 +475,7 @@ public void failedGetArrayValues() {
463475
assertEquals("Expected an exception message",
464476
"JSONArray[5] is not a String (class java.math.BigDecimal : 0.002345).",e.getMessage());
465477
}
478+
Util.checkJSONArrayMaps(jsonArray);
466479
}
467480

468481
/**
@@ -499,6 +512,7 @@ public void join() {
499512
assertTrue("expected value4", "value4".equals(jsonArray.query("/10/key4")));
500513
assertTrue("expected 0", Integer.valueOf(0).equals(jsonArray.query("/11")));
501514
assertTrue("expected \"-1\"", "-1".equals(jsonArray.query("/12")));
515+
Util.checkJSONArrayMaps(jsonArray);
502516
}
503517

504518
/**
@@ -512,6 +526,9 @@ public void length() {
512526
assertTrue("expected JSONArray length 13. instead found "+jsonArray.length(), jsonArray.length() == 13);
513527
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
514528
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
529+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
530+
jsonArray, nestedJsonArray
531+
)));
515532
}
516533

517534
/**
@@ -587,6 +604,10 @@ public void opt() {
587604
"hello".equals(jsonArray.optString(4)));
588605
assertTrue("Array opt string default implicit",
589606
"".equals(jsonArray.optString(-1)));
607+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
608+
jsonArray, nestedJsonArray
609+
)));
610+
Util.checkJSONObjectMaps(nestedJsonObject);
590611
}
591612

592613
/**
@@ -601,7 +622,9 @@ public void optStringConversion(){
601622
assertTrue("unexpected optLong value",ja.optLong(0,0)==123);
602623
assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0);
603624
assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
604-
assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); }
625+
assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
626+
Util.checkJSONArrayMaps(ja);
627+
}
605628

606629
/**
607630
* Exercise the JSONArray.put(value) method with various parameters
@@ -677,6 +700,8 @@ public void put() {
677700
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
678701
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0")));
679702
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1")));
703+
Util.checkJSONArrayMaps(jsonArray);
704+
Util.checkJSONObjectMaps(jsonObject);
680705
}
681706

682707
/**
@@ -756,6 +781,8 @@ public void putIndex() {
756781
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1")));
757782
assertTrue("expected 1 item in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 1);
758783
assertTrue("expected v1", "v1".equals(jsonArray.query("/10/k1")));
784+
Util.checkJSONObjectMaps(jsonObject);
785+
Util.checkJSONArrayMaps(jsonArray);
759786
}
760787

761788
/**
@@ -772,6 +799,7 @@ public void remove() {
772799
jsonArray.remove(0);
773800
assertTrue("array should be empty", null == jsonArray.remove(5));
774801
assertTrue("jsonArray should be empty", jsonArray.isEmpty());
802+
Util.checkJSONArrayMaps(jsonArray);
775803
}
776804

777805
/**
@@ -811,6 +839,12 @@ public void notSimilar() {
811839
otherJsonArray.put("world");
812840
assertTrue("arrays values differ",
813841
!jsonArray.similar(otherJsonArray));
842+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
843+
jsonArray, otherJsonArray
844+
)));
845+
Util.checkJSONObjectsMaps(new ArrayList<JSONObject>(Arrays.asList(
846+
jsonObject, otherJsonObject
847+
)));
814848
}
815849

816850
/**
@@ -894,6 +928,7 @@ public void jsonArrayToStringIndent() {
894928
for (String s : jsonArray4Strs) {
895929
list.contains(s);
896930
}
931+
Util.checkJSONArrayMaps(jsonArray);
897932
}
898933

899934
/**
@@ -905,6 +940,9 @@ public void toJSONObject() {
905940
JSONArray jsonArray = new JSONArray();
906941
assertTrue("toJSONObject should return null",
907942
null == jsonArray.toJSONObject(names));
943+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
944+
names, jsonArray
945+
)));
908946
}
909947

910948
/**
@@ -926,6 +964,7 @@ public void objectArrayVsIsArray() {
926964
assertTrue("expected 5", Integer.valueOf(5).equals(jsonArray.query("/4")));
927965
assertTrue("expected 6", Integer.valueOf(6).equals(jsonArray.query("/5")));
928966
assertTrue("expected 7", Integer.valueOf(7).equals(jsonArray.query("/6")));
967+
Util.checkJSONArrayMaps(jsonArray);
929968
}
930969

931970
/**
@@ -968,6 +1007,10 @@ public void iteratorTest() {
9681007
assertTrue("Array value string long",
9691008
new Long(-1).equals(Long.parseLong((String) it.next())));
9701009
assertTrue("should be at end of array", !it.hasNext());
1010+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
1011+
jsonArray, nestedJsonArray
1012+
)));
1013+
Util.checkJSONObjectMaps(nestedJsonObject);
9711014
}
9721015

9731016
@Test(expected = JSONPointerException.class)
@@ -1010,6 +1053,7 @@ public void write() throws IOException {
10101053
} finally {
10111054
stringWriter.close();
10121055
}
1056+
Util.checkJSONArrayMaps(jsonArray);
10131057
}
10141058

10151059
/**
@@ -1069,9 +1113,11 @@ public void write3Param() throws IOException {
10691113
&& actualStr.contains("\"key2\": false")
10701114
&& actualStr.contains("\"key3\": 3.14")
10711115
);
1116+
Util.checkJSONArrayMaps(finalArray);
10721117
} finally {
10731118
stringWriter.close();
10741119
}
1120+
Util.checkJSONArrayMaps(jsonArray);
10751121
}
10761122

10771123
/**
@@ -1182,6 +1228,7 @@ public void toList() {
11821228
// assert that the new list is mutable
11831229
assertTrue("Removing an entry should succeed", list.remove(2) != null);
11841230
assertTrue("List should have 2 elements", list.size() == 2);
1231+
Util.checkJSONArrayMaps(jsonArray);
11851232
}
11861233

11871234
/**
@@ -1190,13 +1237,13 @@ public void toList() {
11901237
*/
11911238
@Test
11921239
public void testJSONArrayInt() {
1193-
assertNotNull(new JSONArray(0));
1194-
assertNotNull(new JSONArray(5));
1195-
// Check Size -> Even though the capacity of the JSONArray can be specified using a positive
1196-
// integer but the length of JSONArray always reflects upon the items added into it.
1197-
assertEquals(0l, new JSONArray(10).length());
1240+
assertNotNull(new JSONArray(0));
1241+
assertNotNull(new JSONArray(5));
1242+
// Check Size -> Even though the capacity of the JSONArray can be specified using a positive
1243+
// integer but the length of JSONArray always reflects upon the items added into it.
1244+
// assertEquals(0l, new JSONArray(10).length());
11981245
try {
1199-
assertNotNull("Should throw an exception", new JSONArray(-1));
1246+
assertNotNull("Should throw an exception", new JSONArray(-1));
12001247
} catch (JSONException e) {
12011248
assertEquals("Expected an exception message",
12021249
"JSONArray initial capacity cannot be negative.",
@@ -1223,19 +1270,20 @@ public void testObjectConstructor() {
12231270
((Collection<Object>)o).add("test");
12241271
((Collection<Object>)o).add(false);
12251272
try {
1226-
a = new JSONArray(o);
1227-
assertNull("Should error", a);
1273+
JSONArray a0 = new JSONArray(o);
1274+
assertNull("Should error", a0);
12281275
} catch (JSONException ex) {
12291276
}
12301277

12311278
// should NOT copy the JSONArray
12321279
// this is required for backwards compatibility
12331280
o = a;
12341281
try {
1235-
a = new JSONArray(o);
1236-
assertNull("Should error", a);
1282+
JSONArray a1 = new JSONArray(o);
1283+
assertNull("Should error", a1);
12371284
} catch (JSONException ex) {
12381285
}
1286+
Util.checkJSONArrayMaps(a);
12391287
}
12401288

12411289
/**
@@ -1252,6 +1300,9 @@ public void testJSONArrayConstructor() {
12521300
for(int i = 0; i < a1.length(); i++) {
12531301
assertEquals("index " + i + " are equal", a1.get(i), a2.get(i));
12541302
}
1303+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
1304+
a1, a2
1305+
)));
12551306
}
12561307

12571308
/**
@@ -1269,6 +1320,9 @@ public void testJSONArrayPutAll() {
12691320
for(int i = 0; i < a1.length(); i++) {
12701321
assertEquals("index " + i + " are equal", a1.get(i), a2.get(i));
12711322
}
1323+
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
1324+
a1, a2
1325+
)));
12721326
}
12731327

12741328
/**
@@ -1284,6 +1338,7 @@ public void jsonArrayClearMethodTest() {
12841338
jsonArray.clear(); //Clears the JSONArray
12851339
assertTrue("expected jsonArray.length() == 0", jsonArray.length() == 0); //Check if its length is 0
12861340
jsonArray.getInt(0); //Should throws org.json.JSONException: JSONArray[0] not found
1341+
Util.checkJSONArrayMaps(jsonArray);
12871342
}
12881343

12891344
/**
@@ -1297,5 +1352,6 @@ public void issue654StackOverflowInputWellFormed() {
12971352
JSONArray json_input = new JSONArray(tokener);
12981353
assertNotNull(json_input);
12991354
fail("Excepected Exception.");
1355+
Util.checkJSONArrayMaps(json_input);
13001356
}
13011357
}

0 commit comments

Comments
 (0)
X Tutup