All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mkolisnyk.cucumber.assertions.LazyAssert Maven / Gradle / Ivy

Go to download

The part of Cucumber Reports library which contains extended Cucumber-JVM runners and all relevant functionality.

There is a newer version: 1.3.5
Show newest version
package com.github.mkolisnyk.cucumber.assertions;

import org.apache.commons.lang3.StringUtils;
import org.hamcrest.Matcher;
import org.junit.ComparisonFailure;
import org.junit.internal.ArrayComparisonFailure;
import org.junit.internal.ExactComparisonCriteria;
import org.junit.internal.InexactComparisonCriteria;

public final class LazyAssert {

    private LazyAssert() {
    }
/*
    public static void fail() {
        throw new LazyLazyAssertionError();
    }
    public static void fail(String message) {
        throw new LazyLazyAssertionError(message);
    }
    public static void assertTrue(boolean condition, String message) {
        if (!condition) {
            if (message == null) {
                fail();
            } else {
                fail(message);
            }
        }
    }
    public static void assertTrue(boolean condition) {
        assertTrue(condition, null);
    }
*/
    /**
     * Asserts that a condition is true. If it isn't it throws an
     * {@link LazyAssertionError} with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param condition condition to be checked
     */
    public static void assertTrue(String message, boolean condition) {
        if (!condition) {
            fail(message);
        }
    }

    /**
     * Asserts that a condition is true. If it isn't it throws an
     * {@link LazyAssertionError} without a message.
     *
     * @param condition condition to be checked
     */
    public static void assertTrue(boolean condition) {
        assertTrue(null, condition);
    }

    /**
     * Asserts that a condition is false. If it isn't it throws an
     * {@link LazyAssertionError} with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param condition condition to be checked
     */
    public static void assertFalse(String message, boolean condition) {
        assertTrue(message, !condition);
    }

    /**
     * Asserts that a condition is false. If it isn't it throws an
     * {@link LazyAssertionError} without a message.
     *
     * @param condition condition to be checked
     */
    public static void assertFalse(boolean condition) {
        assertFalse(null, condition);
    }

    /**
     * Fails a test with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @see LazyAssertionError
     */
    public static void fail(String message) {
        if (message == null) {
            throw new LazyAssertionError();
        }
        throw new LazyAssertionError(message);
    }

    /**
     * Fails a test with no message.
     */
    public static void fail() {
        fail(null);
    }

    /**
     * Asserts that two objects are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message. If
     * expected and actual are null,
     * they are considered equal.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expected expected value
     * @param actual actual value
     */
    public static void assertEquals(String message, Object expected,
            Object actual) {
        if (equalsRegardingNull(expected, actual)) {
            return;
        } else if (expected instanceof String && actual instanceof String) {
            String cleanMessage = "";
            if (StringUtils.isNotBlank(message)) {
                cleanMessage = message;
            }
            throw new ComparisonFailure(cleanMessage, (String) expected,
                    (String) actual);
        } else {
            failNotEquals(message, expected, actual);
        }
    }

    private static boolean equalsRegardingNull(Object expected, Object actual) {
        if (expected == null) {
            return actual == null;
        }

        return isEquals(expected, actual);
    }

    private static boolean isEquals(Object expected, Object actual) {
        return expected.equals(actual);
    }

    /**
     * Asserts that two objects are equal. If they are not, an
     * {@link LazyAssertionError} without a message is thrown. If
     * expected and actual are null,
     * they are considered equal.
     *
     * @param expected expected value
     * @param actual the value to check against expected
     */
    public static void assertEquals(Object expected, Object actual) {
        assertEquals(null, expected, actual);
    }

    /**
     * Asserts that two objects are not equals. If they are, an
     * {@link LazyAssertionError} is thrown with the given message. If
     * unexpected and actual are null,
     * they are considered equal.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param unexpected unexpected value to check
     * @param actual the value to check against unexpected
     */
    public static void assertNotEquals(String message, Object unexpected,
            Object actual) {
        if (equalsRegardingNull(unexpected, actual)) {
            failEquals(message, actual);
        }
    }

