|
16 | 16 | import java.io.Reader;
|
17 | 17 | import java.io.StringReader;
|
18 | 18 |
|
19 |
| -import org.json.JSONArray; |
20 |
| -import org.json.JSONException; |
21 |
| -import org.json.JSONObject; |
22 |
| -import org.json.JSONTokener; |
| 19 | +import org.json.*; |
23 | 20 | import org.junit.Test;
|
24 | 21 |
|
25 | 22 | /**
|
@@ -98,7 +95,17 @@ public void testValid() {
|
98 | 95 | checkValid(" [] ",JSONArray.class);
|
99 | 96 | checkValid("[1,2]",JSONArray.class);
|
100 | 97 | checkValid("\n\n[1,2]\n\n",JSONArray.class);
|
101 |
| - checkValid("1 2", String.class); |
| 98 | + |
| 99 | + // Test should fail if default strictMode is true, pass if false |
| 100 | + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); |
| 101 | + if (jsonParserConfiguration.isStrictMode()) { |
| 102 | + try { |
| 103 | + checkValid("1 2", String.class); |
| 104 | + assertEquals("Expected to throw exception due to invalid string", true, false); |
| 105 | + } catch (JSONException e) { } |
| 106 | + } else { |
| 107 | + checkValid("1 2", String.class); |
| 108 | + } |
102 | 109 | }
|
103 | 110 |
|
104 | 111 | @Test
|
@@ -330,16 +337,37 @@ public void testAutoClose(){
|
330 | 337 | public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
|
331 | 338 | String input = "{\"invalidInput\": [],}";
|
332 | 339 | JSONTokener tokener = new JSONTokener(input);
|
333 |
| - Object value = tokener.nextValue(); |
334 |
| - assertEquals(new JSONObject(input).toString(), value.toString()); |
| 340 | + |
| 341 | + // Test should fail if default strictMode is true, pass if false |
| 342 | + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); |
| 343 | + if (jsonParserConfiguration.isStrictMode()) { |
| 344 | + try { |
| 345 | + Object value = tokener.nextValue(); |
| 346 | + assertEquals(new JSONObject(input).toString(), value.toString()); |
| 347 | + assertEquals("Expected to throw exception due to invalid string", true, false); |
| 348 | + } catch (JSONException e) { } |
| 349 | + } else { |
| 350 | + Object value = tokener.nextValue(); |
| 351 | + assertEquals(new JSONObject(input).toString(), value.toString()); |
| 352 | + } |
335 | 353 | }
|
336 | 354 |
|
337 | 355 | @Test
|
338 | 356 | public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
|
339 | 357 | String input = "[\"invalidInput\",]";
|
340 | 358 | JSONTokener tokener = new JSONTokener(input);
|
341 |
| - Object value = tokener.nextValue(); |
342 |
| - assertEquals(new JSONArray(input).toString(), value.toString()); |
343 |
| - } |
344 | 359 |
|
| 360 | + // Test should fail if default strictMode is true, pass if false |
| 361 | + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); |
| 362 | + if (jsonParserConfiguration.isStrictMode()) { |
| 363 | + try { |
| 364 | + Object value = tokener.nextValue(); |
| 365 | + assertEquals(new JSONArray(input).toString(), value.toString()); |
| 366 | + assertEquals("Expected to throw exception due to invalid string", true, false); |
| 367 | + } catch (JSONException e) { } |
| 368 | + } else { |
| 369 | + Object value = tokener.nextValue(); |
| 370 | + assertEquals(new JSONArray(input).toString(), value.toString()); |
| 371 | + } |
| 372 | + } |
345 | 373 | }
|
0 commit comments