Fixing and updating unit tests for default strictMode by marilynel · Pull Request #951 · stleary/JSON-java · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/org/json/JSONParserConfiguration.java
13 changes: 9 additions & 4 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,18 @@ public void failedGetArrayValues() {
*/
@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);
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());
}
}
Expand Down
46 changes: 28 additions & 18 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,16 @@ public void jsonObjectByNullBean() {
*/
@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\""));
Expand Down Expand Up @@ -1074,24 +1079,29 @@ public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() {
*/
@Test
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();
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 {
// 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);
Object obj;
obj = jsonObject.get("hexNumber");
Expand Down Expand Up @@ -2274,7 +2284,7 @@ public void jsonObjectParseIllegalEscapeAssertExceptionMessage(){
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 '{'
Expand Down
48 changes: 38 additions & 10 deletions src/test/java/org/json/junit/JSONTokenerTest.java