optimized unit tests to respond accurately to default strictMode

This commit is contained in:
marilynel 2025-02-15 12:30:12 -08:00
parent f112a091aa
commit 3919abd69a
3 changed files with 38 additions and 23 deletions

View File

@ -15,7 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
public JSONParserConfiguration() { public JSONParserConfiguration() {
super(); super();
this.overwriteDuplicateKey = false; this.overwriteDuplicateKey = false;
this.strictMode = true; // 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 '{'