mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-10 12:34:30 +08:00
test(#871-strictMode): added more test cases, improved existing ones
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package org.json.junit;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -13,6 +18,7 @@ import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class JSONParserConfigurationTest {
|
||||
|
||||
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
|
||||
|
||||
@Test(expected = JSONException.class)
|
||||
@@ -23,28 +29,42 @@ public class JSONParserConfigurationTest {
|
||||
@Test
|
||||
public void testOverwrite() {
|
||||
JSONObject jsonObject = new JSONObject(TEST_SOURCE,
|
||||
new JSONParserConfiguration().withOverwriteDuplicateKey(true));
|
||||
new JSONParserConfiguration().withOverwriteDuplicateKey(true));
|
||||
|
||||
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException() {
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONList();
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONList();
|
||||
|
||||
strictModeInputTestCases.forEach(
|
||||
testCase -> assertThrows("expected non-compliant array but got instead: " + testCase, JSONException.class,
|
||||
() -> new JSONArray(testCase, jsonParserConfiguration)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
||||
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
|
||||
String compliantJsonArrayAsString = lines.collect(Collectors.joining());
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArrays_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONList();
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
|
||||
List<String> strictModeInputTestCases = getNonCompliantJSONList();
|
||||
|
||||
strictModeInputTestCases.forEach(testCase -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
}
|
||||
|
||||
@@ -54,24 +74,88 @@ public class JSONParserConfigurationTest {
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCase = "[1,2];[3,4]";
|
||||
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
|
||||
assertEquals("invalid character found after end of array: ; at 6 [character 7 line 1]", je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCase = "[\"1\",\"2\"];[3,4]";
|
||||
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
|
||||
assertEquals("invalid character found after end of array: ; at 10 [character 11 line 1]", je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCase = "[{\"test\": implied}]";
|
||||
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
|
||||
assertEquals("Value is not surrounded by quotes: implied", je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
|
||||
String testCase = "[{\"test\": implied}]";
|
||||
|
||||
new JSONArray(testCase, jsonParserConfiguration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnbalancedQuotes_testStrictModeTrue_shouldThrowJsonExceptionWtihConcreteErrorDescription() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(true);
|
||||
|
||||
String testCaseOne = "[\"abc', \"test\"]";
|
||||
String testCaseTwo = "['abc\", \"test\"]";
|
||||
|
||||
JSONException jeOne = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
|
||||
JSONException jeTwo = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
|
||||
|
||||
assertEquals(
|
||||
"Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]",
|
||||
jeOne.getMessage());
|
||||
assertEquals(
|
||||
"Field contains unbalanced quotes. Starts with ' but ends with double quote. at 6 [character 7 line 1]",
|
||||
jeTwo.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnbalancedQuotes_testStrictModeFalse_shouldThrowJsonException() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withStrictMode(false);
|
||||
|
||||
String testCaseOne = "[\"abc', \"test\"]";
|
||||
String testCaseTwo = "['abc\", \"test\"]";
|
||||
|
||||
JSONException jeOne = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
|
||||
JSONException jeTwo = assertThrows(JSONException.class,
|
||||
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
|
||||
|
||||
assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage());
|
||||
assertEquals("Unterminated string at 15 [character 16 line 1]", jeTwo.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
@@ -81,14 +165,14 @@ public class JSONParserConfigurationTest {
|
||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||
|
||||
assertEquals("Key is not surrounded by quotes: test", je.getMessage());
|
||||
assertEquals(String.format("Value is not surrounded by quotes: %s", "test"), je.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyDuplicateKeyThenMaxDepth() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withOverwriteDuplicateKey(true)
|
||||
.withMaxNestingDepth(42);
|
||||
.withOverwriteDuplicateKey(true)
|
||||
.withMaxNestingDepth(42);
|
||||
|
||||
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
||||
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
||||
@@ -97,16 +181,23 @@ public class JSONParserConfigurationTest {
|
||||
@Test
|
||||
public void verifyMaxDepthThenDuplicateKey() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||
.withMaxNestingDepth(42)
|
||||
.withOverwriteDuplicateKey(true);
|
||||
.withMaxNestingDepth(42)
|
||||
.withOverwriteDuplicateKey(true);
|
||||
|
||||
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
||||
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||
* this class.
|
||||
*
|
||||
* @return List with JSON strings.
|
||||
*/
|
||||
private List<String> getNonCompliantJSONList() {
|
||||
return Arrays.asList(
|
||||
"[1,2];[3,4]",
|
||||
"[test]",
|
||||
"[1, 2,3]:[4,5]",
|
||||
"[{test: implied}]",
|
||||
"[{\"test\": implied}]",
|
||||
|
Reference in New Issue
Block a user