Files
kotlin-fork/libraries/stdlib/test/utils/LazyJVMTest.kt
T
Ilya Gorbunov b71fe4cd25 Lazy: Rename valueCreated property to isInitialized() function.
Notes about behavior of Lazy when initializer throws an exception.
2015-06-19 23:08:15 +03:00

36 lines
1.0 KiB
Kotlin

package test.utils
import kotlin.*
import kotlin.test.*
import java.io.*
import org.junit.Test as test
class LazyJVMTest {
test fun lazyInitializationForcedOnSerialization() {
for(mode in listOf(LazyThreadSafetyMode.SYNCHRONIZED, LazyThreadSafetyMode.NONE)) {
val lazy = lazy(mode) { "initialized" }
assertFalse(lazy.isInitialized())
val lazy2 = serializeAndDeserialize(lazy)
assertTrue(lazy.isInitialized())
assertTrue(lazy2.isInitialized())
assertEquals(lazy.value, lazy2.value)
}
}
private fun serializeAndDeserialize<T>(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
}
}