66
77import org .json .*;
88
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+ */
914public class Util {
1015
11-
12-
1316 /**
14- * Compares two json arrays for equality
17+ * Compares two JSONArrays for equality.
18+ * The arrays need not be in the same order.
1519 * @param jsonArray created by the code to be tested
1620 * @param expectedJsonArray created specifically for comparing
1721 */
@@ -27,7 +31,8 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
2731 }
2832
2933 /**
30- * Compares two json objects for equality
34+ * Compares two JSONObjects for equality. The objects need not be
35+ * in the same order
3136 * @param jsonObject created by the code to be tested
3237 * @param expectedJsonObject created specifically for comparing
3338 */
@@ -68,6 +73,8 @@ private static void compareActualVsExpectedObjects(Object value,
6873 * Certain helper classes (e.g. XML) may create Long instead of
6974 * Integer for small int values. As long as both are Numbers,
7075 * 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.
7178 */
7279 if (!(value instanceof Number && expectedValue instanceof Number )) {
7380 assertTrue ("object types should be equal for actual: " +
@@ -78,12 +85,31 @@ private static void compareActualVsExpectedObjects(Object value,
7885 value .getClass ().toString ().equals (
7986 expectedValue .getClass ().toString ()));
8087 }
88+ /**
89+ * When in doubt, compare by string
90+ * TODO: should not this be an else to the previous condition?
91+ */
8192 assertTrue ("string values should be equal for actual: " +
8293 value .toString ()+" expected: " +expectedValue .toString (),
8394 value .toString ().equals (expectedValue .toString ()));
8495 }
8596 }
8697
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+ */
87113 public static void compareActualVsExpectedStringArrays (String [] names ,
88114 String [] expectedNames ) {
89115 assertTrue ("Array lengths should be equal" ,
@@ -97,6 +123,13 @@ public static void compareActualVsExpectedStringArrays(String[] names,
97123 }
98124 }
99125
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+ */
100133 public static void compareXML (String aXmlStr , String bXmlStr ) {
101134 // TODO For simple tests this may be adequate, but it won't work for
102135 // elements with multiple attributes and possibly other cases as well.
0 commit comments