ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amashen...@apache.org
Subject [ignite] 02/02: WIP. Add serialization tests for complex objects.
Date Fri, 13 Nov 2020 15:25:59 GMT
This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch ignite-13618
in repository https://gitbox.apache.org/repos/asf/ignite.git

commit ca3333ed7d98dff514f400f8c4d257fa12fa9c16
Author: Andrew Mashenkov <andrey.mashenkov@gmail.com>
AuthorDate: Fri Nov 13 18:25:19 2020 +0300

    WIP. Add serialization tests for complex objects.
---
 .../internal/schema/marshaller/JavaSerializer.java |   6 +-
 .../internal/schema/marshaller/Marshaller.java     |   3 +-
 .../internal/schema/marshaller/ObjectFactory.java  |   2 +
 .../ignite/internal/schema/SchemaTestSuite.java    |   2 +
 .../{SchemaTestUtils.java => TestUtils.java}       |   2 +-
 .../apache/ignite/internal/schema/TupleTest.java   |   2 +-
 .../schema/marshaller/FieldAccessorTest.java       | 103 ++++--
 .../schema/marshaller/JavaSerializerTest.java      | 353 ++++++++++++++++++++-
 8 files changed, 417 insertions(+), 56 deletions(-)

diff --git a/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/JavaSerializer.java
b/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/JavaSerializer.java
index c9ce71e..1687ccc 100644
--- a/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/JavaSerializer.java
+++ b/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/JavaSerializer.java
@@ -17,7 +17,6 @@
 
 package org.apache.ignite.internal.schema.marshaller;
 
-import java.lang.reflect.Field;
 import java.util.BitSet;
 import java.util.UUID;
 import org.apache.ignite.internal.schema.ByteBufferTuple;
@@ -27,7 +26,6 @@ import org.apache.ignite.internal.schema.NativeType;
 import org.apache.ignite.internal.schema.SchemaDescriptor;
 import org.apache.ignite.internal.schema.Tuple;
 import org.apache.ignite.internal.schema.TupleAssembler;
-import org.jetbrains.annotations.NotNull;
 
 /**
  * Cache objects (de)serializer.
@@ -268,7 +266,7 @@ public class JavaSerializer {
      * @param aClass Type.
      * @return Marshaller.
      */
