Last active
September 5, 2024 15:37
-
-
Save dacr/cb939af376e1494c05fe92c6e5ae11bf to your computer and use it in GitHub Desktop.
Using standard java object persistence mechanism. / published by https://github.com/dacr/code-examples-manager #73d73891-599c-4bc2-bc15-4be8f2ca19c7/6afaaae3a40bf4ebb9aadff133f6077da79bf6eb
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
// summary : Using standard java object persistence mechanism. | |
// keywords : scala, java, persistence, scalatest | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 73d73891-599c-4bc2-bc15-4be8f2ca19c7 | |
// created-on : 2020-05-31T19:54:52Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.scalatest::scalatest:3.2.16" | |
//> using objectWrapper | |
// --------------------- | |
import org.scalatest._, flatspec._, matchers._ | |
import java.io._ | |
import java.util.Date | |
import java.time.Instant | |
case class A(message:String) | |
case class B(a:A, report:String) | |
case class T1( | |
t1:List[String], | |
date:Date, | |
instant:Instant, | |
a1:Vector[A] | |
) | |
case class T2( | |
t1:Array[String] | |
) | |
class ObjectPersistenceTest extends AnyFlatSpec with should.Matchers { | |
override def suiteName="ObjectPersistence" | |
def save(filename:String, that:Any):Unit = { | |
val output = new ObjectOutputStream(new FileOutputStream(filename)) | |
output.writeObject(that) | |
output.close() | |
} | |
def restore[THAT](filename:String):THAT = { | |
val input = new ObjectInputStream(new FileInputStream(filename)) | |
input.readObject().asInstanceOf[THAT] | |
} | |
// -------------------------------------------------------------------------------- | |
"java persistence" should "be able to save and load scala case classes" in { | |
val filename = "object-persistence.tmp" | |
val b = new B(new A("This is A."), "This is B.") | |
save(filename, b) | |
restore[B](filename) should be(b) | |
} | |
// -------------------------------------------------------------------------------- | |
it should "even save and load more complex data structures" in { | |
val filename = "object-persistence.tmp" | |
val t = T1( | |
List("1", "2", "3"), | |
new Date(), | |
Instant.now(), | |
Vector(A("a1"),A("a2")) | |
) | |
save(filename, t) | |
restore[T1](filename) should be(t) | |
} | |
// -------------------------------------------------------------------------------- | |
it should "work with array but take care of deep equalities within scalatest and arrays... :(" in { | |
val filename = "object-persistence.tmp" | |
val arr1 = Array("1", "2", "3") | |
save(filename, arr1) | |
restore[Array[String]](filename) shouldBe arr1 | |
val arr2 = T2(Array("1", "2", "3")) | |
save(filename, arr2) | |
//restore[T2](filename) shouldBe arr2 // fail | |
val arr3 = T2(Array("1", "2", "3")) | |
save(filename, arr3) | |
restore[T2](filename).t1 shouldEqual arr3.t1 | |
} | |
} | |
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[ObjectPersistenceTest].getName)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment