Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object SerializationUtil {
- def write(obj: Any): String = {
- val temp = Files.createTempFile(null, null).toFile
- val out = new ObjectOutputStream(new FileOutputStream(temp))
- out.writeObject(obj)
- out.close()
- temp.deleteOnExit()
- temp.getAbsolutePath
- }
- def read[T](file: String) = {
- val in = new ObjectInputStream(new FileInputStream(new File(file)))
- val obj = in.readObject().asInstanceOf[T]
- in.close()
- obj
- }
- }
- case class Stats(
- app: String,
- unit: ChronoUnit,
- private var _startupDurations: List[Long]
- ) {
- def startupDurations = _startupDurations.sorted
- def startupDurations_=(durations: List[Long]) = _startupDurations = durations
- @transient lazy val summary: LongSummaryStatistics = {
- _startupDurations.asJava.stream()
- .collect(summarizingLong(identity[Long]))
- }
- }
- "SerializationUtil" should "(de)serialize Stats" in {
- val file = SerializationUtil.write(newStats())
- val state = SerializationUtil.read[Stats](file)
- verifyStats(state)
- }
- java.io.NotSerializableException: org.scalatest.Assertions$AssertionsHelper
- at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
- trait SerializableRunnable[T] extends scala.Serializable with ((T) => Unit)
Add Comment
Please, Sign In to add comment