1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #83 from johnjaylward/FixNPE

add test cases for null keys
This commit is contained in:
Sean Leary 2018-03-11 15:59:21 -05:00 committed by GitHub
commit 37f1f4c8ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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");
}
}