75 lines
1.6 KiB
Kotlin
75 lines
1.6 KiB
Kotlin
package nl.astraeus.persistence
|
|
|
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
|
import nl.astraeus.nl.astraeus.persistence.Persistent
|
|
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
|
import nl.astraeus.nl.astraeus.persistence.reference.reference
|
|
import nl.astraeus.persistence.domain.Company
|
|
import org.junit.jupiter.api.assertThrows
|
|
import java.io.File
|
|
import kotlin.test.Test
|
|
|
|
class TestReferences {
|
|
|
|
class Person(
|
|
override var id: Long = 0,
|
|
override var version: Long = 0,
|
|
val name: String,
|
|
company: Company
|
|
) : Persistable, Cloneable {
|
|
var company: Company by reference(company)
|
|
|
|
companion object {
|
|
private const val serialVersionUID: Long = 1L
|
|
}
|
|
|
|
override fun toString(): String {
|
|
return "Person(id=$id, version=$version, name='$name')"
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun showTransactions() {
|
|
val log = TransactionLog(File("data", "test-references"))
|
|
|
|
log.showTransactions()
|
|
}
|
|
|
|
@Test
|
|
fun testSerializeDeSerializeReferenceList() {
|
|
val pst = Persistent(
|
|
directory = File("data", "test-references"),
|
|
)
|
|
|
|
pst.transaction {
|
|
val company = Company(
|
|
id = 1L,
|
|
name = "ACME"
|
|
)
|
|
|
|
val person = Person(
|
|
id = 0L,
|
|
name = "John Doe",
|
|
company = company
|
|
)
|
|
store(person)
|
|
|
|
store(company)
|
|
|
|
for (p in company.persons) {
|
|
println("Person: $p")
|
|
}
|
|
|
|
delete(company)
|
|
|
|
// company is gone, can't get it through person anymore
|
|
assertThrows<IllegalStateException> {
|
|
println("Company in person: ${person.company}")
|
|
}
|
|
|
|
delete(person)
|
|
}
|
|
}
|
|
|
|
}
|