From 2a05db77d4daee588ebcf2ba88fe2c528219a24c Mon Sep 17 00:00:00 2001 From: Joe Ferner Date: Fri, 26 Feb 2016 23:51:12 -0500 Subject: [PATCH] Adds test for JSONArray toList and JSONObject toMap methods --- src/test/org/json/junit/JSONArrayTest.java | 36 +++++++++++++++++++++ src/test/org/json/junit/JSONObjectTest.java | 33 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/test/org/json/junit/JSONArrayTest.java b/src/test/org/json/junit/JSONArrayTest.java index 455d680..88f9fb1 100644 --- a/src/test/org/json/junit/JSONArrayTest.java +++ b/src/test/org/json/junit/JSONArrayTest.java @@ -1,5 +1,6 @@ package org.json.junit; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.*; @@ -686,4 +687,39 @@ public void iterator() { new Long(-1).equals(Long.parseLong((String) it.next()))); assertTrue("should be at end of array", !it.hasNext()); } + + /** + * Exercise toList. + */ + @Test + public void toList() { + assertEquals("zero length array should return zero length list", + 0, new JSONArray().toList().size()); + + JSONArray jsonArray = new JSONArray(arrayStr); + List jsonArrayAsList = jsonArray.toList(); + assertEquals("lengths should be equals", + jsonArray.length(), jsonArrayAsList.size()); + assertEquals("booleans should remain booleans", + true, jsonArrayAsList.get(0)); + assertEquals("strings should remain strings", + "hello", jsonArrayAsList.get(4)); + assertTrue("arrays should be converted to lists", + List.class.isAssignableFrom(jsonArrayAsList.get(9).getClass())); + assertEquals("arrays should be converted to lists with the same size", + 1, ((List) jsonArrayAsList.get(9)).size()); + assertEquals("arrays should be converted to lists with the same values", + "world", ((List) jsonArrayAsList.get(9)).get(0)); + assertTrue("JSONObjects should be converted to maps", + Map.class.isAssignableFrom(jsonArrayAsList.get(10).getClass())); + assertEquals("JSONObjects should be converted to maps with the same size", + 4, ((Map) jsonArrayAsList.get(10)).size()); + assertTrue("JSONObjects should be converted to maps with the same keys", + ((Map) jsonArrayAsList.get(10)).containsKey("key1")); + + jsonArray = new JSONArray("[null]"); + jsonArrayAsList = jsonArray.toList(); + assertEquals("toList should keep nulls as nulls", + null, jsonArrayAsList.get(0)); + } } diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java index 869fe46..2cc6c2d 100644 --- a/src/test/org/json/junit/JSONObjectTest.java +++ b/src/test/org/json/junit/JSONObjectTest.java @@ -1,5 +1,6 @@ package org.json.junit; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -1816,6 +1817,38 @@ public void equals() { aJsonObject.equals(aJsonObject)); } + /** + * Exercise the JSONObject toMap() method + */ + @Test + public void toMap() { + assertEquals("empty JSONObjects should return 0 length maps", + 0, new JSONObject().toMap().size()); + + String str = "{" + + "\"nullKey\":null," + + "\"stringKey\":\"hello world!\"," + + "\"arrayKey\":[1, 2, 3]," + + "\"objectKey\":{\"subKey\": \"subValue\"}" + + "}"; + JSONObject jsonObject = new JSONObject(str); + Map jsonObjectAsMap = jsonObject.toMap(); + assertEquals("JSONObject and maps should contains the same number of entries", + jsonObject.length(), jsonObjectAsMap.size()); + assertEquals("nulls should remain null", + null, jsonObjectAsMap.get("nullKey")); + assertEquals("strings should remain strings", + "hello world!", jsonObjectAsMap.get("stringKey")); + assertEquals("arrays should be converted to lists of same size", + 3, ((List) jsonObjectAsMap.get("arrayKey")).size()); + assertEquals("arrays should be converted to lists with same content", + 1, ((List) jsonObjectAsMap.get("arrayKey")).get(0)); + assertEquals("objects should be converted to maps of same size", + 1, ((Map) jsonObjectAsMap.get("objectKey")).size()); + assertEquals("objects should be converted to maps with same content", + "subValue", ((Map) jsonObjectAsMap.get("objectKey")).get("subKey")); + } + /** * JSON null is not the same as Java null. This test examines the differences * in how they are handled by JSON-java.