Skip to content

Package: TypeInfoExceptionsTest$InvalidAlias

TypeInfoExceptionsTest$InvalidAlias

nameinstructionbranchcomplexitylinemethod
TypeInfoExceptionsTest.InvalidAlias()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 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 jakarta.json.bind.Jsonb;
20: import jakarta.json.bind.JsonbBuilder;
21: import jakarta.json.bind.JsonbException;
22: import jakarta.json.bind.annotation.JsonbSubtype;
23: import jakarta.json.bind.annotation.JsonbTypeInfo;
24:
25: import org.junit.jupiter.api.Test;
26:
27: import static org.junit.jupiter.api.Assertions.assertThrows;
28:
29: public class TypeInfoExceptionsTest {
30:
31: private final Jsonb jsonb = JsonbBuilder.create();
32:
33: @Test
34: public void testSerializeTypeInfoMultiInheritance() {
35: assertThrows(JsonbException.class, () -> jsonb.toJson(new Dog()),
36: "Serialization of @JsonbTypeInfo multi inheritance is not supported.");
37: }
38:
39: @Test
40: public void testDeserializeTypeInfoMultiInheritance() {
41: assertThrows(JsonbException.class, () -> jsonb.fromJson("{\"@animal\":\"dog\",\"@livingThing\":\"dog\"}", Dog.class),
42: "Deserialization of @JsonbTypeInfo multi inheritance is not supported.");
43: }
44:
45: @Test
46: public void testInvalidAlias() {
47: assertThrows(JsonbException.class, () -> jsonb.toJson(new InvalidAlias()),
48: "Serialization should have failed since set alias is not subtype of the class it is defined on.");
49: }
50:
51: @Test
52: public void testNameCollision() {
53: assertThrows(JsonbException.class, () -> jsonb.toJson(new PropertyNameCollision()),
54: "Serialization of the type information to the property with the name which collides "
55: + "with the class property, is not supported");
56: }
57:
58: //--------------
59:
60: @JsonbTypeInfo(key = "@animal", value = {
61: @JsonbSubtype(alias = "dog", type = Dog.class)
62: })
63: public interface Animal { }
64:
65: @JsonbTypeInfo(key = "@livingThing", value = {
66: @JsonbSubtype(alias = "dog", type = Dog.class)
67: })
68: public interface LivingEntity { }
69:
70: public static final class Dog implements Animal, LivingEntity { }
71:
72: //--------------
73:
74: @JsonbTypeInfo(key = "keyName", value = {
75: @JsonbSubtype(alias = "test", type = PropertyNameCollision.class)
76: })
77: public static class PropertyNameCollision {
78: public String keyName;
79: }
80:
81: //--------------
82:
83: @JsonbTypeInfo({
84: @JsonbSubtype(alias = "integer", type = Integer.class),
85: @JsonbSubtype(alias = "invalid", type = InvalidAlias.class)
86: })
87: public static class InvalidAlias { }
88:
89: }