@@ -13,21 +13,41 @@ class JvmSerializationTest {
1313 @Test
1414 fun serializeInstant () {
1515 roundTripSerialization(Instant .fromEpochSeconds(1234567890 , 123456789 ))
16+ roundTripSerialization(Instant .MIN )
17+ roundTripSerialization(Instant .MAX )
18+ expectedDeserialization(Instant .parse(" +150000-04-30T12:30:25.555998Z" ), " 0d010000043fa44d82612123db30" )
1619 }
1720
1821 @Test
1922 fun serializeLocalTime () {
2023 roundTripSerialization(LocalTime (12 , 34 , 56 , 789 ))
24+ roundTripSerialization(LocalTime .MIN )
25+ roundTripSerialization(LocalTime .MAX )
26+ expectedDeserialization(LocalTime (23 , 59 , 15 , 995_003_220 ), " 090300004e8a52680954" )
27+ }
28+
29+ @Test
30+ fun serializeLocalDate () {
31+ roundTripSerialization(LocalDate (2022 , 1 , 23 ))
32+ roundTripSerialization(LocalDate .MIN )
33+ roundTripSerialization(LocalDate .MAX )
34+ expectedDeserialization(LocalDate (2024 , 8 , 12 ), " 09020000000000004deb" )
2135 }
2236
2337 @Test
2438 fun serializeLocalDateTime () {
2539 roundTripSerialization(LocalDateTime (2022 , 1 , 23 , 21 , 35 , 53 , 125_123_612 ))
40+ roundTripSerialization(LocalDateTime .MIN )
41+ roundTripSerialization(LocalDateTime .MAX )
42+ expectedDeserialization(LocalDateTime (2024 , 8 , 12 , 10 , 15 , 0 , 997_665_331 ), " 11040000000000004deb0000218faedb9233" )
2643 }
2744
2845 @Test
2946 fun serializeUtcOffset () {
3047 roundTripSerialization(UtcOffset (hours = 3 , minutes = 30 , seconds = 15 ))
48+ roundTripSerialization(UtcOffset (java.time.ZoneOffset .MIN ))
49+ roundTripSerialization(UtcOffset (java.time.ZoneOffset .MAX ))
50+ expectedDeserialization(UtcOffset .parse(" -04:15:30" ), " 050affffc41e" )
3151 }
3252
3353 @Test
@@ -37,14 +57,35 @@ class JvmSerializationTest {
3757 }
3858 }
3959
40- private fun < T > roundTripSerialization (value : T ) {
60+ private fun serialize (value : Any? ): ByteArray {
4161 val bos = ByteArrayOutputStream ()
4262 val oos = ObjectOutputStream (bos)
4363 oos.writeObject(value)
44- val serialized = bos.toByteArray()
64+ return bos.toByteArray()
65+ }
66+
67+ private fun deserialize (serialized : ByteArray ): Any? {
4568 val bis = ByteArrayInputStream (serialized)
4669 ObjectInputStream (bis).use { ois ->
47- assertEquals(value, ois.readObject())
70+ return ois.readObject()
71+ }
72+ }
73+
74+ private fun <T > roundTripSerialization (value : T ) {
75+ val serialized = serialize(value)
76+ val deserialized = deserialize(serialized)
77+ assertEquals(value, deserialized)
78+ }
79+
80+ @OptIn(ExperimentalStdlibApi ::class )
81+ private fun expectedDeserialization (expected : Any , blockData : String ) {
82+ val serialized = " aced0005737200296b6f746c696e782e6461746574696d652e696e7465726e616c2e53657269616c697a656456616c756500000000000000000c0000787077${blockData} 78"
83+ val hexFormat = HexFormat { bytes.byteSeparator = " " }
84+
85+ val deserialized = deserialize(serialized.hexToByteArray(hexFormat))
86+ if (expected != deserialized) {
87+ assertEquals(expected, deserialized, " Golden serial form: $serialized \n Actual serial form: ${serialize(expected).toHexString(hexFormat)} " )
4888 }
4989 }
90+
5091}
0 commit comments