Esempio n. 1
1
 protected void compareJSONArrayOfSimpleValues(
     String key, JSONArray expected, JSONArray actual, JSONCompareResult result)
     throws JSONException {
   Map<Object, Integer> expectedCount =
       JSONCompareUtil.getCardinalityMap(jsonArrayToList(expected));
   Map<Object, Integer> actualCount = JSONCompareUtil.getCardinalityMap(jsonArrayToList(actual));
   for (Object o : expectedCount.keySet()) {
     if (!actualCount.containsKey(o)) {
       result.missing(key + "[]", o);
     } else if (!actualCount.get(o).equals(expectedCount.get(o))) {
       result.fail(
           key
               + "[]: Expected "
               + expectedCount.get(o)
               + " occurrence(s) of "
               + o
               + " but got "
               + actualCount.get(o)
               + " occurrence(s)");
     }
   }
   for (Object o : actualCount.keySet()) {
     if (!expectedCount.containsKey(o)) {
       result.unexpected(key + "[]", o);
     }
   }
 }
Esempio n. 2
0
 protected void compareJSONArrayOfJsonObjects(
     String key, JSONArray expected, JSONArray actual, JSONCompareResult result)
     throws JSONException {
   String uniqueKey = findUniqueKey(expected);
   if (uniqueKey == null || !isUsableAsUniqueKey(uniqueKey, actual)) {
     // An expensive last resort
     recursivelyCompareJSONArray(key, expected, actual, result);
     return;
   }
   Map<Object, JSONObject> expectedValueMap = arrayOfJsonObjectToMap(expected, uniqueKey);
   Map<Object, JSONObject> actualValueMap = arrayOfJsonObjectToMap(actual, uniqueKey);
   for (Object id : expectedValueMap.keySet()) {
     if (!actualValueMap.containsKey(id)) {
       result.missing(formatUniqueKey(key, uniqueKey, id), expectedValueMap.get(id));
       continue;
     }
     JSONObject expectedValue = expectedValueMap.get(id);
     JSONObject actualValue = actualValueMap.get(id);
     compareValues(formatUniqueKey(key, uniqueKey, id), expectedValue, actualValue, result);
   }
   for (Object id : actualValueMap.keySet()) {
     if (!expectedValueMap.containsKey(id)) {
       result.unexpected(formatUniqueKey(key, uniqueKey, id), actualValueMap.get(id));
     }
   }
 }