mirror of
https://github.com/corda/corda.git
synced 2025-01-10 06:52:44 +00:00
dad59d116c
Currently the serialised forms and the form fed into the contract aren't quite joined up, because on disk/network a transaction is serialised with input references i.e. (txhash, output index) pairs, but the contract wants to see all input states in fully loaded form. To do that, we need some notion of a database of transactions.
56 lines
1.6 KiB
Kotlin
56 lines
1.6 KiB
Kotlin
package serialization
|
|
|
|
import com.esotericsoftware.kryo.Kryo
|
|
import core.serialization.*
|
|
import org.junit.Test
|
|
import java.time.Instant
|
|
import kotlin.test.assertEquals
|
|
import kotlin.test.assertFailsWith
|
|
import kotlin.test.assertNull
|
|
|
|
class KryoTests {
|
|
data class Person(val name: String, val birthday: Instant?) : SerializeableWithKryo
|
|
data class MustBeWhizzy(val s: String) : SerializeableWithKryo {
|
|
init {
|
|
assert(s.startsWith("whiz")) { "must be whizzy" }
|
|
}
|
|
}
|
|
|
|
private val kryo: Kryo = createKryo().apply {
|
|
registerDataClass<Person>()
|
|
registerDataClass<MustBeWhizzy>()
|
|
}
|
|
|
|
@Test
|
|
fun ok() {
|
|
val april_17th = Instant.parse("1984-04-17T00:30:00.00Z")
|
|
val mike = Person("mike", april_17th)
|
|
val bits = mike.serialize(kryo)
|
|
assertEquals(64, bits.size)
|
|
with(bits.deserialize<Person>(kryo)) {
|
|
assertEquals("mike", name)
|
|
assertEquals(april_17th, birthday)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun nullables() {
|
|
val bob = Person("bob", null)
|
|
val bits = bob.serialize(kryo)
|
|
with(bits.deserialize<Person>(kryo)) {
|
|
assertEquals("bob", name)
|
|
assertNull(birthday)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun constructorInvariant() {
|
|
val pos = MustBeWhizzy("whizzle")
|
|
val bits = pos.serialize(kryo)
|
|
// Hack the serialized bytes here, like a very naughty hacker might.
|
|
bits[10] = 'o'.toByte()
|
|
assertFailsWith<AssertionError>("must be whizzy") {
|
|
bits.deserialize<MustBeWhizzy>(kryo)
|
|
}
|
|
}
|
|
} |