|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Eclipse 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 | +package ee.jakarta.tck.json.bind.defaultmapping.records; |
| 21 | + |
| 22 | +import java.math.BigDecimal; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Disabled; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import ee.jakarta.tck.json.bind.SimpleMappingTester; |
| 29 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.BooleanContainer; |
| 30 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.ByteContainer; |
| 31 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.CharacterContainer; |
| 32 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.DoubleContainer; |
| 33 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.FloatContainer; |
| 34 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.IntegerContainer; |
| 35 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.LongContainer; |
| 36 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.NumberContainer; |
| 37 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.ShortContainer; |
| 38 | +import ee.jakarta.tck.json.bind.defaultmapping.records.model.StringContainer; |
| 39 | +import ee.jakarta.tck.json.bind.records.RecordMappingTester; |
| 40 | + |
| 41 | +/** |
| 42 | + * @test |
| 43 | + * @sources RecordsBasicTypeMappingTest.java |
| 44 | + * @executeClass ee.jakarta.tck.json.bind.defaultmapping.records |
| 45 | + * |
| 46 | + * Assert that records with basic component types are serialized and deserialized the same |
| 47 | + * as fields on a class. |
| 48 | + **/ |
| 49 | +public class RecordsBasicTypeMappingTest { |
| 50 | + |
| 51 | + /* |
| 52 | + * @testName: testStringMapping |
| 53 | + * |
| 54 | + * @assertion_ids: JSONB:SPEC:JSB-3.1-5; JSONB:SPEC:JSB-3.3-1; |
| 55 | + * JSONB:SPEC:JSB-3.3.1-1; JSONB:SPEC:JSB-3.3.1-2 |
| 56 | + * |
| 57 | + * @test_Strategy: Assert that String type is correctly handled, encodings |
| 58 | + * other than UTF-8 are supported and that UTF-8 BOM does not produce an error |
| 59 | + */ |
| 60 | + @Test |
| 61 | + public void testStringMapping() throws Exception { |
| 62 | + RecordMappingTester<String> stringRecordMappingTester = new RecordMappingTester<>(StringContainer.class); |
| 63 | + stringRecordMappingTester.test("Test String", "\"Test String\""); |
| 64 | + stringRecordMappingTester.test(new String(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}, StandardCharsets.UTF_8), |
| 65 | + "\"" + new String(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}, |
| 66 | + StandardCharsets.UTF_8) + "\""); |
| 67 | + stringRecordMappingTester.test(new String("Test String".getBytes(), StandardCharsets.UTF_8), |
| 68 | + "\"" + new String("Test String".getBytes(), StandardCharsets.UTF_8) + "\""); |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + * @testName: testCharacterMapping |
| 73 | + * |
| 74 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.1-1 |
| 75 | + * |
| 76 | + * @test_Strategy: Assert that Character type is correctly handled |
| 77 | + */ |
| 78 | + @Test |
| 79 | + public void testCharacterMapping() { |
| 80 | + RecordMappingTester<Character> characterRecordMappingTester = new RecordMappingTester<>(CharacterContainer.class); |
| 81 | + characterRecordMappingTester.test('c', "\"c\""); |
| 82 | + // RFC 4627, paragraph 2.5: the control characters U+0000 must be |
| 83 | + // escaped |
| 84 | + characterRecordMappingTester.test(Character.MIN_VALUE, "\"\\u0000\""); |
| 85 | + characterRecordMappingTester.test(Character.MAX_VALUE, "\"" + Character.MAX_VALUE + "\""); |
| 86 | + } |
| 87 | + |
| 88 | + /* |
| 89 | + * @testName: testByteMapping |
| 90 | + * |
| 91 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.2-1; |
| 92 | + * JSONB:SPEC:JSB-3.3.2-2 |
| 93 | + * |
| 94 | + * @test_Strategy: Assert that Byte type is correctly handled in accordance |
| 95 | + * with toString and parseByte methods |
| 96 | + */ |
| 97 | + @Test |
| 98 | + public void testByteMapping() { |
| 99 | + RecordMappingTester<Byte> byteRecordMappingTester = new RecordMappingTester<>(ByteContainer.class); |
| 100 | + byteRecordMappingTester.test((byte) 0, "0"); |
| 101 | + byteRecordMappingTester.test(Byte.MIN_VALUE, String.valueOf(Byte.MIN_VALUE)); |
| 102 | + byteRecordMappingTester.test(Byte.MAX_VALUE, String.valueOf(Byte.MAX_VALUE)); |
| 103 | + } |
| 104 | + |
| 105 | + /* |
| 106 | + * @testName: testShortMapping |
| 107 | + * |
| 108 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.2-1; |
| 109 | + * JSONB:SPEC:JSB-3.3.2-2 |
| 110 | + * |
| 111 | + * @test_Strategy: Assert that Short type is correctly handled in accordance |
| 112 | + * with toString and parseShort methods |
| 113 | + */ |
| 114 | + @Test |
| 115 | + public void testShortMapping() { |
| 116 | + RecordMappingTester<Short> shortRecordMappingTester = new RecordMappingTester<>(ShortContainer.class); |
| 117 | + shortRecordMappingTester.test((short) 0, "0"); |
| 118 | + shortRecordMappingTester.test(Short.MIN_VALUE, String.valueOf(Short.MIN_VALUE)); |
| 119 | + shortRecordMappingTester.test(Short.MAX_VALUE, String.valueOf(Short.MAX_VALUE)); |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * @testName: testIntegerMapping |
| 124 | + * |
| 125 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.2-1; |
| 126 | + * JSONB:SPEC:JSB-3.3.2-2 |
| 127 | + * |
| 128 | + * @test_Strategy: Assert that Integer type is correctly handled in accordance |
| 129 | + * with toString and parseInteger methods |
| 130 | + */ |
| 131 | + @Test |
| 132 | + public void testIntegerMapping() { |
| 133 | + RecordMappingTester<Integer> integerRecordMappingTester = new RecordMappingTester<>(IntegerContainer.class); |
| 134 | + integerRecordMappingTester.test(0, "0"); |
| 135 | + integerRecordMappingTester.test(Integer.MIN_VALUE, String.valueOf(Integer.MIN_VALUE)); |
| 136 | + integerRecordMappingTester.test(Integer.MAX_VALUE, String.valueOf(Integer.MAX_VALUE)); |
| 137 | + } |
| 138 | + |
| 139 | + /* |
| 140 | + * @testName: testLongMapping |
| 141 | + * |
| 142 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.2-1; |
| 143 | + * JSONB:SPEC:JSB-3.3.2-2 |
| 144 | + * |
| 145 | + * @test_Strategy: Assert that Long type is correctly handled in accordance |
| 146 | + * with toString and parseLong methods |
| 147 | + */ |
| 148 | + @Test |
| 149 | + @Disabled("See https://github.com/jakartaee/jsonb-api/issues/180") |
| 150 | + public void testLongMapping() { |
| 151 | + RecordMappingTester<Long> longRecordMappingTester = new RecordMappingTester<>(LongContainer.class); |
| 152 | + longRecordMappingTester.test(0L, "0"); |
| 153 | + longRecordMappingTester.test(Long.MIN_VALUE, String.valueOf(Long.MIN_VALUE)); |
| 154 | + longRecordMappingTester.test(Long.MAX_VALUE, String.valueOf(Long.MAX_VALUE)); |
| 155 | + } |
| 156 | + |
| 157 | + /* |
| 158 | + * @testName: testFloatMapping |
| 159 | + * |
| 160 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.2-1; |
| 161 | + * JSONB:SPEC:JSB-3.3.2-2 |
| 162 | + * |
| 163 | + * @test_Strategy: Assert that Float type is correctly handled in accordance |
| 164 | + * with toString and parseFloat methods |
| 165 | + */ |
| 166 | + @Test |
| 167 | + public void testFloatMapping() { |
| 168 | + RecordMappingTester<Float> floatRecordMappingTester = new RecordMappingTester<>(FloatContainer.class); |
| 169 | + floatRecordMappingTester.test(0f, "0.0"); |
| 170 | + floatRecordMappingTester.test(0.5f, "0.5"); |
| 171 | + floatRecordMappingTester.test(Float.MIN_VALUE, String.valueOf(Float.MIN_VALUE)); |
| 172 | + floatRecordMappingTester.test(Float.MAX_VALUE, String.valueOf(Float.MAX_VALUE)); |
| 173 | + } |
| 174 | + |
| 175 | + /* |
| 176 | + * @testName: testDoubleMapping |
| 177 | + * |
| 178 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.2-1; |
| 179 | + * JSONB:SPEC:JSB-3.3.2-2 |
| 180 | + * |
| 181 | + * @test_Strategy: Assert that Double type is correctly handled in accordance |
| 182 | + * with toString and parseDouble methods |
| 183 | + */ |
| 184 | + @Test |
| 185 | + public void testDoubleMapping() { |
| 186 | + RecordMappingTester<Double> doubleRecordMappingTester = new RecordMappingTester<>(DoubleContainer.class); |
| 187 | + doubleRecordMappingTester.test(0.0, "0.0"); |
| 188 | + doubleRecordMappingTester.test(Double.MIN_VALUE, String.valueOf(Double.MIN_VALUE)); |
| 189 | + doubleRecordMappingTester.test(Double.MAX_VALUE, String.valueOf(Double.MAX_VALUE)); |
| 190 | + } |
| 191 | + |
| 192 | + /* |
| 193 | + * @testName: testBooleanMapping |
| 194 | + * |
| 195 | + * @assertion_ids: JSONB:SPEC:JSB-3.3-1; JSONB:SPEC:JSB-3.3.3-1; |
| 196 | + * JSONB:SPEC:JSB-3.3.3-1; JSONB:SPEC:JSB-3.3.3-2 |
| 197 | + * |
| 198 | + * @test_Strategy: Assert that Boolean type is correctly handled in accordance |
| 199 | + * with toString and parseBoolean methods |
| 200 | + */ |
| 201 | + @Test |
| 202 | + public void testBooleanMapping() { |
| 203 | + RecordMappingTester<Boolean> booleanRecordMappingTester = new RecordMappingTester<>(BooleanContainer.class); |
| 204 | + booleanRecordMappingTester.test(true, "true"); |
| 205 | + booleanRecordMappingTester.test(false, "false"); |
| 206 | + booleanRecordMappingTester.test(Boolean.TRUE, Boolean.TRUE.toString()); |
| 207 | + booleanRecordMappingTester.test(Boolean.FALSE, Boolean.FALSE.toString()); |
| 208 | + } |
| 209 | + |
| 210 | + /* |
| 211 | + * @testName: testNumberMapping |
| 212 | + * |
| 213 | + * @assertion_ids: JSONB:SPEC:JSB-3.3.4-1; JSONB:SPEC:JSB-3.3.4-2; |
| 214 | + * JSONB:SPEC:JSB-3.10-1 |
| 215 | + * |
| 216 | + * @test_Strategy: Assert that Number type is correctly marshalled using |
| 217 | + * java.lang.Number.doubleValue() and toString methods and unmarshalled to |
| 218 | + * java.math.BigDecimal using String constructor |
| 219 | + */ |
| 220 | + @Test |
| 221 | + public void testNumberMapping() { |
| 222 | + new SimpleMappingTester<>(NumberContainer.class, NumberContainer.class).test( |
| 223 | + new NumberContainer(0), "\\{\\s*\"instance\"\\s*:\\s*0[\\.0]?+\\s*}", |
| 224 | + "{ \"instance\" : 0 }", new NumberContainer(new BigDecimal("0"))); |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments