6
6
7
7
import static org .junit .Assert .*;
8
8
9
+ import java .io .IOException ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
12
+ import java .nio .file .Paths ;
9
13
import java .util .*;
10
14
15
+ import java .util .stream .Collectors ;
16
+ import java .util .stream .Stream ;
11
17
import org .json .*;
12
18
13
19
/**
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.
17
22
*/
18
23
public class Util {
19
24
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
+
20
33
/**
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
24
37
* @param expectedJsonArray created specifically for comparing
25
38
*/
26
39
public static void compareActualVsExpectedJsonArrays (JSONArray jsonArray ,
27
- JSONArray expectedJsonArray ) {
40
+ JSONArray expectedJsonArray ) {
28
41
assertTrue ("jsonArray lengths should be equal" ,
29
- jsonArray .length () == expectedJsonArray .length ());
42
+ jsonArray .length () == expectedJsonArray .length ());
30
43
for (int i = 0 ; i < jsonArray .length (); ++i ) {
31
44
Object value = jsonArray .get (i );
32
45
Object expectedValue = expectedJsonArray .get (i );
@@ -35,15 +48,15 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
35
48
}
36
49
37
50
/**
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
41
54
* @param expectedJsonObject created specifically for comparing
42
55
*/
43
56
public static void compareActualVsExpectedJsonObjects (
44
- JSONObject jsonObject , JSONObject expectedJsonObject ) {
57
+ JSONObject jsonObject , JSONObject expectedJsonObject ) {
45
58
assertTrue ("jsonObjects should have the same length" ,
46
- jsonObject .length () == expectedJsonObject .length ());
59
+ jsonObject .length () == expectedJsonObject .length ());
47
60
Iterator <String > keys = jsonObject .keys ();
48
61
while (keys .hasNext ()) {
49
62
String key = keys .next ();
@@ -54,25 +67,25 @@ public static void compareActualVsExpectedJsonObjects(
54
67
}
55
68
56
69
/**
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
60
73
* @param expectedValue created specifically for comparing
61
74
*/
62
75
private static void compareActualVsExpectedObjects (Object value ,
63
- Object expectedValue ) {
76
+ Object expectedValue ) {
64
77
if (value instanceof JSONObject && expectedValue instanceof JSONObject ) {
65
78
// Compare JSONObjects
66
- JSONObject jsonObject = (JSONObject )value ;
67
- JSONObject expectedJsonObject = (JSONObject )expectedValue ;
79
+ JSONObject jsonObject = (JSONObject ) value ;
80
+ JSONObject expectedJsonObject = (JSONObject ) expectedValue ;
68
81
compareActualVsExpectedJsonObjects (
69
- jsonObject , expectedJsonObject );
82
+ jsonObject , expectedJsonObject );
70
83
} else if (value instanceof JSONArray && expectedValue instanceof JSONArray ) {
71
84
// Compare JSONArrays
72
- JSONArray jsonArray = (JSONArray )value ;
73
- JSONArray expectedJsonArray = (JSONArray )expectedValue ;
85
+ JSONArray jsonArray = (JSONArray ) value ;
86
+ JSONArray expectedJsonArray = (JSONArray ) expectedValue ;
74
87
compareActualVsExpectedJsonArrays (
75
- jsonArray , expectedJsonArray );
88
+ jsonArray , expectedJsonArray );
76
89
} else {
77
90
/**
78
91
* Compare all other types using toString(). First, the types must
@@ -99,6 +112,7 @@ private static void compareActualVsExpectedObjects(Object value,
99
112
100
113
/**
101
114
* Asserts that all JSONObject maps are the same as the default ctor
115
+ *
102
116
* @param jsonObjects list of objects to be tested
103
117
*/
104
118
public static void checkJSONObjectsMaps (List <JSONObject > jsonObjects ) {
@@ -116,6 +130,7 @@ public static void checkJSONObjectsMaps(List<JSONObject> jsonObjects) {
116
130
117
131
/**
118
132
* Asserts that all JSONObject maps are the same as the default ctor
133
+ *
119
134
* @param jsonObject the object to be tested
120
135
*/
121
136
public static void checkJSONObjectMaps (JSONObject jsonObject ) {
@@ -126,8 +141,9 @@ public static void checkJSONObjectMaps(JSONObject jsonObject) {
126
141
127
142
/**
128
143
* Asserts that all JSONObject maps are the same as mapType
144
+ *
129
145
* @param jsonObject object to be tested
130
- * @param mapType mapType to test against
146
+ * @param mapType mapType to test against
131
147
*/
132
148
public static void checkJSONObjectMaps (JSONObject jsonObject , Class <? extends Map > mapType ) {
133
149
if (mapType == null ) {
@@ -141,14 +157,15 @@ public static void checkJSONObjectMaps(JSONObject jsonObject, Class<? extends Ma
141
157
assertTrue (mapType == ((JSONObject ) val ).getMapType ());
142
158
checkJSONObjectMaps (jsonObjectVal , mapType );
143
159
} else if (val instanceof JSONArray ) {
144
- JSONArray jsonArrayVal = (JSONArray )val ;
160
+ JSONArray jsonArrayVal = (JSONArray ) val ;
145
161
checkJSONArrayMaps (jsonArrayVal , mapType );
146
162
}
147
163
}
148
164
}
149
165
150
166
/**
151
167
* Asserts that all JSONObject maps in the JSONArray object match the default map
168
+ *
152
169
* @param jsonArrays list of JSONArray objects to be tested
153
170
*/
154
171
public static void checkJSONArraysMaps (List <JSONArray > jsonArrays ) {
@@ -165,8 +182,9 @@ public static void checkJSONArraysMaps(List<JSONArray> jsonArrays) {
165
182
166
183
/**
167
184
* Asserts that all JSONObject maps in the JSONArray object match mapType
185
+ *
168
186
* @param jsonArray object to be tested
169
- * @param mapType map type to be tested against
187
+ * @param mapType map type to be tested against
170
188
*/
171
189
public static void checkJSONArrayMaps (JSONArray jsonArray , Class <? extends Map > mapType ) {
172
190
if (jsonArray == null ) {
@@ -179,18 +197,18 @@ public static void checkJSONArrayMaps(JSONArray jsonArray, Class<? extends Map>
179
197
while (it .hasNext ()) {
180
198
Object val = it .next ();
181
199
if (val instanceof JSONObject ) {
182
- JSONObject jsonObjectVal = (JSONObject )val ;
200
+ JSONObject jsonObjectVal = (JSONObject ) val ;
183
201
checkJSONObjectMaps (jsonObjectVal , mapType );
184
202
} else if (val instanceof JSONArray ) {
185
- JSONArray jsonArrayVal = (JSONArray )val ;
203
+ JSONArray jsonArrayVal = (JSONArray ) val ;
186
204
checkJSONArrayMaps (jsonArrayVal , mapType );
187
205
}
188
206
}
189
207
}
190
208
191
209
/**
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
+ *
194
212
* @param jsonArray the object to be tested
195
213
*/
196
214
public static void checkJSONArrayMaps (JSONArray jsonArray ) {
0 commit comments