|
1 | 1 | package org.json.junit; |
2 | 2 |
|
3 | | -import org.json.JSONArray; |
4 | | -import org.json.JSONException; |
5 | | -import org.json.JSONObject; |
6 | | -import org.json.JSONParserConfiguration; |
| 3 | +import org.json.*; |
7 | 4 | import org.junit.Test; |
8 | 5 |
|
9 | 6 | import java.io.IOException; |
@@ -490,6 +487,40 @@ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurround |
490 | 487 | je.getMessage()); |
491 | 488 | } |
492 | 489 |
|
| 490 | + @Test |
| 491 | + public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingJSONTokener_shouldThrowJSONException() { |
| 492 | + JSONException exception = assertThrows(JSONException.class, () -> { |
| 493 | + new JSONObject(new JSONTokener("{\"key\":\"value\"} invalid trailing text"), new JSONParserConfiguration().withStrictMode(true)); |
| 494 | + }); |
| 495 | + |
| 496 | + assertEquals("Strict mode error: Unparsed characters found at end of input text", exception.getMessage()); |
| 497 | + } |
| 498 | + |
| 499 | + @Test |
| 500 | + public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingString_shouldThrowJSONException() { |
| 501 | + JSONException exception = assertThrows(JSONException.class, () -> { |
| 502 | + new JSONObject("{\"key\":\"value\"} invalid trailing text", new JSONParserConfiguration().withStrictMode(true)); |
| 503 | + }); |
| 504 | + assertEquals("Strict mode error: Unparsed characters found at end of input text", exception.getMessage()); |
| 505 | + } |
| 506 | + |
| 507 | + @Test |
| 508 | + public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingJSONTokener_shouldThrowJSONException() { |
| 509 | + JSONException exception = assertThrows(JSONException.class, () -> { |
| 510 | + new JSONArray(new JSONTokener("[\"value\"] invalid trailing text"), new JSONParserConfiguration().withStrictMode(true)); |
| 511 | + }); |
| 512 | + |
| 513 | + assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage()); |
| 514 | + } |
| 515 | + |
| 516 | + @Test |
| 517 | + public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingString_shouldThrowJSONException() { |
| 518 | + JSONException exception = assertThrows(JSONException.class, () -> { |
| 519 | + new JSONArray("[\"value\"] invalid trailing text", new JSONParserConfiguration().withStrictMode(true)); |
| 520 | + }); |
| 521 | + assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage()); |
| 522 | + } |
| 523 | + |
493 | 524 | /** |
494 | 525 | * This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in |
495 | 526 | * this class. |
|
0 commit comments