Skip to content

Package: AdaptersCustomizationTest

AdaptersCustomizationTest

nameinstructionbranchcomplexitylinemethod
AdaptersCustomizationTest()
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%
testAdapterAnnotation()
M: 61 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
testAdapterConfiguration()
M: 73 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 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.customizedmapping.adapters;
22:
23: import jakarta.json.bind.Jsonb;
24: import jakarta.json.bind.JsonbBuilder;
25: import jakarta.json.bind.JsonbConfig;
26:
27: import ee.jakarta.tck.json.bind.customizedmapping.adapters.model.Animal;
28: import ee.jakarta.tck.json.bind.customizedmapping.adapters.model.AnimalShelter;
29: import ee.jakarta.tck.json.bind.customizedmapping.adapters.model.AnimalShelterAdapted;
30: import ee.jakarta.tck.json.bind.customizedmapping.adapters.model.Cat;
31: import ee.jakarta.tck.json.bind.customizedmapping.adapters.model.Dog;
32: import ee.jakarta.tck.json.bind.customizedmapping.adapters.model.adapter.AnimalAdapter;
33: import org.junit.jupiter.api.Test;
34:
35: import static org.hamcrest.MatcherAssert.assertThat;
36: import static org.hamcrest.Matchers.is;
37: import static org.hamcrest.Matchers.matchesPattern;
38:
39: /**
40: * @test
41: * @sources AdaptersCustomizationTest.java
42: * @executeClass com.sun.ts.tests.jsonb.customizedmapping.adapters.AdaptersCustomizationTest
43: **/
44: public class AdaptersCustomizationTest {
45:
46: private static final String PATTERN = "\\{\\s*\"animals\"\\s*:\\s*\\[\\s*"
47: + "\\{\\s*\"age\"\\s*:\\s*5\\s*,\\s*\"cuddly\"\\s*:\\s*true\\s*,\\s*\"furry\"\\s*:\\s*true\\s*,"
48: + "\\s*\"name\"\\s*:\\s*\"Garfield\"\\s*,\\s*\"type\"\\s*:\\s*\"CAT\"\\s*,\\s*\"weight\"\\s*:\\s*10.5\\s*}\\s*,\\s*"
49: + "\\{\\s*\"age\"\\s*:\\s*3\\s*,\\s*\"barking\"\\s*:\\s*true\\s*,\\s*\"furry\"\\s*:\\s*false\\s*,"
50: + "\\s*\"name\"\\s*:\\s*\"Milo\"\\s*,\\s*\"type\"\\s*:\\s*\"DOG\"\\s*,\\s*\"weight\"\\s*:\\s*5.5\\s*}\\s*,\\s*"
51: + "\\{\\s*\"age\"\\s*:\\s*6\\s*,\\s*\"furry\"\\s*:\\s*false\\s*,\\s*\"name\"\\s*:\\s*\"Tweety\"\\s*,"
52: + "\\s*\"type\"\\s*:\\s*\"GENERIC\"\\s*,\\s*\"weight\"\\s*:\\s*0.5\\s*}\\s*"
53: + "]\\s*}";
54:
55: /*
56: * @testName: testAdapterConfiguration
57: *
58: * @assertion_ids: JSONB:SPEC:JSB-4.7.1-1
59: *
60: * @test_Strategy: Assert that JSONB adapters can be configured using
61: * JsonbConfig.withAdapters and are working as expected
62: */
63: @Test
64: public void testAdapterConfiguration() {
65: Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new AnimalAdapter()));
66:
67: AnimalShelter animalShelter = new AnimalShelter();
68: animalShelter.addAnimal(new Cat(5, "Garfield", 10.5f, true, true));
69: animalShelter.addAnimal(new Dog(3, "Milo", 5.5f, false, true));
70: animalShelter.addAnimal(new Animal(6, "Tweety", 0.5f, false));
71:
72: String jsonString = jsonb.toJson(animalShelter);
73:
74: assertThat("Failed to correctly marshall complex type hierarchy using an adapter configured using "
75: + "JsonbConfig.withAdapters to a simpler class.",
76: jsonString, matchesPattern(PATTERN));
77:
78: String toSerializer = "{ \"animals\" : [ "
79: + "{ \"age\" : 5, \"cuddly\" : true, \"furry\" : true, \"name\" : \"Garfield\" , \"type\" : \"CAT\", \"weight\""
80: + " : 10.5}, "
81: + "{ \"age\" : 3, \"barking\" : true, \"furry\" : false, \"name\" : \"Milo\", \"type\" : \"DOG\", \"weight\" : "
82: + "5.5}, "
83: + "{ \"age\" : 6, \"furry\" : false, \"name\" : \"Tweety\", \"type\" : \"GENERIC\", \"weight\" : 0.5}"
84: + " ] }";
85: AnimalShelter unmarshalledObject = jsonb.fromJson(toSerializer, AnimalShelter.class);
86: assertThat("Failed to correctly unmarshall complex type hierarchy using an adapter configured using "
87: + "JsonbConfig.withAdapters to a simpler class.",
88: unmarshalledObject, is(animalShelter));
89: }
90:
91: /*
92: * @testName: testAdapterAnnotation
93: *
94: * @assertion_ids: JSONB:SPEC:JSB-4.7.1-2
95: *
96: * @test_Strategy: Assert that JSONB adapters can be configured using
97: * JsonbTypeAdapter annotation and are working as expected
98: */
99: @Test
100: public void testAdapterAnnotation() {
101: Jsonb jsonb = JsonbBuilder.create();
102: AnimalShelterAdapted animalShelter = new AnimalShelterAdapted();
103: animalShelter.addAnimal(new Cat(5, "Garfield", 10.5f, true, true));
104: animalShelter.addAnimal(new Dog(3, "Milo", 5.5f, false, true));
105: animalShelter.addAnimal(new Animal(6, "Tweety", 0.5f, false));
106:
107: String jsonString = jsonb.toJson(animalShelter);
108:
109: assertThat("Failed to correctly marshall complex type hierarchy using an adapter configured using "
110: + "JsonbTypeAdapter annotation to a simpler class.",
111: jsonString, matchesPattern(PATTERN));
112:
113: String toSerialize = "{ \"animals\" : [ "
114: + "{ \"age\" : 5, \"cuddly\" : true, \"furry\" : true, \"name\" : \"Garfield\" , \"type\" : \"CAT\", \"weight\""
115: + " : 10.5}, "
116: + "{ \"age\" : 3, \"barking\" : true, \"furry\" : false, \"name\" : \"Milo\", \"type\" : \"DOG\", \"weight\" : "
117: + "5.5}, "
118: + "{ \"age\" : 6, \"furry\" : false, \"name\" : \"Tweety\", \"type\" : \"GENERIC\", \"weight\" : 0.5}"
119: + " ] }";
120: AnimalShelterAdapted unmarshalledObject = jsonb.fromJson(toSerialize, AnimalShelterAdapted.class);
121: assertThat("Failed to correctly unmarshall complex type hierarchy using an adapter configured using "
122: + "JsonbTypeAdapter annotation to a simpler class.",
123: unmarshalledObject, is(animalShelter));
124: }
125: }