KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > builder > HashCodeBuilderAndEqualsBuilderTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
24  * Tests {@link org.apache.commons.lang.builder.HashCodeBuilder} and
25  * {@link org.apache.commons.lang.builder.EqualsBuilderTest} to insure that equal
26  * objects must have equal hash codes.
27  *
28  * @author Gary Gregory
29  * @version $Id: HashCodeBuilderAndEqualsBuilderTest.java 161244 2005-04-14 06:16:36Z ggregory $
30  */

31 public class HashCodeBuilderAndEqualsBuilderTest extends TestCase {
32
33     /**
34      * Constructor for HashCodeBuilderAndEqualsBuilderTest.
35      * @param name
36      */

37     public HashCodeBuilderAndEqualsBuilderTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public static void main(String JavaDoc[] 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 JavaDoc {
52         super.setUp();
53     }
54
55     protected void tearDown() throws Exception JavaDoc {
56         super.tearDown();
57     }
58
59     //-----------------------------------------------------------------------
60

61     public void testInteger(boolean testTransients) {
62         Integer JavaDoc i1 = new Integer JavaDoc(12345);
63         Integer JavaDoc i2 = new Integer JavaDoc(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     /**
100      * Asserts that if <code>lhs</code> equals <code>rhs</code>
101      * then their hash codes MUST be identical.
102      *
103      * @param lhs The Left-Hand-Side of the equals test
104      * @param rhs The Right-Hand-Side of the equals test
105      * @param testTransients wether to test transient fields
106      */

107     public void assertEqualsAndHashCodeContract(Object JavaDoc lhs, Object JavaDoc rhs, boolean testTransients) {
108         if (EqualsBuilder.reflectionEquals(lhs, rhs, testTransients)) {
109             // test a couple of times for consistency.
110
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 JavaDoc string;
120         short s;
121
122         TestFixture(int i, char c, String JavaDoc 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 JavaDoc tString;
132
133         SubTestFixture(int i, char c, String JavaDoc string, short s, String JavaDoc 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 JavaDoc string;
143         transient short s;
144
145         AllTransientFixture(int i, char c, String JavaDoc 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 JavaDoc tString;
155
156         SubAllTransientFixture(int i, char c, String JavaDoc string, short s, String JavaDoc tString) {
157             super(i, c, string, s);
158             this.tString = tString;
159         }
160     }
161
162
163 }
164
Popular Tags