1 16 package org.apache.commons.lang.builder; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 31 public class HashCodeBuilderAndEqualsBuilderTest extends TestCase { 32 33 37 public HashCodeBuilderAndEqualsBuilderTest(String name) { 38 super(name); 39 } 40 41 public static void main(String [] args) { 42 TestRunner.run(suite()); 43 } 44 45 public static Test suite() { 46 TestSuite suite = new TestSuite(HashCodeBuilderAndEqualsBuilderTest.class); 47 suite.setName("HashCodeBuilderAndEqualsBuilder Tests"); 48 return suite; 49 } 50 51 protected void setUp() throws Exception { 52 super.setUp(); 53 } 54 55 protected void tearDown() throws Exception { 56 super.tearDown(); 57 } 58 59 61 public void testInteger(boolean testTransients) { 62 Integer i1 = new Integer (12345); 63 Integer i2 = new Integer (12345); 64 assertEqualsAndHashCodeContract(i1, i2, testTransients); 65 } 66 67 public void testInteger() { 68 testInteger(false); 69 } 70 71 public void testIntegerWithTransients() { 72 testInteger(true); 73 } 74 75 public void testFixture() { 76 testFixture(false); 77 } 78 79 public void testFixtureWithTransients() { 80 testFixture(true); 81 } 82 83 public void testFixture(boolean testTransients) { 84 assertEqualsAndHashCodeContract(new TestFixture(2, 'c', "Test", (short) 2), new TestFixture(2, 'c', "Test", (short) 2), testTransients); 85 assertEqualsAndHashCodeContract( 86 new AllTransientFixture(2, 'c', "Test", (short) 2), 87 new AllTransientFixture(2, 'c', "Test", (short) 2), 88 testTransients); 89 assertEqualsAndHashCodeContract( 90 new SubTestFixture(2, 'c', "Test", (short) 2, "Same"), 91 new SubTestFixture(2, 'c', "Test", (short) 2, "Same"), 92 testTransients); 93 assertEqualsAndHashCodeContract( 94 new SubAllTransientFixture(2, 'c', "Test", (short) 2, "Same"), 95 new SubAllTransientFixture(2, 'c', "Test", (short) 2, "Same"), 96 testTransients); 97 } 98 99 107 public void assertEqualsAndHashCodeContract(Object lhs, Object rhs, boolean testTransients) { 108 if (EqualsBuilder.reflectionEquals(lhs, rhs, testTransients)) { 109 assertEquals(HashCodeBuilder.reflectionHashCode(lhs, testTransients), HashCodeBuilder.reflectionHashCode(rhs, testTransients)); 111 assertEquals(HashCodeBuilder.reflectionHashCode(lhs, testTransients), HashCodeBuilder.reflectionHashCode(rhs, testTransients)); 112 assertEquals(HashCodeBuilder.reflectionHashCode(lhs, testTransients), HashCodeBuilder.reflectionHashCode(rhs, testTransients)); 113 } 114 } 115 116 static class TestFixture { 117 int i; 118 char c; 119 String string; 120 short s; 121 122 TestFixture(int i, char c, String string, short s) { 123 this.i = i; 124 this.c = c; 125 this.string = string; 126 this.s = s; 127 } 128 } 129 130 static class SubTestFixture extends TestFixture { 131 transient String tString; 132 133 SubTestFixture(int i, char c, String string, short s, String tString) { 134 super(i, c, string, s); 135 this.tString = tString; 136 } 137 } 138 139 static class AllTransientFixture { 140 transient int i; 141 transient char c; 142 transient String string; 143 transient short s; 144 145 AllTransientFixture(int i, char c, String string, short s) { 146 this.i = i; 147 this.c = c; 148 this.string = string; 149 this.s = s; 150 } 151 } 152 153 static class SubAllTransientFixture extends AllTransientFixture { 154 transient String tString; 155 156 SubAllTransientFixture(int i, char c, String string, short s, String tString) { 157 super(i, c, string, s); 158 this.tString = tString; 159 } 160 } 161 162 163 } 164 | Popular Tags |