Java Assert Equal assertEquality(Object original, Object equal, Object... notEqual)

Here you can find the source of assertEquality(Object original, Object equal, Object... notEqual)

Description

assert Equality

License

Open Source License

Declaration

public static void assertEquality(Object original, Object equal, Object... notEqual) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static void assertEquality(Object original, Object equal, Object... notEqual) {
        if (!original.equals(equal)) {
            throw new AssertionError("Objects where not equal " + original + " != " + equal);
        }// w w w  . j  a  v  a 2s . c  o m
        if (original.hashCode() != equal.hashCode()) {
            throw new AssertionError("Equal objects did not have same hash :" + original + "(" + original.hashCode()
                    + ")" + " " + equal + "(" + equal.hashCode() + ")");
        }

        for (Object notEqualObject : notEqual) {
            checkNotEqual(original, notEqualObject);
            checkNotEqualHash(original, notEqualObject);
        }
    }

    private static void checkNotEqual(Object original, Object notEqualObject) {
        if (original.equals(notEqualObject)) {
            throw new AssertionError("Unequal objects where actually equal: " + original + " == " + notEqualObject);
        }
    }

    private static void checkNotEqualHash(Object original, Object notEqualObject) {
        if (original.hashCode() == notEqualObject.hashCode()) {
            throw new AssertionError("Unequal objects have same hash :" + original + "(" + original.hashCode() + ")"
                    + " " + notEqualObject + "(" + notEqualObject.hashCode() + ")");
        }
    }
}

Related

  1. assertArrayLengthEqual(Object[] array, String name, Object[] array1, String name1)
  2. assertCharacterArraysEqual(char[] first, char[] second)
  3. assertDatesEqual(Calendar first, Calendar second)
  4. assertEqual(final double d1, final double d2, final double precisionRange)
  5. assertEqual(Object obj1, Object obj2)
  6. assertEquals(double x, double y)
  7. assertEquals(double x, double y)
  8. assertEquals(final int expected, final int actual)
  9. assertEquals(final Object actual, final Object expected, final String name)