/** * Asserts that the JSONArray provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertNotEquals((JSONArray) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON object, but passing in a JSON array"); } }
/** * Asserts that the JSONObject provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, JSONObject actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONObject) { assertEquals(message, (JSONObject)expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON array, but passing in a JSON object"); } }
/** * Asserts that the JSONArray provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertNotEquals(message, (JSONArray) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON object, but passing in a JSON array"); } }
/** * Asserts that the JSONArray provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertEquals(message, (JSONArray) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON object, but passing in a JSON array"); } }
/** * Asserts that the JSONObject provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @see #assertEquals(String, JSONObject, JSONCompareMode) * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String message, String expectedStr, JSONObject actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONObject) { assertNotEquals(message, (JSONObject) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON array, but passing in a JSON object"); } }
@Override protected void describeMismatchSafely(final String item, final Description mismatchDescription) { try { Object itemAsPrettyJson = JSONParser.parseJSON(item); mismatchDescription.appendText("was ").appendValue(itemAsPrettyJson); } catch (final JSONException e) { throw new RuntimeException(String.format("Error parsing expected JSON string %s. Got Exception %s", item, e.toString())); } } }
@Override public void describeTo(final Description description) { try { Object expectedJsonAsPrettyJson = JSONParser.parseJSON(expectedJson); description.appendValue(expectedJsonAsPrettyJson); } catch (final JSONException e) { throw new RuntimeException(String.format("Error parsing expected JSON string %s. Got Exception %s", expectedJson, e.toString())); } }
/** * 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); } }
/** * Asserts that the JSONArray provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertNotEquals((JSONArray) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON object, but passing in a JSON array"); } }
/** * Asserts that the JSONArray provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertEquals(message, (JSONArray) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON object, but passing in a JSON array"); } }
/** * Asserts that the JSONArray provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertNotEquals(message, (JSONArray) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON object, but passing in a JSON array"); } }
/** * Asserts that the JSONObject provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, JSONObject actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONObject) { assertEquals(message, (JSONObject)expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON array, but passing in a JSON object"); } }
/** * Asserts that the JSONObject provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @see #assertEquals(String, JSONObject, JSONCompareMode) * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String message, String expectedStr, JSONObject actual, JSONCompareMode compareMode) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONObject) { assertNotEquals(message, (JSONObject) expected, actual, compareMode); } else { throw new AssertionError("Expecting a JSON array, but passing in a JSON object"); } }
/** * 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); } }
@Test public void lenientArrayOfJSONObjectsWithSameKeysButDifferentValues() throws JSONException { final JSONArray array1 = (JSONArray) JSONParser .parseJSON("[ { foo:\"123\" }, { bar:\"456\" } ]"); final JSONArray array2 = (JSONArray) JSONParser .parseJSON("[ { bar:\"456\" }, { foo:\"123\" } ]"); final JSONCompareResult results = JSONCompare.compareJSON(array1, array2, new RegularExpressionJSONComparator(JSONCompareMode.LENIENT)); Assert.assertFalse(results.failed()); }
@Test public void lenientOutOfOrderArraysMixedAndMissingItems() throws JSONException { final JSONArray array1 = (JSONArray) JSONParser.parseJSON("[ { foo:\"123\" }, 789 ]"); final JSONArray array2 = (JSONArray) JSONParser.parseJSON("[ 456, { foo:\"123\" } ]"); final JSONCompareResult results = JSONCompare.compareJSON(array1, array2, new RegularExpressionJSONComparator(JSONCompareMode.LENIENT)); Assert.assertTrue(results.failed()); }
@Test public void strictOutOfOrderArraysContainingArrays() throws JSONException { final JSONArray array1 = (JSONArray) JSONParser.parseJSON("[ [\"123\" ], [\"456\" ] ]"); final JSONArray array2 = (JSONArray) JSONParser.parseJSON("[ [\"456\" ], [\"123\" ] ]"); final JSONCompareResult results = JSONCompare.compareJSON(array1, array2, new RegularExpressionJSONComparator(JSONCompareMode.STRICT_ORDER)); Assert.assertTrue(results.failed()); }
@Test public void strictOutOfOrderArraysContainingObjects() throws JSONException { final JSONArray array1 = (JSONArray) JSONParser .parseJSON("[ { foo:\"123\" }, { bar:\"456\" } ]"); final JSONArray array2 = (JSONArray) JSONParser .parseJSON("[ { bar:\"456\" }, { foo:\"123\" } ]"); final JSONCompareResult results = JSONCompare.compareJSON(array1, array2, new RegularExpressionJSONComparator(JSONCompareMode.STRICT_ORDER)); Assert.assertTrue(results.failed()); }
@Test public void lenientOutOfOrderArrays() throws JSONException { final JSONArray array1 = (JSONArray) JSONParser.parseJSON("[\"123\", \"456\"]"); final JSONArray array2 = (JSONArray) JSONParser.parseJSON("[\"456\", \"123\"]"); final JSONCompareResult results = JSONCompare.compareJSON(array1, array2, new RegularExpressionJSONComparator(JSONCompareMode.LENIENT)); Assert.assertFalse(results.failed()); }
@Test public void lenientOutOfOrderArraysContainingDifferentObjects() throws JSONException { final JSONArray array1 = (JSONArray) JSONParser .parseJSON("[ { foo:\"123\" }, { foo:\"456\" }, { foo:\"BBB\" } ]"); final JSONArray array2 = (JSONArray) JSONParser .parseJSON("[ { bar:\"456\" }, { bar:\"123\" }, { bar:\"AAA\" } ]"); final JSONCompareResult results = JSONCompare.compareJSON(array1, array2, new RegularExpressionJSONComparator(JSONCompareMode.LENIENT)); Assert.assertTrue(results.failed()); }