Skip to content

Commit 20f3f99

Browse files
committed
add new test cases for JSONObject and JSONArray Constructors with JSONTokener and strict mode
they are failing, because the expected strict mode is not validated
1 parent 391c869 commit 20f3f99

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.json.junit;
22

3-
import org.json.JSONArray;
4-
import org.json.JSONException;
5-
import org.json.JSONObject;
6-
import org.json.JSONParserConfiguration;
3+
import org.json.*;
74
import org.junit.Test;
85

96
import java.io.IOException;
@@ -490,6 +487,40 @@ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurround
490487
je.getMessage());
491488
}
492489

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

0 commit comments

Comments
 (0)