Skip to content

Commit 75e5a3d

Browse files
authored
Merge pull request #951 from marilynel/master
Fixing and updating unit tests for default strictMode
2 parents 42afb34 + 3919abd commit 75e5a3d

File tree

4 files changed

+76
-32
lines changed

4 files changed

+76
-32
lines changed

src/main/java/org/json/JSONParserConfiguration.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
1515
public JSONParserConfiguration() {
1616
super();
1717
this.overwriteDuplicateKey = false;
18+
// this.strictMode = true;
1819
}
1920

2021
/**

src/test/java/org/json/junit/JSONArrayTest.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,18 @@ public void failedGetArrayValues() {
477477
*/
478478
@Test
479479
public void unquotedText() {
480+
String str = "[value1, something!, (parens), [email protected], 23, 23+45]";
481+
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "[email protected]", 23, "23+45");
482+
483+
// Test should fail if default strictMode is true, pass if false
480484
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
481485
if (jsonParserConfiguration.isStrictMode()) {
482-
System.out.println("Skipping JSONArrayTest unquotedText() when strictMode default is true");
483-
} else {
484-
String str = "[value1, something!, (parens), [email protected], 23, 23+45]";
486+
try {
485487
JSONArray jsonArray = new JSONArray(str);
486-
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "[email protected]", 23, "23+45");
488+
assertEquals("Expected to throw exception due to invalid string", true, false);
489+
} catch (JSONException e) { }
490+
} else {
491+
JSONArray jsonArray = new JSONArray(str);
487492
assertEquals(expected, jsonArray.toList());
488493
}
489494
}

src/test/java/org/json/junit/JSONObjectTest.java

