Skip to content

Commit

Permalink
Test that nulls are correctly serialised
Browse files Browse the repository at this point in the history
  • Loading branch information
badgerwithagun committed Dec 22, 2023
1 parent 6068624 commit 3e1f367
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
3 changes: 2 additions & 1 deletion transactionoutbox-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>transactionoutbox-parent</artifactId>
<groupId>com.gruelbox</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import java.io.StringWriter;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DynamicNode;
Expand All @@ -32,9 +29,10 @@ Stream<DynamicNode> versions() {
Stream.of(Arguments.of(1), Arguments.of(2), Arguments.of(new Object[] {null})));
}

@SuppressWarnings("JUnitMalformedDeclaration")
static class Inner {

private DefaultInvocationSerializer serializer;
private final DefaultInvocationSerializer serializer;

Inner(Integer version) {
this.serializer =
Expand Down Expand Up @@ -121,10 +119,17 @@ void testJavaDateEnums() {
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

@Test
void testJavaDateEnumsNulls() {
Class<?>[] primitives = {DayOfWeek.class, Month.class, ChronoUnit.class};
Object[] values = {null, null, null};
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

@Test
void testJavaUtilDate() {
Class<?>[] primitives = {Date.class};
Object[] values = {new Date()};
Class<?>[] primitives = {Date.class, Date.class};
Object[] values = {new Date(), null};
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

Expand All @@ -135,7 +140,6 @@ void testJavaTimeClasses() {
Instant.class,
LocalDate.class,
LocalDateTime.class,
LocalDateTime.class,
MonthDay.class,
Period.class,
Year.class,
Expand All @@ -147,7 +151,6 @@ void testJavaTimeClasses() {
Instant.now(),
LocalDate.now(),
LocalDateTime.now(),
null,
MonthDay.of(1, 1),
Period.ofMonths(1),
Year.now(),
Expand All @@ -157,13 +160,37 @@ void testJavaTimeClasses() {
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

@Test
void testJavaTimeClassesNulls() {
Class<?>[] primitives = {
Duration.class,
Instant.class,
LocalDate.class,
LocalDateTime.class,
MonthDay.class,
Period.class,
Year.class,
YearMonth.class,
ZonedDateTime.class
};
Object[] values = new Object[9];
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

@Test
void testCustomEnum() {
Class<?>[] primitives = {ExampleCustomEnum.class, ExampleCustomEnum.class};
Object[] values = {ExampleCustomEnum.ONE, ExampleCustomEnum.TWO};
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

@Test
void testCustomEnumNulls() {
Class<?>[] primitives = {ExampleCustomEnum.class};
Object[] values = {null};
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

@Test
void testCustomComplexClass() {
Class<?>[] primitives = {ExampleCustomClass.class, ExampleCustomClass.class};
Expand All @@ -188,6 +215,13 @@ void testUUID() {
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

@Test
void testUUIDNull() {
Class<?>[] primitives = {UUID.class};
Object[] values = {null};
check(new Invocation(CLASS_NAME, METHOD_NAME, primitives, values));
}

void check(Invocation invocation) {
Invocation deserialized = serdeser(invocation);
Assertions.assertEquals(deserialized, serdeser(invocation));
Expand All @@ -197,7 +231,7 @@ void check(Invocation invocation) {
Invocation serdeser(Invocation invocation) {
var writer = new StringWriter();
serializer.serializeInvocation(invocation, writer);
log.info("Serialised as: {}", writer.toString());
log.info("Serialised as: {}", writer);
return serializer.deserializeInvocation(new StringReader(writer.toString()));
}
}
Expand All @@ -207,6 +241,7 @@ enum ExampleCustomEnum {
TWO
}

@Getter
static class ExampleCustomClass {

private final String arg1;
Expand All @@ -217,14 +252,6 @@ static class ExampleCustomClass {
this.arg2 = arg2;
}

public String getArg1() {
return arg1;
}

public String getArg2() {
return arg2;
}

@Override
public String toString() {
return "ExampleCustomClass{" + "arg1='" + arg1 + '\'' + ", arg2='" + arg2 + '\'' + '}';
Expand Down

0 comments on commit 3e1f367

Please sign in to comment.