    /**
     * Asserts that two objects are not equals. If they are, an
     * {@link LazyAssertionError} without a message is thrown. If
     * unexpected and actual are null,
     * they are considered equal.
     *
     * @param unexpected unexpected value to check
     * @param actual the value to check against unexpected
     */
    public static void assertNotEquals(Object unexpected, Object actual) {
        assertNotEquals(null, unexpected, actual);
    }

    private static void failEquals(String message, Object actual) {
        String formatted = "Values should be different. ";
        if (message != null) {
            formatted = message + ". ";
        }

        formatted += "Actual: " + actual;
        fail(formatted);
    }

    /**
     * Asserts that two longs are not equals. If they are, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param unexpected unexpected value to check
     * @param actual the value to check against unexpected
     */
    public static void assertNotEquals(String message, long unexpected, long actual) {
        if (unexpected == actual) {
            failEquals(message, Long.valueOf(actual));
        }
    }

    /**
     * Asserts that two longs are not equals. If they are, an
     * {@link LazyAssertionError} without a message is thrown.
     *
     * @param unexpected unexpected value to check
     * @param actual the value to check against unexpected
     */
    public static void assertNotEquals(long unexpected, long actual) {
        assertNotEquals(null, unexpected, actual);
    }

    /**
     * Asserts that two doubles are not equal to within a positive delta.
     * If they are, an {@link LazyAssertionError} is thrown with the given
     * message. If the unexpected value is infinity then the delta value is
     * ignored. NaNs are considered equal:
     * assertNotEquals(Double.NaN, Double.NaN, *) fails
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param unexpected unexpected value
     * @param actual the value to check against unexpected
     * @param delta the maximum delta between unexpected and
     * actual for which both numbers are still
     * considered equal.
     */
    public static void assertNotEquals(String message, double unexpected,
            double actual, double delta) {
        if (!doubleIsDifferent(unexpected, actual, delta)) {
            failEquals(message, Double.valueOf(actual));
        }
    }

    /**
     * Asserts that two doubles are not equal to within a positive delta.
     * If they are, an {@link LazyAssertionError} is thrown. If the unexpected
     * value is infinity then the delta value is ignored.NaNs are considered
     * equal: assertNotEquals(Double.NaN, Double.NaN, *) fails
     *
     * @param unexpected unexpected value
     * @param actual the value to check against unexpected
     * @param delta the maximum delta between unexpected and
     * actual for which both numbers are still
     * considered equal.
     */
    public static void assertNotEquals(double unexpected, double actual, double delta) {
        assertNotEquals(null, unexpected, actual, delta);
    }

    /**
     * Asserts that two floats are not equal to within a positive delta.
     * If they are, an {@link LazyAssertionError} is thrown. If the unexpected
     * value is infinity then the delta value is ignored.NaNs are considered
     * equal: assertNotEquals(Float.NaN, Float.NaN, *) fails
     *
     * @param unexpected unexpected value
     * @param actual the value to check against unexpected
     * @param delta the maximum delta between unexpected and
     * actual for which both numbers are still
     * considered equal.
     */
    public static void assertNotEquals(float unexpected, float actual, float delta) {
        assertNotEquals(null, unexpected, actual, delta);
    }

