Skip to content

Package: NumberFormatCustomizationTest$1

NumberFormatCustomizationTest$1

nameinstructionbranchcomplexitylinemethod
{...}
M: 10 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.customizedmapping.numberformat;
22:
23: import jakarta.json.bind.Jsonb;
24: import jakarta.json.bind.JsonbBuilder;
25:
26: import ee.jakarta.tck.json.bind.customizedmapping.numberformat.model.AccessorCustomizedDoubleContainer;
27: import ee.jakarta.tck.json.bind.customizedmapping.numberformat.model.FieldCustomizedDoubleContainer;
28: import ee.jakarta.tck.json.bind.customizedmapping.numberformat.model.TypeCustomizedDoubleContainer;
29: import ee.jakarta.tck.json.bind.customizedmapping.numberformat.model.TypeCustomizedFieldOverriddenDoubleContainer;
30: import ee.jakarta.tck.json.bind.customizedmapping.numberformat.model.customized.PackageCustomizedDoubleContainer;
31: import ee.jakarta.tck.json.bind.customizedmapping.numberformat.model.customized.PackageCustomizedTypeOverriddenDoubleContainer;
32: import ee.jakarta.tck.json.bind.customizedmapping.numberformat.model.customized.PackageCustomizedTypeOverriddenFieldOverriddenDoubleContainer;
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 NumberFormatCustomizationTest.java
42: * @executeClass com.sun.ts.tests.jsonb.customizedmapping.numberformat.NumberFormatCustomizationTest
43: **/
44: public class NumberFormatCustomizationTest {
45:
46: private static final String FRENCH_NUMBER = "\"123\\u00a0456,789\"";
47:
48: private final Jsonb jsonb = JsonbBuilder.create();
49:
50: /*
51: * @testName: testNumberFormatPackage
52: *
53: * @assertion_ids: JSONB:SPEC:JSB-4.9-1
54: *
55: * @test_Strategy: Assert that package annotation with JsonbNumberFormat is
56: * correctly applied
57: */
58: @Test
59: public void testNumberFormatPackage() {
60: String jsonString = jsonb.toJson(new PackageCustomizedDoubleContainer() {{
61: setInstance(123456.789);
62: }});
63: assertThat("Failed to correctly customize number format during marshalling using JsonbNumberFormat annotation on "
64: + "package.",
65: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*:\\s*\"123.456,8\"\\s*\\}"));
66:
67: PackageCustomizedDoubleContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : \"123.456,789\" }",
68: PackageCustomizedDoubleContainer.class);
69:
70: assertThat(
71: "Failed to correctly customize number format during unmarshalling using JsonbNumberFormat annotation on package.",
72: unmarshalledObject.getInstance(),
73: is(123456.789));
74: }
75:
76: /*
77: * @testName: testNumberFormatType
78: *
79: * @assertion_ids: JSONB:SPEC:JSB-4.9-1
80: *
81: * @test_Strategy: Assert that type annotation with JsonbNumberFormat is
82: * correctly applied
83: */
84: @Test
85: public void testNumberFormatType() {
86: String jsonString = jsonb.toJson(new TypeCustomizedDoubleContainer() {{
87: setInstance(123456.789);
88: }});
89: assertThat("Failed to correctly customize number format during marshalling using JsonbNumberFormat annotation on type.",
90: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*:\\s*\"123,456.79\"\\s*\\}"));
91:
92: TypeCustomizedDoubleContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : \"123,456.789\" }",
93: TypeCustomizedDoubleContainer.class);
94: assertThat("Failed to correctly customize number format during unmarshalling using JsonbNumberFormat annotation on type.",
95: unmarshalledObject.getInstance(), is(123456.789));
96:
97: }
98:
99: /*
100: * @testName: testNumberFormatField
101: *
102: * @assertion_ids: JSONB:SPEC:JSB-4.9-1
103: *
104: * @test_Strategy: Assert that field annotation with JsonbNumberFormat is
105: * correctly applied
106: */
107: @Test
108: public void testNumberFormatField() {
109: String jsonString = jsonb.toJson(new FieldCustomizedDoubleContainer() {{
110: setInstance(123456.789);
111: }});
112: assertThat("Failed to correctly customize number format during marshalling using JsonbNumberFormat annotation on field.",
113: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*:\\s*\"123\\u00a0456,789\"\\s*\\}"));
114:
115: FieldCustomizedDoubleContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : " + FRENCH_NUMBER + " }",
116: FieldCustomizedDoubleContainer.class);
117: assertThat("Failed to correctly customize number format during unmarshalling using JsonbNumberFormat annotation on "
118: + "field.",
119: unmarshalledObject.getInstance(), is(123456.789));
120: }
121:
122: /*
123: * @testName: testNumberFormatAccessors
124: *
125: * @assertion_ids: JSONB:SPEC:JSB-4.9-1
126: *
127: * @test_Strategy: Assert that accessor annotation with JsonbNumberFormat is
128: * correctly individually applied for marshalling and unmarshalling
129: */
130: @Test
131: public void testNumberFormatAccessors() {
132: String jsonString = jsonb.toJson(new AccessorCustomizedDoubleContainer() {{
133: setInstance(123456.789);
134: }});
135: assertThat("Failed to correctly customize number format during marshalling using JsonbNumberFormat annotation on getter.",
136: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*:\\s*\"123,456.79\"\\s*\\}"));
137:
138: AccessorCustomizedDoubleContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : " + FRENCH_NUMBER + " }",
139: AccessorCustomizedDoubleContainer.class);
140: assertThat(
141: "Failed to correctly customize number format during unmarshalling using JsonbNumberFormat annotation on setter.",
142: unmarshalledObject.getInstance(),
143: is(123456.789));
144: }
145:
146: /*
147: * @testName: testNumberFormatPackageTypeOverride
148: *
149: * @assertion_ids: JSONB:SPEC:JSB-4.9-1; JSONB:SPEC:JSB-4.9-2
150: *
151: * @test_Strategy: Assert that package annotation with JsonbNumberFormat is
152: * correctly overridden by type annotation with JsonbNumberFormat
153: */
154: @Test
155: public void testNumberFormatPackageTypeOverride() {
156: String jsonString = jsonb.toJson(new PackageCustomizedTypeOverriddenDoubleContainer() {{
157: setInstance(123456.789);
158: }});
159: assertThat("Failed to correctly override number format customization using JsonbNumberFormat annotation on "
160: + "package during marshalling using JsonbNumberFormat annotation on type.",
161: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*:\\s*\"123,456.79\"\\s*\\}"));
162:
163: PackageCustomizedTypeOverriddenDoubleContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : \"123,456.789\" }",
164: PackageCustomizedTypeOverriddenDoubleContainer.class);
165: assertThat("Failed to correctly override number format customization using JsonbNumberFormat annotation on "
166: + "package during unmarshalling using JsonbNumberFormat annotation on type.",
167: unmarshalledObject.getInstance(), is(123456.789));
168: }
169:
170: /*
171: * @testName: testNumberFormatTypeFieldOverride
172: *
173: * @assertion_ids: JSONB:SPEC:JSB-4.9-1; JSONB:SPEC:JSB-4.9-2
174: *
175: * @test_Strategy: Assert that type annotation with JsonbNumberFormat is
176: * correctly overridden by field annotation with JsonbNumberFormat
177: */
178: @Test
179: public void testNumberFormatTypeFieldOverride() {
180: String jsonString = jsonb.toJson(new TypeCustomizedFieldOverriddenDoubleContainer() {{
181: setInstance(123456.789);
182: }});
183: assertThat("Failed to correctly customize number format during marshalling using JsonbNumberFormat annotation on type.",
184: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*:\\s*\"123,456.8\"\\s*\\}"));
185:
186: TypeCustomizedFieldOverriddenDoubleContainer unmarshalledObject = jsonb.fromJson("{ \"instance\" : \"123,456.789\" }",
187: TypeCustomizedFieldOverriddenDoubleContainer.class);
188: assertThat("Failed to correctly customize number format during unmarshalling using JsonbNumberFormat annotation on type.",
189: unmarshalledObject.getInstance(), is(123456.789));
190: }
191:
192: /*
193: * @testName: testNumberFormatPackageTypeOverrideFieldOverride
194: *
195: * @assertion_ids: JSONB:SPEC:JSB-4.9-1; JSONB:SPEC:JSB-4.9-2
196: *
197: * @test_Strategy: Assert that package and type annotation with
198: * JsonbNumberFormat is correctly overridden by field annotation with
199: * JsonbNumberFormat
200: */
201: @Test
202: public void testNumberFormatPackageTypeOverrideFieldOverride() {
203: String jsonString = jsonb.toJson(new PackageCustomizedTypeOverriddenFieldOverriddenDoubleContainer() {{
204: setInstance(123456.789);
205: }});
206: assertThat("Failed to correctly override number format customization using JsonbNumberFormat annotation on "
207: + "package during marshalling using JsonbNumberFormat annotation on type.",
208: jsonString, matchesPattern("\\{\\s*\"instance\"\\s*:\\s*\"123.456,789\"\\s*\\}"));
209:
210: PackageCustomizedTypeOverriddenFieldOverriddenDoubleContainer unmarshalledObject =
211: jsonb.fromJson("{ \"instance\" : \"123.456,789\" }",
212: PackageCustomizedTypeOverriddenFieldOverriddenDoubleContainer.class);
213: assertThat("Failed to correctly override number format customization using JsonbNumberFormat annotation on "
214: + "package during unmarshalling using JsonbNumberFormat annotation on type.",
215: unmarshalledObject.getInstance(), is(123456.789));
216: }
217:
218: }