From aa5e80bc8d694bf8a552401534d93d8938eaa665 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 7 Mar 2018 12:11:17 -0500 Subject: [PATCH] add test cases for null keys --- .../java/org/json/junit/JSONObjectTest.java | 72 ++++++++++++++++--- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 80283ae..5ada98c 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1973,13 +1973,7 @@ public class JSONObjectTest { "Null pointer", e.getMessage()); } - try { - // null put key - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.put(null, 0); - fail("Expected an exception"); - } catch (NullPointerException ignored) { - } + try { // multiple putOnce key JSONObject jsonObject = new JSONObject("{}"); @@ -2182,6 +2176,10 @@ public class JSONObjectTest { JSONObject jsonObject = new JSONObject(); jsonObject.putOnce(null, null); assertTrue("jsonObject should be empty", jsonObject.length() == 0); + jsonObject.putOnce("", null); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + jsonObject.putOnce(null, ""); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); } /** @@ -2453,7 +2451,7 @@ public class JSONObjectTest { * Confirms that exceptions thrown when writing values are wrapped properly. */ @Test - public void testJSONWriterException() throws IOException { + public void testJSONWriterException() { final JSONObject jsonObject = new JSONObject(); jsonObject.put("someKey",new BrokenToString()); @@ -2893,4 +2891,62 @@ public class JSONObjectTest { assertTrue(jo.get("closeable") instanceof JSONObject); assertTrue(jo.getJSONObject("closeable").has("string")); } + + @Test(expected=NullPointerException.class) + public void testPutNullBoolean() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, false); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullCollection() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, Collections.emptySet()); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullDouble() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0.0d); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullFloat() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0.0f); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullInt() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullLong() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0L); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullMap() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, Collections.emptyMap()); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullObject() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, new Object()); + fail("Expected an exception"); + } + }