|
| 1 | +/* |
| 2 | + * Copyright 2019-2024 JetBrains s.r.o. and contributors. |
| 3 | + * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +package kotlinx.datetime |
| 7 | + |
| 8 | +import java.io.* |
| 9 | +import kotlin.test.* |
| 10 | + |
| 11 | +class JvmSerializationTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + fun serializeLocalTime() { |
| 15 | + roundTripSerialization(LocalTime(12, 34, 56, 789)) |
| 16 | + roundTripSerialization(LocalTime.MIN) |
| 17 | + roundTripSerialization(LocalTime.MAX) |
| 18 | + expectedDeserialization(LocalTime(23, 59, 15, 995_003_220), "090300004e8a52680954") |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun serializeLocalDate() { |
| 23 | + roundTripSerialization(LocalDate(2022, 1, 23)) |
| 24 | + roundTripSerialization(LocalDate.MIN) |
| 25 | + roundTripSerialization(LocalDate.MAX) |
| 26 | + expectedDeserialization(LocalDate(2024, 8, 12), "09020000000000004deb") |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun serializeLocalDateTime() { |
| 31 | + roundTripSerialization(LocalDateTime(2022, 1, 23, 21, 35, 53, 125_123_612)) |
| 32 | + roundTripSerialization(LocalDateTime.MIN) |
| 33 | + roundTripSerialization(LocalDateTime.MAX) |
| 34 | + expectedDeserialization(LocalDateTime(2024, 8, 12, 10, 15, 0, 997_665_331), "11040000000000004deb0000218faedb9233") |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun serializeUtcOffset() { |
| 39 | + roundTripSerialization(UtcOffset(hours = 3, minutes = 30, seconds = 15)) |
| 40 | + roundTripSerialization(UtcOffset(java.time.ZoneOffset.MIN)) |
| 41 | + roundTripSerialization(UtcOffset(java.time.ZoneOffset.MAX)) |
| 42 | + expectedDeserialization(UtcOffset.parse("-04:15:30"), "050affffc41e") |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun serializeTimeZone() { |
| 47 | + assertFailsWith<NotSerializableException> { |
| 48 | + roundTripSerialization(TimeZone.of("Europe/Moscow")) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private fun serialize(value: Any?): ByteArray { |
| 53 | + val bos = ByteArrayOutputStream() |
| 54 | + val oos = ObjectOutputStream(bos) |
| 55 | + oos.writeObject(value) |
| 56 | + return bos.toByteArray() |
| 57 | + } |
| 58 | + |
| 59 | + private fun deserialize(serialized: ByteArray): Any? { |
| 60 | + val bis = ByteArrayInputStream(serialized) |
| 61 | + ObjectInputStream(bis).use { ois -> |
| 62 | + return ois.readObject() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private fun <T> roundTripSerialization(value: T) { |
| 67 | + val serialized = serialize(value) |
| 68 | + val deserialized = deserialize(serialized) |
| 69 | + assertEquals(value, deserialized) |
| 70 | + } |
| 71 | + |
| 72 | + @OptIn(ExperimentalStdlibApi::class) |
| 73 | + private fun expectedDeserialization(expected: Any, blockData: String) { |
| 74 | + val serialized = "aced0005737200146b6f746c696e782e6461746574696d652e53657200000000000000000c0000787077${blockData}78" |
| 75 | + val hexFormat = HexFormat { bytes.byteSeparator = "" } |
| 76 | + |
| 77 | + try { |
| 78 | + val deserialized = deserialize(serialized.hexToByteArray(hexFormat)) |
| 79 | + if (expected != deserialized) { |
| 80 | + assertEquals(expected, deserialized, "Golden serial form: $serialized\nActual serial form: ${serialize(expected).toHexString(hexFormat)}") |
| 81 | + } |
| 82 | + } catch (e: Throwable) { |
| 83 | + fail("Failed to deserialize $serialized\nActual serial form: ${serialize(expected).toHexString(hexFormat)}", e) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments