6
6
7
7
import org .json .*;
8
8
9
+ /**
10
+ * These are helpful utility methods that perform basic comparisons
11
+ * between various objects. In most cases, the comparisons are not
12
+ * order-dependent, or else the order is known.
13
+ */
9
14
public class Util {
10
15
11
-
12
-
13
16
/**
14
- * Compares two json arrays for equality
17
+ * Compares two JSONArrays for equality.
18
+ * The arrays need not be in the same order.
15
19
* @param jsonArray created by the code to be tested
16
20
* @param expectedJsonArray created specifically for comparing
17
21
*/
@@ -27,7 +31,8 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
27
31
}
28
32
29
33
/**
30
- * Compares two json objects for equality
34
+ * Compares two JSONObjects for equality. The objects need not be
35
+ * in the same order
31
36
* @param jsonObject created by the code to be tested
32
37
* @param expectedJsonObject created specifically for comparing
33
38
*/
@@ -68,6 +73,8 @@ private static void compareActualVsExpectedObjects(Object value,
68
73
* Certain helper classes (e.g. XML) may create Long instead of
69
74
* Integer for small int values. As long as both are Numbers,
70
75
* just compare the toString() values.
76
+ * TODO: this may not work in the case where the underlying types
77
+ * do not have the same precision.
71
78
*/
72
79
if (!(value instanceof Number && expectedValue instanceof Number )) {
73
80
assertTrue ("object types should be equal for actual: " +
@@ -78,12 +85,31 @@ private static void compareActualVsExpectedObjects(Object value,
78
85
value .getClass ().toString ().equals (
79
86
expectedValue .getClass ().toString ()));
80
87
}
88
+ /**
89
+ * When in doubt, compare by string
90
+ * TODO: should not this be an else to the previous condition?
91
+ */
81
92
assertTrue ("string values should be equal for actual: " +
82
93
value .toString ()+" expected: " +expectedValue .toString (),
83
94
value .toString ().equals (expectedValue .toString ()));
84
95
}
85
96
}
86
97
98
+ /**
99
+ * Sometimes test completion requires comparison of JSONArray objects that
100
+ * were produced from a JSONObject, and so unordered. This method is
101
+ * imperfect since it only compares the array elements and won't catch
102
+ * JSON syntax errors but at least it does not rely on ordering
103
+ * <p>
104
+ * It is expected that the arrays to be compared come from JSONArray
105
+ * instances which have been rendered by toString(), and whose syntax
106
+ * chars have been removed.
107
+ * <p>
108
+ * TODO: why are we not simply comparing the JSONArrays?
109
+ * <p>
110
+ * @param names an array of strings for comparison
111
+ * @param expectedNames the other array of strings for comparison
112
+ */
87
113
public static void compareActualVsExpectedStringArrays (String [] names ,
88
114
String [] expectedNames ) {
89
115
assertTrue ("Array lengths should be equal" ,
@@ -97,6 +123,13 @@ public static void compareActualVsExpectedStringArrays(String[] names,
97
123
}
98
124
}
99
125
126
+ /**
127
+ * This is stopgap test utility. It is meant to compare strings
128
+ * of XML, but it does not take ordering into account and should
129
+ * not be expected to work correctly with complex XML.
130
+ * @param aXmlStr an XML doc to be compared
131
+ * @param bXmlStr the other XML doc to be compared
132
+ */
100
133
public static void compareXML (String aXmlStr , String bXmlStr ) {
101
134
// TODO For simple tests this may be adequate, but it won't work for
102
135
// elements with multiple attributes and possibly other cases as well.
0 commit comments