001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang.builder;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * Tests {@link org.apache.commons.lang.builder.HashCodeBuilder} and
026: * {@link org.apache.commons.lang.builder.EqualsBuilderTest} to insure that equal
027: * objects must have equal hash codes.
028: *
029: * @author Gary Gregory
030: * @version $Id: HashCodeBuilderAndEqualsBuilderTest.java 437554 2006-08-28 06:21:41Z bayard $
031: */
032: public class HashCodeBuilderAndEqualsBuilderTest extends TestCase {
033:
034: /**
035: * Constructor for HashCodeBuilderAndEqualsBuilderTest.
036: * @param name
037: */
038: public HashCodeBuilderAndEqualsBuilderTest(String name) {
039: super (name);
040: }
041:
042: public static void main(String[] args) {
043: TestRunner.run(suite());
044: }
045:
046: public static Test suite() {
047: TestSuite suite = new TestSuite(
048: HashCodeBuilderAndEqualsBuilderTest.class);
049: suite.setName("HashCodeBuilderAndEqualsBuilder Tests");
050: return suite;
051: }
052:
053: protected void setUp() throws Exception {
054: super .setUp();
055: }
056:
057: protected void tearDown() throws Exception {
058: super .tearDown();
059: }
060:
061: //-----------------------------------------------------------------------
062:
063: public void testInteger(boolean testTransients) {
064: Integer i1 = new Integer(12345);
065: Integer i2 = new Integer(12345);
066: assertEqualsAndHashCodeContract(i1, i2, testTransients);
067: }
068:
069: public void testInteger() {
070: testInteger(false);
071: }
072:
073: public void testIntegerWithTransients() {
074: testInteger(true);
075: }
076:
077: public void testFixture() {
078: testFixture(false);
079: }
080:
081: public void testFixtureWithTransients() {
082: testFixture(true);
083: }
084:
085: public void testFixture(boolean testTransients) {
086: assertEqualsAndHashCodeContract(new TestFixture(2, 'c', "Test",
087: (short) 2), new TestFixture(2, 'c', "Test", (short) 2),
088: testTransients);
089: assertEqualsAndHashCodeContract(new AllTransientFixture(2, 'c',
090: "Test", (short) 2), new AllTransientFixture(2, 'c',
091: "Test", (short) 2), testTransients);
092: assertEqualsAndHashCodeContract(new SubTestFixture(2, 'c',
093: "Test", (short) 2, "Same"), new SubTestFixture(2, 'c',
094: "Test", (short) 2, "Same"), testTransients);
095: assertEqualsAndHashCodeContract(new SubAllTransientFixture(2,
096: 'c', "Test", (short) 2, "Same"),
097: new SubAllTransientFixture(2, 'c', "Test", (short) 2,
098: "Same"), testTransients);
099: }
100:
101: /**
102: * Asserts that if <code>lhs</code> equals <code>rhs</code>
103: * then their hash codes MUST be identical.
104: *
105: * @param lhs The Left-Hand-Side of the equals test
106: * @param rhs The Right-Hand-Side of the equals test
107: * @param testTransients wether to test transient fields
108: */
109: public void assertEqualsAndHashCodeContract(Object lhs, Object rhs,
110: boolean testTransients) {
111: if (EqualsBuilder.reflectionEquals(lhs, rhs, testTransients)) {
112: // test a couple of times for consistency.
113: assertEquals(HashCodeBuilder.reflectionHashCode(lhs,
114: testTransients), HashCodeBuilder
115: .reflectionHashCode(rhs, testTransients));
116: assertEquals(HashCodeBuilder.reflectionHashCode(lhs,
117: testTransients), HashCodeBuilder
118: .reflectionHashCode(rhs, testTransients));
119: assertEquals(HashCodeBuilder.reflectionHashCode(lhs,
120: testTransients), HashCodeBuilder
121: .reflectionHashCode(rhs, testTransients));
122: }
123: }
124:
125: static class TestFixture {
126: int i;
127: char c;
128: String string;
129: short s;
130:
131: TestFixture(int i, char c, String string, short s) {
132: this .i = i;
133: this .c = c;
134: this .string = string;
135: this .s = s;
136: }
137: }
138:
139: static class SubTestFixture extends TestFixture {
140: transient String tString;
141:
142: SubTestFixture(int i, char c, String string, short s,
143: String tString) {
144: super (i, c, string, s);
145: this .tString = tString;
146: }
147: }
148:
149: static class AllTransientFixture {
150: transient int i;
151: transient char c;
152: transient String string;
153: transient short s;
154:
155: AllTransientFixture(int i, char c, String string, short s) {
156: this .i = i;
157: this .c = c;
158: this .string = string;
159: this .s = s;
160: }
161: }
162:
163: static class SubAllTransientFixture extends AllTransientFixture {
164: transient String tString;
165:
166: SubAllTransientFixture(int i, char c, String string, short s,
167: String tString) {
168: super(i, c, string, s);
169: this.tString = tString;
170: }
171: }
172:
173: }
|