+28-18
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,16 @@ public void jsonObjectByNullBean() {
218218
*/
219219
@Test
220220
public void unquotedText() {
221+
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
222+
223+
// Test should fail if default strictMode is true, pass if false
221224
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
222225
if (jsonParserConfiguration.isStrictMode()) {
223-
System.out.println("Skipping JSONObjectTest unquotedText() when strictMode default is true");
226+
try {
227+
JSONObject jsonObject = new JSONObject(str);
228+
assertEquals("Expected to throw exception due to invalid string", true, false);
229+
} catch (JSONException e) { }
224230
} else {
225-
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
226231
JSONObject jsonObject = new JSONObject(str);
227232
String textStr = jsonObject.toString();
228233
assertTrue("expected key1", textStr.contains("\"key1\""));
@@ -1074,24 +1079,29 @@ public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() {
10741079
*/
10751080
@Test
10761081
public void jsonInvalidNumberValues() {
1082+
// Number-notations supported by Java and invalid as JSON
1083+
String str =
1084+
"{" +
1085+
"\"hexNumber\":-0x123," +
1086+
"\"tooManyZeros\":00," +
1087+
"\"negativeInfinite\":-Infinity," +
1088+
"\"negativeNaN\":-NaN," +
1089+
"\"negativeFraction\":-.01," +
1090+
"\"tooManyZerosFraction\":00.001," +
1091+
"\"negativeHexFloat\":-0x1.fffp1," +
1092+
"\"hexFloat\":0x1.0P-1074," +
1093+
"\"floatIdentifier\":0.1f," +
1094+
"\"doubleIdentifier\":0.1d" +
1095+
"}";
1096+
1097+
// Test should fail if default strictMode is true, pass if false
10771098
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
10781099
if (jsonParserConfiguration.isStrictMode()) {
1079-
System.out.println("Skipping JSONObjectTest jsonInvalidNumberValues() when strictMode default is true");
1100+
try {
1101+
JSONObject jsonObject = new JSONObject(str);
1102+
assertEquals("Expected to throw exception due to invalid string", true, false);
1103+
} catch (JSONException e) { }
10801104
} else {
1081-
// Number-notations supported by Java and invalid as JSON
1082-
String str =
1083-
"{" +
1084-
"\"hexNumber\":-0x123," +
1085-
"\"tooManyZeros\":00," +
1086-
"\"negativeInfinite\":-Infinity," +
1087-
"\"negativeNaN\":-NaN," +
1088-
"\"negativeFraction\":-.01," +
1089-
"\"tooManyZerosFraction\":00.001," +
1090-
"\"negativeHexFloat\":-0x1.fffp1," +
1091-
"\"hexFloat\":0x1.0P-1074," +
1092-
"\"floatIdentifier\":0.1f," +
1093-
"\"doubleIdentifier\":0.1d" +
1094-
"}";
10951105
JSONObject jsonObject = new JSONObject(str);
10961106
Object obj;
10971107
obj = jsonObject.get("hexNumber");
@@ -2274,7 +2284,7 @@ public void jsonObjectParseIllegalEscapeAssertExceptionMessage(){
22742284
public void jsonObjectParsingErrors() {
22752285
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
22762286
if (jsonParserConfiguration.isStrictMode()) {
2277-
System.out.println("Skipping JSONObjectTest jaonObjectParsingErrors() when strictMode default is true");
2287+
System.out.println("Skipping JSONObjectTest jsonObjectParsingErrors() when strictMode default is true");
22782288
} else {
22792289
try {
22802290
// does not start with '{'

src/test/java/org/json/junit/JSONTokenerTest.java

+38-10
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import java.io.Reader;
1717
import java.io.StringReader;
1818

19-
import org.json.JSONArray;
20-
import org.json.JSONException;
21-
import org.json.JSONObject;
22-
import org.json.JSONTokener;
19+
import org.json.*;
2320
import org.junit.Test;
2421

2522
/**
@@ -98,7 +95,17 @@ public void testValid() {
9895
checkValid(" [] ",JSONArray.class);
9996
checkValid("[1,2]",JSONArray.class);
10097
checkValid("\n\n[1,2]\n\n",JSONArray.class);
101-
checkValid("1 2", String.class);
98+
99+
// Test should fail if default strictMode is true, pass if false
100+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
101+
if (jsonParserConfiguration.isStrictMode()) {
102+
try {
103+
checkValid("1 2", String.class);
104+
assertEquals("Expected to throw exception due to invalid string", true, false);
105+
} catch (JSONException e) { }
106+
} else {
107+
checkValid("1 2", String.class);
108+
}
102109
}
103110

104111
@Test
@@ -330,16 +337,37 @@ public void testAutoClose(){
330337
public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() {
331338
String input = "{\"invalidInput\": [],}";
332339
JSONTokener tokener = new JSONTokener(input);
333-
Object value = tokener.nextValue();
334-
assertEquals(new JSONObject(input).toString(), value.toString());
340+
341+
// Test should fail if default strictMode is true, pass if false
342+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
343+
if (jsonParserConfiguration.isStrictMode()) {
344+
try {
345+
Object value = tokener.nextValue();
346+
assertEquals(new JSONObject(input).toString(), value.toString());
347+
assertEquals("Expected to throw exception due to invalid string", true, false);
348+
} catch (JSONException e) { }
349+
} else {
350+
Object value = tokener.nextValue();
351+
assertEquals(new JSONObject(input).toString(), value.toString());
352+
}
335353
}
336354

337355
@Test
338356
public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() {
339357
String input = "[\"invalidInput\",]";
340358
JSONTokener tokener = new JSONTokener(input);
341-
Object value = tokener.nextValue();
342-
assertEquals(new JSONArray(input).toString(), value.toString());
343-
}
344359

360+
// Test should fail if default strictMode is true, pass if false
361+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
362+
if (jsonParserConfiguration.isStrictMode()) {
363+
try {
364+
Object value = tokener.nextValue();
365+
assertEquals(new JSONArray(input).toString(), value.toString());
366+
assertEquals("Expected to throw exception due to invalid string", true, false);
367+
} catch (JSONException e) { }
368+
} else {
369+
Object value = tokener.nextValue();
370+
assertEquals(new JSONArray(input).toString(), value.toString());
371+
}
372+
}
345373
}

0 commit comments

Comments
 (0)