    /**
     * Asserts that two object arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message. If
     * expecteds and actuals are null,
     * they are considered equal.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds Object array or array of arrays (multi-dimensional array) with
     * expected values.
     * @param actuals Object array or array of arrays (multi-dimensional array) with
     * actual values
     */
    public static void assertArrayEquals(String message, Object[] expecteds,
            Object[] actuals) throws ArrayComparisonFailure {
        internalArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two object arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown. If expected and
     * actual are null, they are considered
     * equal.
     *
     * @param expecteds Object array or array of arrays (multi-dimensional array) with
     * expected values
     * @param actuals Object array or array of arrays (multi-dimensional array) with
     * actual values
     */
    public static void assertArrayEquals(Object[] expecteds, Object[] actuals) {
        assertArrayEquals(null, expecteds, actuals);
    }

    /**
     * Asserts that two boolean arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message. If
     * expecteds and actuals are null,
     * they are considered equal.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds boolean array with expected values.
     * @param actuals boolean array with expected values.
     */
    public static void assertArrayEquals(String message, boolean[] expecteds,
            boolean[] actuals) throws ArrayComparisonFailure {
        internalArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two boolean arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown. If expected and
     * actual are null, they are considered
     * equal.
     *
     * @param expecteds boolean array with expected values.
     * @param actuals boolean array with expected values.
     */
    public static void assertArrayEquals(boolean[] expecteds, boolean[] actuals) {
        assertArrayEquals(null, expecteds, actuals);
    }

    /**
     * Asserts that two byte arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds byte array with expected values.
     * @param actuals byte array with actual values
     */
    public static void assertArrayEquals(String message, byte[] expecteds,
            byte[] actuals) throws ArrayComparisonFailure {
        internalArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two byte arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expecteds byte array with expected values.
     * @param actuals byte array with actual values
     */
    public static void assertArrayEquals(byte[] expecteds, byte[] actuals) {
        assertArrayEquals(null, expecteds, actuals);
    }

    /**
     * Asserts that two char arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds char array with expected values.
     * @param actuals char array with actual values
     */
    public static void assertArrayEquals(String message, char[] expecteds,
            char[] actuals) throws ArrayComparisonFailure {
        internalArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two char arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expecteds char array with expected values.
     * @param actuals char array with actual values
     */
    public static void assertArrayEquals(char[] expecteds, char[] actuals) {
        assertArrayEquals(null, expecteds, actuals);
    }

    /**
     * Asserts that two short arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds short array with expected values.
     * @param actuals short array with actual values
     */
    public static void assertArrayEquals(String message, short[] expecteds,
            short[] actuals) throws ArrayComparisonFailure {
        internalArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two short arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expecteds short array with expected values.
     * @param actuals short array with actual values
     */
    public static void assertArrayEquals(short[] expecteds, short[] actuals) {
        assertArrayEquals(null, expecteds, actuals);
    }

    /**
     * Asserts that two int arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds int array with expected values.
     * @param actuals int array with actual values
     */
    public static void assertArrayEquals(String message, int[] expecteds,
            int[] actuals) throws ArrayComparisonFailure {
        internalArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two int arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expecteds int array with expected values.
     * @param actuals int array with actual values
     */
    public static void assertArrayEquals(int[] expecteds, int[] actuals) {
        assertArrayEquals(null, expecteds, actuals);
    }

    /**
     * Asserts that two long arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds long array with expected values.
     * @param actuals long array with actual values
     */
    public static void assertArrayEquals(String message, long[] expecteds,
            long[] actuals) throws ArrayComparisonFailure {
        internalArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two long arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expecteds long array with expected values.
     * @param actuals long array with actual values
     */
    public static void assertArrayEquals(long[] expecteds, long[] actuals) {
        assertArrayEquals(null, expecteds, actuals);
    }

    /**
     * Asserts that two double arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds double array with expected values.
     * @param actuals double array with actual values
     * @param delta the maximum delta between expecteds[i] and
     * actuals[i] for which both numbers are still
     * considered equal.
     */
    public static void assertArrayEquals(String message, double[] expecteds,
            double[] actuals, double delta) throws ArrayComparisonFailure {
        try {
            new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals);
        } catch (AssertionError e) {
            throw new LazyAssertionError(message, e);
        }
    }

    /**
     * Asserts that two double arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expecteds double array with expected values.
     * @param actuals double array with actual values
     * @param delta the maximum delta between expecteds[i] and
     * actuals[i] for which both numbers are still
     * considered equal.
     */
    public static void assertArrayEquals(double[] expecteds, double[] actuals, double delta) {
        assertArrayEquals(null, expecteds, actuals, delta);
    }

    /**
     * Asserts that two float arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds float array with expected values.
     * @param actuals float array with actual values
     * @param delta the maximum delta between expecteds[i] and
     * actuals[i] for which both numbers are still
     * considered equal.
     */
    public static void assertArrayEquals(String message, float[] expecteds,
            float[] actuals, float delta) throws ArrayComparisonFailure {
        try {
            new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals);
        } catch (AssertionError e) {
            throw new LazyAssertionError(message, e);
        }
    }

    /**
     * Asserts that two float arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expecteds float array with expected values.
     * @param actuals float array with actual values
     * @param delta the maximum delta between expecteds[i] and
     * actuals[i] for which both numbers are still
     * considered equal.
     */
    public static void assertArrayEquals(float[] expecteds, float[] actuals, float delta) {
        assertArrayEquals(null, expecteds, actuals, delta);
    }

    /**
     * Asserts that two object arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message. If
     * expecteds and actuals are null,
     * they are considered equal.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds Object array or array of arrays (multi-dimensional array) with
     * expected values.
     * @param actuals Object array or array of arrays (multi-dimensional array) with
     * actual values
     */
    private static void internalArrayEquals(String message, Object expecteds,
            Object actuals) throws ArrayComparisonFailure {
        try {
            new ExactComparisonCriteria().arrayEquals(message, expecteds, actuals);
        } catch (AssertionError e) {
            throw new LazyAssertionError(message, e);
        }
    }

    /**
     * Asserts that two doubles are equal to within a positive delta.
     * If they are not, an {@link LazyAssertionError} is thrown with the given
     * message. If the expected value is infinity then the delta value is
     * ignored. NaNs are considered equal:
     * assertEquals(Double.NaN, Double.NaN, *) passes
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expected expected value
     * @param actual the value to check against expected
     * @param delta the maximum delta between expected and
     * actual for which both numbers are still
     * considered equal.
     */
    public static void assertEquals(String message, double expected,
            double actual, double delta) {
        if (doubleIsDifferent(expected, actual, delta)) {
            failNotEquals(message, Double.valueOf(expected), Double.valueOf(actual));
        }
    }

    /**
     * Asserts that two floats are equal to within a positive delta.
     * If they are not, an {@link LazyAssertionError} is thrown with the given
     * message. If the expected value is infinity then the delta value is
     * ignored. NaNs are considered equal:
     * assertEquals(Float.NaN, Float.NaN, *) passes
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expected expected value
     * @param actual the value to check against expected
     * @param delta the maximum delta between expected and
     * actual for which both numbers are still
     * considered equal.
     */
    public static void assertEquals(String message, float expected,
            float actual, float delta) {
        if (floatIsDifferent(expected, actual, delta)) {
            failNotEquals(message, Float.valueOf(expected), Float.valueOf(actual));
        }
    }

    /**
     * Asserts that two floats are not equal to within a positive delta.
     * If they are, an {@link LazyAssertionError} is thrown with the given
     * message. If the unexpected value is infinity then the delta value is
     * ignored. NaNs are considered equal:
     * assertNotEquals(Float.NaN, Float.NaN, *) fails
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param unexpected unexpected value
     * @param actual the value to check against unexpected
     * @param delta the maximum delta between unexpected and
     * actual for which both numbers are still
     * considered equal.
     */
    public static void assertNotEquals(String message, float unexpected,
            float actual, float delta) {
        if (!floatIsDifferent(unexpected, actual, delta)) {
            failEquals(message, Float.valueOf(actual));
        }
    }

    private static boolean doubleIsDifferent(double d1, double d2, double delta) {
        if (Double.compare(d1, d2) == 0) {
            return false;
        }
        if (Math.abs(d1 - d2) <= delta) {
            return false;
        }

        return true;
    }

    private static boolean floatIsDifferent(float f1, float f2, float delta) {
        if (Float.compare(f1, f2) == 0) {
            return false;
        }
        if (Math.abs(f1 - f2) <= delta) {
            return false;
        }

        return true;
    }

    /**
     * Asserts that two longs are equal. If they are not, an
     * {@link LazyAssertionError} is thrown.
     *
     * @param expected expected long value.
     * @param actual actual long value
     */
    public static void assertEquals(long expected, long actual) {
        assertEquals(null, expected, actual);
    }

    /**
     * Asserts that two longs are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expected long expected value.
     * @param actual long actual value
     */
    public static void assertEquals(String message, long expected, long actual) {
        if (expected != actual) {
            failNotEquals(message, Long.valueOf(expected), Long.valueOf(actual));
        }
    }

    /**
     * Asserts that two doubles are equal to within a positive delta.
     * If they are not, an {@link LazyAssertionError} is thrown. If the expected
     * value is infinity then the delta value is ignored.NaNs are considered
     * equal: assertEquals(Double.NaN, Double.NaN, *) passes
     *
     * @param expected expected value
     * @param actual the value to check against expected
     * @param delta the maximum delta between expected and
     * actual for which both numbers are still
     * considered equal.
     */
    public static void assertEquals(double expected, double actual, double delta) {
        assertEquals(null, expected, actual, delta);
    }

    /**
     * Asserts that two floats are equal to within a positive delta.
     * If they are not, an {@link LazyAssertionError} is thrown. If the expected
     * value is infinity then the delta value is ignored. NaNs are considered
     * equal: assertEquals(Float.NaN, Float.NaN, *) passes
     *
     * @param expected expected value
     * @param actual the value to check against expected
     * @param delta the maximum delta between expected and
     * actual for which both numbers are still
     * considered equal.
     */

    public static void assertEquals(float expected, float actual, float delta) {
        assertEquals(null, expected, actual, delta);
    }

    /**
     * Asserts that an object isn't null. If it is an {@link LazyAssertionError} is
     * thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param object Object to check or null
     */
    public static void assertNotNull(String message, Object object) {
        assertTrue(message, object != null);
    }

    /**
     * Asserts that an object isn't null. If it is an {@link LazyAssertionError} is
     * thrown.
     *
     * @param object Object to check or null
     */
    public static void assertNotNull(Object object) {
        assertNotNull(null, object);
    }

    /**
     * Asserts that an object is null. If it is not, an {@link LazyAssertionError}
     * is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param object Object to check or null
     */
    public static void assertNull(String message, Object object) {
        if (object == null) {
            return;
        }
        failNotNull(message, object);
    }

    /**
     * Asserts that an object is null. If it isn't an {@link LazyAssertionError} is
     * thrown.
     *
     * @param object Object to check or null
     */
    public static void assertNull(Object object) {
        assertNull(null, object);
    }

    private static void failNotNull(String message, Object actual) {
        String formatted = "";
        if (message != null) {
            formatted = message + " ";
        }
        fail(formatted + "expected null, but was:<" + actual + ">");
    }

    /**
     * Asserts that two objects refer to the same object. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expected the expected object
     * @param actual the object to compare to expected
     */
    public static void assertSame(String message, Object expected, Object actual) {
        if (expected == actual) {
            return;
        }
        failNotSame(message, expected, actual);
    }

    /**
     * Asserts that two objects refer to the same object. If they are not the
     * same, an {@link LazyAssertionError} without a message is thrown.
     *
     * @param expected the expected object
     * @param actual the object to compare to expected
     */
    public static void assertSame(Object expected, Object actual) {
        assertSame(null, expected, actual);
    }

    /**
     * Asserts that two objects do not refer to the same object. If they do
     * refer to the same object, an {@link LazyAssertionError} is thrown with the
     * given message.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param unexpected the object you don't expect
     * @param actual the object to compare to unexpected
     */
    public static void assertNotSame(String message, Object unexpected,
            Object actual) {
        if (unexpected == actual) {
            failSame(message);
        }
    }

    /**
     * Asserts that two objects do not refer to the same object. If they do
     * refer to the same object, an {@link LazyAssertionError} without a message is
     * thrown.
     *
     * @param unexpected the object you don't expect
     * @param actual the object to compare to unexpected
     */
    public static void assertNotSame(Object unexpected, Object actual) {
        assertNotSame(null, unexpected, actual);
    }

    private static void failSame(String message) {
        String formatted = "";
        if (message != null) {
            formatted = message + " ";
        }
        fail(formatted + "expected not same");
    }

    private static void failNotSame(String message, Object expected,
            Object actual) {
        String formatted = "";
        if (message != null) {
            formatted = message + " ";
        }
        fail(formatted + "expected same:<" + expected + "> was not:<" + actual
                + ">");
    }

    private static void failNotEquals(String message, Object expected,
            Object actual) {
        fail(format(message, expected, actual));
    }

    static String format(String message, Object expected, Object actual) {
        String formatted = "";
        if (message != null && !message.equals("")) {
            formatted = message + " ";
        }
        String expectedString = String.valueOf(expected);
        String actualString = String.valueOf(actual);
        if (expectedString.equals(actualString)) {
            return formatted + "expected: "
                    + formatClassAndValue(expected, expectedString)
                    + " but was: " + formatClassAndValue(actual, actualString);
        } else {
            return formatted + "expected:<" + expectedString + "> but was:<"
                    + actualString + ">";
        }
    }

    private static String formatClassAndValue(Object value, String valueString) {
        String className = "null";
        if (value != null) {
            className = value.getClass().getName();
        }
        return className + "<" + valueString + ">";
    }

    /**
     * Asserts that two object arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown with the given message. If
     * expecteds and actuals are null,
     * they are considered equal.
     *
     * @param message the identifying message for the {@link LazyAssertionError} (null
     * okay)
     * @param expecteds Object array or array of arrays (multi-dimensional array) with
     * expected values.
     * @param actuals Object array or array of arrays (multi-dimensional array) with
     * actual values
     * @deprecated use assertArrayEquals
     */
    @Deprecated
    public static void assertEquals(String message, Object[] expecteds,
            Object[] actuals) {
        assertArrayEquals(message, expecteds, actuals);
    }

    /**
     * Asserts that two object arrays are equal. If they are not, an
     * {@link LazyAssertionError} is thrown. If expected and
     * actual are null, they are considered
     * equal.
     *
     * @param expecteds Object array or array of arrays (multi-dimensional array) with
     * expected values
     * @param actuals Object array or array of arrays (multi-dimensional array) with
     * actual values
     * @deprecated use assertArrayEquals
     */
    @Deprecated
    public static void assertEquals(Object[] expecteds, Object[] actuals) {
        assertArrayEquals(expecteds, actuals);
    }

    /**
     * Asserts that actual satisfies the condition specified by
     * matcher. If not, an {@link LazyAssertionError} is thrown with
     * information about the matcher and failing value. Example:
     *
     * 
     *   assertThat(0, is(1)); // fails:
     *     // failure message:
     *     // expected: is <1>
     *     // got value: <0>
     *   assertThat(0, is(not(1))) // passes
     * 
* * org.hamcrest.Matcher does not currently document the meaning * of its type parameter T. This method assumes that a matcher * typed as Matcher<T> can be meaningfully applied only * to values that could be assigned to a variable of type T. * * @param the static type accepted by the matcher (this can flag obvious * compile-time problems such as {@code assertThat(1, is("a"))} * @param actual the computed value being compared * @param matcher an expression, built of {@link Matcher}s, specifying allowed * values * @see org.hamcrest.CoreMatchers * @see org.hamcrest.MatcherAssert */ public static void assertThat(T actual, Matcher matcher) { assertThat("", actual, matcher); } /** * Asserts that actual satisfies the condition specified by * matcher. If not, an {@link LazyAssertionError} is thrown with * the reason and information about the matcher and failing value. Example: * *
     *   assertThat("Help! Integers don't work", 0, is(1)); // fails:
     *     // failure message:
     *     // Help! Integers don't work
     *     // expected: is <1>
     *     // got value: <0>
     *   assertThat("Zero is one", 0, is(not(1))) // passes
     * 
* * org.hamcrest.Matcher does not currently document the meaning * of its type parameter T. This method assumes that a matcher * typed as Matcher<T> can be meaningfully applied only * to values that could be assigned to a variable of type T. * * @param reason additional information about the error * @param the static type accepted by the matcher (this can flag obvious * compile-time problems such as {@code assertThat(1, is("a"))} * @param actual the computed value being compared * @param matcher an expression, built of {@link Matcher}s, specifying allowed * values * @see org.hamcrest.CoreMatchers * @see org.hamcrest.MatcherAssert */ public static void assertThat(String reason, T actual, Matcher matcher) { MatcherLazyAssert.assertThat(reason, actual, matcher); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy