Skip to content

Commit 1d81e88

Browse files
authored
Merge pull request #938 from Simulant87/remove-references
Strict mode unit tests
2 parents dde9d7e + 94341cd commit 1d81e88

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

src/main/java/org/json/JSONTokener.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,18 @@ public class JSONTokener {
3838
/**
3939
* Construct a JSONTokener from a Reader. The caller must close the Reader.
4040
*
41-
* @param reader A reader.
41+
* @param reader the source.
42+
*/
43+
public JSONTokener(Reader reader) {
44+
this(reader, new JSONParserConfiguration());
45+
}
46+
47+
/**
48+
* Construct a JSONTokener from a Reader with a given JSONParserConfiguration. The caller must close the Reader.
49+
*
50+
* @param reader the source.
51+
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
52+
*
4253
*/
4354
public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) {
4455
this.jsonParserConfiguration = jsonParserConfiguration;
@@ -54,10 +65,6 @@ public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguratio
5465
this.line = 1;
5566
}
5667

57-
public JSONTokener(Reader reader) {
58-
this(reader, new JSONParserConfiguration());
59-
}
60-
6168
/**
6269
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
6370
* @param inputStream The source.
@@ -69,23 +76,29 @@ public JSONTokener(InputStream inputStream) {
6976
/**
7077
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
7178
* @param inputStream The source.
79+
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
7280
*/
7381
public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) {
74-
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")),jsonParserConfiguration);
82+
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")), jsonParserConfiguration);
7583
}
7684

7785

7886
/**
7987
* Construct a JSONTokener from a string.
8088
*
81-
* @param s A source string.
89+
* @param source A source string.
8290
*/
83-
public JSONTokener(String s) {
84-
this(new StringReader(s));
91+
public JSONTokener(String source) {
92+
this(new StringReader(source));
8593
}
8694

87-
public JSONTokener(String s, JSONParserConfiguration jsonParserConfiguration) {
88-
this(new StringReader(s), jsonParserConfiguration);
95+
/**
96+
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
97+
* @param source The source.
98+
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
99+
*/
100+
public JSONTokener(String source, JSONParserConfiguration jsonParserConfiguration) {
101+
this(new StringReader(source), jsonParserConfiguration);
89102
}
90103

91104
/**

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

+35
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.

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

+17
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,21 @@ public void testAutoClose(){
325325
assertEquals("Stream closed", exception.getMessage());
326326
}
327327
}
328+
329+
@Test
330+
public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
331+
String input = "{\"invalidInput\": [],}";
332+
JSONTokener tokener = new JSONTokener(input);
333+
Object value = tokener.nextValue();
334+
assertEquals(new JSONObject(input).toString(), value.toString());
335+
}
336+
337+
@Test
338+
public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
339+
String input = "[\"invalidInput\",]";
340+
JSONTokener tokener = new JSONTokener(input);
341+
Object value = tokener.nextValue();
342+
assertEquals(new JSONArray(input).toString(), value.toString());
343+
}
344+
328345
}

0 commit comments

Comments
 (0)