-    @NotNull private static Marshaller createMarshaller(Columns cols, int firstColId, Class<?>
aClass) {
+    private static Marshaller createMarshaller(Columns cols, int firstColId, Class<?>
aClass) {
         final BinaryMode mode = mode(aClass);
 
         if (mode != null) {
@@ -325,7 +323,7 @@ public class JavaSerializer {
      * @return Tuple assembler.
      * @throws SerializationException If failed.
      */
-    @NotNull private TupleAssembler createAssembler(Object key, Object val) throws SerializationException
{
+    private TupleAssembler createAssembler(Object key, Object val) throws SerializationException
{
         ObjectStatistic keyStat = collectObjectStats(schema.keyColumns(), keyMarsh, key);
         ObjectStatistic valStat = collectObjectStats(schema.valueColumns(), valMarsh, val);
 
diff --git a/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/Marshaller.java
b/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/Marshaller.java
index 9085700..f907c16 100644
--- a/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/Marshaller.java
+++ b/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/Marshaller.java
@@ -37,6 +37,7 @@ package org.apache.ignite.internal.schema.marshaller;
 import java.util.Objects;
 import org.apache.ignite.internal.schema.Tuple;
 import org.apache.ignite.internal.schema.TupleAssembler;
+import org.jetbrains.annotations.Nullable;
 
 /**
  * Marshaller.
@@ -85,7 +86,7 @@ public class Marshaller {
      * @return Field value.
      * @throws SerializationException If failed.
      */
-    public Object value(Object obj, int fldIdx) throws SerializationException {
+    public @Nullable Object value(Object obj, int fldIdx) throws SerializationException {
         return fieldAccessors[fldIdx].value(obj);
     }
 
diff --git a/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/ObjectFactory.java
b/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/ObjectFactory.java
index f3b6520..954b656 100644
--- a/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/ObjectFactory.java
+++ b/modules/commons/src/main/java/org/apache/ignite/internal/schema/marshaller/ObjectFactory.java
@@ -51,6 +51,8 @@ class ObjectFactory<T> {
      */
     private ObjectFactory(Constructor<T> ctor) {
         this.ctor = ctor;
+
+        ctor.setAccessible(true);
     }
 
     /**
diff --git a/modules/commons/src/test/java/org/apache/ignite/internal/schema/SchemaTestSuite.java
b/modules/commons/src/test/java/org/apache/ignite/internal/schema/SchemaTestSuite.java
index dbf8e46..0b64ff2 100644
--- a/modules/commons/src/test/java/org/apache/ignite/internal/schema/SchemaTestSuite.java
+++ b/modules/commons/src/test/java/org/apache/ignite/internal/schema/SchemaTestSuite.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.schema;
 
+import org.apache.ignite.internal.schema.marshaller.FieldAccessorTest;
 import org.apache.ignite.internal.schema.marshaller.JavaSerializerTest;
 import org.junit.platform.runner.JUnitPlatform;
 import org.junit.platform.suite.api.SelectClasses;
@@ -29,6 +30,7 @@ import org.junit.runner.RunWith;
 @RunWith(JUnitPlatform.class)
 @SelectClasses({
     JavaSerializerTest.class,
+    FieldAccessorTest.class,
     ExpandableByteBufTest.class,
     NativeTypeTest.class,
     ColumnTest.class,
diff --git a/modules/commons/src/test/java/org/apache/ignite/internal/schema/SchemaTestUtils.java
b/modules/commons/src/test/java/org/apache/ignite/internal/schema/TestUtils.java
similarity index 99%
rename from modules/commons/src/test/java/org/apache/ignite/internal/schema/SchemaTestUtils.java
rename to modules/commons/src/test/java/org/apache/ignite/internal/schema/TestUtils.java
index fb62517..4ed5fec 100644
--- a/modules/commons/src/test/java/org/apache/ignite/internal/schema/SchemaTestUtils.java
+++ b/modules/commons/src/test/java/org/apache/ignite/internal/schema/TestUtils.java
@@ -23,7 +23,7 @@ import java.util.Random;
 /**
  * Test utility class.
  */
-public class SchemaTestUtils {
+public class TestUtils {
     /**
      * Generates randon value of given type.
      *
diff --git a/modules/commons/src/test/java/org/apache/ignite/internal/schema/TupleTest.java
b/modules/commons/src/test/java/org/apache/ignite/internal/schema/TupleTest.java
index 3741d98..2b8efee 100644
--- a/modules/commons/src/test/java/org/apache/ignite/internal/schema/TupleTest.java
+++ b/modules/commons/src/test/java/org/apache/ignite/internal/schema/TupleTest.java
@@ -191,7 +191,7 @@ public class TupleTest {
      *
      */
     private Object generateRandomValue(NativeType type) {
-        return SchemaTestUtils.generateRandomValue(rnd, type);
+        return TestUtils.generateRandomValue(rnd, type);
     }
 
     /**
diff --git a/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/FieldAccessorTest.java
b/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/FieldAccessorTest.java
index 396c6ca..461d66a 100644
--- a/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/FieldAccessorTest.java
+++ b/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/FieldAccessorTest.java
@@ -18,12 +18,14 @@
 package org.apache.ignite.internal.schema.marshaller;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.BitSet;
+import java.util.Objects;
 import java.util.Random;
 import java.util.UUID;
 import org.apache.ignite.internal.schema.Bitmask;
 import org.apache.ignite.internal.schema.Column;
-import org.apache.ignite.internal.schema.SchemaTestUtils;
+import org.apache.ignite.internal.schema.TestUtils;
 import org.apache.ignite.internal.schema.Tuple;
 import org.apache.ignite.internal.schema.TupleAssembler;
 import org.apache.ignite.internal.util.Pair;
@@ -60,7 +62,7 @@ public class FieldAccessorTest {
     public void initRandom() {
         long seed = System.currentTimeMillis();
 
-        System.out.println("Using seed: " + seed + "L; //");
+        System.out.println("Using seed: " + seed + "L;");
 
         rnd = new Random(seed);
     }
@@ -96,7 +98,7 @@ public class FieldAccessorTest {
         final TupleAssembler tupleAssembler = mocks.getFirst();
         final Tuple tuple = mocks.getSecond();
 
-        final TestObject obj = generateRandomObject();
+        final TestObject obj = TestObject.randomObject(rnd);
 
         for (int i = 0; i < cols.length; i++) {
             FieldAccessor accessor = FieldAccessor.create(TestObject.class, cols[i], i);
@@ -167,7 +169,8 @@ public class FieldAccessorTest {
         assertThrows(
             SerializationException.class,
             () -> accessor.write("Other string", mocks.getFirst()),
-            "Failed to write field [id=42]");
+            "Failed to write field [id=42]"
+        );
     }
 
     /**
@@ -182,7 +185,7 @@ public class FieldAccessorTest {
         final Tuple mockedTuple = Mockito.mock(Tuple.class);
 
         final Answer<Void> asmAnswer = new Answer<Void>() {
-            @Override public Void answer(InvocationOnMock invocation) throws Throwable {
+            @Override public Void answer(InvocationOnMock invocation) {
                 vals.add(invocation.getArguments()[0]);
 
                 return null;
@@ -190,7 +193,7 @@ public class FieldAccessorTest {
         };
 
         final Answer<Object> tupleAnswer = new Answer<Object>() {
-            @Override public Object answer(InvocationOnMock invocation) throws Throwable
{
+            @Override public Object answer(InvocationOnMock invocation) {
                 final int idx = invocation.getArgumentAt(0, Integer.class);
 
                 return vals.get(idx);
@@ -232,37 +235,38 @@ public class FieldAccessorTest {
     }
 
     /**
-     * @return Random TestObject.
-     */
-    private TestObject generateRandomObject() {
-        final TestObject obj = new TestObject();
-
-        obj.pByteCol = (byte)rnd.nextInt(255);
-        obj.pShortCol = (short)rnd.nextInt(65535);
-        obj.pIntCol = rnd.nextInt();
-        obj.pLongCol = rnd.nextLong();
-        obj.pFloatCol = rnd.nextFloat();
-        obj.pDoubleCol = rnd.nextDouble();
-
-        obj.byteCol = (byte)rnd.nextInt(255);
-        obj.shortCol = (short)rnd.nextInt(65535);
-        obj.intCol = rnd.nextInt();
-        obj.longCol = rnd.nextLong();
-        obj.floatCol = rnd.nextFloat();
-        obj.doubleCol = rnd.nextDouble();
-
-        obj.uuidCol = new UUID(rnd.nextLong(), rnd.nextLong());
-        obj.bitmaskCol = SchemaTestUtils.randomBitSet(rnd, rnd.nextInt(42));
-        obj.stringCol = SchemaTestUtils.randomString(rnd, rnd.nextInt(255));
-        obj.bytesCol = SchemaTestUtils.randomBytes(rnd, rnd.nextInt(255));
-
-        return obj;
-    }
-
-    /**
      * Test object.
      */
     private static class TestObject {
+        /**
+         * @return Random TestObject.
+         */
+        public static TestObject randomObject(Random rnd) {
+            final TestObject obj = new TestObject();
+
+            obj.pByteCol = (byte)rnd.nextInt(255);
+            obj.pShortCol = (short)rnd.nextInt(65535);
+            obj.pIntCol = rnd.nextInt();
+            obj.pLongCol = rnd.nextLong();
+            obj.pFloatCol = rnd.nextFloat();
+            obj.pDoubleCol = rnd.nextDouble();
+
+            obj.byteCol = (byte)rnd.nextInt(255);
+            obj.shortCol = (short)rnd.nextInt(65535);
+            obj.intCol = rnd.nextInt();
+            obj.longCol = rnd.nextLong();
+            obj.floatCol = rnd.nextFloat();
+            obj.doubleCol = rnd.nextDouble();
+
+            obj.uuidCol = new UUID(rnd.nextLong(), rnd.nextLong());
+            obj.bitmaskCol = TestUtils.randomBitSet(rnd, rnd.nextInt(42));
+            obj.stringCol = TestUtils.randomString(rnd, rnd.nextInt(255));
+            obj.bytesCol = TestUtils.randomBytes(rnd, rnd.nextInt(255));
+
+            return obj;
+        }
+
+        // Primitive typed
         private byte pByteCol;
         private short pShortCol;
         private int pIntCol;
@@ -270,6 +274,7 @@ public class FieldAccessorTest {
         private float pFloatCol;
         private double pDoubleCol;
 
+        // Reference typed
         private Byte byteCol;
         private Short shortCol;
         private Integer intCol;
@@ -281,5 +286,35 @@ public class FieldAccessorTest {
         private BitSet bitmaskCol;
         private String stringCol;
         private byte[] bytesCol;
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+            if (o == null || getClass() != o.getClass())
+                return false;
+            TestObject object = (TestObject)o;
+            return pByteCol == object.pByteCol &&
+                pShortCol == object.pShortCol &&
+                pIntCol == object.pIntCol &&
+                pLongCol == object.pLongCol &&
+                Float.compare(object.pFloatCol, pFloatCol) == 0 &&
+                Double.compare(object.pDoubleCol, pDoubleCol) == 0 &&
+                Objects.equals(byteCol, object.byteCol) &&
+                Objects.equals(shortCol, object.shortCol) &&
+                Objects.equals(intCol, object.intCol) &&
+                Objects.equals(longCol, object.longCol) &&
+                Objects.equals(floatCol, object.floatCol) &&
+                Objects.equals(doubleCol, object.doubleCol) &&
+                Objects.equals(uuidCol, object.uuidCol) &&
+                Objects.equals(bitmaskCol, object.bitmaskCol) &&
+                Objects.equals(stringCol, object.stringCol) &&
+                Arrays.equals(bytesCol, object.bytesCol);
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return 73;
+        }
     }
 }
diff --git a/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/JavaSerializerTest.java
b/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/JavaSerializerTest.java
index 5eddf24..372c27e 100644
--- a/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/JavaSerializerTest.java
+++ b/modules/commons/src/test/java/org/apache/ignite/internal/schema/marshaller/JavaSerializerTest.java
@@ -17,14 +17,18 @@
 
 package org.apache.ignite.internal.schema.marshaller;
 
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Objects;
 import java.util.Random;
+import java.util.UUID;
 import org.apache.ignite.internal.schema.Bitmask;
 import org.apache.ignite.internal.schema.Column;
 import org.apache.ignite.internal.schema.Columns;
 import org.apache.ignite.internal.schema.NativeType;
 import org.apache.ignite.internal.schema.NativeTypeSpec;
 import org.apache.ignite.internal.schema.SchemaDescriptor;
-import org.apache.ignite.internal.schema.SchemaTestUtils;
+import org.apache.ignite.internal.schema.TestUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -39,6 +43,7 @@ import static org.apache.ignite.internal.schema.NativeType.STRING;
 import static org.apache.ignite.internal.schema.NativeType.UUID;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
@@ -55,25 +60,16 @@ public class JavaSerializerTest {
     public void initRandom() {
         long seed = System.currentTimeMillis();
 
-        System.out.println("Using seed: " + seed + "L; //");
+        System.out.println("Using seed: " + seed + "L;");
 
         rnd = new Random(seed);
     }
 
     /**
-     * Generates randon value of given type.
-     *
-     * @param type Type.
-     */
-    private Object generateRandomValue(NativeType type) {
-        return SchemaTestUtils.generateRandomValue(rnd, type);
-    }
-
-    /**
-     * @throws Exception If failed.
+     * @throws SerializationException If serialization failed.
      */
     @Test
-    public void testBasicTypes() throws Exception {
+    public void testBasicTypes() throws SerializationException {
         // Fixed types:
         checkBasicType(BYTE, BYTE);
         checkBasicType(SHORT, SHORT);
@@ -96,9 +92,156 @@ public class JavaSerializerTest {
     }
 
     /**
-     * @throws Exception If failed.
+     * @throws SerializationException If serialization failed.
+     */
+    @Test
+    public void testComplexType() throws SerializationException {
+        Column[] cols = new Column[] {
+            new Column("pByteCol", BYTE, false),
+            new Column("pShortCol", SHORT, false),
+            new Column("pIntCol", INTEGER, false),
+            new Column("pLongCol", LONG, false),
+            new Column("pFloatCol", FLOAT, false),
+            new Column("pDoubleCol", DOUBLE, false),
+
+            new Column("byteCol", BYTE, true),
+            new Column("shortCol", SHORT, true),
+            new Column("intCol", INTEGER, true),
+            new Column("longCol", LONG, true),
+            new Column("floatCol", FLOAT, true),
+            new Column("doubleCol", DOUBLE, true),
+
+            new Column("uuidCol", UUID, true),
+            new Column("bitmaskCol", Bitmask.of(42), true),
+            new Column("stringCol", STRING, true),
+            new Column("bytesCol", BYTES, true),
+        };
+
+        SchemaDescriptor schema = new SchemaDescriptor(1, new Columns(cols), new Columns(cols.clone()));
+
+        final Object key = TestObject.randomObject(rnd);
+        final Object val = TestObject.randomObject(rnd);
+
+        JavaSerializer serializer = new JavaSerializer(schema, key.getClass(), val.getClass());
+
+        byte[] bytes = serializer.serialize(key, val);
+
+        Object key1 = serializer.deserializeKey(bytes);
+        Object val1 = serializer.deserializeValue(bytes);
+
+        assertTrue(key.getClass().isInstance(key1));
+        assertTrue(val.getClass().isInstance(val1));
+
+        assertEquals(key, key);
+        assertEquals(val, val1);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testClassWithIncorrectBitmaskSize() {
+        Column[] cols = new Column[] {
+            new Column("pLongCol", LONG, false),
+            new Column("bitmaskCol", Bitmask.of(9), true),
+        };
+
+        SchemaDescriptor schema = new SchemaDescriptor(1, new Columns(cols), new Columns(cols.clone()));
+
+        final Object key = TestObject.randomObject(rnd);
+        final Object val = TestObject.randomObject(rnd);
+
+        JavaSerializer serializer = new JavaSerializer(schema, key.getClass(), val.getClass());
+
+        assertThrows(
+            SerializationException.class,
+            () -> serializer.serialize(key, val),
+            "Failed to write field [name=bitmaskCol]"
+        );
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testClassWithWrongFieldType() {
+        Column[] cols = new Column[] {
+            new Column("bitmaskCol", Bitmask.of(42), true),
+            new Column("shortCol", UUID, true)
+        };
+
+        SchemaDescriptor schema = new SchemaDescriptor(1, new Columns(cols), new Columns(cols.clone()));
+
+        final Object key = TestObject.randomObject(rnd);
+        final Object val = TestObject.randomObject(rnd);
+
+        JavaSerializer serializer = new JavaSerializer(schema, key.getClass(), val.getClass());
+
+        assertThrows(
+            SerializationException.class,
+            () -> serializer.serialize(key, val),
+            "Failed to write field [name=shortCol]"
+        );
+    }
+
+    /**
+     *
      */
-    private void checkBasicType(NativeType keyType, NativeType valType) throws Exception
{
+    @Test
+    public void testClassWithPrivateConstructor() throws SerializationException {
+        Column[] cols = new Column[] {
+            new Column("pLongCol", LONG, false),
+        };
+
+        SchemaDescriptor schema = new SchemaDescriptor(1, new Columns(cols), new Columns(cols.clone()));
+
+        final Object key = PrivateTestObject.randomObject(rnd);
+        final Object val = PrivateTestObject.randomObject(rnd);
+
+        JavaSerializer serializer = new JavaSerializer(schema, key.getClass(), val.getClass());
+
+        byte[] bytes = serializer.serialize(key, val);
+
+        Object key1 = serializer.deserializeKey(bytes);
+        Object val1 = serializer.deserializeValue(bytes);
+
+        assertTrue(key.getClass().isInstance(key1));
+        assertTrue(val.getClass().isInstance(val1));
+
+        assertEquals(key, key);
+        assertEquals(val, val1);
+    }
+
+    /**
+     *
+     */
+    @SuppressWarnings("ResultOfObjectAllocationIgnored")
+    @Test
+    public void testClassWithNoDefaultConstructor() {
+        Column[] cols = new Column[] {
+            new Column("pLongCol", LONG, false),
+        };
+
+        SchemaDescriptor schema = new SchemaDescriptor(1, new Columns(cols), new Columns(cols.clone()));
+
+        final Object key = WrongTestObject.randomObject(rnd);
+        final Object val = WrongTestObject.randomObject(rnd);
+
+        assertThrows(IllegalStateException.class,
+            () -> new JavaSerializer(schema, key.getClass(), val.getClass()),
+            "No default constructor found for class: WrongTestObject"
+        );
+    }
+
+    /**
+     * Generate random key-value pair of given types and
+     * check serialization and deserialization works fine.
+     *
+     * @param keyType Key type.
+     * @param valType Value type.
+     * @throws SerializationException If (de)serialization failed.
+     */
+    private void checkBasicType(NativeType keyType, NativeType valType) throws SerializationException
{
         final Object key = generateRandomValue(keyType);
         final Object val = generateRandomValue(valType);
 
@@ -134,4 +277,184 @@ public class JavaSerializerTest {
         else
             assertEquals(exp, act);
     }
+
+    /**
+     * Generates randon value of given type.
+     *
+     * @param type Type.
+     */
+    private Object generateRandomValue(NativeType type) {
+        return TestUtils.generateRandomValue(rnd, type);
+    }
+
+    /**
+     * Test object.
+     */
+    public static class TestObject {
+        /**
+         * @return Random TestObject.
+         */
+        public static TestObject randomObject(Random rnd) {
+            final TestObject obj = new TestObject();
+
+            obj.pByteCol = (byte)rnd.nextInt(255);
+            obj.pShortCol = (short)rnd.nextInt(65535);
+            obj.pIntCol = rnd.nextInt();
+            obj.pLongCol = rnd.nextLong();
+            obj.pFloatCol = rnd.nextFloat();
+            obj.pDoubleCol = rnd.nextDouble();
+
+            obj.byteCol = (byte)rnd.nextInt(255);
+            obj.shortCol = (short)rnd.nextInt(65535);
+            obj.intCol = rnd.nextInt();
+            obj.longCol = rnd.nextLong();
+            obj.floatCol = rnd.nextFloat();
+            obj.doubleCol = rnd.nextDouble();
+
+            obj.uuidCol = new UUID(rnd.nextLong(), rnd.nextLong());
+            obj.bitmaskCol = TestUtils.randomBitSet(rnd, 42);
+            obj.stringCol = TestUtils.randomString(rnd, rnd.nextInt(255));
+            obj.bytesCol = TestUtils.randomBytes(rnd, rnd.nextInt(255));
+
+            return obj;
+        }
+
+        // Primitive typed
+        private byte pByteCol;
+        private short pShortCol;
+        private int pIntCol;
+        private long pLongCol;
+        private float pFloatCol;
+        private double pDoubleCol;
+
+        // Reference typed
+        private Byte byteCol;
+        private Short shortCol;
+        private Integer intCol;
+        private Long longCol;
+        private Float floatCol;
+        private Double doubleCol;
+
+        private UUID uuidCol;
+        private BitSet bitmaskCol;
+        private String stringCol;
+        private byte[] bytesCol;
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            TestObject object = (TestObject)o;
+
+            return pByteCol == object.pByteCol &&
+                pShortCol == object.pShortCol &&
+                pIntCol == object.pIntCol &&
+                pLongCol == object.pLongCol &&
+                Float.compare(object.pFloatCol, pFloatCol) == 0 &&
+                Double.compare(object.pDoubleCol, pDoubleCol) == 0 &&
+                Objects.equals(byteCol, object.byteCol) &&
+                Objects.equals(shortCol, object.shortCol) &&
+                Objects.equals(intCol, object.intCol) &&
+                Objects.equals(longCol, object.longCol) &&
+                Objects.equals(floatCol, object.floatCol) &&
+                Objects.equals(doubleCol, object.doubleCol) &&
+                Objects.equals(uuidCol, object.uuidCol) &&
+                Objects.equals(bitmaskCol, object.bitmaskCol) &&
+                Objects.equals(stringCol, object.stringCol) &&
+                Arrays.equals(bytesCol, object.bytesCol);
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return 73;
+        }
+    }
+
+    /**
+     * Test object with private constructor.
+     */
+    private static class PrivateTestObject {
+        /**
+         * @return Random TestObject.
+         */
+        static PrivateTestObject randomObject(Random rnd) {
+            final PrivateTestObject obj = new PrivateTestObject();
+
+            obj.pLongCol = rnd.nextLong();
+
+            return obj;
+        }
+
+        /** Value. */
+        private long pLongCol;
+
+        /**
+         * Private constructor.
+         */
+        @SuppressWarnings("RedundantNoArgConstructor")
+        private PrivateTestObject() {
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            PrivateTestObject object = (PrivateTestObject)o;
+
+            return pLongCol == object.pLongCol;
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return Objects.hash(pLongCol);
+        }
+    }
+
+    /**
+     * Test object without default constructor.
+     */
+    private static class WrongTestObject {
+        /**
+         * @return Random TestObject.
+         */
+        static WrongTestObject randomObject(Random rnd) {
+            return new WrongTestObject(rnd.nextLong());
+        }
+
+        /** Value. */
+        private final long pLongCol;
+
+        /**
+         * Private constructor.
+         */
+        private WrongTestObject(long val) {
+            pLongCol = val;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            WrongTestObject object = (WrongTestObject)o;
+
+            return pLongCol == object.pLongCol;
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return Objects.hash(pLongCol);
+        }
+    }
 }


Mime
View raw message