From b83b7adaab007575f4d84aa2519018e3ec971be5 Mon Sep 17 00:00:00 2001 From: jakob Date: Fri, 6 Oct 2017 11:54:40 +0200 Subject: [PATCH] Added convenience methods for JSONObject comparison using a custom JSONComparator --- .../skyscreamer/jsonassert/JSONAssert.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java index d2a31cd1..efebf43c 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java @@ -515,6 +515,70 @@ public static void assertNotEquals(String message, String expectedStr, String ac } } + /** + * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an + * {@link AssertionError}. + * + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + * @throws JSONException JSON parsing error + */ + public static void assertEquals(JSONObject expected, JSONObject actual, JSONComparator comparator) + throws JSONException { + assertEquals("", expected, actual, comparator); + } + + /** + * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an + * {@link AssertionError}. + * + * @param message Error message to be displayed in case of assertion failure + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + * @throws JSONException JSON parsing error + */ + public static void assertEquals(String message, JSONObject expected, JSONObject actual, JSONComparator comparator) + throws JSONException { + JSONCompareResult result = JSONCompare.compareJSON(expected, actual, comparator); + if (result.failed()) { + throw new AssertionError(getCombinedMessage(message, result.getMessage())); + } + } + + /** + * Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws an + * {@link AssertionError}. + * + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + * @throws JSONException JSON parsing error + */ + public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONComparator comparator) + throws JSONException { + assertNotEquals("", expected, actual, comparator); + } + + /** + * Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws an + * {@link AssertionError}. + * + * @param message Error message to be displayed in case of assertion failure + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + * @throws JSONException JSON parsing error + */ + public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, JSONComparator comparator) + throws JSONException { + JSONCompareResult result = JSONCompare.compareJSON(expected, actual, comparator); + if (result.passed()) { + throw new AssertionError(getCombinedMessage(message, result.getMessage())); + } + } + /** * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an * {@link AssertionError}.