/** Assert set equals */ public static void assertEquals(Set<?> actual, Set<?> expected, String message) { if (actual == expected) { return; } if (actual == null || expected == null) { // Keep the back compatible if (message == null) message = "Sets not equal: expected: " + expected + " and actual: " + actual; log.error(message(message, expected, actual)); if (haultonfailure) fail(message); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } if (!actual.equals(expected)) { if (message == null) message = "Sets differ: expected " + expected + " but got " + actual; log.error(message(message, expected, actual)); if (haultonfailure) fail(message); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } }
/** Asserts that two maps are equal. */ public static void assertEquals(Map<?, ?> actual, Map<?, ?> expected) { if (actual == expected) { return; } if (actual == null || expected == null) { log.error(message(null, expected.toString(), actual.toString())); if (haultonfailure) fail("Maps not equal: expected: " + expected + " and actual: " + actual); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( "Maps not equal: " + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } if (actual.size() != expected.size()) { if (haultonfailure) fail("Maps do not have the same size:" + actual.size() + " != " + expected.size()); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( "Maps do not have the same size: " + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } Set<?> entrySet = actual.entrySet(); for (Iterator<?> iterator = entrySet.iterator(); iterator.hasNext(); ) { Map.Entry<?, ?> entry = (Map.Entry<?, ?>) iterator.next(); Object key = entry.getKey(); Object value = entry.getValue(); Object expectedValue = expected.get(key); assertEquals( value, expectedValue, "Maps do not match for key:" + key + " actual:" + value + " expected:" + expectedValue); } }
/** * Asserts that two iterables return iterators with the same elements in the same order. If they * do not, an AssertionError, with the given message, is thrown. * * @param actual the actual value * @param expected the expected value * @param message the assertion error message */ public static void assertEquals(Iterable<?> actual, Iterable<?> expected, String message) { if (actual == expected) { return; } if (actual == null || expected == null) { log.error(message(message, expected, actual)); if (message == null) { message = "Iterables not equal:"; } if (haultonfailure) fail(message); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } Iterator<?> actIt = actual.iterator(); Iterator<?> expIt = expected.iterator(); assertEquals(actIt, expIt, message); }
private static void failnotequals(Object actual, Object expected, String message) { log.error(message(message, expected.toString(), actual.toString())); if (haultonfailure) { failNotEquals(actual, expected, message); } else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), expected + SEPARATOR + actual); } }
/** * Asserts that a condition is false. If it isn't, an AssertionError, with the given message, is * thrown if haultonfailure. * * @param condition the condition to evaluate * @param message the assertion error message */ public static void assertFalse(boolean condition, String message) { if (condition) { log.error(message(message, Boolean.FALSE.toString(), String.valueOf(condition))); if (haultonfailure) { failNotEquals(Boolean.valueOf(condition), Boolean.FALSE, message); } else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), Boolean.FALSE + SEPARATOR + condition); } } }
/** * Asserts that two collections contain the same elements in the same order. If they do not, an * AssertionError, with the given message, is thrown. * * @param actual the actual value * @param expected the expected value * @param message the assertion error message */ public static void assertEquals(Collection<?> actual, Collection<?> expected, String message) { if (actual == expected) { return; } if (actual == null || expected == null) { log.error(message(message, actual, expected)); if (message == null) { message = "Collections not equal:"; } if (haultonfailure) fail(message); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } assertEquals(actual.size(), expected.size(), message + ": lists don't have the same size"); // if(actual.size() != expected.size())return; Iterator<?> actIt = actual.iterator(); Iterator<?> expIt = expected.iterator(); int i = -1; while (actIt.hasNext() && expIt.hasNext()) { i++; Object e = expIt.next(); Object a = actIt.next(); String explanation = "Lists differ at element [" + i + "]: " + e + " != " + a; String errorMessage = message == null ? explanation : message + ": " + explanation; assertEquals(a, e, errorMessage); } }
/** * Asserts that two arrays contain the same elements in the same order. If they do not, an * AssertionError, with the given message, is thrown. * * @param actual the actual value * @param expected the expected value * @param message the assertion error message */ public static void assertEquals(Object[] actual, Object[] expected, String message) { if (actual == expected) { return; } if ((actual == null && expected != null) || (actual != null && expected == null)) { log.error(message(message, expected, actual)); if (message == null) { message = "Iterables not equal:"; } if (haultonfailure) fail(message); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } assertEquals(Arrays.asList(actual), Arrays.asList(expected), message); }
/** * Asserts that two arrays contain the same elements in the same order. If they do not, an * AssertionError, with the given message, is thrown. * * @param actual the actual value * @param expected the expected value * @param message the assertion error message */ public static void assertEquals( final byte[] actual, final byte[] expected, final String message) { if (expected == actual) { return; } if (null == expected) { if (haultonfailure) fail("expected a null array, but not null found. " + message); else { if (map == null) map = new LinkedHashMap<String, String>(); map.put( "expected a null array, but not null found. " + SEPARATOR + message + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } if (null == actual) { if (haultonfailure) fail("expected not null array, but null found. " + message); else { if (map == null) map = new LinkedHashMap<String, String>(); map.put( "expected not null array, but null found. " + SEPARATOR + message + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } assertEquals(actual.length, expected.length, "arrays don't have the same size. " + message); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { if (haultonfailure) fail( "arrays differ firstly at element [" + i + "]; " + "expected value is <" + expected[i] + "> but was <" + actual[i] + ">. " + message); else { if (map == null) map = new LinkedHashMap<String, String>(); map.put( "arrays differ firstly at element [" + i + "]; " + "expected value is <" + expected[i] + "> but was <" + actual[i] + ">. " + message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } } }
/** * Asserts that two arrays contain the same elements in no particular order. If they do not, an * AssertionError, with the given message, is thrown. * * @param actual the actual value * @param expected the expected value * @param message the assertion error message */ public static void assertEqualsNoOrder(Object[] actual, Object[] expected, String message) { if (actual == expected) { return; } if ((actual == null && expected != null) || (actual != null && expected == null)) { if (message == null) message = "Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual); log.error(message(message, actual, expected)); if (haultonfailure) fail(message(message, expected, actual)); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } if (actual.length != expected.length) { if (message == null) message = "Arrays do not have the same size:" + actual.length + " != " + expected.length; log.error(message(message, expected, actual)); if (haultonfailure) fail(message(message, String.valueOf(expected.length), String.valueOf(actual.length))); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } List<Object> actualCollection = Lists.newArrayList(); // need to look into src for (Object a : actual) { actualCollection.add(a); } for (Object o : expected) { actualCollection.remove(o); } if (actualCollection.size() != 0) { if (message == null) message = "Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual); log.error(message(message, expected, actual)); if (haultonfailure) fail(message(message, String.valueOf(Arrays.toString(expected)), Arrays.toString(actual))); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } }
/** * Asserts that two iterators return the same elements in the same order. If they do not, an * AssertionError, with the given message, is thrown. Please note that this assert iterates over * the elements and modifies the state of the iterators. * * @param actual the actual value * @param expected the expected value * @param message the assertion error message */ public static void assertEquals(Iterator<?> actual, Iterator<?> expected, String message) { if (actual == expected) { return; } if (actual == null || expected == null) { log.error(message(message, actual, expected)); if (message == null) { message = "Iterators not equal:"; } if (haultonfailure) fail(message + "\n"); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } int i = -1; while (actual.hasNext() && expected.hasNext()) { i++; Object e = expected.next(); Object a = actual.next(); String explanation = "Iterators differ at element [" + i + "]: " + e + " != " + a; String errorMessage = message == null ? explanation : message + ": " + explanation; assertEquals(a, e, errorMessage); } if (actual.hasNext()) { String explanation = "Actual iterator returned more elements than the expected iterator."; String errorMessage = message == null ? explanation : message + ": " + explanation; if (haultonfailure) fail(message + "\n"); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } else if (expected.hasNext()) { String explanation = "Expected iterator returned more elements than the actual iterator."; String errorMessage = message == null ? explanation : message + ": " + explanation; if (haultonfailure) fail(message + "\n"); else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), actual + SEPARATOR + expected); } } }
/** * Asserts that two objects are equal. It they are not, an AssertionError, with given message, is * thrown if haultonfailure. * * @param actual the actual value * @param expected the expected value (should be an non-null array value) * @param message the assertion error message */ private static void assertArrayEquals(Object actual, Object expected, String message) { // is called only when expected is an array if (actual.getClass().isArray()) { int expectedLength = Array.getLength(expected); if (expectedLength == Array.getLength(actual)) { for (int i = 0; i < expectedLength; i++) { Object _actual = Array.get(actual, i); Object _expected = Array.get(expected, i); try { assertEquals(_actual, _expected); } catch (AssertionError ae) { log.error(message(message, expected.toString(), actual.toString())); if (haultonfailure) { failNotEquals(actual, expected, message); } else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message == null ? "" : message + " (values at index " + i + " are not the same)" + SEPARATOR + System.currentTimeMillis(), expected + SEPARATOR + actual); return; } } } // array values matched return; } else { log.error(message(message, expected.toString(), actual.toString())); if (haultonfailure) { failNotEquals(actual, expected, message); } else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message == null ? "" : message + " (Array lengths are not the same)" + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), expectedLength + SEPARATOR + Array.getLength(actual)); } } } log.error(message(message, expected.toString(), actual.toString())); if (haultonfailure) { failNotEquals(actual, expected, message); } else { if (map == null) { map = new LinkedHashMap<String, String>(); } map.put( message + SEPARATOR + UtilityMethods.getRandomNumber() + System.currentTimeMillis(), expected + SEPARATOR + actual); } }