Skip to content

Commit ad44a92

Browse files
committed
add new test cases for JSONObject and JSONArray Constructors with JSONTokener and strict mode
1 parent 3b7ba07 commit ad44a92

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.json.JSONException;
55
import org.json.JSONObject;
66
import org.json.JSONParserConfiguration;
7+
import org.json.JSONTokener;
78
import org.junit.Test;
89

910
import java.io.IOException;
@@ -490,6 +491,40 @@ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurround
490491
je.getMessage());
491492
}
492493

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+
493528
/**
494529
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
495530
* this class.

0 commit comments

Comments
 (0)