Refactor serialization tests and move them to test/io package
Add tests for singleton deserializing.
This commit is contained in:
@@ -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 <T> 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<Any>())
|
||||
@@ -225,14 +214,6 @@ class CollectionJVMTest {
|
||||
val actual = deserializeFromHex<Any>(hexValue)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
private fun hexToBytes(value: String): ByteArray = value.split(" ").map { Integer.parseInt(it, 16).toByte() }.toByteArray()
|
||||
|
||||
private fun <T> deserializeFromHex(value: String) = hexToBytes(value).let {
|
||||
val inputStream = ByteArrayInputStream(it)
|
||||
val inputObjectStream = ObjectInputStream(inputStream)
|
||||
inputObjectStream.readObject() as T
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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 <T> 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 <T> deserializeFromHex(value: String) = hexToBytes(value).let {
|
||||
val inputStream = ByteArrayInputStream(it)
|
||||
val inputObjectStream = ObjectInputStream(inputStream)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inputObjectStream.readObject() as T
|
||||
}
|
||||
@@ -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 <T> 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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user