restore-jsonparserconfiguration: fix unit tests to work when strictMode default is true

This commit is contained in:
Sean Leary 2024-12-15 13:18:39 -06:00
parent 09536cd6c8
commit d3c7eaf17e
8 changed files with 461 additions and 399 deletions

View File

@ -29,7 +29,7 @@ public class CDLTest {
"1, 2, 3, 4\t, 5, 6, 7\n" + "1, 2, 3, 4\t, 5, 6, 7\n" +
"true, false, true, true, false, false, false\n" + "true, false, true, true, false, false, false\n" +
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va'l6, val7\n"; "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"va'l6\", val7\n";
/** /**
@ -38,11 +38,54 @@ public class CDLTest {
* values all must be quoted in the cases where the JSONObject parsing * values all must be quoted in the cases where the JSONObject parsing
* might normally convert the value into a non-string. * might normally convert the value into a non-string.
*/ */
private static final String EXPECTED_LINES = "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, " + private static final String EXPECTED_LINES =
"{Col 1:\"1\", Col 2:\"2\", Col 3:\"3\", Col 4:\"4\", Col 5:\"5\", Col 6:\"6\", Col 7:\"7\"}, " + "[ " +
"{Col 1:\"true\", Col 2:\"false\", Col 3:\"true\", Col 4:\"true\", Col 5:\"false\", Col 6:\"false\", Col 7:\"false\"}, " + "{" +
"{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, " + "\"Col 1\":\"val1\", " +
"{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va'l6, Col 7:val7}]"; "\"Col 2\":\"val2\", " +
"\"Col 3\":\"val3\", " +
"\"Col 4\":\"val4\", " +
"\"Col 5\":\"val5\", " +
"\"Col 6\":\"val6\", " +
"\"Col 7\":\"val7\"" +
"}, " +
" {" +
"\"Col 1\":\"1\", " +
"\"Col 2\":\"2\", " +
"\"Col 3\":\"3\", " +
"\"Col 4\":\"4\", " +
"\"Col 5\":\"5\", " +
"\"Col 6\":\"6\", " +
"\"Col 7\":\"7\"" +
"}, " +
" {" +
"\"Col 1\":\"true\", " +
"\"Col 2\":\"false\", " +
"\"Col 3\":\"true\", " +
"\"Col 4\":\"true\", " +
"\"Col 5\":\"false\", " +
"\"Col 6\":\"false\", " +
"\"Col 7\":\"false\"" +
"}, " +
"{" +
"\"Col 1\":\"0.23\", " +
"\"Col 2\":\"57.42\", " +
"\"Col 3\":\"5e27\", " +
"\"Col 4\":\"-234.879\", " +
"\"Col 5\":\"2.34e5\", " +
"\"Col 6\":\"0.0\", " +
"\"Col 7\":\"9e-3\"" +
"}, " +
"{" +
"\"Col 1\":\"va\tl1\", " +
"\"Col 2\":\"v\bal2\", " +
"\"Col 3\":\"val3\", " +
"\"Col 4\":\"val\f4\", " +
"\"Col 5\":\"val5\", " +
"\"Col 6\":\"va'l6\", " +
"\"Col 7\":\"val7\"" +
"}" +
"]";
/** /**
* Attempts to create a JSONArray from a null string. * Attempts to create a JSONArray from a null string.
@ -283,11 +326,11 @@ public class CDLTest {
*/ */
@Test @Test
public void jsonArrayToJSONArray() { public void jsonArrayToJSONArray() {
String nameArrayStr = "[Col1, Col2]"; String nameArrayStr = "[\"Col1\", \"Col2\"]";
String values = "V1, V2"; String values = "V1, V2";
JSONArray nameJSONArray = new JSONArray(nameArrayStr); JSONArray nameJSONArray = new JSONArray(nameArrayStr);
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]"); JSONArray expectedJsonArray = new JSONArray("[{\"Col1\":\"V1\",\"Col2\":\"V2\"}]");
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
} }

View File

@ -476,11 +476,16 @@ public class JSONArrayTest {
*/ */
@Test @Test
public void unquotedText() { public void unquotedText() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONArrayTest unquotedText() when strictMode default is true");
} else {
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]"; 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"); List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
assertEquals(expected, jsonArray.toList()); assertEquals(expected, jsonArray.toList());
} }
}
/** /**
* Exercise JSONArray.join() by converting a JSONArray into a * Exercise JSONArray.join() by converting a JSONArray into a
@ -690,8 +695,8 @@ public class JSONArrayTest {
String jsonArrayStr = String jsonArrayStr =
"["+ "["+
"hello,"+ "\"hello\","+
"world"+ "\"world\""+
"]"; "]";
// 2 // 2
jsonArray.put(new JSONArray(jsonArrayStr)); jsonArray.put(new JSONArray(jsonArrayStr));
@ -768,8 +773,8 @@ public class JSONArrayTest {
String jsonArrayStr = String jsonArrayStr =
"["+ "["+
"hello,"+ "\"hello\","+
"world"+ "\"world\""+
"]"; "]";
// 2 // 2
jsonArray.put(2, new JSONArray(jsonArrayStr)); jsonArray.put(2, new JSONArray(jsonArrayStr));

View File

@ -625,7 +625,7 @@ public class JSONMLTest {
"\"subValue\","+ "\"subValue\","+
"{\"svAttr\":\"svValue\"},"+ "{\"svAttr\":\"svValue\"},"+
"\"abc\""+ "\"abc\""+
"],"+ "]"+
"],"+ "],"+
"[\"value\",3],"+ "[\"value\",3],"+
"[\"value\",4.1],"+ "[\"value\",4.1],"+

View File

@ -23,18 +23,18 @@ public class JSONObjectNumberTest {
@Parameters(name = "{index}: {0}") @Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{ return Arrays.asList(new Object[][]{
{"{value:50}", 1}, {"{\"value\":50}", 1},
{"{value:50.0}", 1}, {"{\"value\":50.0}", 1},
{"{value:5e1}", 1}, {"{\"value\":5e1}", 1},
{"{value:5E1}", 1}, {"{\"value\":5E1}", 1},
{"{value:5e1}", 1}, {"{\"value\":5e1}", 1},
{"{value:'50'}", 1}, {"{\"value\":\"50\"}", 1},
{"{value:-50}", -1}, {"{\"value\":-50}", -1},
{"{value:-50.0}", -1}, {"{\"value\":-50.0}", -1},
{"{value:-5e1}", -1}, {"{\"value\":-5e1}", -1},
{"{value:-5E1}", -1}, {"{\"value\":-5E1}", -1},
{"{value:-5e1}", -1}, {"{\"value\":-5e1}", -1},
{"{value:'-50'}", -1} {"{\"value\":\"-50\"}", -1}
// JSON does not support octal or hex numbers; // JSON does not support octal or hex numbers;
// see https://stackoverflow.com/a/52671839/6323312 // see https://stackoverflow.com/a/52671839/6323312
// "{value:062}", // octal 50 // "{value:062}", // octal 50

View File

@ -218,6 +218,10 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void unquotedText() { public void unquotedText() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONObjectTest unquotedText() when strictMode default is true");
} else {
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}"; 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();
@ -231,6 +235,7 @@ public class JSONObjectTest {
assertTrue("expected something!", textStr.contains("\"something!\"")); assertTrue("expected something!", textStr.contains("\"something!\""));
Util.checkJSONObjectMaps(jsonObject); Util.checkJSONObjectMaps(jsonObject);
} }
}
@Test @Test
public void testLongFromString(){ public void testLongFromString(){
@ -1069,6 +1074,10 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void jsonInvalidNumberValues() { public void jsonInvalidNumberValues() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONObjectTest jsonInvalidNumberValues() when strictMode default is true");
} else {
// Number-notations supported by Java and invalid as JSON // Number-notations supported by Java and invalid as JSON
String str = String str =
"{" + "{" +
@ -1112,6 +1121,7 @@ public class JSONObjectTest {
jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1))); jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
Util.checkJSONObjectMaps(jsonObject); Util.checkJSONObjectMaps(jsonObject);
} }
}
/** /**
* Tests how JSONObject get[type] handles incorrect types * Tests how JSONObject get[type] handles incorrect types
@ -1528,7 +1538,7 @@ public class JSONObjectTest {
"{"+ "{"+
"\"trueKey\":true,"+ "\"trueKey\":true,"+
"\"falseKey\":false,"+ "\"falseKey\":false,"+
"\"stringKey\":\"hello world!\","+ "\"stringKey\":\"hello world!\""+
"}"; "}";
JSONObject jsonObject2 = new JSONObject(str); JSONObject jsonObject2 = new JSONObject(str);
names = JSONObject.getNames(jsonObject2); names = JSONObject.getNames(jsonObject2);
@ -1623,7 +1633,7 @@ public class JSONObjectTest {
"{"+ "{"+
"\"trueKey\":true,"+ "\"trueKey\":true,"+
"\"falseKey\":false,"+ "\"falseKey\":false,"+
"\"stringKey\":\"hello world!\","+ "\"stringKey\":\"hello world!\""+
"}"; "}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
@ -2262,6 +2272,10 @@ public class JSONObjectTest {
@SuppressWarnings({"boxing", "unused"}) @SuppressWarnings({"boxing", "unused"})
@Test @Test
public void jsonObjectParsingErrors() { public void jsonObjectParsingErrors() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONObjectTest jaonObjectParsingErrors() when strictMode default is true");
} else {
try { try {
// does not start with '{' // does not start with '{'
String str = "abc"; String str = "abc";
@ -2579,6 +2593,7 @@ public class JSONObjectTest {
e.getMessage()); e.getMessage());
} }
} }
}
/** /**
* Confirm behavior when putOnce() is called with null parameters * Confirm behavior when putOnce() is called with null parameters
@ -3815,21 +3830,21 @@ public class JSONObjectTest {
// Behavior documented in #826 JSONObject parsing 0-led numeric strings as ints // Behavior documented in #826 JSONObject parsing 0-led numeric strings as ints
// After reverting the code, personId is stored as a string, and the behavior is as expected // After reverting the code, personId is stored as a string, and the behavior is as expected
String personId = "0123"; String personId = "\"0123\"";
JSONObject j1 = new JSONObject("{personId: " + personId + "}"); JSONObject j1 = new JSONObject("{\"personId\": " + personId + "}");
assertEquals(j1.getString("personId"), "0123"); assertEquals(j1.getString("personId"), "0123");
// Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number. // Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number.
// This example was mentioned in the same ticket // This example was mentioned in the same ticket
// After reverting the code, personId is stored as a string, and the behavior is as expected // After reverting the code, personId is stored as a string, and the behavior is as expected
JSONObject j2 = new JSONObject("{\"personId\":0123}"); JSONObject j2 = new JSONObject("{\"personId\":\"0123\"}");
assertEquals(j2.getString("personId"), "0123"); assertEquals(j2.getString("personId"), "0123");
// Behavior uncovered while working on the code // Behavior uncovered while working on the code
// All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect // All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect
JSONObject j3 = new JSONObject("{ " + JSONObject j3 = new JSONObject("{ " +
"\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", " + "\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", " +
"\"hex4\": 00e0, \"hex5\": 00f0, \"hex6\": 0011 }"); "\"hex4\": 00e0, \"hex5\": \"00f0\", \"hex6\": \"0011\" }");
assertEquals(j3.getString("hex1"), "010e4"); assertEquals(j3.getString("hex1"), "010e4");
assertEquals(j3.getString("hex2"), "00f0"); assertEquals(j3.getString("hex2"), "00f0");
assertEquals(j3.getString("hex3"), "0011"); assertEquals(j3.getString("hex3"), "0011");

View File

@ -384,8 +384,7 @@ public class JSONPointerTest {
String str = "{"+ String str = "{"+
"\"string\\\\\\\\Key\":\"hello world!\","+ "\"string\\\\\\\\Key\":\"hello world!\","+
"\"\\\\\":\"slash test\"," + "\"\\\\\":\"slash test\"" +
"}"+
"}"; "}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held //Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held

View File

@ -270,9 +270,9 @@ public class XMLConfigurationTest {
String expectedStr = String expectedStr =
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+ "\"name\":\"Joe Tester\",\"NothingHere\":\"\",\"TrueValue\":true,\n"+
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+ "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+ "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":\"-23x.45\",\n"+
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
"},\"xsi:noNamespaceSchemaLocation\":"+ "},\"xsi:noNamespaceSchemaLocation\":"+
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+

View File

@ -267,9 +267,9 @@ public class XMLTest {
String expectedStr = String expectedStr =
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+ "\"name\":\"Joe Tester\",\"NothingHere\":\"\",\"TrueValue\":true,\n"+
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+ "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+ "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":\"-23x.45\",\n"+
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
"},\"xsi:noNamespaceSchemaLocation\":"+ "},\"xsi:noNamespaceSchemaLocation\":"+
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
@ -1180,7 +1180,7 @@ public class XMLTest {
@Test @Test
public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){ public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}"; String jsonString = "{\"outer\":{\"innerOne\":\"\", \"innerTwo\":\"two\"}}";
JSONObject jsonObject = new JSONObject(jsonString); JSONObject jsonObject = new JSONObject(jsonString);
String expectedXmlString = "<encloser><outer><innerOne></innerOne><innerTwo>two</innerTwo></outer></encloser>"; String expectedXmlString = "<encloser><outer><innerOne></innerOne><innerTwo>two</innerTwo></outer></encloser>";
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true)); String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true));
@ -1191,7 +1191,7 @@ public class XMLTest {
@Test @Test
public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){ public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}"; String jsonString = "{\"outer\":{\"innerOne\":\"\", \"innerTwo\":\"two\"}}";
JSONObject jsonObject = new JSONObject(jsonString); JSONObject jsonObject = new JSONObject(jsonString);
String expectedXmlString = "<encloser><outer><innerOne/><innerTwo>two</innerTwo></outer></encloser>"; String expectedXmlString = "<encloser><outer><innerOne/><innerTwo>two</innerTwo></outer></encloser>";
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false)); String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false));