Merge pull request #951 from marilynel/master

Fixing and updating unit tests for default strictMode
This commit is contained in:
Sean Leary 2025-02-21 07:37:47 -06:00 committed by GitHub
commit 75e5a3d646
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 76 additions and 32 deletions

View File

@ -15,6 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
public JSONParserConfiguration() { public JSONParserConfiguration() {
super(); super();
this.overwriteDuplicateKey = false; this.overwriteDuplicateKey = false;
// this.strictMode = true;
} }
/** /**

View File

@ -477,13 +477,18 @@ public class JSONArrayTest {
*/ */
@Test @Test
public void unquotedText() { public void unquotedText() {
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) { if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONArrayTest unquotedText() when strictMode default is true"); try {
} else {
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
JSONArray jsonArray = new JSONArray(str); JSONArray jsonArray = new JSONArray(str);
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45"); assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else {
JSONArray jsonArray = new JSONArray(str);
assertEquals(expected, jsonArray.toList()); assertEquals(expected, jsonArray.toList());
} }
} }

View File

@ -218,11 +218,16 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void unquotedText() { public void unquotedText() {
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) { if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONObjectTest unquotedText() when strictMode default is true"); try {
JSONObject jsonObject = new JSONObject(str);
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else { } else {
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
String textStr = jsonObject.toString(); String textStr = jsonObject.toString();
assertTrue("expected key1", textStr.contains("\"key1\"")); assertTrue("expected key1", textStr.contains("\"key1\""));
@ -1074,24 +1079,29 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void jsonInvalidNumberValues() { public void jsonInvalidNumberValues() {
// Number-notations supported by Java and invalid as JSON
String str =
"{" +
"\"hexNumber\":-0x123," +
"\"tooManyZeros\":00," +
"\"negativeInfinite\":-Infinity," +
"\"negativeNaN\":-NaN," +
"\"negativeFraction\":-.01," +
"\"tooManyZerosFraction\":00.001," +
"\"negativeHexFloat\":-0x1.fffp1," +
"\"hexFloat\":0x1.0P-1074," +
"\"floatIdentifier\":0.1f," +
"\"doubleIdentifier\":0.1d" +
"}";
// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) { if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONObjectTest jsonInvalidNumberValues() when strictMode default is true"); try {
JSONObject jsonObject = new JSONObject(str);
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else { } else {
// Number-notations supported by Java and invalid as JSON
String str =
"{" +
"\"hexNumber\":-0x123," +
"\"tooManyZeros\":00," +
"\"negativeInfinite\":-Infinity," +
"\"negativeNaN\":-NaN," +
"\"negativeFraction\":-.01," +
"\"tooManyZerosFraction\":00.001," +
"\"negativeHexFloat\":-0x1.fffp1," +
"\"hexFloat\":0x1.0P-1074," +
"\"floatIdentifier\":0.1f," +
"\"doubleIdentifier\":0.1d" +
"}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
Object obj; Object obj;
obj = jsonObject.get("hexNumber"); obj = jsonObject.get("hexNumber");
@ -2274,7 +2284,7 @@ public class JSONObjectTest {
public void jsonObjectParsingErrors() { public void jsonObjectParsingErrors() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) { if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONObjectTest jaonObjectParsingErrors() when strictMode default is true"); System.out.println("Skipping JSONObjectTest jsonObjectParsingErrors() when strictMode default is true");
} else { } else {
try { try {
// does not start with '{' // does not start with '{'

View File

@ -16,10 +16,7 @@ import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import org.json.JSONArray; import org.json.*;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test; import org.junit.Test;
/** /**
@ -98,7 +95,17 @@ public class JSONTokenerTest {
checkValid(" [] ",JSONArray.class); checkValid(" [] ",JSONArray.class);
checkValid("[1,2]",JSONArray.class); checkValid("[1,2]",JSONArray.class);
checkValid("\n\n[1,2]\n\n",JSONArray.class); checkValid("\n\n[1,2]\n\n",JSONArray.class);
checkValid("1 2", String.class);
// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
try {
checkValid("1 2", String.class);
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else {
checkValid("1 2", String.class);
}
} }
@Test @Test
@ -330,16 +337,37 @@ public class JSONTokenerTest {
public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() { public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
String input = "{\"invalidInput\": [],}"; String input = "{\"invalidInput\": [],}";
JSONTokener tokener = new JSONTokener(input); JSONTokener tokener = new JSONTokener(input);
Object value = tokener.nextValue();
assertEquals(new JSONObject(input).toString(), value.toString()); // Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
try {
Object value = tokener.nextValue();
assertEquals(new JSONObject(input).toString(), value.toString());
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else {
Object value = tokener.nextValue();
assertEquals(new JSONObject(input).toString(), value.toString());
}
} }
@Test @Test
public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() { public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
String input = "[\"invalidInput\",]"; String input = "[\"invalidInput\",]";
JSONTokener tokener = new JSONTokener(input); JSONTokener tokener = new JSONTokener(input);
Object value = tokener.nextValue();
assertEquals(new JSONArray(input).toString(), value.toString());
}
// Test should fail if default strictMode is true, pass if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
try {
Object value = tokener.nextValue();
assertEquals(new JSONArray(input).toString(), value.toString());
assertEquals("Expected to throw exception due to invalid string", true, false);
} catch (JSONException e) { }
} else {
Object value = tokener.nextValue();
assertEquals(new JSONArray(input).toString(), value.toString());
}
}
} }