From d2ed9244eaf058466145bb6924703a0e2a225b70 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 30 Nov 2023 23:25:49 +1300 Subject: [PATCH] #189 - Add blackbox tests for Json.Creator --- blackbox-test/pom.xml | 4 +-- .../example/customer/creator/Kingfisher.java | 22 +++++++++++++ .../customer/creator/KingfisherMixin.java | 15 +++++++++ .../creator/StudentViaConstructor.java | 32 +++++++++++++++++++ .../creator/StudentViaStaticMethod.java | 31 ++++++++++++++++++ .../customer/creator/KingFisherTest.java | 24 ++++++++++++++ .../creator/StudentViaConstructorTest.java | 24 ++++++++++++++ .../creator/StudentViaStaticMethodTest.java | 23 +++++++++++++ 8 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 blackbox-test/src/main/java/org/example/customer/creator/Kingfisher.java create mode 100644 blackbox-test/src/main/java/org/example/customer/creator/KingfisherMixin.java create mode 100644 blackbox-test/src/main/java/org/example/customer/creator/StudentViaConstructor.java create mode 100644 blackbox-test/src/main/java/org/example/customer/creator/StudentViaStaticMethod.java create mode 100644 blackbox-test/src/test/java/org/example/customer/creator/KingFisherTest.java create mode 100644 blackbox-test/src/test/java/org/example/customer/creator/StudentViaConstructorTest.java create mode 100644 blackbox-test/src/test/java/org/example/customer/creator/StudentViaStaticMethodTest.java diff --git a/blackbox-test/pom.xml b/blackbox-test/pom.xml index cc7371c2..3d90d40f 100644 --- a/blackbox-test/pom.xml +++ b/blackbox-test/pom.xml @@ -28,7 +28,7 @@ io.avaje avaje-jsonb - 1.10-RC1 + 1.10-RC2 @@ -47,7 +47,7 @@ io.avaje avaje-jsonb-generator - 1.10-RC1 + 1.10-RC2 provided diff --git a/blackbox-test/src/main/java/org/example/customer/creator/Kingfisher.java b/blackbox-test/src/main/java/org/example/customer/creator/Kingfisher.java new file mode 100644 index 00000000..600baadb --- /dev/null +++ b/blackbox-test/src/main/java/org/example/customer/creator/Kingfisher.java @@ -0,0 +1,22 @@ +package org.example.customer.creator; + +public class Kingfisher { + private final String name; + private int fishCaught; + + public Kingfisher(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public int getFishCaught() { + return fishCaught; + } + + public void setFishCaught(int fishCaught) { + this.fishCaught = fishCaught; + } +} diff --git a/blackbox-test/src/main/java/org/example/customer/creator/KingfisherMixin.java b/blackbox-test/src/main/java/org/example/customer/creator/KingfisherMixin.java new file mode 100644 index 00000000..55c55eeb --- /dev/null +++ b/blackbox-test/src/main/java/org/example/customer/creator/KingfisherMixin.java @@ -0,0 +1,15 @@ +package org.example.customer.creator; + +import io.avaje.jsonb.Json; +import io.avaje.jsonb.Json.MixIn; + +@MixIn(Kingfisher.class) +public interface KingfisherMixin { + + @Json.Creator + static Kingfisher construct(String name) { + var kf = new Kingfisher(name); + kf.setFishCaught(42); + return kf; + } +} diff --git a/blackbox-test/src/main/java/org/example/customer/creator/StudentViaConstructor.java b/blackbox-test/src/main/java/org/example/customer/creator/StudentViaConstructor.java new file mode 100644 index 00000000..a38f1bdc --- /dev/null +++ b/blackbox-test/src/main/java/org/example/customer/creator/StudentViaConstructor.java @@ -0,0 +1,32 @@ +package org.example.customer.creator; + +import io.avaje.jsonb.Json; + +@Json +public class StudentViaConstructor { + private final String name; + private int rollNo; + + @Json.Creator + public StudentViaConstructor(@Json.Alias("theName") String name, long rolling) { + this.name = name; + this.rollNo = name.length(); + } + + public StudentViaConstructor(String name, int rollNo) { + this.name = name; + this.rollNo = rollNo; + } + + public String getName() { + return name; + } + + public int getRollNo() { + return rollNo; + } + + public void setRollNo(int rollNo) { + this.rollNo = rollNo; + } +} diff --git a/blackbox-test/src/main/java/org/example/customer/creator/StudentViaStaticMethod.java b/blackbox-test/src/main/java/org/example/customer/creator/StudentViaStaticMethod.java new file mode 100644 index 00000000..001d577e --- /dev/null +++ b/blackbox-test/src/main/java/org/example/customer/creator/StudentViaStaticMethod.java @@ -0,0 +1,31 @@ +package org.example.customer.creator; + +import io.avaje.jsonb.Json; + +@Json +public class StudentViaStaticMethod { + private final String name; + private int rollNo; + + @Json.Creator + public static StudentViaStaticMethod create(@Json.Alias("theName") String name, long rolling) { + return new StudentViaStaticMethod(name, name.length()); + } + + public StudentViaStaticMethod(String name, int rollNo) { + this.name = name; + this.rollNo = rollNo; + } + + public String getName() { + return name; + } + + public int getRollNo() { + return rollNo; + } + + public void setRollNo(int rollNo) { + this.rollNo = rollNo; + } +} diff --git a/blackbox-test/src/test/java/org/example/customer/creator/KingFisherTest.java b/blackbox-test/src/test/java/org/example/customer/creator/KingFisherTest.java new file mode 100644 index 00000000..4a1e4392 --- /dev/null +++ b/blackbox-test/src/test/java/org/example/customer/creator/KingFisherTest.java @@ -0,0 +1,24 @@ +package org.example.customer.creator; + +import io.avaje.jsonb.Jsonb; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class KingFisherTest { + + Jsonb jsonb = Jsonb.builder().build(); + + @Test + void asJson() { + Kingfisher kf = new Kingfisher("hi"); + kf.setFishCaught(90); + + String asJson = jsonb.toJson(kf); + assertThat(asJson).isEqualTo("{\"name\":\"hi\",\"fishCaught\":90}"); + + Kingfisher fromJson = jsonb.type(Kingfisher.class).fromJson(asJson); + assertThat(fromJson.getName()).isEqualTo("hi"); + assertThat(fromJson.getFishCaught()).isEqualTo(42); + } +} diff --git a/blackbox-test/src/test/java/org/example/customer/creator/StudentViaConstructorTest.java b/blackbox-test/src/test/java/org/example/customer/creator/StudentViaConstructorTest.java new file mode 100644 index 00000000..a33a013e --- /dev/null +++ b/blackbox-test/src/test/java/org/example/customer/creator/StudentViaConstructorTest.java @@ -0,0 +1,24 @@ +package org.example.customer.creator; + +import io.avaje.jsonb.Jsonb; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class StudentViaConstructorTest { + + Jsonb jsonb = Jsonb.builder().build(); + + @Test + void asJson() { + StudentViaConstructor stu = new StudentViaConstructor("one", 1); + + String asJson = jsonb.toJson(stu); + assertThat(asJson).isEqualTo("{\"name\":\"one\",\"rollNo\":1}"); + + StudentViaConstructor fromJson = jsonb.type(StudentViaConstructor.class).fromJson(asJson); + assertThat(fromJson.getName()).isEqualTo("one"); + assertThat(fromJson.getRollNo()).isEqualTo(3); // the length of name + } +} diff --git a/blackbox-test/src/test/java/org/example/customer/creator/StudentViaStaticMethodTest.java b/blackbox-test/src/test/java/org/example/customer/creator/StudentViaStaticMethodTest.java new file mode 100644 index 00000000..61233b7e --- /dev/null +++ b/blackbox-test/src/test/java/org/example/customer/creator/StudentViaStaticMethodTest.java @@ -0,0 +1,23 @@ +package org.example.customer.creator; + +import io.avaje.jsonb.Jsonb; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class StudentViaStaticMethodTest { + + Jsonb jsonb = Jsonb.builder().build(); + + @Test + void asJson() { + StudentViaStaticMethod stu = new StudentViaStaticMethod("one", 1); + + String asJson = jsonb.toJson(stu); + assertThat(asJson).isEqualTo("{\"name\":\"one\",\"rollNo\":1}"); + + StudentViaStaticMethod fromJson = jsonb.type(StudentViaStaticMethod.class).fromJson(asJson); + assertThat(fromJson.getName()).isEqualTo("one"); + assertThat(fromJson.getRollNo()).isEqualTo(3); // the length of name + } +}