summaryrefslogtreecommitdiff
path: root/eaaf_core_utils/src/test
diff options
context:
space:
mode:
authorThomas <>2023-05-15 08:32:34 +0200
committerThomas <>2023-05-15 08:32:34 +0200
commit45a7ae26f0172c636e42b530561eb11efa0083d5 (patch)
treedcb4d604bac49e723f9cf780f43254bb229d803f /eaaf_core_utils/src/test
parent54f16e552a3d6c5579282de5c8d6a92395f6c049 (diff)
downloadEAAF-Components-45a7ae26f0172c636e42b530561eb11efa0083d5.tar.gz
EAAF-Components-45a7ae26f0172c636e42b530561eb11efa0083d5.tar.bz2
EAAF-Components-45a7ae26f0172c636e42b530561eb11efa0083d5.zip
feat(utils): optimize custom Java serialization implementation
Diffstat (limited to 'eaaf_core_utils/src/test')
-rw-r--r--eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/utils/EaafSerializationUtilsTest.java214
1 files changed, 214 insertions, 0 deletions
diff --git a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/utils/EaafSerializationUtilsTest.java b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/utils/EaafSerializationUtilsTest.java
new file mode 100644
index 00000000..98747b41
--- /dev/null
+++ b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/utils/EaafSerializationUtilsTest.java
@@ -0,0 +1,214 @@
+package at.gv.egiz.eaaf.core.test.utils;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.Serializable;
+import java.util.Collections;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.BlockJUnit4ClassRunner;
+
+import com.google.common.collect.Sets;
+
+import at.gv.egiz.eaaf.core.exceptions.EaafAuthenticationException;
+import at.gv.egiz.eaaf.core.exceptions.EaafException;
+import at.gv.egiz.eaaf.core.impl.utils.EaafSerializationUtils;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+
+@RunWith(BlockJUnit4ClassRunner.class)
+public class EaafSerializationUtilsTest {
+
+ @Test
+ public void nullInput() {
+ assertNull(EaafSerializationUtils.serialize(null));
+ assertNull(EaafSerializationUtils.strictDeserialize(null, Sets.newHashSet(Throwable.class)));
+ assertNull(EaafSerializationUtils.typeSpecificDeserialize(null,
+ Sets.newHashSet(Throwable.class), String.class));
+
+ }
+
+ @Test
+ public void strictMode() {
+ DummyClassA dummyA = new DummyClassA(rand());
+ dummyA.setMemberB(new DummyClassB(rand()));
+
+ byte[] object = EaafSerializationUtils.serialize(dummyA);
+
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.strictDeserialize(
+ object, Sets.newHashSet(Throwable.class)));
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.strictDeserialize(
+ object, Sets.newHashSet(DummyClassA.class)));
+
+ assertNotNull(EaafSerializationUtils.strictDeserialize(object, Sets.newHashSet(
+ DummyClassA.class, DummyClassB.class)));
+
+ }
+
+ @Test
+ public void typeModeSimple() {
+ DummyClassC dummyC = new DummyClassC(rand());
+
+ byte[] object = EaafSerializationUtils.serialize(dummyC);
+
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class), String.class));
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassB.class), DummyClassC.class));
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassC.class), DummyClassA.class));
+
+ assertNotNull(EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class), DummyClassC.class));
+ assertNotNull(EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class), DummyClassA.class));
+
+ }
+
+ @Test
+ public void typeModeComplex() {
+
+ DummyClassC dummyC = new DummyClassC(rand());
+ dummyC.setMemberB(new DummyClassB(rand()));
+
+ byte[] object = EaafSerializationUtils.serialize(dummyC);
+
+ // missing DummyClassB
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class), DummyClassC.class));
+
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassC.class, DummyClassB.class), DummyClassC.class));
+
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassB.class, DummyClassA.class), DummyClassB.class));
+
+ assertNotNull(EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class, DummyClassB.class), DummyClassA.class));
+ assertNotNull(EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class, DummyClassB.class), DummyClassC.class));
+
+ }
+
+ @Test
+ public void typeModeComplexWithObject() {
+
+ DummyClassC dummyC = new DummyClassC(rand());
+ DummyClassD dummyD = new DummyClassD(rand());
+ dummyC.setMemberB(dummyD);
+ dummyD.setAnyType(new EaafException(rand()));
+
+ byte[] object = EaafSerializationUtils.serialize(dummyC);
+
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class, DummyClassB.class), DummyClassA.class));
+
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class, DummyClassB.class), DummyClassC.class));
+
+ assertNotNull(EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class, DummyClassB.class,
+ Throwable.class, StackTraceElement[].class,
+ StackTraceElement.class, Collections.EMPTY_LIST.getClass()), DummyClassA.class));
+
+ }
+
+ @Test
+ public void typeModeComplexNullObject() {
+
+ DummyClassC dummyC = new DummyClassC(rand());
+ DummyClassD dummyD = new DummyClassD(rand());
+ dummyC.setMemberB(dummyD);
+
+ byte[] object = EaafSerializationUtils.serialize(dummyC);
+
+ assertNotNull(EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(DummyClassA.class, DummyClassB.class), DummyClassA.class));
+
+ }
+
+ @Test
+ public void typeModeWithExceptions() {
+ EaafException error1 = new EaafException(rand());
+ EaafAuthenticationException error2 = new EaafAuthenticationException(rand(), null, error1);
+
+ byte[] object = EaafSerializationUtils.serialize(error2);
+
+ // check if less allowed classes throw a deserialization exception
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(Throwable.class), Throwable.class));
+ assertThrows(IllegalArgumentException.class, () -> EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(Throwable.class, StackTraceElement[].class, StackTraceElement.class),
+ Throwable.class));
+
+ // minimum allowed classes to de-serialize an Throwable
+ assertNotNull(EaafSerializationUtils.typeSpecificDeserialize(
+ object, Sets.newHashSet(Throwable.class, StackTraceElement[].class,
+ StackTraceElement.class, Collections.EMPTY_LIST.getClass()),
+ Throwable.class));
+
+ }
+
+ private String rand() {
+ return RandomStringUtils.randomAlphanumeric(10);
+ }
+
+ @Getter
+ @RequiredArgsConstructor
+ public static class DummyClassA implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String test;
+
+ @Setter
+ private DummyClassB memberB;
+
+ }
+
+ @Getter
+ @AllArgsConstructor
+ public static class DummyClassB implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String test;
+
+ }
+
+ @Getter
+ public static class DummyClassD extends DummyClassB implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ public DummyClassD(String data) {
+ super(data);
+ }
+
+ @Setter
+ private Object anyType;
+
+ }
+
+ @Getter
+ public static class DummyClassC extends DummyClassA implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ public DummyClassC(String data) {
+ super(data);
+
+ this.test = data;
+ }
+
+ private final String test;
+
+ }
+
+}