From 4a2f9b8cd395cf60582ec44fe0ebd66c3ba26769 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 14 Oct 2015 15:15:24 -0400 Subject: [PATCH] Adds test cases for corrected generic constructors and put methods --- JSONArrayTest.java | 123 +++++++++++++++++++++++++-- JSONObjectTest.java | 203 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 285 insertions(+), 41 deletions(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index 4c21456..1cb5dc3 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -1,10 +1,17 @@ package org.json.junit; -import static org.junit.Assert.*; - -import java.util.*; - -import org.json.*; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; @@ -80,6 +87,112 @@ public void badObject() { equals(e.getMessage())); } } + + /** + * Verifies that the constructor has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyConstructor() { + + final JSONArray expected = new JSONArray("[10]"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(myRawC); + + Collection myCInt = Collections.singleton(Integer.valueOf(10)); + JSONArray jaInt = new JSONArray(myCInt); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONArray jaObj = new JSONArray(myCObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + } + + /** + * Verifies that the put Collection has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutCollection() { + + final JSONArray expected = new JSONArray("[[10]]"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(); + jaRaw.put(myRawC); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONArray jaObj = new JSONArray(); + jaObj.put(myCObj); + + Collection myCInt = Collections.singleton(Integer.valueOf(10)); + JSONArray jaInt = new JSONArray(); + jaInt.put(myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + } + + + /** + * Verifies that the put Map has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutMap() { + + final JSONArray expected = new JSONArray("[{\"myKey\":10}]"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(); + jaRaw.put(myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONArray jaStrObj = new JSONArray(); + jaStrObj.put(myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONArray jaStrInt = new JSONArray(); + jaStrInt.put(myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONArray jaObjObj = new JSONArray(); + jaObjObj.put(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } /** * Create a JSONArray doc with a variety of different elements. diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 240bbeb..1eec143 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1,15 +1,32 @@ package org.json.junit; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import java.io.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.*; - -import org.json.*; -import org.junit.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import org.json.CDL; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONString; +import org.json.XML; +import org.junit.Test; /** * Used in testing when a JSONString is needed @@ -149,6 +166,122 @@ public void jsonObjectByMap() { JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + + /** + * Verifies that the constructor has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyConstructor() { + + final JSONObject expected = new JSONObject("{\"myKey\":10}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } + + /** + * Verifies that the put Collection has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutCollection() { + + final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myCollection", myRawC); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONObject jaObj = new JSONObject(); + jaObj.put("myCollection", myCObj); + + Collection myCInt = Collections.singleton(Integer + .valueOf(10)); + JSONObject jaInt = new JSONObject(); + jaInt.put("myCollection", myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + } + + + /** + * Verifies that the put Map has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutMap() { + + final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myMap", myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(); + jaStrObj.put("myMap", myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(); + jaStrInt.put("myMap", myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(); + jaObjObj.put("myMap", myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } + /** * JSONObjects can be built from a Map. @@ -1229,10 +1362,9 @@ public void jsonObjectToString() { * Confirm that map and nested JSONObject have the same contents. */ @Test - @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToMap() { JSONObject jsonObject = new JSONObject(); - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("abc", "def"); jsonObject.put("key", map); String toStr = jsonObject.toString(); @@ -1245,7 +1377,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { * in the debugger, one is a map and the other is a JSONObject. * TODO: write a util method for such comparisons */ - map = (Map)jsonObject.get("key"); + assertTrue("Maps should be entered as JSONObject", jsonObject.get("key") instanceof JSONObject); JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); assertTrue("value size should be equal", map.size() == mapJsonObject.length() && map.size() == 1); @@ -1264,32 +1396,31 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { * Confirm that collection and nested JSONArray have the same contents. */ @Test - @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToCollection() { - JSONObject jsonObject = new JSONObject(); - Collection collection = new ArrayList(); - collection.add("abc"); - // ArrayList will be added as an object - jsonObject.put("key", collection); - String toStr = jsonObject.toString(); - // [abc] will be added as a JSONArray - JSONObject expectedJsonObject = new JSONObject(toStr); - /** - * Can't do a Util compare because although they look the same - * in the debugger, one is a collection and the other is a JSONArray. - */ - assertTrue("keys should be equal", - jsonObject.keySet().iterator().next().equals( - expectedJsonObject.keySet().iterator().next())); - collection = (Collection)jsonObject.get("key"); - JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); - assertTrue("value size should be equal", - collection.size() == jsonArray.length()); - Iterator it = collection.iterator(); - for (int i = 0; i < collection.size(); ++i) { - assertTrue("items should be equal for index: "+i, - jsonArray.get(i).toString().equals(it.next().toString())); - } + JSONObject jsonObject = new JSONObject(); + Collection collection = new ArrayList(); + collection.add("abc"); + // ArrayList will be added as an object + jsonObject.put("key", collection); + String toStr = jsonObject.toString(); + // [abc] will be added as a JSONArray + JSONObject expectedJsonObject = new JSONObject(toStr); + /** + * Can't do a Util compare because although they look the same in the + * debugger, one is a collection and the other is a JSONArray. + */ + assertTrue("keys should be equal", jsonObject.keySet().iterator() + .next().equals(expectedJsonObject.keySet().iterator().next())); + assertTrue("Collections should be converted to JSONArray", + jsonObject.get("key") instanceof JSONArray); + JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); + assertTrue("value size should be equal", + collection.size() == jsonArray.length()); + Iterator it = collection.iterator(); + for (int i = 0; i < collection.size(); ++i) { + assertTrue("items should be equal for index: " + i, jsonArray + .get(i).toString().equals(it.next().toString())); + } } /**