Skip to content

Commit aa5e80b

Browse files
author
John J. Aylward
committed
add test cases for null keys
1 parent fc881e2 commit aa5e80b

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

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

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,13 +1973,7 @@ public void jsonObjectParsingErrors() {
19731973
"Null pointer",
19741974
e.getMessage());
19751975
}
1976-
try {
1977-
// null put key
1978-
JSONObject jsonObject = new JSONObject("{}");
1979-
jsonObject.put(null, 0);
1980-
fail("Expected an exception");
1981-
} catch (NullPointerException ignored) {
1982-
}
1976+
19831977
try {
19841978
// multiple putOnce key
19851979
JSONObject jsonObject = new JSONObject("{}");
@@ -2182,6 +2176,10 @@ public void jsonObjectPutOnceNull() {
21822176
JSONObject jsonObject = new JSONObject();
21832177
jsonObject.putOnce(null, null);
21842178
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
2179+
jsonObject.putOnce("", null);
2180+
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
2181+
jsonObject.putOnce(null, "");
2182+
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
21852183
}
21862184

21872185
/**
@@ -2453,7 +2451,7 @@ public void write() throws IOException {
24532451
* Confirms that exceptions thrown when writing values are wrapped properly.
24542452
*/
24552453
@Test
2456-
public void testJSONWriterException() throws IOException {
2454+
public void testJSONWriterException() {
24572455
final JSONObject jsonObject = new JSONObject();
24582456

24592457
jsonObject.put("someKey",new BrokenToString());
@@ -2893,4 +2891,62 @@ public void testExceptionalBean() {
28932891
assertTrue(jo.get("closeable") instanceof JSONObject);
28942892
assertTrue(jo.getJSONObject("closeable").has("string"));
28952893
}
2894+
2895+
@Test(expected=NullPointerException.class)
2896+
public void testPutNullBoolean() {
2897+
// null put key
2898+
JSONObject jsonObject = new JSONObject("{}");
2899+
jsonObject.put(null, false);
2900+
fail("Expected an exception");
2901+
}
2902+
@Test(expected=NullPointerException.class)
2903+
public void testPutNullCollection() {
2904+
// null put key
2905+
JSONObject jsonObject = new JSONObject("{}");
2906+
jsonObject.put(null, Collections.emptySet());
2907+
fail("Expected an exception");
2908+
}
2909+
@Test(expected=NullPointerException.class)
2910+
public void testPutNullDouble() {
2911+
// null put key
2912+
JSONObject jsonObject = new JSONObject("{}");
2913+
jsonObject.put(null, 0.0d);
2914+
fail("Expected an exception");
2915+
}
2916+
@Test(expected=NullPointerException.class)
2917+
public void testPutNullFloat() {
2918+
// null put key
2919+
JSONObject jsonObject = new JSONObject("{}");
2920+
jsonObject.put(null, 0.0f);
2921+
fail("Expected an exception");
2922+
}
2923+
@Test(expected=NullPointerException.class)
2924+
public void testPutNullInt() {
2925+
// null put key
2926+
JSONObject jsonObject = new JSONObject("{}");
2927+
jsonObject.put(null, 0);
2928+
fail("Expected an exception");
2929+
}
2930+
@Test(expected=NullPointerException.class)
2931+
public void testPutNullLong() {
2932+
// null put key
2933+
JSONObject jsonObject = new JSONObject("{}");
2934+
jsonObject.put(null, 0L);
2935+
fail("Expected an exception");
2936+
}
2937+
@Test(expected=NullPointerException.class)
2938+
public void testPutNullMap() {
2939+
// null put key
2940+
JSONObject jsonObject = new JSONObject("{}");
2941+
jsonObject.put(null, Collections.emptyMap());
2942+
fail("Expected an exception");
2943+
}
2944+
@Test(expected=NullPointerException.class)
2945+
public void testPutNullObject() {
2946+
// null put key
2947+
JSONObject jsonObject = new JSONObject("{}");
2948+
jsonObject.put(null, new Object());
2949+
fail("Expected an exception");
2950+
}
2951+
28962952
}

0 commit comments

Comments
 (0)