Port to JUnit 4.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1185705 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2011-10-18 14:59:56 +00:00
parent 1b2fa6a727
commit 9506bec96f
1 changed files with 48 additions and 6 deletions

View File

@ -17,14 +17,18 @@
package org.apache.commons.lang3.builder;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.junit.Test;
/**
* Unit tests {@link org.apache.commons.lang3.builder.HashCodeBuilder}.
*
* @version $Id$
*/
public class HashCodeBuilderTest extends TestCase {
public class HashCodeBuilderTest {
/**
* A reflection test fixture.
@ -50,12 +54,9 @@ public int hashCode() {
}
}
public HashCodeBuilderTest(String name) {
super(name);
}
// -----------------------------------------------------------------------
@Test
public void testConstructorEx1() {
try {
new HashCodeBuilder(0, 0);
@ -66,6 +67,7 @@ public void testConstructorEx1() {
fail();
}
@Test
public void testConstructorEx2() {
try {
new HashCodeBuilder(2, 2);
@ -133,11 +135,13 @@ public boolean equals(Object o) {
}
}
@Test
public void testReflectionHashCode() {
assertEquals(17 * 37, HashCodeBuilder.reflectionHashCode(new TestObject(0)));
assertEquals(17 * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestObject(123456)));
}
@Test
public void testReflectionHierarchyHashCode() {
assertEquals(17 * 37 * 37, HashCodeBuilder.reflectionHashCode(new TestSubObject(0, 0, 0)));
assertEquals(17 * 37 * 37 * 37, HashCodeBuilder.reflectionHashCode(new TestSubObject(0, 0, 0), true));
@ -147,6 +151,7 @@ public void testReflectionHierarchyHashCode() {
123456, 7890, 0), true));
}
@Test
public void testReflectionHierarchyHashCodeEx1() {
try {
HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(0, 0, 0), true);
@ -156,6 +161,7 @@ public void testReflectionHierarchyHashCodeEx1() {
fail();
}
@Test
public void testReflectionHierarchyHashCodeEx2() {
try {
HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(0, 0, 0), true);
@ -165,6 +171,7 @@ public void testReflectionHierarchyHashCodeEx2() {
fail();
}
@Test
public void testReflectionHashCodeEx1() {
try {
HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0), true);
@ -174,6 +181,7 @@ public void testReflectionHashCodeEx1() {
fail();
}
@Test
public void testReflectionHashCodeEx2() {
try {
HashCodeBuilder.reflectionHashCode(2, 2, new TestObject(0), true);
@ -183,6 +191,7 @@ public void testReflectionHashCodeEx2() {
fail();
}
@Test
public void testReflectionHashCodeEx3() {
try {
HashCodeBuilder.reflectionHashCode(13, 19, null, true);
@ -192,12 +201,14 @@ public void testReflectionHashCodeEx3() {
fail();
}
@Test
public void testSuper() {
Object obj = new Object();
assertEquals(17 * 37 + (19 * 41 + obj.hashCode()), new HashCodeBuilder(17, 37).appendSuper(
new HashCodeBuilder(19, 41).append(obj).toHashCode()).toHashCode());
}
@Test
public void testObject() {
Object obj = null;
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
@ -205,6 +216,7 @@ public void testObject() {
assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testObjectBuild() {
Object obj = null;
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).build().intValue());
@ -212,6 +224,7 @@ public void testObjectBuild() {
assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17, 37).append(obj).build().intValue());
}
@Test
@SuppressWarnings("cast") // cast is not really needed, keep for consistency
public void testLong() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long) 0L).toHashCode());
@ -219,27 +232,32 @@ public void testLong() {
(long) 123456789L).toHashCode());
}
@Test
@SuppressWarnings("cast") // cast is not really needed, keep for consistency
public void testInt() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int) 0).toHashCode());
assertEquals(17 * 37 + 123456, new HashCodeBuilder(17, 37).append((int) 123456).toHashCode());
}
@Test
public void testShort() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short) 0).toHashCode());
assertEquals(17 * 37 + 12345, new HashCodeBuilder(17, 37).append((short) 12345).toHashCode());
}
@Test
public void testChar() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((char) 0).toHashCode());
assertEquals(17 * 37 + 1234, new HashCodeBuilder(17, 37).append((char) 1234).toHashCode());
}
@Test
public void testByte() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte) 0).toHashCode());
assertEquals(17 * 37 + 123, new HashCodeBuilder(17, 37).append((byte) 123).toHashCode());
}
@Test
@SuppressWarnings("cast") // cast is not really needed, keep for consistency
public void testDouble() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((double) 0d).toHashCode());
@ -248,6 +266,7 @@ public void testDouble() {
assertEquals(17 * 37 + (int) (l ^ (l >> 32)), new HashCodeBuilder(17, 37).append(d).toHashCode());
}
@Test
@SuppressWarnings("cast") // cast is not really needed, keep for consistency
public void testFloat() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float) 0f).toHashCode());
@ -256,11 +275,13 @@ public void testFloat() {
assertEquals(17 * 37 + i, new HashCodeBuilder(17, 37).append(f).toHashCode());
}
@Test
public void testBoolean() {
assertEquals(17 * 37 + 0, new HashCodeBuilder(17, 37).append(true).toHashCode());
assertEquals(17 * 37 + 1, new HashCodeBuilder(17, 37).append(false).toHashCode());
}
@Test
public void testObjectArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((Object[]) null).toHashCode());
Object[] obj = new Object[2];
@ -272,6 +293,7 @@ public void testObjectArray() {
.toHashCode());
}
@Test
public void testObjectArrayAsObject() {
Object[] obj = new Object[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -282,6 +304,7 @@ public void testObjectArrayAsObject() {
(Object) obj).toHashCode());
}
@Test
public void testLongArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long[]) null).toHashCode());
long[] obj = new long[2];
@ -294,6 +317,7 @@ public void testLongArray() {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testLongArrayAsObject() {
long[] obj = new long[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -305,6 +329,7 @@ public void testLongArrayAsObject() {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testIntArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int[]) null).toHashCode());
int[] obj = new int[2];
@ -315,6 +340,7 @@ public void testIntArray() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testIntArrayAsObject() {
int[] obj = new int[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -324,6 +350,7 @@ public void testIntArrayAsObject() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testShortArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short[]) null).toHashCode());
short[] obj = new short[2];
@ -334,6 +361,7 @@ public void testShortArray() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testShortArrayAsObject() {
short[] obj = new short[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -343,6 +371,7 @@ public void testShortArrayAsObject() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testCharArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((char[]) null).toHashCode());
char[] obj = new char[2];
@ -353,6 +382,7 @@ public void testCharArray() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testCharArrayAsObject() {
char[] obj = new char[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -362,6 +392,7 @@ public void testCharArrayAsObject() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testByteArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte[]) null).toHashCode());
byte[] obj = new byte[2];
@ -372,6 +403,7 @@ public void testByteArray() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testByteArrayAsObject() {
byte[] obj = new byte[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -381,6 +413,7 @@ public void testByteArrayAsObject() {
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testDoubleArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((double[]) null).toHashCode());
double[] obj = new double[2];
@ -395,6 +428,7 @@ public void testDoubleArray() {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testDoubleArrayAsObject() {
double[] obj = new double[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -408,6 +442,7 @@ public void testDoubleArrayAsObject() {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testFloatArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float[]) null).toHashCode());
float[] obj = new float[2];
@ -420,6 +455,7 @@ public void testFloatArray() {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testFloatArrayAsObject() {
float[] obj = new float[2];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -431,6 +467,7 @@ public void testFloatArrayAsObject() {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testBooleanArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((boolean[]) null).toHashCode());
boolean[] obj = new boolean[2];
@ -441,6 +478,7 @@ public void testBooleanArray() {
assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testBooleanArrayAsObject() {
boolean[] obj = new boolean[2];
assertEquals((17 * 37 + 1) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
@ -450,6 +488,7 @@ public void testBooleanArrayAsObject() {
assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
public void testBooleanMultiArray() {
boolean[][] obj = new boolean[2][];
assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
@ -465,6 +504,7 @@ public void testBooleanMultiArray() {
assertEquals((((17 * 37 + 0) * 37 + 1) * 37 + 1), new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
public void testReflectionHashCodeExcludeFields() throws Exception {
TestObjectWithMultipleFields x = new TestObjectWithMultipleFields(1, 2, 3);
@ -503,6 +543,7 @@ public TestObjectWithMultipleFields(int one, int two, int three) {
/**
* Test Objects pointing to each other.
*/
@Test
public void testReflectionObjectCycle() {
ReflectionTestCycleA a = new ReflectionTestCycleA();
ReflectionTestCycleB b = new ReflectionTestCycleB();
@ -535,6 +576,7 @@ public void testReflectionObjectCycle() {
/**
* Ensures LANG-520 remains true
*/
@Test
public void testToHashCodeEqualsHashCode() {
HashCodeBuilder hcb = new HashCodeBuilder(17, 37).append(new Object()).append('a');
assertEquals("hashCode() is no longer returning the same value as toHashCode() - see LANG-520",