/** * Identify the missing field * @param field missing field * @param expected expected result * @return result of comparison */ public JSONCompareResult missing(String field, Object expected) { _fieldMissing.add(new FieldComparisonFailure(field, expected, null)); fail(formatMissing(field, expected)); return this; }
/** * Identify unexpected field * @param field unexpected field * @param actual actual result * @return result of comparison */ public JSONCompareResult unexpected(String field, Object actual) { _fieldUnexpected.add(new FieldComparisonFailure(field, null, actual)); fail(formatUnexpected(field, actual)); return this; }
@Override public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException { if (areNumbers(expectedValue, actualValue)) { if (areNotSameDoubles(expectedValue, actualValue)) { result.fail(prefix, expectedValue, actualValue); } } else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) { if (expectedValue instanceof JSONArray) { compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result); } else if (expectedValue instanceof JSONObject) { compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result); } else if (!expectedValue.equals(actualValue)) { result.fail(prefix, expectedValue, actualValue); } } else { result.fail(prefix, expectedValue, actualValue); } }
@Override public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException { Customization customization = getCustomization(prefix); if (customization != null) { try { if (!customization.matches(prefix, actualValue, expectedValue, result)) { result.fail(prefix, expectedValue, actualValue); } } catch (ValueMatcherException e) { result.fail(prefix, e); } } else { super.compareValues(prefix, expectedValue, actualValue, result); } }
/** * Identify that the comparison failed * @param field Which field failed * @param expected Expected result * @param actual Actual result * @return result of comparision */ public JSONCompareResult fail(String field, Object expected, Object actual) { _fieldFailures.add(new FieldComparisonFailure(field, expected, actual)); this._field = field; this._expected = expected; this._actual = actual; fail(formatFailureMessage(field, expected, actual)); return this; }
String arrayPrefix = prefix + "[]"; if (expected.length() < 1 || expected.length() > 2) { result.fail(MessageFormat .format("{0}: invalid expectation: expected array should contain either 1 or 2 elements but contains {1} elements", arrayPrefix, expected.length())); result.fail(MessageFormat .format("{0}: invalid expectation: {1}expected array size ''{2}'' not a number", arrayPrefix, (expected.length() == 1? "": "minimum "), expected.get(0))); result.fail(MessageFormat .format("{0}: invalid expectation: maximum expected array size ''{1}'' not a number", arrayPrefix, expected.get(1))); result.fail(MessageFormat .format("{0}: invalid expectation: minimum expected array size ''{1}'' negative", arrayPrefix, minExpectedLength)); : minExpectedLength; if (maxExpectedLength < minExpectedLength) { result.fail(MessageFormat .format("{0}: invalid expectation: maximum expected array size ''{1}'' less than minimum expected array size ''{2}''", arrayPrefix, maxExpectedLength, minExpectedLength)); result.fail( arrayPrefix, MessageFormat.format(
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); } } }
result.fail(key + "[" + i + "] Could not find match for element " + expectedElement); return;
/** * Compares {@link JSONString} provided to the expected {@code JSONString}, checking that the * {@link org.json.JSONString#toJSONString()} are equal. * * @param expected Expected {@code JSONstring} * @param actual {@code JSONstring} to compare * @return result of the comparison */ public static JSONCompareResult compareJson(final JSONString expected, final JSONString actual) { final JSONCompareResult result = new JSONCompareResult(); final String expectedJson = expected.toJSONString(); final String actualJson = actual.toJSONString(); if (!expectedJson.equals(actualJson)) { result.fail(""); } return result; }
/** * Identify that the comparison failed * @param field Which field failed * @param exception exception containing details of match failure * @return result of comparision */ public JSONCompareResult fail(String field, ValueMatcherException exception) { fail(field + ": " + exception.getMessage(), exception.getExpected(), exception.getActual()); return this; }
@Override public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException { if (expected.length() != actual.length()) { result.fail(prefix + "[]: Expected " + expected.length() + " values but got " + actual.length()); return; } else if (expected.length() == 0) { return; // Nothing to compare } if (mode.hasStrictOrder()) { compareJSONArrayWithStrictOrder(prefix, expected, actual, result); } else if (allSimpleValues(expected)) { compareJSONArrayOfSimpleValues(prefix, expected, actual, result); } else if (allJSONObjects(expected)) { compareJSONArrayOfJsonObjects(prefix, expected, actual, result); } else { // An expensive last resort recursivelyCompareJSONArray(prefix, expected, actual, result); } }
/** * Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of * the comparison. * @param expectedStr Expected JSON string * @param actualStr JSON string to compare * @param comparator Comparator to use * @return result of the comparison * @throws JSONException JSON parsing error * @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr */ public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); Object actual = JSONParser.parseJSON(actualStr); if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) { return compareJSON((JSONObject) expected, (JSONObject) actual, comparator); } else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) { return compareJSON((JSONArray)expected, (JSONArray)actual, comparator); } else if (expected instanceof JSONString && actual instanceof JSONString) { return compareJson((JSONString) expected, (JSONString) actual); } else if (expected instanceof JSONObject) { return new JSONCompareResult().fail("", expected, actual); } else { return new JSONCompareResult().fail("", expected, actual); } }
/** * Identify the missing field * @param field missing field * @param expected expected result * @return result of comparison */ public JSONCompareResult missing(String field, Object expected) { _fieldMissing.add(new FieldComparisonFailure(field, expected, null)); fail(formatMissing(field, expected)); return this; }
@Override public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException { if (areNumbers(expectedValue, actualValue)) { if (areNotSameDoubles(expectedValue, actualValue)) { result.fail(prefix, expectedValue, actualValue); } } else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) { if (expectedValue instanceof JSONArray) { compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result); } else if (expectedValue instanceof JSONObject) { compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result); } else if (!expectedValue.equals(actualValue)) { result.fail(prefix, expectedValue, actualValue); } } else { result.fail(prefix, expectedValue, actualValue); } }
/** * Identify unexpected field * @param field unexpected field * @param actual actual result * @return result of comparison */ public JSONCompareResult unexpected(String field, Object actual) { _fieldUnexpected.add(new FieldComparisonFailure(field, null, actual)); fail(formatUnexpected(field, actual)); return this; }
@Override public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException { Customization customization = getCustomization(prefix); if (customization != null) { try { if (!customization.matches(prefix, actualValue, expectedValue, result)) { result.fail(prefix, expectedValue, actualValue); } } catch (ValueMatcherException e) { result.fail(prefix, e); } } else { super.compareValues(prefix, expectedValue, actualValue, result); } }
/** * Identify that the comparison failed * @param field Which field failed * @param expected Expected result * @param actual Actual result * @return result of comparision */ public JSONCompareResult fail(String field, Object expected, Object actual) { _fieldFailures.add(new FieldComparisonFailure(field, expected, actual)); this._field = field; this._expected = expected; this._actual = actual; fail(formatFailureMessage(field, expected, actual)); return this; }
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); } } }
/** * Compares {@link JSONString} provided to the expected {@code JSONString}, checking that the * {@link org.json.JSONString#toJSONString()} are equal. * * @param expected Expected {@code JSONstring} * @param actual {@code JSONstring} to compare * @return result of the comparison */ public static JSONCompareResult compareJson(final JSONString expected, final JSONString actual) { final JSONCompareResult result = new JSONCompareResult(); final String expectedJson = expected.toJSONString(); final String actualJson = actual.toJSONString(); if (!expectedJson.equals(actualJson)) { result.fail(""); } return result; }
/** * Identify that the comparison failed * @param field Which field failed * @param exception exception containing details of match failure * @return result of comparision */ public JSONCompareResult fail(String field, ValueMatcherException exception) { fail(field + ": " + exception.getMessage(), exception.getExpected(), exception.getActual()); return this; }