|
24 | 24 | import java.util.Map; |
25 | 25 |
|
26 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
27 | 28 |
|
28 | 29 | class Jackson3ObjectSerializerTest { |
29 | 30 |
|
@@ -177,6 +178,225 @@ void deserializeMap() throws IOException { |
177 | 178 | assertThat(result).containsEntry("key", "value"); |
178 | 179 | } |
179 | 180 |
|
| 181 | + // --- Primitive default values for null data --- |
| 182 | + |
| 183 | + @Test |
| 184 | + @DisplayName("deserialize boolean primitive with empty data should return false") |
| 185 | + void deserializeBooleanPrimitiveEmpty() throws IOException { |
| 186 | + boolean result = serializer.deserialize(new byte[]{}, TypeRef.BOOLEAN); |
| 187 | + assertThat(result).isFalse(); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + @DisplayName("deserialize byte primitive with null data should return 0") |
| 192 | + void deserializeBytePrimitiveNull() throws IOException { |
| 193 | + byte result = serializer.deserialize(null, TypeRef.BYTE); |
| 194 | + assertThat(result).isEqualTo((byte) 0); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + @DisplayName("deserialize byte primitive with empty data should return 0") |
| 199 | + void deserializeBytePrimitiveEmpty() throws IOException { |
| 200 | + byte result = serializer.deserialize(new byte[]{}, TypeRef.BYTE); |
| 201 | + assertThat(result).isEqualTo((byte) 0); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + @DisplayName("deserialize short primitive with null data should return 0") |
| 206 | + void deserializeShortPrimitiveNull() throws IOException { |
| 207 | + short result = serializer.deserialize(null, TypeRef.SHORT); |
| 208 | + assertThat(result).isEqualTo((short) 0); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + @DisplayName("deserialize short primitive with empty data should return 0") |
| 213 | + void deserializeShortPrimitiveEmpty() throws IOException { |
| 214 | + short result = serializer.deserialize(new byte[]{}, TypeRef.SHORT); |
| 215 | + assertThat(result).isEqualTo((short) 0); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + @DisplayName("deserialize long primitive with null data should return 0") |
| 220 | + void deserializeLongPrimitiveNull() throws IOException { |
| 221 | + long result = serializer.deserialize(null, TypeRef.LONG); |
| 222 | + assertThat(result).isEqualTo(0L); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + @DisplayName("deserialize long primitive with empty data should return 0") |
| 227 | + void deserializeLongPrimitiveEmpty() throws IOException { |
| 228 | + long result = serializer.deserialize(new byte[]{}, TypeRef.LONG); |
| 229 | + assertThat(result).isEqualTo(0L); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + @DisplayName("deserialize float primitive with null data should return 0") |
| 234 | + void deserializeFloatPrimitiveNull() throws IOException { |
| 235 | + float result = serializer.deserialize(null, TypeRef.FLOAT); |
| 236 | + assertThat(result).isEqualTo(0f); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + @DisplayName("deserialize float primitive with empty data should return 0") |
| 241 | + void deserializeFloatPrimitiveEmpty() throws IOException { |
| 242 | + float result = serializer.deserialize(new byte[]{}, TypeRef.FLOAT); |
| 243 | + assertThat(result).isEqualTo(0f); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + @DisplayName("deserialize double primitive with null data should return 0") |
| 248 | + void deserializeDoublePrimitiveNull() throws IOException { |
| 249 | + double result = serializer.deserialize(null, TypeRef.DOUBLE); |
| 250 | + assertThat(result).isEqualTo(0d); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + @DisplayName("deserialize double primitive with empty data should return 0") |
| 255 | + void deserializeDoublePrimitiveEmpty() throws IOException { |
| 256 | + double result = serializer.deserialize(new byte[]{}, TypeRef.DOUBLE); |
| 257 | + assertThat(result).isEqualTo(0d); |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + @DisplayName("deserialize char primitive with null data should return MIN_VALUE") |
| 262 | + void deserializeCharPrimitiveNull() throws IOException { |
| 263 | + char result = serializer.deserialize(null, TypeRef.CHAR); |
| 264 | + assertThat(result).isEqualTo(Character.MIN_VALUE); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + @DisplayName("deserialize char primitive with empty data should return MIN_VALUE") |
| 269 | + void deserializeCharPrimitiveEmpty() throws IOException { |
| 270 | + char result = serializer.deserialize(new byte[]{}, TypeRef.CHAR); |
| 271 | + assertThat(result).isEqualTo(Character.MIN_VALUE); |
| 272 | + } |
| 273 | + |
| 274 | + // --- Primitive deserialization with actual data --- |
| 275 | + |
| 276 | + @Test |
| 277 | + @DisplayName("deserialize boolean primitive with actual data should parse correctly") |
| 278 | + void deserializeBooleanPrimitiveWithData() throws IOException { |
| 279 | + byte[] data = "true".getBytes(); |
| 280 | + boolean result = serializer.deserialize(data, TypeRef.BOOLEAN); |
| 281 | + assertThat(result).isTrue(); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + @DisplayName("deserialize long primitive with actual data should parse correctly") |
| 286 | + void deserializeLongPrimitiveWithData() throws IOException { |
| 287 | + byte[] data = "123456789".getBytes(); |
| 288 | + long result = serializer.deserialize(data, TypeRef.LONG); |
| 289 | + assertThat(result).isEqualTo(123456789L); |
| 290 | + } |
| 291 | + |
| 292 | + @Test |
| 293 | + @DisplayName("deserialize float primitive with actual data should parse correctly") |
| 294 | + void deserializeFloatPrimitiveWithData() throws IOException { |
| 295 | + byte[] data = "3.14".getBytes(); |
| 296 | + float result = serializer.deserialize(data, TypeRef.FLOAT); |
| 297 | + assertThat(result).isEqualTo(3.14f); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + @DisplayName("deserialize double primitive with actual data should parse correctly") |
| 302 | + void deserializeDoublePrimitiveWithData() throws IOException { |
| 303 | + byte[] data = "3.14159".getBytes(); |
| 304 | + double result = serializer.deserialize(data, TypeRef.DOUBLE); |
| 305 | + assertThat(result).isEqualTo(3.14159d); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + @DisplayName("deserialize short primitive with actual data should parse correctly") |
| 310 | + void deserializeShortPrimitiveWithData() throws IOException { |
| 311 | + byte[] data = "123".getBytes(); |
| 312 | + short result = serializer.deserialize(data, TypeRef.SHORT); |
| 313 | + assertThat(result).isEqualTo((short) 123); |
| 314 | + } |
| 315 | + |
| 316 | + // --- Void primitive type --- |
| 317 | + |
| 318 | + @Test |
| 319 | + @DisplayName("deserialize void primitive type with null data should return null") |
| 320 | + void deserializeVoidPrimitiveNull() throws IOException { |
| 321 | + assertThat(serializer.deserialize(null, TypeRef.VOID)).isNull(); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + @DisplayName("deserialize void primitive type with empty data should return null") |
| 326 | + void deserializeVoidPrimitiveEmpty() throws IOException { |
| 327 | + assertThat(serializer.deserialize(new byte[]{}, TypeRef.VOID)).isNull(); |
| 328 | + } |
| 329 | + |
| 330 | + // --- IOException wrapping --- |
| 331 | + |
| 332 | + @Test |
| 333 | + @DisplayName("serialize invalid object should throw IOException") |
| 334 | + void serializeInvalidObjectThrowsIOException() { |
| 335 | + Object unserializable = new Object() { |
| 336 | + // Jackson cannot serialize this because of the self-reference |
| 337 | + public Object getSelf() { |
| 338 | + return this; |
| 339 | + } |
| 340 | + }; |
| 341 | + assertThatThrownBy(() -> serializer.serialize(unserializable)) |
| 342 | + .isInstanceOf(IOException.class); |
| 343 | + } |
| 344 | + |
| 345 | + @Test |
| 346 | + @DisplayName("deserialize invalid JSON should throw IOException") |
| 347 | + void deserializeInvalidJsonThrowsIOException() { |
| 348 | + byte[] invalidJson = "{not valid json".getBytes(); |
| 349 | + assertThatThrownBy(() -> serializer.deserialize(invalidJson, TypeRef.get(TestData.class))) |
| 350 | + .isInstanceOf(IOException.class); |
| 351 | + } |
| 352 | + |
| 353 | + @Test |
| 354 | + @DisplayName("deserialize invalid JSON for primitive should throw IOException") |
| 355 | + void deserializeInvalidJsonForPrimitiveThrowsIOException() { |
| 356 | + byte[] invalidJson = "not_a_number".getBytes(); |
| 357 | + assertThatThrownBy(() -> serializer.deserialize(invalidJson, TypeRef.INT)) |
| 358 | + .isInstanceOf(IOException.class); |
| 359 | + } |
| 360 | + |
| 361 | + // --- Additional edge cases --- |
| 362 | + |
| 363 | + @Test |
| 364 | + @DisplayName("deserialize null data for byte[] type should return null") |
| 365 | + void deserializeNullDataForByteArray() throws IOException { |
| 366 | + byte[] result = serializer.deserialize(null, TypeRef.BYTE_ARRAY); |
| 367 | + assertThat(result).isNull(); |
| 368 | + } |
| 369 | + |
| 370 | + @Test |
| 371 | + @DisplayName("serialize integer should return JSON bytes") |
| 372 | + void serializeInteger() throws IOException { |
| 373 | + byte[] result = serializer.serialize(42); |
| 374 | + assertThat(new String(result)).isEqualTo("42"); |
| 375 | + } |
| 376 | + |
| 377 | + @Test |
| 378 | + @DisplayName("serialize boolean should return JSON bytes") |
| 379 | + void serializeBoolean() throws IOException { |
| 380 | + byte[] result = serializer.serialize(true); |
| 381 | + assertThat(new String(result)).isEqualTo("true"); |
| 382 | + } |
| 383 | + |
| 384 | + @Test |
| 385 | + @DisplayName("serialize list should return JSON array bytes") |
| 386 | + void serializeList() throws IOException { |
| 387 | + byte[] result = serializer.serialize(List.of("a", "b", "c")); |
| 388 | + assertThat(new String(result)).isEqualTo("[\"a\",\"b\",\"c\"]"); |
| 389 | + } |
| 390 | + |
| 391 | + @Test |
| 392 | + @DisplayName("round-trip serialize and deserialize list should preserve data") |
| 393 | + void roundTripList() throws IOException { |
| 394 | + List<String> original = List.of("x", "y", "z"); |
| 395 | + byte[] serialized = serializer.serialize(original); |
| 396 | + List<String> deserialized = serializer.deserialize(serialized, new TypeRef<List<String>>() {}); |
| 397 | + assertThat(deserialized).containsExactlyElementsOf(original); |
| 398 | + } |
| 399 | + |
180 | 400 | public static class TestData { |
181 | 401 | private String name; |
182 | 402 | private int value; |
|
0 commit comments