mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 11:25:30 -04:00
Merge pull request #938 from Simulant87/remove-references
Strict mode unit tests
This commit is contained in:
commit
1d81e8879a
@ -38,7 +38,18 @@ public class JSONTokener {
|
|||||||
/**
|
/**
|
||||||
* Construct a JSONTokener from a Reader. The caller must close the Reader.
|
* 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) {
|
public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) {
|
||||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||||
@ -54,10 +65,6 @@ public class JSONTokener {
|
|||||||
this.line = 1;
|
this.line = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONTokener(Reader reader) {
|
|
||||||
this(reader, new JSONParserConfiguration());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
|
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
|
||||||
* @param inputStream The source.
|
* @param inputStream The source.
|
||||||
@ -69,23 +76,29 @@ public class JSONTokener {
|
|||||||
/**
|
/**
|
||||||
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
|
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
|
||||||
* @param inputStream The source.
|
* @param inputStream The source.
|
||||||
|
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||||
*/
|
*/
|
||||||
public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) {
|
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.
|
* Construct a JSONTokener from a string.
|
||||||
*
|
*
|
||||||
* @param s A source string.
|
* @param source A source string.
|
||||||
*/
|
*/
|
||||||
public JSONTokener(String s) {
|
public JSONTokener(String source) {
|
||||||
this(new StringReader(s));
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,6 +4,7 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONParserConfiguration;
|
import org.json.JSONParserConfiguration;
|
||||||
|
import org.json.JSONTokener;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -490,6 +491,40 @@ public class JSONParserConfigurationTest {
|
|||||||
je.getMessage());
|
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 method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||||
* this class.
|
* this class.
|
||||||
|
@ -325,4 +325,21 @@ public class JSONTokenerTest {
|
|||||||
assertEquals("Stream closed", exception.getMessage());
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user