fixed failing unit tests in strict mode, issue 940

This commit is contained in:
marilynel 2025-02-15 12:03:03 -08:00
parent 42afb34045
commit f112a091aa
2 changed files with 39 additions and 10 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

@ -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());
}
}
} }