feat(#871-strictMode): strictMode JSONArray initial implementation

test(#871-strictMode): initial test implementation
This commit is contained in:
rikkarth 2024-03-15 00:45:32 +00:00
parent dcbbccc76c
commit 63e8314deb
2 changed files with 518 additions and 722 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,19 @@
package org.json.junit; package org.json.junit;
import java.util.Arrays;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONParserConfiguration; import org.json.JSONParserConfiguration;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class JSONParserConfigurationTest { public class JSONParserConfigurationTest {
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}"; private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
@Test(expected = JSONException.class) @Test(expected = JSONException.class)
@ -19,16 +24,30 @@ public class JSONParserConfigurationTest {
@Test @Test
public void testOverwrite() { public void testOverwrite() {
JSONObject jsonObject = new JSONObject(TEST_SOURCE, JSONObject jsonObject = new JSONObject(TEST_SOURCE,
new JSONParserConfiguration().withOverwriteDuplicateKey(true)); new JSONParserConfiguration().withOverwriteDuplicateKey(true));
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
} }
@Test
public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException() {
List<String> strictModeInputTestCases = Arrays.asList("[1,2];[3,4]", "", "[1, 2,3]:[4,5]"/*, "[{test: implied}]"*/);
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withStrictMode(true);
strictModeInputTestCases.forEach(testCase -> {
System.out.println("Test case: " + testCase);
assertThrows("expected non-compliant array but got instead: " + testCase, JSONException.class,
() -> new JSONArray(testCase, jsonParserConfiguration));
System.out.println("Passed");
});
}
@Test @Test
public void verifyDuplicateKeyThenMaxDepth() { public void verifyDuplicateKeyThenMaxDepth() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withOverwriteDuplicateKey(true) .withOverwriteDuplicateKey(true)
.withMaxNestingDepth(42); .withMaxNestingDepth(42);
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
@ -37,8 +56,8 @@ public class JSONParserConfigurationTest {
@Test @Test
public void verifyMaxDepthThenDuplicateKey() { public void verifyMaxDepthThenDuplicateKey() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withMaxNestingDepth(42) .withMaxNestingDepth(42)
.withOverwriteDuplicateKey(true); .withOverwriteDuplicateKey(true);
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());