Created
October 5, 2021 06:32
-
-
Save elizarov/91680b7bfa51892f7696357c1231ddc2 to your computer and use it in GitHub Desktop.
KT-46872
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.* | |
import java.lang.NullPointerException | |
data class A(val s: String?) : Serializable { | |
fun add(b: B) { | |
bs.add(b) | |
} | |
// we'll add B(this) to 'bs' | |
private val bs = mutableSetOf<B>() | |
// check is B(this) is in 'bs' | |
fun isValid() = bs.contains(B(this)) | |
} | |
data class B(val a: A) : Serializable | |
fun main() { | |
try { | |
val expected = A("A") | |
expected.add(B(expected)) | |
val baos = ByteArrayOutputStream() | |
ObjectOutputStream(baos).use { | |
with(it) { | |
writeObject(expected) | |
flush() | |
} | |
} | |
val bais = ByteArrayInputStream(baos.toByteArray()) | |
val actual = ObjectInputStream(bais).use { | |
it.readObject() | |
} as A | |
println(if (expected == actual) "pass" else "fail") | |
// BUT DID IT REALLY PASS? IS THE DESERIALIZED OBJECT VALID? | |
println("Is 'expected' valid? ${expected.isValid()}") | |
println("is 'actual' valid? ${actual.isValid()}") | |
} catch (npe: NullPointerException) { | |
println("fail") | |
npe.printStackTrace() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment