pre-release-20260719 oops forgot to update new unit tests for strict mode

This commit is contained in:
Sean Leary
2026-07-19 13:48:18 -05:00
parent d24bc9e5d6
commit da757c6656
@@ -3885,6 +3885,7 @@ public class JSONObjectTest {
@Test @Test
public void testMaxNumberLength() { public void testMaxNumberLength() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {
sb.append("9999999999"); sb.append("9999999999");
@@ -3896,11 +3897,14 @@ public class JSONObjectTest {
// JSONArray with number just under the max length limit // JSONArray with number just under the max length limit
checkJSONArrayMaxLen(sb.toString(), true, null); checkJSONArrayMaxLen(sb.toString(), true, null);
// numbers without quotes are not allowed in strict mode
if (!jsonParserConfiguration.isStrictMode()) {
// JSONObject with number just over the max length limit // JSONObject with number just over the max length limit
checkJSONObjectMaxLen(sb + "9", false, null); checkJSONObjectMaxLen(sb + "9", false, null);
// JSONArray with number just over the max length limit // JSONArray with number just over the max length limit
checkJSONArrayMaxLen(sb + "9", false, null); checkJSONArrayMaxLen(sb + "9", false, null);
}
// JSONObject with number at config max length limit // JSONObject with number at config max length limit
checkJSONObjectMaxLen(sb.toString() + sb.toString(), true, new JSONParserConfiguration().withMaxNumberLength(2000)); checkJSONObjectMaxLen(sb.toString() + sb.toString(), true, new JSONParserConfiguration().withMaxNumberLength(2000));
@@ -3908,11 +3912,14 @@ public class JSONObjectTest {
// JSONArray with number at config max length limit // JSONArray with number at config max length limit
checkJSONArrayMaxLen((sb.toString() + sb), true, new JSONParserConfiguration().withMaxNumberLength(2000)); checkJSONArrayMaxLen((sb.toString() + sb), true, new JSONParserConfiguration().withMaxNumberLength(2000));
// numbers without quotes are not allowed in strict mode
if (!jsonParserConfiguration.isStrictMode()) {
// JSONObject with number just over config max length limit // JSONObject with number just over config max length limit
checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", false, new JSONParserConfiguration().withMaxNumberLength(2000)); checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", false, new JSONParserConfiguration().withMaxNumberLength(2000));
// JSONArray with number just over config max length limit // JSONArray with number just over config max length limit
checkJSONArrayMaxLen((sb.toString() + sb + "9"), false, new JSONParserConfiguration().withMaxNumberLength(2000)); checkJSONArrayMaxLen((sb.toString() + sb + "9"), false, new JSONParserConfiguration().withMaxNumberLength(2000));
}
// JSONObject with large number, no checks // JSONObject with large number, no checks
checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", true, checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", true,
@@ -3975,6 +3982,8 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void testMaxNumberLengthNegativeInteger() { public void testMaxNumberLengthNegativeInteger() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
// Build a negative number string of exactly 1000 chars: "-" + 999 digits // Build a negative number string of exactly 1000 chars: "-" + 999 digits
StringBuilder sb = new StringBuilder("-"); StringBuilder sb = new StringBuilder("-");
for (int i = 0; i < 999; ++i) { for (int i = 0; i < 999; ++i) {
@@ -3985,17 +3994,22 @@ public class JSONObjectTest {
// at max length: parsed as number // at max length: parsed as number
checkJSONObjectMaxLen(sb.toString(), true, null); checkJSONObjectMaxLen(sb.toString(), true, null);
// numbers without quotes are not allowed in strict mode
if (!jsonParserConfiguration.isStrictMode()) {
// over max length: returned as string // over max length: returned as string
sb.append("1"); sb.append("1");
assertEquals(1001, sb.length()); assertEquals(1001, sb.length());
checkJSONObjectMaxLen(sb.toString(), false, null); checkJSONObjectMaxLen(sb.toString(), false, null);
} }
}
/** /**
* Test max number length for decimal notation strings via stringToValue. * Test max number length for decimal notation strings via stringToValue.
*/ */
@Test @Test
public void testMaxNumberLengthDecimal() { public void testMaxNumberLengthDecimal() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
// Build a decimal number string of exactly 1000 chars: 499 digits + "." + 500 // Build a decimal number string of exactly 1000 chars: 499 digits + "." + 500
// digits // digits
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@@ -4011,17 +4025,22 @@ public class JSONObjectTest {
// at max length: parsed as number (BigDecimal) // at max length: parsed as number (BigDecimal)
checkJSONObjectMaxLen(sb.toString(), true, null); checkJSONObjectMaxLen(sb.toString(), true, null);
// numbers without quotes are not allowed in strict mode
if (!jsonParserConfiguration.isStrictMode()) {
// over max length: returned as string // over max length: returned as string
sb.append("3"); sb.append("3");
assertEquals(1001, sb.length()); assertEquals(1001, sb.length());
checkJSONObjectMaxLen(sb.toString(), false, null); checkJSONObjectMaxLen(sb.toString(), false, null);
} }
}
/** /**
* Test max number length with scientific notation via stringToValue. * Test max number length with scientific notation via stringToValue.
*/ */
@Test @Test
public void testMaxNumberLengthScientificNotation() { public void testMaxNumberLengthScientificNotation() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
// Build a scientific notation string of exactly 1000 chars // Build a scientific notation string of exactly 1000 chars
StringBuilder sb = new StringBuilder("1."); StringBuilder sb = new StringBuilder("1.");
for (int i = 0; i < 994; ++i) { for (int i = 0; i < 994; ++i) {
@@ -4038,10 +4057,14 @@ public class JSONObjectTest {
for (int i = 0; i < 995; ++i) { for (int i = 0; i < 995; ++i) {
sb.append("0"); sb.append("0");
} }
// numbers without quotes are not allowed in strict mode
if (!jsonParserConfiguration.isStrictMode()) {
sb.append("e100"); sb.append("e100");
assertEquals(1001, sb.length()); assertEquals(1001, sb.length());
checkJSONObjectMaxLen(sb.toString(), false, null); checkJSONObjectMaxLen(sb.toString(), false, null);
} }
}
/** /**
* Test that optBigDecimal enforces max number length for string values. * Test that optBigDecimal enforces max number length for string values.
@@ -4297,6 +4320,9 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void testStringToNumberInvalidFormats() { public void testStringToNumberInvalidFormats() {
// this test should only run in non-strict mode, otherwise it will throw an exception
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (!jsonParserConfiguration.isStrictMode()) {
// Leading zero followed by digit → treated as string (not octal) // Leading zero followed by digit → treated as string (not octal)
JSONObject jo = new JSONObject("{\"a\": 01}"); JSONObject jo = new JSONObject("{\"a\": 01}");
// JSONTokener with strict mode would reject, but default mode stores as string // JSONTokener with strict mode would reject, but default mode stores as string
@@ -4309,5 +4335,6 @@ public class JSONObjectTest {
val = jo.get("a"); val = jo.get("a");
assertTrue("-01 should be stored as string, got " + val.getClass(), val instanceof String); assertTrue("-01 should be stored as string, got " + val.getClass(), val instanceof String);
} }
}
} }