Skip to content

Commit

Permalink
Merge pull request #32 from devjeonghwan/main
Browse files Browse the repository at this point in the history
Add test case in AnnotationWithTypeTest.java and Reformat code
  • Loading branch information
devjeonghwan authored Aug 23, 2023
2 parents 9364c3e + 49336e7 commit cfb1304
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 33 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/realtimetech/opack/annotation/WithType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> opackArray = (OpackArray<Object>) object;

if (List.class.isAssignableFrom(goalType)) {
try {
List<Object> list = (List<Object>) ReflectionUtil.createInstance(goalType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double> listWithTransformer;
private List<Double> listWithTransformer;

@WithType
private List<Double>[] listArrayWithTransformer;

@WithType
private List<Double> listWithAnnotation;

@WithType
private List<ObjectClass> objectListWithTransformer;

@WithType
private @NotNull List<Double> @NotNull [] listArrayWithTransformer;
private List<ObjectClass>[] objectListArrayWithTransformer;

@WithType
private @NotNull List<Double> listWithAnnotation;
private List<ObjectClass> objectListWithAnnotation;

public WithTypeClass() {
this.listWithTransformer = new LinkedList<>();
Expand All @@ -54,30 +82,58 @@ 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<>();
}

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<Double> list) {
private void addItemsRandomly(List<Double> list) {
for (int index = 0; index < 32; index++) {
list.add(Math.random());
}
}

private void addObjectItemsRandomly(List<ObjectClass> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@
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;
import java.util.Date;

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();
Expand All @@ -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);
Expand Down

0 comments on commit cfb1304

Please sign in to comment.