You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/07/02 09:38:38 UTC

[isis] branch master updated: ISIS-2340: serialization roundtrip testing; fix empty Can equals

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 2c3cf12  ISIS-2340: serialization roundtrip testing; fix empty Can<T> equals
2c3cf12 is described below

commit 2c3cf12074be921df6b50d8b8546a2f94baab48b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jul 2 11:38:23 2020 +0200

    ISIS-2340: serialization roundtrip testing; fix empty Can<T> equals
---
 .../isis/core/commons/collections/Can_Empty.java   |  7 ++-
 .../isis/core/commons/SerializationTester.java     | 73 ++++++++++++++++++++++
 .../isis/core/commons/collections/CanTest.java     | 65 +++++++++++++++++++
 3 files changed, 144 insertions(+), 1 deletion(-)

diff --git a/core/commons/src/main/java/org/apache/isis/core/commons/collections/Can_Empty.java b/core/commons/src/main/java/org/apache/isis/core/commons/collections/Can_Empty.java
index 8042578..adf1522 100644
--- a/core/commons/src/main/java/org/apache/isis/core/commons/collections/Can_Empty.java
+++ b/core/commons/src/main/java/org/apache/isis/core/commons/collections/Can_Empty.java
@@ -123,7 +123,12 @@ final class Can_Empty<T> implements Can<T> {
     
     @Override
     public boolean equals(Object obj) {
-        return INSTANCE == obj;
+        if(INSTANCE == obj) {
+            return true; // optimization not strictly necessary
+        }
+        return (obj instanceof Can)
+                ? ((Can<?>)obj).isEmpty()
+                : false;
     }
     
     @Override
diff --git a/core/commons/src/test/java/org/apache/isis/core/commons/SerializationTester.java b/core/commons/src/test/java/org/apache/isis/core/commons/SerializationTester.java
new file mode 100644
index 0000000..499c75a
--- /dev/null
+++ b/core/commons/src/test/java/org/apache/isis/core/commons/SerializationTester.java
@@ -0,0 +1,73 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.core.commons;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
+import org.junit.jupiter.api.Assertions;
+
+import lombok.val;
+
+/**
+ * in-memory serialization round-tripping
+ * @since Jul 2, 2020
+ *
+ */
+public class SerializationTester {
+
+    public static byte[] marshall(Serializable object) throws IOException {
+        val bos = new ByteArrayOutputStream(16*4096); // 16k initial buffer size
+        val oos = new ObjectOutputStream(bos);
+        oos.writeObject(object);
+        oos.flush();
+        oos.close();
+        return bos.toByteArray();
+    }
+
+    public static <T> T unmarshall(byte[] input) throws IOException, ClassNotFoundException {
+        val bis = new ByteArrayInputStream(input);
+        val ois = new ObjectInputStream(bis);
+        @SuppressWarnings("unchecked")
+        val t = (T) ois.readObject();
+        bis.close(); 
+        return t;
+    }
+
+    public static <T extends Serializable> T roundtrip(T object) throws IOException, ClassNotFoundException {
+        val bytes = marshall(object);
+        return unmarshall(bytes);
+    }
+
+    public static <T extends Serializable> void assertEqualsOnRoundtrip(T object) {
+        T afterRoundtrip;
+        try {
+            afterRoundtrip = roundtrip(object);
+        } catch (ClassNotFoundException | IOException e) {
+            Assertions.fail(e);
+            return;
+        }
+        Assertions.assertEquals(object, afterRoundtrip);
+    }
+
+}
diff --git a/core/commons/src/test/java/org/apache/isis/core/commons/collections/CanTest.java b/core/commons/src/test/java/org/apache/isis/core/commons/collections/CanTest.java
new file mode 100644
index 0000000..58fba10
--- /dev/null
+++ b/core/commons/src/test/java/org/apache/isis/core/commons/collections/CanTest.java
@@ -0,0 +1,65 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.core.commons.collections;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.isis.core.commons.SerializationTester;
+
+class CanTest {
+
+    @BeforeEach
+    void setUp() throws Exception {
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+    }
+
+    @Test
+    void tester_selftest() {
+        SerializationTester.assertEqualsOnRoundtrip("Hello World!");
+    }
+    
+    @Test
+    void emptyCans_shouldBeEqual() {
+        assertEquals(Can.empty(), Can.<String>of());
+    }
+    
+    @Test
+    void emptyCan_shouldBeSerializable() {
+        SerializationTester.assertEqualsOnRoundtrip(Can.empty());
+        SerializationTester.assertEqualsOnRoundtrip(Can.<String>of());
+    }
+    
+    @Test
+    void singletonCan_shouldBeSerializable() {
+        SerializationTester.assertEqualsOnRoundtrip(Can.<String>of("hi"));
+    }
+    
+    @Test
+    void multiCan_shouldBeSerializable() {
+        SerializationTester.assertEqualsOnRoundtrip(Can.<String>of("hi", "there"));
+    }
+
+}