Skip to content

Commit d335447

Browse files
author
rikkarth
committed
test(#871-strictMode): add two more test which validate error correctness
1 parent 8f66865 commit d335447

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void testThrowException() {
2323
@Test
2424
public void testOverwrite() {
2525
JSONObject jsonObject = new JSONObject(TEST_SOURCE,
26-
new JSONParserConfiguration().withOverwriteDuplicateKey(true));
26+
new JSONParserConfiguration().withOverwriteDuplicateKey(true));
2727

2828
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
2929
}
@@ -48,6 +48,42 @@ public void givenInvalidInputArrays_testStrictModeFalse_shouldNotThrowAnyExcepti
4848
strictModeInputTestCases.forEach(testCase -> new JSONArray(testCase, jsonParserConfiguration));
4949
}
5050

51+
@Test
52+
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
53+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
54+
.withStrictMode(true);
55+
56+
String testCase = "[1,2];[3,4]";
57+
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
58+
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
59+
60+
assertEquals("invalid character found after end of array: ; at 6 [character 7 line 1]", je.getMessage());
61+
}
62+
63+
@Test
64+
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
65+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
66+
.withStrictMode(true);
67+
68+
String testCase = "[{\"test\": implied}]";
69+
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
70+
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
71+
72+
assertEquals("Value is not surrounded by quotes: implied", je.getMessage());
73+
}
74+
75+
@Test
76+
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
77+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
78+
.withStrictMode(true);
79+
80+
String testCase = "[{test: implied}]";
81+
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
82+
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
83+
84+
assertEquals("Key is not surrounded by quotes: test", je.getMessage());
85+
}
86+
5187
@Test
5288
public void verifyDuplicateKeyThenMaxDepth() {
5389
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()

0 commit comments

Comments
 (0)