mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Merge pull request #951 from marilynel/master
Fixing and updating unit tests for default strictMode
This commit is contained in:
commit
75e5a3d646
@ -15,6 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
||||
public JSONParserConfiguration() {
|
||||
super();
|
||||
this.overwriteDuplicateKey = false;
|
||||
// this.strictMode = true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -477,13 +477,18 @@ public class JSONArrayTest {
|
||||
*/
|
||||
@Test
|
||||
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();
|
||||
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]";
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(str);
|
||||
assertEquals("Expected to throw exception due to invalid string", true, false);
|
||||
} catch (JSONException e) { }
|
||||
} else {
|
||||
JSONArray jsonArray = new JSONArray(str);
|
||||
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
|
||||
assertEquals(expected, jsonArray.toList());
|
||||
}
|
||||
}
|
||||
|
@ -218,11 +218,16 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
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();
|
||||
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 {
|
||||
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
String textStr = jsonObject.toString();
|
||||
assertTrue("expected key1", textStr.contains("\"key1\""));
|
||||
@ -1074,10 +1079,6 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
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
|
||||
String str =
|
||||
"{" +
|
||||
@ -1092,6 +1093,15 @@ public class JSONObjectTest {
|
||||
"\"floatIdentifier\":0.1f," +
|
||||
"\"doubleIdentifier\":0.1d" +
|
||||
"}";
|
||||
|
||||
// Test should fail if default strictMode is true, pass if false
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||
if (jsonParserConfiguration.isStrictMode()) {
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
assertEquals("Expected to throw exception due to invalid string", true, false);
|
||||
} catch (JSONException e) { }
|
||||
} else {
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
Object obj;
|
||||
obj = jsonObject.get("hexNumber");
|
||||
@ -2274,7 +2284,7 @@ public class JSONObjectTest {
|
||||
public void jsonObjectParsingErrors() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||
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 {
|
||||
try {
|
||||
// does not start with '{'
|
||||
|
@ -16,10 +16,7 @@ import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
@ -98,7 +95,17 @@ public class JSONTokenerTest {
|
||||
checkValid(" [] ",JSONArray.class);
|
||||
checkValid("[1,2]",JSONArray.class);
|
||||
checkValid("\n\n[1,2]\n\n",JSONArray.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
|
||||
@ -330,16 +337,37 @@ public class JSONTokenerTest {
|
||||
public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
|
||||
String input = "{\"invalidInput\": [],}";
|
||||
JSONTokener tokener = new JSONTokener(input);
|
||||
|
||||
// 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
|
||||
public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
|
||||
String input = "[\"invalidInput\",]";
|
||||
JSONTokener tokener = new JSONTokener(input);
|
||||
|
||||
// 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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user