Skip to content

Commit 778f134

Browse files
committed
cleanup, initial unit-tests
1 parent c5b9b6c commit 778f134

File tree

3 files changed

+67
-61
lines changed

3 files changed

+67
-61
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
209209
case '}':
210210
if (jsonParserConfiguration.isStrictMode()) {
211211
c = x.nextClean();
212-
System.out.println(c);
213212
if (c != 0) {
214-
System.out.println("yes");
215213
throw x.syntaxError(String.format("Invalid char '%s' after '}'", c));
216214
}
217215
x.back();

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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,41 +47,31 @@ public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException(
4747
}
4848

4949
@Test
50-
public void givenInvalidJsonObjects_testStrictModeTrue_shouldThrowJsonException() throws IOException {
51-
/*assertThrows(JSONException.class,
52-
() -> new JSONObject("{}abc", new JSONParserConfiguration().withStrictMode(true)));*/
53-
54-
try (final Stream<String> lines = Files.lines(Paths.get("src/test/resources/Issue884-validJsonObj.json"))) {
55-
final String resultJsonAsString = lines.collect(Collectors.joining());
56-
57-
final JSONObject jsonObject = new JSONObject(resultJsonAsString, new JSONParserConfiguration().withStrictMode(true));
58-
59-
System.out.println(jsonObject.toString(2));
60-
} catch (Exception e){
61-
e.printStackTrace();
62-
}
63-
64-
//JSONObject jsonObject = new JSONObject("{\"test\": {\"key\": \"val\", \"key2\"}}", new JSONParserConfiguration().withStrictMode(true));
65-
//JSONObject jsonObject2 = new JSONObject("{}}", new JSONParserConfiguration().withStrictMode(true));
50+
public void givenInvalidJsonObjects_testStrictModeTrue_shouldThrowJsonException() {
51+
final List<String> jsonObjects = Arrays.asList(
52+
"{}}",
53+
"{}abc"
54+
);
55+
56+
jsonObjects.forEach(jsonObject ->
57+
assertThrows(JSONException.class,
58+
() -> new JSONObject(jsonObject, new JSONParserConfiguration().withStrictMode(true))));
6659
}
6760

6861
@Test
69-
public void givenValidJsonObject_testStrictModeTrue_shouldThrowJsonException() throws IOException {
70-
/*assertThrows(JSONException.class,
71-
() -> new JSONObject("{}abc", new JSONParserConfiguration().withStrictMode(true)));*/
72-
73-
try (final Stream<String> lines = Files.lines(Paths.get("src/test/resources/Issue884-validJsonObj.json"))) {
74-
final String resultJsonAsString = lines.collect(Collectors.joining());
62+
public void givenValidJsonObjects_testStrictModeTrue() {
63+
final List<String> jsonPaths = Arrays.asList(
64+
"src/test/resources/Issue884-validJsonObj.json"
65+
);
7566

76-
final JSONObject jsonObject = new JSONObject(resultJsonAsString, new JSONParserConfiguration().withStrictMode(true));
67+
jsonPaths.forEach(path -> {
68+
final String resultJsonAsString = Util.getJsonStringFromFilePath(Paths.get(path));
7769

78-
System.out.println(jsonObject.toString(2));
79-
} catch (Exception e){
80-
e.printStackTrace();
81-
}
70+
final JSONObject resultJsonObject = new JSONObject(resultJsonAsString,
71+
new JSONParserConfiguration().withStrictMode(true));
8272

83-
//JSONObject jsonObject = new JSONObject("{\"test\": {\"key\": \"val\", \"key2\"}}", new JSONParserConfiguration().withStrictMode(true));
84-
//JSONObject jsonObject2 = new JSONObject("{}}", new JSONParserConfiguration().withStrictMode(true));
73+
assertEquals(resultJsonAsString.replaceAll("\\s", "").length(), resultJsonObject.toString().length());
74+
});
8575
}
8676

8777
@Test

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

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,40 @@
66

77
import static org.junit.Assert.*;
88

9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
913
import java.util.*;
1014

15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
1117
import org.json.*;
1218

1319
/**
14-
* These are helpful utility methods that perform basic comparisons
15-
* between various objects. In most cases, the comparisons are not
16-
* order-dependent, or else the order is known.
20+
* These are helpful utility methods that perform basic comparisons between various objects. In most cases, the
21+
* comparisons are not order-dependent, or else the order is known.
1722
*/
1823
public class Util {
1924

25+
public static String getJsonStringFromFilePath(final Path path) {
26+
try (final Stream<String> lines = Files.lines(path)) {
27+
return lines.collect(Collectors.joining());
28+
} catch (final IOException ioe) {
29+
throw new IllegalStateException(String.format("Unable to retrieve path: %s", path));
30+
}
31+
}
32+
2033
/**
21-
* Compares two JSONArrays for equality.
22-
* The arrays need not be in the same order.
23-
* @param jsonArray created by the code to be tested
34+
* Compares two JSONArrays for equality. The arrays need not be in the same order.
35+
*
36+
* @param jsonArray created by the code to be tested
2437
* @param expectedJsonArray created specifically for comparing
2538
*/
2639
public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
27-
JSONArray expectedJsonArray) {
40+
JSONArray expectedJsonArray) {
2841
assertTrue("jsonArray lengths should be equal",
29-
jsonArray.length() == expectedJsonArray.length());
42+
jsonArray.length() == expectedJsonArray.length());
3043
for (int i = 0; i < jsonArray.length(); ++i) {
3144
Object value = jsonArray.get(i);
3245
Object expectedValue = expectedJsonArray.get(i);
@@ -35,15 +48,15 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
3548
}
3649

3750
/**
38-
* Compares two JSONObjects for equality. The objects need not be
39-
* in the same order
40-
* @param jsonObject created by the code to be tested
51+
* Compares two JSONObjects for equality. The objects need not be in the same order
52+
*
53+
* @param jsonObject created by the code to be tested
4154
* @param expectedJsonObject created specifically for comparing
4255
*/
4356
public static void compareActualVsExpectedJsonObjects(
44-
JSONObject jsonObject, JSONObject expectedJsonObject) {
57+
JSONObject jsonObject, JSONObject expectedJsonObject) {
4558
assertTrue("jsonObjects should have the same length",
46-
jsonObject.length() == expectedJsonObject.length());
59+
jsonObject.length() == expectedJsonObject.length());
4760
Iterator<String> keys = jsonObject.keys();
4861
while (keys.hasNext()) {
4962
String key = keys.next();
@@ -54,25 +67,25 @@ public static void compareActualVsExpectedJsonObjects(
5467
}
5568

5669
/**
57-
* Compare two objects for equality. Might be JSONArray, JSONObject,
58-
* or something else.
59-
* @param value created by the code to be tested
70+
* Compare two objects for equality. Might be JSONArray, JSONObject, or something else.
71+
*
72+
* @param value created by the code to be tested
6073
* @param expectedValue created specifically for comparing
6174
*/
6275
private static void compareActualVsExpectedObjects(Object value,
63-
Object expectedValue) {
76+
Object expectedValue) {
6477
if (value instanceof JSONObject && expectedValue instanceof JSONObject) {
6578
// Compare JSONObjects
66-
JSONObject jsonObject = (JSONObject)value;
67-
JSONObject expectedJsonObject = (JSONObject)expectedValue;
79+
JSONObject jsonObject = (JSONObject) value;
80+
JSONObject expectedJsonObject = (JSONObject) expectedValue;
6881
compareActualVsExpectedJsonObjects(
69-
jsonObject, expectedJsonObject);
82+
jsonObject, expectedJsonObject);
7083
} else if (value instanceof JSONArray && expectedValue instanceof JSONArray) {
7184
// Compare JSONArrays
72-
JSONArray jsonArray = (JSONArray)value;
73-
JSONArray expectedJsonArray = (JSONArray)expectedValue;
85+
JSONArray jsonArray = (JSONArray) value;
86+
JSONArray expectedJsonArray = (JSONArray) expectedValue;
7487
compareActualVsExpectedJsonArrays(
75-
jsonArray, expectedJsonArray);
88+
jsonArray, expectedJsonArray);
7689
} else {
7790
/**
7891
* Compare all other types using toString(). First, the types must
@@ -99,6 +112,7 @@ private static void compareActualVsExpectedObjects(Object value,
99112

100113
/**
101114
* Asserts that all JSONObject maps are the same as the default ctor
115+
*
102116
* @param jsonObjects list of objects to be tested
103117
*/
104118
public static void checkJSONObjectsMaps(List<JSONObject> jsonObjects) {
@@ -116,6 +130,7 @@ public static void checkJSONObjectsMaps(List<JSONObject> jsonObjects) {
116130

117131
/**
118132
* Asserts that all JSONObject maps are the same as the default ctor
133+
*
119134
* @param jsonObject the object to be tested
120135
*/
121136
public static void checkJSONObjectMaps(JSONObject jsonObject) {
@@ -126,8 +141,9 @@ public static void checkJSONObjectMaps(JSONObject jsonObject) {
126141

127142
/**
128143
* Asserts that all JSONObject maps are the same as mapType
144+
*
129145
* @param jsonObject object to be tested
130-
* @param mapType mapType to test against
146+
* @param mapType mapType to test against
131147
*/
132148
public static void checkJSONObjectMaps(JSONObject jsonObject, Class<? extends Map> mapType) {
133149
if (mapType == null) {
@@ -141,14 +157,15 @@ public static void checkJSONObjectMaps(JSONObject jsonObject, Class<? extends Ma
141157
assertTrue(mapType == ((JSONObject) val).getMapType());
142158
checkJSONObjectMaps(jsonObjectVal, mapType);
143159
} else if (val instanceof JSONArray) {
144-
JSONArray jsonArrayVal = (JSONArray)val;
160+
JSONArray jsonArrayVal = (JSONArray) val;
145161
checkJSONArrayMaps(jsonArrayVal, mapType);
146162
}
147163
}
148164
}
149165

150166
/**
151167
* Asserts that all JSONObject maps in the JSONArray object match the default map
168+
*
152169
* @param jsonArrays list of JSONArray objects to be tested
153170
*/
154171
public static void checkJSONArraysMaps(List<JSONArray> jsonArrays) {
@@ -165,8 +182,9 @@ public static void checkJSONArraysMaps(List<JSONArray> jsonArrays) {
165182

166183
/**
167184
* Asserts that all JSONObject maps in the JSONArray object match mapType
185+
*
168186
* @param jsonArray object to be tested
169-
* @param mapType map type to be tested against
187+
* @param mapType map type to be tested against
170188
*/
171189
public static void checkJSONArrayMaps(JSONArray jsonArray, Class<? extends Map> mapType) {
172190
if (jsonArray == null) {
@@ -179,18 +197,18 @@ public static void checkJSONArrayMaps(JSONArray jsonArray, Class<? extends Map>
179197
while (it.hasNext()) {
180198
Object val = it.next();
181199
if (val instanceof JSONObject) {
182-
JSONObject jsonObjectVal = (JSONObject)val;
200+
JSONObject jsonObjectVal = (JSONObject) val;
183201
checkJSONObjectMaps(jsonObjectVal, mapType);
184202
} else if (val instanceof JSONArray) {
185-
JSONArray jsonArrayVal = (JSONArray)val;
203+
JSONArray jsonArrayVal = (JSONArray) val;
186204
checkJSONArrayMaps(jsonArrayVal, mapType);
187205
}
188206
}
189207
}
190208

191209
/**
192-
* Asserts that all JSONObject maps nested in the JSONArray match
193-
* the default mapType
210+
* Asserts that all JSONObject maps nested in the JSONArray match the default mapType
211+
*
194212
* @param jsonArray the object to be tested
195213
*/
196214
public static void checkJSONArrayMaps(JSONArray jsonArray) {

0 commit comments

Comments
 (0)