From d335447ab4508944ba53a3d48d5f3d4ccd603e64 Mon Sep 17 00:00:00 2001 From: rikkarth Date: Fri, 22 Mar 2024 18:28:56 +0000 Subject: [PATCH] test(#871-strictMode): add two more test which validate error correctness --- .../junit/JSONParserConfigurationTest.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index 3c1f61a..0666cb7 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -23,7 +23,7 @@ public class JSONParserConfigurationTest { @Test public void testOverwrite() { JSONObject jsonObject = new JSONObject(TEST_SOURCE, - new JSONParserConfiguration().withOverwriteDuplicateKey(true)); + new JSONParserConfiguration().withOverwriteDuplicateKey(true)); assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); } @@ -48,6 +48,42 @@ public class JSONParserConfigurationTest { strictModeInputTestCases.forEach(testCase -> new JSONArray(testCase, jsonParserConfiguration)); } + @Test + public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(true); + + String testCase = "[1,2];[3,4]"; + JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase, + JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration)); + + assertEquals("invalid character found after end of array: ; at 6 [character 7 line 1]", je.getMessage()); + } + + @Test + public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(true); + + String testCase = "[{\"test\": implied}]"; + JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase, + JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration)); + + assertEquals("Value is not surrounded by quotes: implied", je.getMessage()); + } + + @Test + public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withStrictMode(true); + + String testCase = "[{test: implied}]"; + JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase, + JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration)); + + assertEquals("Key is not surrounded by quotes: test", je.getMessage()); + } + @Test public void verifyDuplicateKeyThenMaxDepth() { JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()