forked from stleary/JSON-Java-unit-test
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
Latest commit
142 lines (131 loc) · 6 KB
/
Copy pathUtil.java
File metadata and controls
142 lines (131 loc) · 6 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
packageorg.json.junit;
importstaticorg.junit.Assert.*;
importjava.util.*;
importorg.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.
*/
publicclassUtil {
/**
* 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
*/
publicstaticvoidcompareActualVsExpectedJsonArrays(JSONArrayjsonArray,
JSONArrayexpectedJsonArray) {
assertTrue("jsonArray lengths should be equal",
jsonArray.length() == expectedJsonArray.length());
for (inti = 0; i < jsonArray.length(); ++i) {
Objectvalue = jsonArray.get(i);
ObjectexpectedValue = 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
*/
publicstaticvoidcompareActualVsExpectedJsonObjects(
JSONObjectjsonObject, JSONObjectexpectedJsonObject) {
assertTrue("jsonObjects should have the same length",
jsonObject.length() == expectedJsonObject.length());
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
Stringkey = keys.next();
Objectvalue = jsonObject.get(key);
ObjectexpectedValue = 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
*/
privatestaticvoidcompareActualVsExpectedObjects(Objectvalue,
ObjectexpectedValue) {
if (valueinstanceofJSONObject && expectedValueinstanceofJSONObject) {
JSONObjectjsonObject = (JSONObject)value;
JSONObjectexpectedJsonObject = (JSONObject)expectedValue;
compareActualVsExpectedJsonObjects(
jsonObject, expectedJsonObject);
} elseif (valueinstanceofJSONArray && expectedValueinstanceofJSONArray) {
JSONArrayjsonArray = (JSONArray)value;
JSONArrayexpectedJsonArray = (JSONArray)expectedValue;
compareActualVsExpectedJsonArrays(
jsonArray, expectedJsonArray);
} else {
/**
* Certain helper classes (e.g. XML) may create Long instead of
* Integer for small int values. As long as both are Numbers,
* just compare the toString() values.
* TODO: this may not work in the case where the underlying types
* do not have the same precision.
*/
if (!(valueinstanceofNumber && expectedValueinstanceofNumber)) {
assertTrue("object types should be equal for actual: "+
value.toString()+" ("+
value.getClass().toString()+") expected: "+
expectedValue.toString()+" ("+
expectedValue.getClass().toString()+")",
value.getClass().toString().equals(
expectedValue.getClass().toString()));
}
/**
* When in doubt, compare by string
* TODO: should not this be an else to the previous condition?
*/
assertTrue("string values should be equal for actual: "+
value.toString()+" expected: "+expectedValue.toString(),
value.toString().equals(expectedValue.toString()));
}
}
/**
* Sometimes test completion requires comparison of JSONArray objects that
* were produced from a JSONObject, and so unordered. This method is
* imperfect since it only compares the array elements and won't catch
* JSON syntax errors but at least it does not rely on ordering
* <p>
* It is expected that the arrays to be compared come from JSONArray
* instances which have been rendered by toString(), and whose syntax
* chars have been removed.
* <p>
* TODO: why are we not simply comparing the JSONArrays?
* <p>
* @param names an array of strings for comparison
* @param expectedNames the other array of strings for comparison
*/
publicstaticvoidcompareActualVsExpectedStringArrays(String[] names,
String [] expectedNames) {
assertTrue("Array lengths should be equal",
names.length == expectedNames.length);
List<String> lNames = newArrayList<String>(Arrays.asList(names));
for (inti = 0; i < expectedNames.length; ++i) {
StringexpectedName = expectedNames[i];
assertTrue("expected to find "+expectedName,
lNames.contains(expectedName));
lNames.remove(expectedName);
}
}
/**
* This is stopgap test utility. It is meant to compare strings
* of XML, but it does not take ordering into account and should
* not be expected to work correctly with complex XML.
* @param aXmlStr an XML doc to be compared
* @param bXmlStr the other XML doc to be compared
*/
publicstaticvoidcompareXML(StringaXmlStr, StringbXmlStr) {
// TODO For simple tests this may be adequate, but it won't work for
// elements with multiple attributes and possibly other cases as well.
// Should use XMLUnit or similar.
assertTrue("expected equal XML strings \naXmlStr: "+
aXmlStr+ "\nbXmlStr: " +bXmlStr, aXmlStr.equals(bXmlStr));
}
}