Skip to content

Commit

Permalink
Merge pull request #938 from Simulant87/remove-references
Browse files Browse the repository at this point in the history
Strict mode unit tests
  • Loading branch information
stleary authored Jan 19, 2025
2 parents dde9d7e + 94341cd commit 1d81e88
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ public class JSONTokener {
/**
* Construct a JSONTokener from a Reader. The caller must close the Reader.
*
* @param reader A reader.
* @param reader the source.
*/
public JSONTokener(Reader reader) {
this(reader, new JSONParserConfiguration());
}

/**
* Construct a JSONTokener from a Reader with a given JSONParserConfiguration. The caller must close the Reader.
*
* @param reader the source.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
*
*/
public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) {
this.jsonParserConfiguration = jsonParserConfiguration;
Expand All @@ -54,10 +65,6 @@ public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguratio
this.line = 1;
}

public JSONTokener(Reader reader) {
this(reader, new JSONParserConfiguration());
}

/**
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
* @param inputStream The source.
Expand All @@ -69,23 +76,29 @@ public JSONTokener(InputStream inputStream) {
/**
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
* @param inputStream The source.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
*/
public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) {
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")),jsonParserConfiguration);
this(new InputStreamReader(inputStream, Charset.forName("UTF-8")), jsonParserConfiguration);
}


/**
* Construct a JSONTokener from a string.
*
* @param s A source string.
* @param source A source string.
*/
public JSONTokener(String s) {
this(new StringReader(s));
public JSONTokener(String source) {
this(new StringReader(source));
}

public JSONTokener(String s, JSONParserConfiguration jsonParserConfiguration) {
this(new StringReader(s), jsonParserConfiguration);
/**
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
* @param source The source.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
*/
public JSONTokener(String source, JSONParserConfiguration jsonParserConfiguration) {
this(new StringReader(source), jsonParserConfiguration);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/json/junit/JSONParserConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONParserConfiguration;
import org.json.JSONTokener;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -490,6 +491,40 @@ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurround
je.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingJSONTokener_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONObject(new JSONTokener("{\"key\":\"value\"} invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
});

assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingString_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONObject("{\"key\":\"value\"} invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
});
assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingJSONTokener_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONArray(new JSONTokener("[\"value\"] invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
});

assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
}

@Test
public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingString_shouldThrowJSONException() {
JSONException exception = assertThrows(JSONException.class, () -> {
new JSONArray("[\"value\"] invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
});
assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
}

/**
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
* this class.
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/json/junit/JSONTokenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,21 @@ public void testAutoClose(){
assertEquals("Stream closed", exception.getMessage());
}
}

@Test
public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
String input = "{\"invalidInput\": [],}";
JSONTokener tokener = new JSONTokener(input);
Object value = tokener.nextValue();
assertEquals(new JSONObject(input).toString(), value.toString());
}

@Test
public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
String input = "[\"invalidInput\",]";
JSONTokener tokener = new JSONTokener(input);
Object value = tokener.nextValue();
assertEquals(new JSONArray(input).toString(), value.toString());
}

}

0 comments on commit 1d81e88

Please sign in to comment.