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