forked from stleary/JSON-java
/
Util.java
120 lines (108 loc) · 4.82 KB
/
Util.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import static org.junit.Assert.*;
import java.util.*;
import org.json.*;
/**
* These are helpful utility methods that perform basic comparisons
* between various objects. In most cases, the comparisons are not
* order-dependent, or else the order is known.
*/
public class Util {
/**
* Compares two JSONArrays for equality.
* The arrays need not be in the same order.
* @param jsonArray created by the code to be tested
* @param expectedJsonArray created specifically for comparing
*/
public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
JSONArray expectedJsonArray) {
assertTrue("jsonArray lengths should be equal",
jsonArray.length() == expectedJsonArray.length());
for (int i = 0; i < jsonArray.length(); ++i) {
Object value = jsonArray.get(i);
Object expectedValue = expectedJsonArray.get(i);
compareActualVsExpectedObjects(value, expectedValue);
}
}
/**
* Compares two JSONObjects for equality. The objects need not be
* in the same order
* @param jsonObject created by the code to be tested
* @param expectedJsonObject created specifically for comparing
*/
public static void compareActualVsExpectedJsonObjects(
JSONObject jsonObject, JSONObject expectedJsonObject) {
assertTrue("jsonObjects should have the same length",
jsonObject.length() == expectedJsonObject.length());
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = jsonObject.get(key);
Object expectedValue = expectedJsonObject.get(key);
compareActualVsExpectedObjects(value, expectedValue);
}
}
/**
* Compare two objects for equality. Might be JSONArray, JSONObject,
* or something else.
* @param value created by the code to be tested
* @param expectedValue created specifically for comparing
* @param key key to the jsonObject entry to be compared
*/
private static void compareActualVsExpectedObjects(Object value,
Object expectedValue) {
if (value instanceof JSONObject && expectedValue instanceof JSONObject) {
// Compare JSONObjects
JSONObject jsonObject = (JSONObject)value;
JSONObject expectedJsonObject = (JSONObject)expectedValue;
compareActualVsExpectedJsonObjects(
jsonObject, expectedJsonObject);
} else if (value instanceof JSONArray && expectedValue instanceof JSONArray) {
// Compare JSONArrays
JSONArray jsonArray = (JSONArray)value;
JSONArray expectedJsonArray = (JSONArray)expectedValue;
compareActualVsExpectedJsonArrays(
jsonArray, expectedJsonArray);
} else {
/**
* Compare all other types using toString(). First, the types must
* also be equal, unless both are Number type. Certain helper
* classes (e.g. XML) may create Long instead of Integer for small
* int values.
*/
if (!(value instanceof Number && expectedValue instanceof Number)) {
// Non-Number and non-matching types
assertEquals("object types should be equal ",
expectedValue.getClass().toString(),
value.getClass().toString()
);
}
/**
* Same types or both Numbers, compare by toString()
*/
assertEquals("values should be equal",
expectedValue.toString(),
value.toString()
);
}
}
}