001    /*
002     * Created on Apr 12, 2009
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2009 the original author or authors.
015     */
016    package org.fest.test;
017    
018    import static org.junit.Assert.assertEquals;
019    import static org.junit.Assert.assertFalse;
020    
021    /**
022     * Understands assert methods for <code>{@link EqualsHashCodeContractTestCase}</code>.
023     *
024     * @author Alex Ruiz
025     */
026    public final class EqualsHashCodeContractAssert {
027    
028      /**
029       * Verifies that the "equals" implementation of the given object returns <code>false</code> when the object is
030       * compared to <code>null</code>.
031       * @param obj the object to verify.
032       * @throws AssertionError if the "equals" implementation of the given objects returns <code>true</code> when the
033       * object compared to <code>null</code>.
034       * @see EqualsHashCodeContractTestCase#should_not_be_equal_to_null()
035       */
036      public static void assertIsNotEqualToNull(Object obj) {
037        assertFalse(obj.equals(null));
038      }
039    
040      /**
041       * Verifies that the "equals" implementation of the given object is reflexive.
042       * @param obj the object to verify.
043       * @throws AssertionError if the "equals" implementation of the given object is reflexive.
044       * @see EqualsHashCodeContractTestCase#should_have_reflexive_equals()
045       */
046      public static void assertEqualsIsReflexive(Object obj) {
047        assertEquals(obj, obj);
048      }
049    
050      /**
051       * Verifies that the "equals" implementation of the given objects is symmetric.
052       * @param obj1 the object to verify.
053       * @param obj2 the object to compare to.
054       * @throws AssertionError if the "equals" implementation of the given object is not symmetric.
055       * @see EqualsHashCodeContractTestCase#should_have_symmetric_equals()
056       */
057      public static void assertEqualsIsSymmetric(Object obj1, Object obj2) {
058        assertEquals(obj1, obj2);
059        assertEquals(obj2, obj1);
060      }
061    
062      /**
063       * Verifies that the "equals" implementation of the given objects is transitive.
064       * @param obj1 the object to verify.
065       * @param obj2 an object to compare to.
066       * @param obj3 an object to compare to.
067       * @throws AssertionError if the "equals" implementation of the given objects is not transitive.
068       * @see EqualsHashCodeContractTestCase#should_have_transitive_equals()
069       */
070      public static void assertEqualsIsTransitive(Object obj1, Object obj2, Object obj3) {
071        assertEquals(obj1, obj2);
072        assertEquals(obj2, obj3);
073        assertEquals(obj1, obj3);
074      }
075    
076      /**
077       * Verifies that the "equals/hashCode" contract of the given objects is implemented correctly.
078       * @param obj1 the object to verify.
079       * @param obj2 the object to compare to.
080       * @throws AssertionError if the "equals/hashCode" contract of the given objects is not implemented correctly.
081       * @see EqualsHashCodeContractTestCase#should_maintain_equals_and_hashCode_contract()
082       */
083      public static void assertMaintainsEqualsAndHashCodeContract(Object obj1, Object obj2) {
084        assertEquals(obj1, obj2);
085        assertEquals(obj1.hashCode(), obj2.hashCode());
086      }
087    
088      private EqualsHashCodeContractAssert() {}
089    }