Advertisement
dig090

LocalEqualsHashCodeTest

Jan 3rd, 2012
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1. package com.j256.test;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4. import static org.junit.Assert.assertFalse;
  5. import static org.junit.Assert.assertNotNull;
  6. import static org.junit.Assert.assertNotSame;
  7.  
  8. import org.junit.Before;
  9. import org.junit.Test;
  10.  
  11. /**
  12.  * Basically a copy of the EqualsHashCodeTestCase but with Junit 1.5 annotations
  13.  * instead of extending TestCase.
  14.  *
  15.  * This should be used whenever you are overriding .equals and .hashcode.
  16.  *
  17.  * @author graywatson
  18.  */
  19. public abstract class LocalEqualsHashCodeTest<T> {
  20.  
  21.     private Object eq1;
  22.     private Object eq2;
  23.     private Object eq3;
  24.     private Object neq;
  25.     private static final int NUM_ITERATIONS = 100;
  26.  
  27.     /**
  28.      * Creates and returns an instance of the class under test.
  29.      *
  30.      * @return  a new instance of the class under test; each object returned
  31.      * from this method should compare equal to each other.
  32.      * @throws  Exception
  33.      */
  34.     protected abstract T createInstance() throws Exception;
  35.  
  36.     /**
  37.      * Creates and returns an instance of the class under test.
  38.      *
  39.      * @return a new instance of the class under test; each object returned
  40.      * from this method should compare equal to each other, but not to the
  41.      * objects returned from {@link #createInstance() createInstance}.
  42.      * @throws Exception
  43.      */
  44.     protected abstract T createNotEqualInstance() throws Exception;
  45.  
  46.     /**
  47.      * Sets up the test fixture.
  48.      *
  49.      * @throws Exception
  50.      */
  51.     @Before
  52.     public void setUp() throws Exception {
  53.  
  54.         eq1 = createInstance();
  55.         eq2 = createInstance();
  56.         eq3 = createInstance();
  57.         neq = createNotEqualInstance();
  58.  
  59.         // We want these assertions to yield errors, not failures.
  60.         try {
  61.             assertNotNull("createInstance() returned null", eq1);
  62.             assertNotNull("2nd createInstance() returned null", eq2);
  63.             assertNotNull("3rd createInstance() returned null", eq3);
  64.             assertNotNull("createNotEqualInstance() returned null", neq);
  65.  
  66.             assertNotSame(eq1, eq2);
  67.             assertNotSame(eq1, eq3);
  68.             assertNotSame(eq1, neq);
  69.             assertNotSame(eq2, eq3);
  70.             assertNotSame(eq2, neq);
  71.             assertNotSame(eq3, neq);
  72.  
  73.             assertEquals(
  74.                     "1st and 2nd equal instances of different classes",
  75.                     eq1.getClass(),
  76.                     eq2.getClass());
  77.             assertEquals(
  78.                     "1st and 3rd equal instances of different classes",
  79.                     eq1.getClass(),
  80.                     eq3.getClass());
  81.             assertEquals(
  82.                     "1st equal instance and not-equal instance of different classes",
  83.                     eq1.getClass(),
  84.                     neq.getClass());
  85.         } catch (AssertionError ex) {
  86.             throw new IllegalArgumentException(ex.getMessage());
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Tests whether <code>equals</code> holds up against a new
  92.      * <code>Object</code> (should always be <code>false</code>).
  93.      */
  94.     @Test
  95.     public final void testEqualsAgainstNewObject() {
  96.         Object o = new Object();
  97.  
  98.         assertFalse("new Object() vs. 1st", eq1.equals(o));
  99.         assertFalse("new Object() vs. 2nd", eq2.equals(o));
  100.         assertFalse("new Object() vs. 3rd", eq3.equals(o));
  101.         assertFalse("new Object() vs. not-equal", neq.equals(o));
  102.     }
  103.  
  104.     /**
  105.      * Tests whether <code>equals</code> holds up against <code>null</code>.
  106.      */
  107.     @Test
  108.     public final void testEqualsAgainstNull() {
  109.         assertFalse("null vs. 1st", eq1.equals(null));
  110.         assertFalse("null vs. 2nd", eq2.equals(null));
  111.         assertFalse("null vs. 3rd", eq3.equals(null));
  112.         assertFalse("null vs. not-equal", neq.equals(null));
  113.     }
  114.  
  115.     /**
  116.      * Tests whether <code>equals</code> holds up against objects that should
  117.      * not compare equal.
  118.      */
  119.     @Test
  120.     public final void testEqualsAgainstUnequalObjects() {
  121.         assertFalse("1st vs. not-equal", eq1.equals(neq));
  122.         assertFalse("2nd vs. not-equal", eq2.equals(neq));
  123.         assertFalse("3rd vs. not-equal", eq3.equals(neq));
  124.  
  125.         assertFalse("not-equal vs. 1st", neq.equals(eq1));
  126.         assertFalse("not-equal vs. 2nd", neq.equals(eq2));
  127.         assertFalse("not-equal vs. 3rd", neq.equals(eq3));
  128.     }
  129.  
  130.     /**
  131.      * Tests whether <code>equals</code> is <em>consistent</em>.
  132.      */
  133.     @Test
  134.     public final void testEqualsIsConsistentAcrossInvocations() {
  135.         for (int i = 0; i < NUM_ITERATIONS; ++i) {
  136.             testEqualsAgainstNewObject();
  137.             testEqualsAgainstNull();
  138.             testEqualsAgainstUnequalObjects();
  139.             testEqualsIsReflexive();
  140.             testEqualsIsSymmetricAndTransitive();
  141.         }
  142.     }
  143.  
  144.     /**
  145.      * Tests whether <code>equals</code> is <em>reflexive</em>.
  146.      */
  147.     @Test
  148.     public final void testEqualsIsReflexive() {
  149.         assertEquals("1st equal instance", eq1, eq1);
  150.         assertEquals("2nd equal instance", eq2, eq2);
  151.         assertEquals("3rd equal instance", eq3, eq3);
  152.         assertEquals("not-equal instance", neq, neq);
  153.     }
  154.  
  155.     /**
  156.      * Tests whether <code>equals</code> is <em>symmetric</em> and
  157.      * <em>transitive</em>.
  158.      */
  159.     @Test
  160.     public final void testEqualsIsSymmetricAndTransitive() {
  161.         assertEquals("1st vs. 2nd", eq1, eq2);
  162.         assertEquals("2nd vs. 1st", eq2, eq1);
  163.  
  164.         assertEquals("1st vs. 3rd", eq1, eq3);
  165.         assertEquals("3rd vs. 1st", eq3, eq1);
  166.  
  167.         assertEquals("2nd vs. 3rd", eq2, eq3);
  168.         assertEquals("3rd vs. 2nd", eq3, eq2);
  169.     }
  170.  
  171.     /**
  172.      * Tests the <code>hashCode</code> contract.
  173.      */
  174.     @Test
  175.     public final void testHashCodeContract() {
  176.         assertEquals("1st vs. 2nd", eq1.hashCode(), eq2.hashCode());
  177.         assertEquals("1st vs. 3rd", eq1.hashCode(), eq3.hashCode());
  178.         assertEquals("2nd vs. 3rd", eq2.hashCode(), eq3.hashCode());
  179.     }
  180.  
  181.     /**
  182.      * Tests the consistency of <code>hashCode</code>.
  183.      */
  184.     @Test
  185.     public final void testHashCodeIsConsistentAcrossInvocations() {
  186.         int eq1Hash = eq1.hashCode();
  187.         int eq2Hash = eq2.hashCode();
  188.         int eq3Hash = eq3.hashCode();
  189.         int neqHash = neq.hashCode();
  190.  
  191.         for (int i = 0; i < NUM_ITERATIONS; ++i) {
  192.             assertEquals("1st equal instance", eq1Hash, eq1.hashCode());
  193.             assertEquals("2nd equal instance", eq2Hash, eq2.hashCode());
  194.             assertEquals("3rd equal instance", eq3Hash, eq3.hashCode());
  195.             assertEquals("not-equal instance", neqHash, neq.hashCode());
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement