Skip to content

Package: MultipleTypeInfoTest$Animal

MultipleTypeInfoTest$Animal

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 jakarta.json.bind.Jsonb;
20: import jakarta.json.bind.JsonbBuilder;
21: import jakarta.json.bind.annotation.JsonbSubtype;
22: import jakarta.json.bind.annotation.JsonbTypeInfo;
23:
24: import org.junit.jupiter.api.Test;
25:
26: import static org.hamcrest.CoreMatchers.instanceOf;
27: import static org.hamcrest.MatcherAssert.assertThat;
28: import static org.hamcrest.Matchers.matchesPattern;
29:
30: public class MultipleTypeInfoTest {
31:
32: private static final Jsonb JSONB = JsonbBuilder.create();
33:
34: @Test
35: public void testMultipleTypeInfoPropertySerialization() {
36: String expected = "\\{\\s*\"@living\"\\s*:\\s*\"animal\"\\s*,"
37: + "\\s*\"@animal\"\\s*:\\s*\"dog\"\\s*,"
38: + "\\s*\"@dogRace\"\\s*:\\s*\"labrador\"\\s*,"
39: + "\\s*\"isLabrador\"\\s*:\\s*true\\s*\\}";
40: Labrador labrador = new Labrador();
41: assertThat(JSONB.toJson(labrador), matchesPattern(expected));
42: }
43:
44: @Test
45: public void testMultipleTypeInfoPropertyDeserialization() {
46: String json = "{\"@living\":\"animal\",\"@animal\":\"dog\",\"@dogRace\":\"labrador\",\"isLabrador\":true}";
47: assertThat(JSONB.fromJson(json, Labrador.class), instanceOf(Labrador.class));
48: }
49:
50: @Test
51: public void testSerializeMultipleTypeInfoInSingleChain() {
52: String expected = "\\{"
53: + "\\s*\"@machine\"\\s*:\\s*\"vehicle\"\\s*,"
54: + "\\s*\"@vehicle\"\\s*:\\s*\"car\"\\s*,"
55: + "\\s*\"machineProperty\"\\s*:\\s*\"machineProperty\"\\s*,"
56: + "\\s*\"vehicleProperty\"\\s*:\\s*\"vehicleProperty\"\\s*,"
57: + "\\s*\"carProperty\"\\s*:\\s*\"carProperty\"\\s*"
58: + "\\}";
59: Car car = new Car();
60: assertThat(JSONB.toJson(car), matchesPattern(expected));
61: }
62:
63: @Test
64: public void testDeserializeMultipleTypeInfoInSingleChain() {
65: String json = "{"
66: + "\"@machine\":\"vehicle\","
67: + "\"@vehicle\":\"car\","
68: + "\"machineProperty\":\"machineProperty\","
69: + "\"vehicleProperty\":\"vehicleProperty\","
70: + "\"carProperty\":\"carProperty\""
71: + "}";
72: Machine machine = JSONB.fromJson(json, Machine.class);
73: assertThat(machine, instanceOf(Car.class));
74: }
75:
76: @JsonbTypeInfo(key = "@living", value = {
77: @JsonbSubtype(alias = "animal", type = Animal.class)
78: })
79: public interface LivingThing { }
80:
81: @JsonbTypeInfo(key = "@animal", value = {
82: @JsonbSubtype(alias = "dog", type = Dog.class)
83: })
84: public interface Animal extends LivingThing {
85: }
86:
87: @JsonbTypeInfo(key = "@dogRace", value = {
88: @JsonbSubtype(alias = "labrador", type = Labrador.class)
89: })
90: public interface Dog extends Animal {
91: }
92:
93: public static class Labrador implements Dog {
94:
95: public boolean isLabrador = true;
96:
97: }
98:
99: @JsonbTypeInfo(key = "@machine", value = {
100: @JsonbSubtype(alias = "vehicle", type = Vehicle.class)
101: })
102: public static class Machine {
103: public String machineProperty = "machineProperty";
104: }
105:
106: @JsonbTypeInfo(key = "@vehicle", value = {
107: @JsonbSubtype(alias = "car", type = Car.class)
108: })
109: public static class Vehicle extends Machine {
110: public String vehicleProperty = "vehicleProperty";
111: }
112:
113: public static class Car extends Vehicle {
114: public String carProperty = "carProperty";
115: }
116: }