Skip to content

Package: AnnotationTypeInfoTest$DateConstructor

AnnotationTypeInfoTest$DateConstructor

nameinstructionbranchcomplexitylinemethod
AnnotationTypeInfoTest.DateConstructor(LocalDate)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package ee.jakarta.tck.json.bind.defaultmapping.polymorphictypes;
18:
19: import java.time.LocalDate;
20:
21: import jakarta.json.bind.Jsonb;
22: import jakarta.json.bind.JsonbBuilder;
23: import jakarta.json.bind.JsonbException;
24: import jakarta.json.bind.annotation.JsonbCreator;
25: import jakarta.json.bind.annotation.JsonbDateFormat;
26: import jakarta.json.bind.annotation.JsonbProperty;
27: import jakarta.json.bind.annotation.JsonbSubtype;
28: import jakarta.json.bind.annotation.JsonbTypeInfo;
29:
30: import org.junit.jupiter.api.Test;
31:
32: import static org.hamcrest.MatcherAssert.assertThat;
33: import static org.hamcrest.Matchers.arrayWithSize;
34: import static org.hamcrest.Matchers.instanceOf;
35: import static org.hamcrest.Matchers.is;
36: import static org.hamcrest.Matchers.matchesPattern;
37: import static org.junit.jupiter.api.Assertions.assertThrows;
38:
39: /**
40: * Tests for verification of proper type inheritance handling based on annotation with property format.
41: */
42: public class AnnotationTypeInfoTest {
43:
44: private final Jsonb jsonb = JsonbBuilder.create();
45:
46: @Test
47: public void testBasicSerialization() {
48: Dog dog = new Dog();
49: String jsonString = jsonb.toJson(dog);
50: assertThat("Failed to serialize Dog class correctly.",
51: jsonString, matchesPattern("\\{\\s*\"@type\"\\s*:\\s*\"dog\"\\s*,\\s*\"isDog\"\\s*:\\s*true\\s*\\}"));
52:
53: Cat cat = new Cat();
54: jsonString = jsonb.toJson(cat);
55: assertThat("Failed to serialize Cat class correctly",
56: jsonString, matchesPattern("\\{\\s*\"@type\"\\s*:\\s*\"cat\"\\s*,\\s*\"isCat\"\\s*:\\s*true\\s*\\}"));
57: }
58:
59: @Test
60: public void testBasicDeserialization() {
61: Animal dog = jsonb.fromJson("{\"@type\":\"dog\",\"isDog\":false}", Animal.class);
62: assertThat("Incorrectly deserialized to the type. Expected was Dog instance. Got instance of class " + dog.getClass(),
63: dog, instanceOf(Dog.class));
64: assertThat("Incorrectly deserialized field of the Dog instance. Field \"isDog\" should have been false.",
65: ((Dog) dog).isDog, is(false));
66:
67: Animal cat = jsonb.fromJson("{\"@type\":\"cat\",\"isCat\":false}", Animal.class);
68: assertThat("Incorrectly deserialized to the type. Expected was Cat instance. Got instance of class " + cat.getClass(),
69: cat, instanceOf(Cat.class));
70: assertThat("Incorrectly deserialized field of the Cat instance. Field \"isCat\" should have been false.",
71: ((Cat) cat).isCat, is(false));
72: }
73:
74: @Test
75: public void testUnknownAliasDeserialization() {
76: assertThrows(JsonbException.class,
77: () -> jsonb.fromJson("{\"@type\":\"rat\",\"isRat\":false}", Animal.class),
78: "Deserialization should fail. Alias \"rat\" is not valid alias of the class Animal.");
79: }
80:
81: @Test
82: public void testCreatorDeserialization() {
83: SomeDateType deserialized = jsonb.fromJson("{\"@dateType\":\"constructor\",\"localDate\":\"26-02-2021\"}",
84: SomeDateType.class);
85: assertThat("Incorrectly deserialized according to the type information. Expected was DateConstructor instance. "
86: + "Got instance of class " + deserialized.getClass(),
87: deserialized, instanceOf(DateConstructor.class));
88: }
89:
90: @Test
91: public void testArraySerialization() {
92: String expected = "\\["
93: + "\\s*\\{\\s*\"@type\"\\s*:\\s*\"dog\"\\s*,\\s*\"isDog\"\\s*:\\s*true\\s*\\}\\s*,"
94: + "\\s*\\{\\s*\"@type\"\\s*:\\s*\"cat\"\\s*,\\s*\"isCat\"\\s*:\\s*true\\s*\\}\\s*,"
95: + "\\s*\\{\\s*\"@type\"\\s*:\\s*\"dog\"\\s*,\\s*\"isDog\"\\s*:\\s*true\\s*\\}\\s*"
96: + "\\]";
97: Animal[] animals = new Animal[] {new Dog(), new Cat(), new Dog()};
98: String jsonString = jsonb.toJson(animals);
99: assertThat("Array values were not properly serialized with type information.",
100: jsonString, matchesPattern(expected));
101: }
102:
103: @Test
104: public void testArrayDeserialization() {
105: String array = "[{\"@type\":\"dog\",\"isDog\":true},{\"@type\":\"cat\",\"isCat\":true},"
106: + "{\"@type\":\"dog\",\"isDog\":true}]";
107: Animal[] deserialized = jsonb.fromJson(array, Animal[].class);
108: assertThat("Array should have exactly 3 values.", deserialized, arrayWithSize(3));
109: assertThat("Array value at index 0 was incorrectly deserialized according to the type information. "
110: + "Expected was Dog instance. Got instance of class " + deserialized[0].getClass(),
111: deserialized[0], instanceOf(Dog.class));
112: assertThat("Array value at index 1 was incorrectly deserialized according to the type information. "
113: + "Expected was Cat instance. Got instance of class " + deserialized[1].getClass(),
114: deserialized[1], instanceOf(Cat.class));
115: assertThat("Array value at index 2 was incorrectly deserialized according to the type information. "
116: + "Expected was Dog instance. Got instance of class " + deserialized[2].getClass(),
117: deserialized[2], instanceOf(Dog.class));
118: }
119:
120: @JsonbTypeInfo({
121: @JsonbSubtype(alias = "dog", type = Dog.class),
122: @JsonbSubtype(alias = "cat", type = Cat.class),
123: @JsonbSubtype(alias = "elephant", type = Elephant.class)
124: })
125: public interface Animal {
126:
127: }
128:
129: public static class Dog implements Animal {
130:
131: public boolean isDog = true;
132:
133: }
134:
135: public static class Cat implements Animal {
136:
137: public boolean isCat = true;
138:
139: }
140:
141: public static class Elephant implements Animal {
142:
143: public boolean isElephant = true;
144: public String testProperty = "value";
145:
146: }
147:
148: @JsonbTypeInfo(key = "@dateType", value = {
149: @JsonbSubtype(alias = "constructor", type = DateConstructor.class)
150: })
151: public interface SomeDateType {
152:
153: }
154:
155: public static final class DateConstructor implements SomeDateType {
156:
157: public LocalDate localDate;
158:
159: @JsonbCreator
160: public DateConstructor(@JsonbProperty("localDate") @JsonbDateFormat(value = "dd-MM-yyyy", locale = "nl-NL") LocalDate localDate) {
161: this.localDate = localDate;
162: }
163:
164: }
165:
166: }