You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/08/26 19:09:06 UTC
[commons-lang] 06/10: Sort members
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit ce40e5bdf29138c17b934b84206b31ece5ebd3e3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 26 14:48:19 2022 -0400
Sort members
---
.../commons/lang3/builder/HashCodeBuilderTest.java | 572 ++++++++++-----------
1 file changed, 286 insertions(+), 286 deletions(-)
diff --git a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java
index d2ae2f207..8741f2104 100644
--- a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java
+++ b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java
@@ -53,26 +53,6 @@ public class HashCodeBuilderTest extends AbstractLangTest {
}
}
- @Test
- public void testConstructorExZero() {
- assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(0, 0));
- }
-
- @Test
- public void testConstructorExEvenFirst() {
- assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(2, 3));
- }
-
- @Test
- public void testConstructorExEvenSecond() {
- assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(3, 2));
- }
-
- @Test
- public void testConstructorExEvenNegative() {
- assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(-2, -2));
- }
-
static class TestObject {
private int a;
@@ -92,6 +72,10 @@ public class HashCodeBuilderTest extends AbstractLangTest {
return a == rhs.a;
}
+ public int getA() {
+ return a;
+ }
+
@Override
public int hashCode() {
return a;
@@ -100,10 +84,62 @@ public class HashCodeBuilderTest extends AbstractLangTest {
public void setA(final int a) {
this.a = a;
}
+ }
+
+ static class TestObjectHashCodeExclude {
+ @HashCodeExclude
+ private final int a;
+ private final int b;
+
+ TestObjectHashCodeExclude(final int a, final int b) {
+ this.a = a;
+ this.b = b;
+ }
public int getA() {
return a;
}
+
+ public int getB() {
+ return b;
+ }
+ }
+
+ static class TestObjectHashCodeExclude2 {
+ @HashCodeExclude
+ private final int a;
+ @HashCodeExclude
+ private final int b;
+
+ TestObjectHashCodeExclude2(final int a, final int b) {
+ this.a = a;
+ this.b = b;
+ }
+
+ public int getA() {
+ return a;
+ }
+
+ public int getB() {
+ return b;
+ }
+ }
+
+ static class TestObjectWithMultipleFields {
+ @SuppressWarnings("unused")
+ private int one = 0;
+
+ @SuppressWarnings("unused")
+ private int two = 0;
+
+ @SuppressWarnings("unused")
+ private int three = 0;
+
+ TestObjectWithMultipleFields(final int one, final int two, final int three) {
+ this.one = one;
+ this.two = two;
+ this.three = three;
+ }
}
static class TestSubObject extends TestObject {
@@ -141,117 +177,6 @@ public class HashCodeBuilderTest extends AbstractLangTest {
}
- @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));
- assertEquals((17 * 37 + 7890) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(123456, 7890,
- 0)));
- assertEquals(((17 * 37 + 7890) * 37 + 0) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(
- 123456, 7890, 0), true));
- }
-
- @Test
- public void testReflectionHierarchyHashCodeEx1() {
- assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(0, 0, 0), true));
- }
-
- @Test
- public void testReflectionHierarchyHashCodeEx2() {
- assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(0, 0, 0), true));
- }
-
- @Test
- public void testReflectionHashCodeEx1() {
- assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0), true));
- }
-
- @Test
- public void testReflectionHashCodeEx2() {
- assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(2, 2, new TestObject(0), true));
- }
-
- @Test
- public void testReflectionHashCodeEx3() {
- assertThrows(NullPointerException.class, () -> HashCodeBuilder.reflectionHashCode(13, 19, null, true));
- }
-
- @Test
- public void testSuper() {
- final 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());
- obj = new Object();
- 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());
- obj = new Object();
- assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17, 37).append(obj).build().intValue());
- }
-
- @Test
- public void testLong() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0L).toHashCode());
- assertEquals(17 * 37 + (int) (123456789L ^ 123456789L >> 32), new HashCodeBuilder(17, 37).append(
- 123456789L).toHashCode());
- }
-
- @Test
- public void testInt() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0).toHashCode());
- assertEquals(17 * 37 + 123456, new HashCodeBuilder(17, 37).append(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
- public void testDouble() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0d).toHashCode());
- final double d = 1234567.89;
- final long l = Double.doubleToLongBits(d);
- assertEquals(17 * 37 + (int) (l ^ l >> 32), new HashCodeBuilder(17, 37).append(d).toHashCode());
- }
-
- @Test
- public void testFloat() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0f).toHashCode());
- final float f = 1234.89f;
- final int i = Float.floatToIntBits(f);
- 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());
@@ -259,93 +184,73 @@ public class HashCodeBuilderTest extends AbstractLangTest {
}
@Test
- public void testObjectArray() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((Object[]) null).toHashCode());
- final Object[] obj = new Object[2];
- assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = new Object();
- assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[1] = new Object();
- assertEquals((17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append(obj)
- .toHashCode());
+ public void testBooleanArray() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((boolean[]) null).toHashCode());
+ final boolean[] obj = new boolean[2];
+ assertEquals((17 * 37 + 1) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0] = true;
+ assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[1] = false;
+ assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
- public void testObjectArrayAsObject() {
- final Object[] obj = new Object[2];
- assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[0] = new Object();
- assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[1] = new Object();
- assertEquals((17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append(
- (Object) obj).toHashCode());
+ public void testBooleanArrayAsObject() {
+ final boolean[] obj = new boolean[2];
+ assertEquals((17 * 37 + 1) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[0] = true;
+ assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[1] = false;
+ assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
@Test
- public void testLongArray() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long[]) null).toHashCode());
- final long[] obj = new long[2];
+ public void testBooleanMultiArray() {
+ final boolean[][] obj = new boolean[2][];
assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = 5L;
- final int h1 = (int) (5L ^ 5L >> 32);
- assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[1] = 6L;
- final int h2 = (int) (6L ^ 6L >> 32);
- assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0] = new boolean[0];
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0] = new boolean[1];
+ assertEquals((17 * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0] = new boolean[2];
+ assertEquals(((17 * 37 + 1) * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0][0] = true;
+ assertEquals(((17 * 37 + 0) * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[1] = new boolean[1];
+ assertEquals(((17 * 37 + 0) * 37 + 1) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
- public void testLongArrayAsObject() {
- final long[] obj = new long[2];
- assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[0] = 5L;
- final int h1 = (int) (5L ^ 5L >> 32);
- assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[1] = 6L;
- final int h2 = (int) (6L ^ 6L >> 32);
- assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ 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
- public void testIntArray() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int[]) null).toHashCode());
- final int[] obj = new int[2];
+ public void testByteArray() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte[]) null).toHashCode());
+ final byte[] obj = new byte[2];
assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = 5;
+ obj[0] = (byte) 5;
assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[1] = 6;
+ obj[1] = (byte) 6;
assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
- public void testIntArrayAsObject() {
- final int[] obj = new int[2];
+ public void testByteArrayAsObject() {
+ final byte[] obj = new byte[2];
assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[0] = 5;
+ obj[0] = (byte) 5;
assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[1] = 6;
+ obj[1] = (byte) 6;
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());
- final short[] obj = new short[2];
- assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = (short) 5;
- assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[1] = (short) 6;
- assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- }
-
- @Test
- public void testShortArrayAsObject() {
- final short[] obj = new short[2];
- assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[0] = (short) 5;
- assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[1] = (short) 6;
- assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ 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
@@ -370,24 +275,31 @@ public class HashCodeBuilderTest extends AbstractLangTest {
}
@Test
- public void testByteArray() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte[]) null).toHashCode());
- final byte[] obj = new byte[2];
- assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = (byte) 5;
- assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[1] = (byte) 6;
- assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ public void testConstructorExEvenFirst() {
+ assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(2, 3));
+ }
+
+ @Test
+ public void testConstructorExEvenNegative() {
+ assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(-2, -2));
+ }
+
+ @Test
+ public void testConstructorExEvenSecond() {
+ assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(3, 2));
+ }
+
+ @Test
+ public void testConstructorExZero() {
+ assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(0, 0));
}
@Test
- public void testByteArrayAsObject() {
- final byte[] obj = new byte[2];
- assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[0] = (byte) 5;
- assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[1] = (byte) 6;
- assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ public void testDouble() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0d).toHashCode());
+ final double d = 1234567.89;
+ final long l = Double.doubleToLongBits(d);
+ assertEquals(17 * 37 + (int) (l ^ l >> 32), new HashCodeBuilder(17, 37).append(d).toHashCode());
}
@Test
@@ -419,6 +331,14 @@ public class HashCodeBuilderTest extends AbstractLangTest {
assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
}
+ @Test
+ public void testFloat() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0f).toHashCode());
+ final float f = 1234.89f;
+ final int i = Float.floatToIntBits(f);
+ assertEquals(17 * 37 + i, new HashCodeBuilder(17, 37).append(f).toHashCode());
+ }
+
@Test
public void testFloatArray() {
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float[]) null).toHashCode());
@@ -445,40 +365,122 @@ public class HashCodeBuilderTest extends AbstractLangTest {
}
@Test
- public void testBooleanArray() {
- assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((boolean[]) null).toHashCode());
- final boolean[] obj = new boolean[2];
- assertEquals((17 * 37 + 1) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = true;
- assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[1] = false;
- assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ public void testInt() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0).toHashCode());
+ assertEquals(17 * 37 + 123456, new HashCodeBuilder(17, 37).append(123456).toHashCode());
}
@Test
- public void testBooleanArrayAsObject() {
- final boolean[] obj = new boolean[2];
- assertEquals((17 * 37 + 1) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[0] = true;
- assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
- obj[1] = false;
- assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ public void testIntArray() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int[]) null).toHashCode());
+ final int[] obj = new int[2];
+ assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0] = 5;
+ assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[1] = 6;
+ assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
}
@Test
- public void testBooleanMultiArray() {
- final boolean[][] obj = new boolean[2][];
+ public void testIntArrayAsObject() {
+ final int[] obj = new int[2];
+ assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[0] = 5;
+ assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[1] = 6;
+ assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ }
+
+ @Test
+ public void testLong() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(0L).toHashCode());
+ assertEquals(17 * 37 + (int) (123456789L ^ 123456789L >> 32), new HashCodeBuilder(17, 37).append(
+ 123456789L).toHashCode());
+ }
+
+ @Test
+ public void testLongArray() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long[]) null).toHashCode());
+ final long[] obj = new long[2];
assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = new boolean[0];
+ obj[0] = 5L;
+ final int h1 = (int) (5L ^ 5L >> 32);
+ assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[1] = 6L;
+ final int h2 = (int) (6L ^ 6L >> 32);
+ assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ }
+
+ @Test
+ public void testLongArrayAsObject() {
+ final long[] obj = new long[2];
+ assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[0] = 5L;
+ final int h1 = (int) (5L ^ 5L >> 32);
+ assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[1] = 6L;
+ final int h2 = (int) (6L ^ 6L >> 32);
+ assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ }
+
+ @Test
+ public void testObject() {
+ Object obj = null;
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = new boolean[1];
- assertEquals((17 * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0] = new boolean[2];
- assertEquals(((17 * 37 + 1) * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[0][0] = true;
- assertEquals(((17 * 37 + 0) * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
- obj[1] = new boolean[1];
- assertEquals(((17 * 37 + 0) * 37 + 1) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj = new Object();
+ assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ }
+
+ @Test
+ public void testObjectArray() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((Object[]) null).toHashCode());
+ final Object[] obj = new Object[2];
+ assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0] = new Object();
+ assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[1] = new Object();
+ assertEquals((17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append(obj)
+ .toHashCode());
+ }
+
+ @Test
+ public void testObjectArrayAsObject() {
+ final Object[] obj = new Object[2];
+ assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[0] = new Object();
+ assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[1] = new Object();
+ assertEquals((17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append(
+ (Object) obj).toHashCode());
+ }
+
+ @Test
+ public void testObjectBuild() {
+ Object obj = null;
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).build().intValue());
+ obj = new Object();
+ assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17, 37).append(obj).build().intValue());
+ }
+
+ @Test
+ public void testReflectionHashCode() {
+ assertEquals(17 * 37, HashCodeBuilder.reflectionHashCode(new TestObject(0)));
+ assertEquals(17 * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestObject(123456)));
+ }
+
+ @Test
+ public void testReflectionHashCodeEx1() {
+ assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0), true));
+ }
+
+ @Test
+ public void testReflectionHashCodeEx2() {
+ assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(2, 2, new TestObject(0), true));
+ }
+
+ @Test
+ public void testReflectionHashCodeEx3() {
+ assertThrows(NullPointerException.class, () -> HashCodeBuilder.reflectionHashCode(13, 19, null, true));
}
@Test
@@ -500,21 +502,24 @@ public class HashCodeBuilderTest extends AbstractLangTest {
assertEquals(17, HashCodeBuilder.reflectionHashCode(x, "one", "two", "three", "xxx"));
}
- static class TestObjectWithMultipleFields {
- @SuppressWarnings("unused")
- private int one = 0;
-
- @SuppressWarnings("unused")
- private int two = 0;
+ @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));
+ assertEquals((17 * 37 + 7890) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(123456, 7890,
+ 0)));
+ assertEquals(((17 * 37 + 7890) * 37 + 0) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(
+ 123456, 7890, 0), true));
+ }
- @SuppressWarnings("unused")
- private int three = 0;
+ @Test
+ public void testReflectionHierarchyHashCodeEx1() {
+ assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(0, 0, 0), true));
+ }
- TestObjectWithMultipleFields(final int one, final int two, final int three) {
- this.one = one;
- this.two = two;
- this.three = three;
- }
+ @Test
+ public void testReflectionHierarchyHashCodeEx2() {
+ assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(0, 0, 0), true));
}
/**
@@ -550,6 +555,40 @@ public class HashCodeBuilderTest extends AbstractLangTest {
assertNull(HashCodeBuilder.getRegistry());
}
+ @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 testShortArray() {
+ assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short[]) null).toHashCode());
+ final short[] obj = new short[2];
+ assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[0] = (short) 5;
+ assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ obj[1] = (short) 6;
+ assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
+ }
+
+ @Test
+ public void testShortArrayAsObject() {
+ final short[] obj = new short[2];
+ assertEquals(17 * 37 * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[0] = (short) 5;
+ assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ obj[1] = (short) 6;
+ assertEquals((17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
+ }
+
+ @Test
+ public void testSuper() {
+ final Object obj = new Object();
+ assertEquals(17 * 37 + 19 * 41 + obj.hashCode(), new HashCodeBuilder(17, 37).appendSuper(
+ new HashCodeBuilder(19, 41).append(obj).toHashCode()).toHashCode());
+ }
+
/**
* Ensures LANG-520 remains true
*/
@@ -560,45 +599,6 @@ public class HashCodeBuilderTest extends AbstractLangTest {
"hashCode() is no longer returning the same value as toHashCode() - see LANG-520");
}
- static class TestObjectHashCodeExclude {
- @HashCodeExclude
- private final int a;
- private final int b;
-
- TestObjectHashCodeExclude(final int a, final int b) {
- this.a = a;
- this.b = b;
- }
-
- public int getA() {
- return a;
- }
-
- public int getB() {
- return b;
- }
- }
-
- static class TestObjectHashCodeExclude2 {
- @HashCodeExclude
- private final int a;
- @HashCodeExclude
- private final int b;
-
- TestObjectHashCodeExclude2(final int a, final int b) {
- this.a = a;
- this.b = b;
- }
-
- public int getA() {
- return a;
- }
-
- public int getB() {
- return b;
- }
- }
-
@Test
public void testToHashCodeExclude() {
final TestObjectHashCodeExclude one = new TestObjectHashCodeExclude(1, 2);