Skip to content

Commit 4929fc9

Browse files
author
rikkarth
committed
test(stleary#871-strictMode): added more test cases, improved existing ones
1 parent 372f5ca commit 4929fc9

File tree

2 files changed

+416
-8
lines changed

2 files changed

+416
-8
lines changed

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

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package org.json.junit;
22

3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
36
import java.util.Arrays;
47
import java.util.List;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
510
import org.json.JSONArray;
611
import org.json.JSONException;
712
import org.json.JSONObject;
@@ -13,6 +18,7 @@
1318
import static org.junit.Assert.assertTrue;
1419

1520
public class JSONParserConfigurationTest {
21+
1622
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
1723

1824
@Test(expected = JSONException.class)
@@ -23,28 +29,42 @@ public void testThrowException() {
2329
@Test
2430
public void testOverwrite() {
2531
JSONObject jsonObject = new JSONObject(TEST_SOURCE,
26-
new JSONParserConfiguration().withOverwriteDuplicateKey(true));
32+
new JSONParserConfiguration().withOverwriteDuplicateKey(true));
2733

2834
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
2935
}
3036

3137
@Test
3238
public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException() {
33-
List<String> strictModeInputTestCases = getNonCompliantJSONList();
3439
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
3540
.withStrictMode(true);
3641

42+
List<String> strictModeInputTestCases = getNonCompliantJSONList();
43+
3744
strictModeInputTestCases.forEach(
3845
testCase -> assertThrows("expected non-compliant array but got instead: " + testCase, JSONException.class,
3946
() -> new JSONArray(testCase, jsonParserConfiguration)));
4047
}
4148

49+
@Test
50+
public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
51+
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
52+
String compliantJsonArrayAsString = lines.collect(Collectors.joining());
53+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
54+
.withStrictMode(true);
55+
56+
new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration);
57+
}
58+
59+
}
60+
4261
@Test
4362
public void givenInvalidInputArrays_testStrictModeFalse_shouldNotThrowAnyException() {
44-
List<String> strictModeInputTestCases = getNonCompliantJSONList();
4563
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
4664
.withStrictMode(false);
4765

66+
List<String> strictModeInputTestCases = getNonCompliantJSONList();
67+
4868
strictModeInputTestCases.forEach(testCase -> new JSONArray(testCase, jsonParserConfiguration));
4969
}
5070

@@ -54,24 +74,88 @@ public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacte
5474
.withStrictMode(true);
5575

5676
String testCase = "[1,2];[3,4]";
77+
5778
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
5879
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
5980

6081
assertEquals("invalid character found after end of array: ; at 6 [character 7 line 1]", je.getMessage());
6182
}
6283

84+
@Test
85+
public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
86+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
87+
.withStrictMode(true);
88+
89+
String testCase = "[\"1\",\"2\"];[3,4]";
90+
91+
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
92+
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
93+
94+
assertEquals("invalid character found after end of array: ; at 10 [character 11 line 1]", je.getMessage());
95+
}
96+
6397
@Test
6498
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
6599
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
66100
.withStrictMode(true);
67101

68102
String testCase = "[{\"test\": implied}]";
103+
69104
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
70105
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
71106

72107
assertEquals("Value is not surrounded by quotes: implied", je.getMessage());
73108
}
74109

110+
@Test
111+
public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() {
112+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
113+
.withStrictMode(false);
114+
115+
String testCase = "[{\"test\": implied}]";
116+
117+
new JSONArray(testCase, jsonParserConfiguration);
118+
}
119+
120+
@Test
121+
public void givenUnbalancedQuotes_testStrictModeTrue_shouldThrowJsonExceptionWtihConcreteErrorDescription() {
122+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
123+
.withStrictMode(true);
124+
125+
String testCaseOne = "[\"abc', \"test\"]";
126+
String testCaseTwo = "['abc\", \"test\"]";
127+
128+
JSONException jeOne = assertThrows(JSONException.class,
129+
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
130+
JSONException jeTwo = assertThrows(JSONException.class,
131+
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
132+
133+
assertEquals(
134+
"Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]",
135+
jeOne.getMessage());
136+
assertEquals(
137+
"Field contains unbalanced quotes. Starts with ' but ends with double quote. at 6 [character 7 line 1]",
138+
jeTwo.getMessage());
139+
}
140+
141+
@Test
142+
public void givenUnbalancedQuotes_testStrictModeFalse_shouldThrowJsonException() {
143+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
144+
.withStrictMode(false);
145+
146+
String testCaseOne = "[\"abc', \"test\"]";
147+
String testCaseTwo = "['abc\", \"test\"]";
148+
149+
JSONException jeOne = assertThrows(JSONException.class,
150+
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
151+
JSONException jeTwo = assertThrows(JSONException.class,
152+
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
153+
154+
assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage());
155+
assertEquals("Unterminated string at 15 [character 16 line 1]", jeTwo.getMessage());
156+
}
157+
158+
75159
@Test
76160
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
77161
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
@@ -81,14 +165,14 @@ public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurrounde
81165
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
82166
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
83167

84-
assertEquals("Key is not surrounded by quotes: test", je.getMessage());
168+
assertEquals(String.format("Value is not surrounded by quotes: %s", "test"), je.getMessage());
85169
}
86170

87171
@Test
88172
public void verifyDuplicateKeyThenMaxDepth() {
89173
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
90-
.withOverwriteDuplicateKey(true)
91-
.withMaxNestingDepth(42);
174+
.withOverwriteDuplicateKey(true)
175+
.withMaxNestingDepth(42);
92176

93177
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
94178
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
@@ -97,16 +181,23 @@ public void verifyDuplicateKeyThenMaxDepth() {
97181
@Test
98182
public void verifyMaxDepthThenDuplicateKey() {
99183
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
100-
.withMaxNestingDepth(42)
101-
.withOverwriteDuplicateKey(true);
184+
.withMaxNestingDepth(42)
185+
.withOverwriteDuplicateKey(true);
102186

103187
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
104188
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
105189
}
106190

191+
/**
192+
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
193+
* this class.
194+
*
195+
* @return List with JSON strings.
196+
*/
107197
private List<String> getNonCompliantJSONList() {
108198
return Arrays.asList(
109199
"[1,2];[3,4]",
200+
"[test]",
110201
"[1, 2,3]:[4,5]",
111202
"[{test: implied}]",
112203
"[{\"test\": implied}]",

0 commit comments

Comments
 (0)