From 6af986cfddd9f9fe029b0363445341f75065ed27 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 13 Feb 2017 18:42:44 +0300 Subject: [PATCH] Refactor serialization tests and move them to test/io package Add tests for singleton deserializing. --- .../test/collections/CollectionJVMTest.kt | 23 +----- .../test/concurrent/SerializableTest.kt | 75 ----------------- libraries/stdlib/test/io/SerializableTest.kt | 81 +++++++++++++++++++ libraries/stdlib/test/utils/LazyJVMTest.kt | 16 +--- 4 files changed, 84 insertions(+), 111 deletions(-) delete mode 100644 libraries/stdlib/test/concurrent/SerializableTest.kt create mode 100644 libraries/stdlib/test/io/SerializableTest.kt diff --git a/libraries/stdlib/test/collections/CollectionJVMTest.kt b/libraries/stdlib/test/collections/CollectionJVMTest.kt index 353f3dd2de1..532b59689e3 100644 --- a/libraries/stdlib/test/collections/CollectionJVMTest.kt +++ b/libraries/stdlib/test/collections/CollectionJVMTest.kt @@ -10,6 +10,8 @@ import kotlin.comparisons.* import java.util.* import org.junit.Test +import test.io.deserializeFromHex +import test.io.serializeAndDeserialize class CollectionJVMTest { @@ -196,19 +198,6 @@ class CollectionJVMTest { assertTrue(value === result) } - private fun serializeAndDeserialize(value: T): T { - val outputStream = ByteArrayOutputStream() - val objectOutputStream = ObjectOutputStream(outputStream) - - objectOutputStream.writeObject(value) - objectOutputStream.close() - outputStream.close() - - val inputStream = ByteArrayInputStream(outputStream.toByteArray()) - val inputObjectStream = ObjectInputStream(inputStream) - return inputObjectStream.readObject() as T - } - @Test fun deserializeEmptyList() = testPersistedDeserialization( "ac ed 00 05 73 72 00 1c 6b 6f 74 6c 69 6e 2e 63 6f 6c 6c 65 63 74 69 6f 6e 73 2e 45 6d 70 74 79 4c 69 73 74 99 6f c7 d0 a7 e0 60 32 02 00 00 78 70", emptyList()) @@ -225,14 +214,6 @@ class CollectionJVMTest { val actual = deserializeFromHex(hexValue) assertEquals(expected, actual) } - - private fun hexToBytes(value: String): ByteArray = value.split(" ").map { Integer.parseInt(it, 16).toByte() }.toByteArray() - - private fun deserializeFromHex(value: String) = hexToBytes(value).let { - val inputStream = ByteArrayInputStream(it) - val inputObjectStream = ObjectInputStream(inputStream) - inputObjectStream.readObject() as T - } } diff --git a/libraries/stdlib/test/concurrent/SerializableTest.kt b/libraries/stdlib/test/concurrent/SerializableTest.kt deleted file mode 100644 index 01ba2760f7f..00000000000 --- a/libraries/stdlib/test/concurrent/SerializableTest.kt +++ /dev/null @@ -1,75 +0,0 @@ -@file:kotlin.jvm.JvmVersion -package test.concurrent - -import java.io.ObjectOutputStream -import java.io.ByteArrayOutputStream -import java.io.ByteArrayInputStream -import java.io.ObjectInputStream -import java.io.Serializable -import junit.framework.TestCase -import org.junit.Assert - -private class Serial(val name: String) : Serializable { - override fun toString() = name -} - -private data class DataType(val name: String, val value: Int, val percent: Double) : Serializable - -class SerializableTest() : TestCase() { - fun testClosure() { - val tuple = Triple("Ivan", 12, Serial("serial")) - val fn = { tuple.toString() } - - val byteOutputStream = ByteArrayOutputStream() - val bytes = with(byteOutputStream) { - val objectStream = ObjectOutputStream(byteOutputStream) - objectStream.writeObject(fn) - objectStream.close() - toByteArray() - } - - val byteInputStream = ByteArrayInputStream(bytes) - val objectStream = ObjectInputStream(byteInputStream) - val deserialized = objectStream.readObject() as (() -> String) - - Assert.assertEquals(fn(), deserialized()) - } - - fun testComplexClosure() { - val y = 12 - val fn1 = { x: Int -> (x + y).toString() } - val fn2: Int.(Int) -> String = { fn1(this + it) } - - val byteOutputStream = ByteArrayOutputStream() - val bytes = with(byteOutputStream) { - val objectStream = ObjectOutputStream(byteOutputStream) - objectStream.writeObject(fn2) - objectStream.close() - toByteArray() - } - - val byteInputStream = ByteArrayInputStream(bytes) - val objectStream = ObjectInputStream(byteInputStream) - val deserialized = objectStream.readObject() as (Int.(Int) -> String) - - Assert.assertEquals(5.fn2(10), 5.deserialized(10)) - } - - fun testDataClass() { - val data = DataType("name", 176, 1.4) - - val byteOutputStream = ByteArrayOutputStream() - val bytes = with(byteOutputStream) { - val objectStream = ObjectOutputStream(byteOutputStream) - objectStream.writeObject(data) - objectStream.close() - toByteArray() - } - - val byteInputStream = ByteArrayInputStream(bytes) - val objectStream = ObjectInputStream(byteInputStream) - val deserialized = objectStream.readObject() as DataType - - Assert.assertEquals(data, deserialized) - } -} diff --git a/libraries/stdlib/test/io/SerializableTest.kt b/libraries/stdlib/test/io/SerializableTest.kt new file mode 100644 index 00000000000..3d0272a0a3f --- /dev/null +++ b/libraries/stdlib/test/io/SerializableTest.kt @@ -0,0 +1,81 @@ +@file:kotlin.jvm.JvmVersion +package test.io + +import java.io.* +import org.junit.Test +import kotlin.test.* + +private class Serial(val name: String) : Serializable { + override fun toString() = name +} + +private data class DataType(val name: String, val value: Int, val percent: Double) : Serializable + +private enum class EnumSingleton { INSTANCE } +private object ObjectSingleton : Serializable { + private fun readResolve(): Any = ObjectSingleton +} + +private class OldSchoolSingleton private constructor() : Serializable { + private fun readResolve(): Any = INSTANCE + + companion object { + val INSTANCE = OldSchoolSingleton() + } +} + + +class SerializableTest { + @Test fun testClosure() { + val tuple = Triple("Ivan", 12, Serial("serial")) + val fn = { tuple.toString() } + val deserialized = serializeAndDeserialize(fn) + + assertEquals(fn(), deserialized()) + } + + @Test fun testComplexClosure() { + val y = 12 + val fn1 = { x: Int -> (x + y).toString() } + val fn2: Int.(Int) -> String = { fn1(this + it) } + val deserialized = serializeAndDeserialize(fn2) + + assertEquals(5.fn2(10), 5.deserialized(10)) + } + + @Test fun testDataClass() { + val data = DataType("name", 176, 1.4) + val deserialized = serializeAndDeserialize(data) + + assertEquals(data, deserialized) + } + + @Test fun testSingletons() { + assertTrue(EnumSingleton.INSTANCE === serializeAndDeserialize(EnumSingleton.INSTANCE)) + assertTrue(OldSchoolSingleton.INSTANCE === serializeAndDeserialize(OldSchoolSingleton.INSTANCE)) + assertTrue(ObjectSingleton === serializeAndDeserialize(ObjectSingleton)) + } +} + +public fun serializeAndDeserialize(value: T): T { + val outputStream = ByteArrayOutputStream() + val objectOutputStream = ObjectOutputStream(outputStream) + + objectOutputStream.writeObject(value) + objectOutputStream.close() + outputStream.close() + + val inputStream = ByteArrayInputStream(outputStream.toByteArray()) + val inputObjectStream = ObjectInputStream(inputStream) + @Suppress("UNCHECKED_CAST") + return inputObjectStream.readObject() as T +} + +private fun hexToBytes(value: String): ByteArray = value.split(" ").map { Integer.parseInt(it, 16).toByte() }.toByteArray() + +public fun deserializeFromHex(value: String) = hexToBytes(value).let { + val inputStream = ByteArrayInputStream(it) + val inputObjectStream = ObjectInputStream(inputStream) + @Suppress("UNCHECKED_CAST") + inputObjectStream.readObject() as T +} diff --git a/libraries/stdlib/test/utils/LazyJVMTest.kt b/libraries/stdlib/test/utils/LazyJVMTest.kt index d130080330f..cd852f421a0 100644 --- a/libraries/stdlib/test/utils/LazyJVMTest.kt +++ b/libraries/stdlib/test/utils/LazyJVMTest.kt @@ -9,6 +9,7 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicInteger import kotlin.concurrent.thread import org.junit.Test +import test.io.serializeAndDeserialize class LazyJVMTest { @@ -85,19 +86,4 @@ class LazyJVMTest { assertEquals(lazy.value, lazy2.value) } } - - - private fun serializeAndDeserialize(value: T): T { - val outputStream = ByteArrayOutputStream() - val objectOutputStream = ObjectOutputStream(outputStream) - - objectOutputStream.writeObject(value) - objectOutputStream.close() - outputStream.close() - - val inputStream = ByteArrayInputStream(outputStream.toByteArray()) - val inputObjectStream = ObjectInputStream(inputStream) - return inputObjectStream.readObject() as T - } - } \ No newline at end of file