From 49336e7c839753026530371e7aabcd3080c82b46 Mon Sep 17 00:00:00 2001 From: devjeonghwan Date: Wed, 23 Aug 2023 13:27:59 +0900 Subject: [PATCH] Add test case in AnnotationWithTypeTest.java and Reformat code --- .../opack/annotation/WithType.java | 2 - .../impl/list/ListTransformer.java | 2 +- .../impl/list/WrapListTransformer.java | 4 +- .../impl/map/WrapMapTransformer.java | 6 +- .../annotation/AnnotationWithTypeTest.java | 72 ++++++++++++++++--- .../test/opacker/map/WrapMapElementTest.java | 6 +- .../opacker/transform/TransformClassTest.java | 7 +- .../opacker/transform/TransformFieldTest.java | 1 + .../transform/file/FileTransformTest.java | 4 +- .../transform/path/PathTransformTest.java | 4 +- .../time/Java8TimeTransformTest.java | 8 +-- .../transform/time/TimeTransformTest.java | 6 +- 12 files changed, 89 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/realtimetech/opack/annotation/WithType.java b/src/main/java/com/realtimetech/opack/annotation/WithType.java index 051fe56..9adb6e2 100644 --- a/src/main/java/com/realtimetech/opack/annotation/WithType.java +++ b/src/main/java/com/realtimetech/opack/annotation/WithType.java @@ -22,8 +22,6 @@ package com.realtimetech.opack.annotation; -import org.jetbrains.annotations.NotNull; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java index e3d57d6..e02edea 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java @@ -74,7 +74,7 @@ public class ListTransformer extends DataStructureTransformer { public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { if (object instanceof OpackArray) { OpackArray opackArray = (OpackArray) object; - + if (List.class.isAssignableFrom(goalType)) { try { List list = (List) ReflectionUtil.createInstance(goalType); diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java index bbaa30a..2713808 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java @@ -26,7 +26,6 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.transformer.impl.TypeWrapper; -import com.realtimetech.opack.value.OpackObject; import com.realtimetech.opack.value.OpackValue; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -51,11 +50,10 @@ public class WrapListTransformer extends ListTransformer { * @param opacker the opacker * @param element the element to be deserialized * @return deserialized element - * @throws ClassNotFoundException if the class cannot be located * @throws DeserializeException if a problem occurs during deserializing */ @Override - protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws ClassNotFoundException, DeserializeException { + protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws DeserializeException { return TypeWrapper.unwrapObject(opacker, element); } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java index eae125d..0062116 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java @@ -26,7 +26,6 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.transformer.impl.TypeWrapper; -import com.realtimetech.opack.value.OpackObject; import com.realtimetech.opack.value.OpackValue; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -42,7 +41,7 @@ public class WrapMapTransformer extends MapTransformer { */ @Override protected @Nullable Object serializeObject(@NotNull Opacker opacker, @Nullable Object element) throws SerializeException { - return TypeWrapper.wrapObject(opacker,element); + return TypeWrapper.wrapObject(opacker, element); } /** @@ -51,11 +50,10 @@ public class WrapMapTransformer extends MapTransformer { * @param opacker the opacker * @param element the element to be deserialized * @return deserialized element - * @throws ClassNotFoundException if the class cannot be located * @throws DeserializeException if a problem occurs during deserializing */ @Override - protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws ClassNotFoundException, DeserializeException { + protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws DeserializeException { return TypeWrapper.unwrapObject(opacker, element); } } diff --git a/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java b/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java index 4ff5598..bd107ab 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java @@ -27,24 +27,52 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; +import com.realtimetech.opack.test.opacker.single.ObjectTest; import com.realtimetech.opack.value.OpackValue; -import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import java.util.Random; public class AnnotationWithTypeTest { + static final Random RANDOM = new Random(); + + public static class ObjectClass { + private Object nullValue; + + private String stringValue; + private int intValue; + private Integer integerValue; + + public ObjectClass() { + this.nullValue = null; + + this.stringValue = "object_string_value" + System.currentTimeMillis(); + this.intValue = RANDOM.nextInt(); + this.integerValue = RANDOM.nextInt(); + } + } + public static class WithTypeClass { @WithType - private @NotNull List listWithTransformer; + private List listWithTransformer; + + @WithType + private List[] listArrayWithTransformer; + + @WithType + private List listWithAnnotation; + + @WithType + private List objectListWithTransformer; @WithType - private @NotNull List @NotNull [] listArrayWithTransformer; + private List[] objectListArrayWithTransformer; @WithType - private @NotNull List listWithAnnotation; + private List objectListWithAnnotation; public WithTypeClass() { this.listWithTransformer = new LinkedList<>(); @@ -54,8 +82,8 @@ public WithTypeClass() { this.addItemsRandomly(this.listWithTransformer); this.addItemsRandomly(this.listWithAnnotation); - for(int index = 0; index < this.listArrayWithTransformer.length; index++) { - if(index % 2 == 0) { + for (int index = 0; index < this.listArrayWithTransformer.length; index++) { + if (index % 2 == 0) { this.listArrayWithTransformer[index] = new ArrayList<>(); } else { this.listArrayWithTransformer[index] = new LinkedList<>(); @@ -63,21 +91,49 @@ public WithTypeClass() { this.addItemsRandomly(this.listArrayWithTransformer[index]); } + + + this.objectListWithTransformer = new LinkedList<>(); + this.objectListArrayWithTransformer = new List[8]; + this.objectListWithAnnotation = new LinkedList<>(); + + this.addObjectItemsRandomly(this.objectListWithTransformer); + this.addObjectItemsRandomly(this.objectListWithAnnotation); + + for (int index = 0; index < this.objectListArrayWithTransformer.length; index++) { + if (index % 2 == 0) { + this.objectListArrayWithTransformer[index] = new ArrayList<>(); + } else { + this.objectListArrayWithTransformer[index] = new LinkedList<>(); + } + + this.addObjectItemsRandomly(this.objectListArrayWithTransformer[index]); + } } - private void addItemsRandomly(@NotNull List list) { + private void addItemsRandomly(List list) { for (int index = 0; index < 32; index++) { list.add(Math.random()); } } + + private void addObjectItemsRandomly(List list) { + for (int index = 0; index < 32; index++) { + list.add(new ObjectClass()); + } + } } @Test public void test() throws SerializeException, DeserializeException, OpackAssert.AssertException { - Opacker opacker = new Opacker.Builder().create(); + Opacker opacker = new Opacker.Builder() + .setEnableWrapListElementType(true) + .setEnableWrapMapElementType(true) + .create(); WithTypeClass originalObject = new WithTypeClass(); OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; WithTypeClass deserialized = opacker.deserialize(WithTypeClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java b/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java index 14c9590..34a8785 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java @@ -31,7 +31,10 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.TreeMap; public class WrapMapElementTest { static final Random RANDOM = new Random(); @@ -91,6 +94,7 @@ private void common(boolean enableWrapMapElementType) throws SerializeException, WrapMapClass originalObject = new WrapMapClass(); OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; WrapMapClass deserialized = opacker.deserialize(WrapMapClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java index 1823fac..ee74281 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java @@ -32,7 +32,6 @@ import com.realtimetech.opack.util.ReflectionUtil; import com.realtimetech.opack.value.OpackObject; import com.realtimetech.opack.value.OpackValue; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -43,7 +42,7 @@ public class TransformClassTest { public static class ClassTransformer implements Transformer { @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + public @Nullable Object serialize(Opacker opacker, Class originalType, @Nullable Object value) throws SerializeException { if (value instanceof ClassTransformInheritable) { ClassTransformInheritable classTransformInheritable = (ClassTransformInheritable) value; return new String(classTransformInheritable.bytes, StandardCharsets.UTF_8); @@ -57,7 +56,7 @@ public static class ClassTransformer implements Transformer { } @Override - public Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, Object value) throws DeserializeException { + public Object deserialize(Opacker opacker, Class goalType, Object value) throws DeserializeException { if (value instanceof String && ClassTransformInheritable.class.isAssignableFrom(goalType)) { try { ClassTransformInheritable classTransformInheritable = (ClassTransformInheritable) ReflectionUtil.createInstanceUnsafe(goalType); @@ -166,6 +165,8 @@ public void test() throws SerializeException, DeserializeException, OpackAssert. OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; + Assertions.assertEquals(((OpackObject) serialized).get("classTransformInheritableValue").getClass(), String.class); Assertions.assertEquals(((OpackObject) serialized).get("classTransformInheritableChildValue").getClass(), String.class); Assertions.assertEquals(((OpackObject) serialized).get("explicitClassTransformInheritableValue").getClass(), String.class); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java index 776650d..1c93a09 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java @@ -80,6 +80,7 @@ public void test() throws SerializeException, DeserializeException, OpackAssert. FieldTransformClass originalObject = new FieldTransformClass(); OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; FieldTransformClass deserialized = opacker.deserialize(FieldTransformClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java index 12a8fb3..750067f 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java @@ -27,14 +27,13 @@ import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; import com.realtimetech.opack.value.OpackValue; -import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.io.File; public class FileTransformTest { public static class FileTransformClass { - private @NotNull File file; + private File file; public FileTransformClass() { this.file = new File("src/test/java/com/realtimetech/opack/test/opacker/transform/FileTransformTest.java"); @@ -47,6 +46,7 @@ public void test() throws SerializeException, DeserializeException, OpackAssert. FileTransformClass originalObject = new FileTransformClass(); OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; FileTransformClass deserialized = opacker.deserialize(FileTransformClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java index 2df0761..1d5c4b4 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java @@ -27,14 +27,13 @@ import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; import com.realtimetech.opack.value.OpackValue; -import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.nio.file.Path; public class PathTransformTest { public static class PathTransformClass { - private @NotNull Path path; + private Path path; public PathTransformClass() { this.path = Path.of("src/test/java/com/realtimetech/opack/test/opacker/transform/FileTransformTest.java"); @@ -47,6 +46,7 @@ public void test() throws SerializeException, DeserializeException, OpackAssert. PathTransformClass originalObject = new PathTransformClass(); OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; PathTransformClass deserialized = opacker.deserialize(PathTransformClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java index fbc17db..ad0ba9a 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java @@ -27,7 +27,6 @@ import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; import com.realtimetech.opack.value.OpackValue; -import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.time.LocalDate; @@ -36,9 +35,9 @@ public class Java8TimeTransformTest { public static class Java8TimeTransformClass { - private @NotNull LocalDate localDate; - private @NotNull LocalTime localTime; - private @NotNull LocalDateTime localDateTime; + private LocalDate localDate; + private LocalTime localTime; + private LocalDateTime localDateTime; public Java8TimeTransformClass() { this.localDate = LocalDate.now(); @@ -53,6 +52,7 @@ public void test() throws SerializeException, DeserializeException, OpackAssert. Java8TimeTransformClass originalObject = new Java8TimeTransformClass(); OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; Java8TimeTransformClass deserialized = opacker.deserialize(Java8TimeTransformClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java index 03c3905..b359580 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java @@ -27,7 +27,6 @@ import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; import com.realtimetech.opack.value.OpackValue; -import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.util.Calendar; @@ -35,8 +34,8 @@ public class TimeTransformTest { public static class TimeTransformClass { - private @NotNull Date date; - private @NotNull Calendar calendar; + private Date date; + private Calendar calendar; public TimeTransformClass() { this.date = new Date(); @@ -52,6 +51,7 @@ public void test() throws SerializeException, DeserializeException, OpackAssert. TimeTransformClass originalObject = new TimeTransformClass(); OpackValue serialized = opacker.serialize(originalObject); + assert serialized != null; TimeTransformClass deserialized = opacker.deserialize(TimeTransformClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized);