Skip to content

Package: GenericsMappingTest$1

GenericsMappingTest$1

nameinstructionbranchcomplexitylinemethod
{...}
M: 9 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) 2017, 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: /*
18: * $Id$
19: */
20:
21: package ee.jakarta.tck.json.bind.defaultmapping.generics;
22:
23: import java.lang.reflect.Type;
24: import java.util.ArrayList;
25: import java.util.Arrays;
26: import java.util.LinkedList;
27: import java.util.List;
28: import java.util.Map;
29:
30: import jakarta.json.bind.Jsonb;
31: import jakarta.json.bind.JsonbBuilder;
32:
33: import ee.jakarta.tck.json.bind.defaultmapping.generics.model.CollectionContainer;
34: import ee.jakarta.tck.json.bind.defaultmapping.generics.model.GenericContainer;
35: import ee.jakarta.tck.json.bind.defaultmapping.generics.model.MultipleBoundsContainer;
36: import ee.jakarta.tck.json.bind.defaultmapping.generics.model.NumberContainer;
37: import ee.jakarta.tck.json.bind.defaultmapping.generics.model.StringContainer;
38: import ee.jakarta.tck.json.bind.defaultmapping.generics.model.WildcardContainer;
39: import org.junit.jupiter.api.Test;
40:
41: import static org.hamcrest.MatcherAssert.assertThat;
42: import static org.hamcrest.Matchers.hasEntry;
43: import static org.hamcrest.Matchers.instanceOf;
44: import static org.hamcrest.Matchers.is;
45: import static org.hamcrest.Matchers.matchesPattern;
46:
47: /**
48: * @test
49: * @sources GenericsMappingTest.java
50: * @executeClass com.sun.ts.tests.jsonb.defaultmapping.generics.GenericsMappingTest
51: **/
52: public class GenericsMappingTest {
53:
54: private final Jsonb jsonb = JsonbBuilder.create();
55:
56: /*
57: * @testName: testClassInformationOnRuntime
58: *
59: * @assertion_ids: JSONB:SPEC:JSB-3.17-1; JSONB:SPEC:JSB-3.17-3;
60: * JSONB:SPEC:JSB-3.17.1-1; JSONB:SPEC:JSB-3.17.1-15
61: *
62: * @test_Strategy: Assert that passing Type information on runtime is handled
63: * as expected
64: */
65: @Test
66: public void testClassInformationOnRuntime() {
67: String jsonString = jsonb.toJson(new GenericContainer<String>() {{
68: setInstance("Test String");
69: }});
70: assertThat("Failed to marshal generic object with String attribute value.",
71: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*\\:\\s*\"Test String\"\\s*\\}"));
72:
73: Type runtimeType = new GenericContainer<String>() { }.getClass().getGenericSuperclass();
74: GenericContainer<String> unmarshalledObject = jsonb.fromJson("{ \"instance\" : \"Test String\" }", runtimeType);
75: assertThat("Failed to unmarshal generic object with String attribute value.",
76: unmarshalledObject.getInstance(), is("Test String"));
77: }
78:
79: /*
80: * @testName: testClassFileAvailable
81: *
82: * @assertion_ids: JSONB:SPEC:JSB-3.17-1; JSONB:SPEC:JSB-3.17-2;
83: * JSONB:SPEC:JSB-3.17.1-1; JSONB:SPEC:JSB-3.17.1-5; JSONB:SPEC:JSB-3.17.1-6
84: *
85: * @test_Strategy: Assert that static type information is handled as expected
86: */
87: @Test
88: public void testClassFileAvailable() {
89: String jsonString = jsonb.toJson(new GenericContainer<String>() {{
90: setInstance("Test String");
91: }});
92: assertThat("Failed to marshal generic object with String attribute value.",
93: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*\\:\\s*\"Test String\"\\s*\\}"));
94:
95: GenericContainer<String> unmarshalledObject = jsonb.fromJson("{ \"instance\" : \"Test String\" }", StringContainer.class);
96: assertThat("Failed to unmarshal generic object with String attribute value.",
97: unmarshalledObject.getInstance(), is("Test String"));
98: }
99:
100: /*
101: * @testName: testRawTypeInformation
102: *
103: * @assertion_ids: JSONB:SPEC:JSB-3.17-1; JSONB:SPEC:JSB-3.17.1-1;
104: * JSONB:SPEC:JSB-3.17.1-3; JSONB:SPEC:JSB-3.17.1-4; JSONB:SPEC:JSB-3.17.1-8
105: *
106: * @test_Strategy: Assert that raw type information is handled as expected
107: */
108: @Test
109: public void testRawTypeInformation() {
110: final List<String> list = Arrays.asList("Test 1", "Test 2");
111: String jsonString = jsonb.toJson(new CollectionContainer() {{
112: setInstance(list);
113: }});
114: assertThat("Failed to marshal object with raw List attribute.",
115: jsonString,
116: matchesPattern("\\{\\s*\"instance\"\\s*\\:\\s*\\[\\s*\"Test 1\"\\s*,\\s*\"Test 2\"\\s*\\]\\s*\\}"));
117:
118: CollectionContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : [ \"Test 1\", \"Test 2\" ] }",
119: CollectionContainer.class);
120: assertThat("Failed to unmarshal object with raw List type attribute.",
121: unmarshalledObject.getInstance(), is(list));
122: }
123:
124: /*
125: * @testName: testNoTypeInformation
126: *
127: * @assertion_ids: JSONB:SPEC:JSB-3.17-1; JSONB:SPEC:JSB-3.17.1-1;
128: * JSONB:SPEC:JSB-3.17.1-2; JSONB:SPEC:JSB-3.17.1-9; JSONB:SPEC:JSB-3.17.1-14
129: *
130: * @test_Strategy: Assert that if no type information is provided, type is
131: * treated as java.lang.Object
132: */
133: @SuppressWarnings("unchecked")
134: @Test
135: public void testNoTypeInformation() {
136: String jsonString = jsonb.toJson(new GenericContainer<String>() {{
137: setInstance("Test String");
138: }});
139: assertThat("Failed to marshal generic object with String attribute value.",
140: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*\\:\\s*\"Test String\"\\s*\\}"));
141:
142: GenericContainer<?> unmarshalledObject = jsonb.fromJson("{ \"instance\" : {\"value\":\"Test String\" } }",
143: GenericContainer.class);
144: String validationMessage = "Failed to unmarshal generic object without type information with String attribute value.";
145: Object evaluatedInstance = unmarshalledObject.getInstance();
146: assertThat(validationMessage, evaluatedInstance, instanceOf(Map.class));
147: Map<String, Object> map = (Map<String, Object>) evaluatedInstance;
148: assertThat(validationMessage, map.size(), is(1));
149: assertThat(validationMessage, map, hasEntry("value", "Test String"));
150: }
151:
152: /*
153: * @testName: testBoundedTypeInformation
154: *
155: * @assertion_ids: JSONB:SPEC:JSB-3.17-1; JSONB:SPEC:JSB-3.17.1-5;
156: * JSONB:SPEC:JSB-3.17.1-7
157: *
158: * @test_Strategy: Assert that bounded type information is treated as expected
159: */
160: @Test
161: public void testBoundedTypeInformation() {
162: String jsonString = jsonb.toJson(new NumberContainer<Integer>() {{
163: setInstance(Integer.MAX_VALUE);
164: }});
165: assertThat("Failed to marshal object with bounded Number attribute.",
166: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*\\:\\s*" + Integer.MAX_VALUE + "\\s*\\}"));
167: Type runtimeType = new NumberContainer<Integer>() { }.getClass().getGenericSuperclass();
168: NumberContainer<Integer> unmarshalledObject = jsonb.fromJson("{ \"instance\" : " + Integer.MAX_VALUE + " }", runtimeType);
169: assertThat("Failed to unmarshal object with bounded Number attribute.",
170: unmarshalledObject.getInstance(), is(Integer.MAX_VALUE));
171: }
172:
173: /*
174: * @testName: testMultipleBoundsTypeInformation
175: *
176: * @assertion_ids: JSONB:SPEC:JSB-3.17-1; JSONB:SPEC:JSB-3.17.1-5;
177: * JSONB:SPEC:JSB-3.17.1-7; JSONB:SPEC:JSB-3.17.1-10;
178: * JSONB:SPEC:JSB-3.17.1-11; JSONB:SPEC:JSB-3.17.1-12
179: *
180: * @test_Strategy: Assert that when multiple bounds exist, the most specific
181: * type is used
182: */
183: @Test
184: public void testMultipleBoundsTypeInformation() {
185: final LinkedList<String> list = new LinkedList<>(Arrays.asList("Test 1", "Test 2"));
186: MultipleBoundsContainer<LinkedList<String>> container = new MultipleBoundsContainer<>();
187: container.setInstance(new ArrayList<>());
188: container.getInstance().add(list);
189:
190: final Type type = new MultipleBoundsContainer<LinkedList<String>>() { }.getClass().getGenericSuperclass();
191: String jsonString = jsonb.toJson(container, type);
192: assertThat("Failed to marshal object with multiple bounded attribute.",
193: jsonString,
194: matchesPattern("\\{\\s*\"instance\"\\s*\\:\\s*\\[\\[\\s*\"Test 1\"\\s*,\\s*\"Test 2\"\\s*\\]\\]\\s*\\}"));
195:
196: String toDeserialize = "{ \"instance\" : [[ \"Test 1\", \"Test 2\" ]] }";
197: MultipleBoundsContainer<LinkedList<String>> unmarshalledObject = jsonb.fromJson(toDeserialize, type);
198: assertThat("Failed to unmarshal object with multiple bounded attribute.",
199: unmarshalledObject.getInstance(), is(container.getInstance()));
200: }
201:
202: /*
203: * @testName: testWildcardTypeInformation
204: *
205: * @assertion_ids: JSONB:SPEC:JSB-3.17-1; JSONB:SPEC:JSB-3.17.1-5;
206: * JSONB:SPEC:JSB-3.17.1-13
207: *
208: * @test_Strategy: Assert that wildcard type is handled as java.lang.Object
209: */
210: @Test
211: public void testWildcardTypeInformation() {
212: final List<String> list = Arrays.asList("Test 1", "Test 2");
213: String jsonString = jsonb.toJson(new WildcardContainer() {{
214: setInstance(list);
215: }});
216: assertThat("Failed to marshal object with unbound collection attribute.",
217: jsonString,
218: matchesPattern("\\{\\s*\"instance\"\\s*\\:\\s*\\[\\s*\"Test 1\"\\s*,\\s*\"Test 2\"\\s*\\]\\s*\\}"));
219:
220: WildcardContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : [ \"Test 1\", \"Test 2\" ] }",
221: WildcardContainer.class);
222: assertThat("Failed to unmarshal object with unbound collection attribute.",
223: unmarshalledObject.getInstance(), is(list));
224: }
225: }