Guest User

Untitled

a guest
Dec 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. object SerializationUtil {
  2. def write(obj: Any): String = {
  3. val temp = Files.createTempFile(null, null).toFile
  4. val out = new ObjectOutputStream(new FileOutputStream(temp))
  5. out.writeObject(obj)
  6. out.close()
  7.  
  8. temp.deleteOnExit()
  9. temp.getAbsolutePath
  10. }
  11.  
  12. def read[T](file: String) = {
  13. val in = new ObjectInputStream(new FileInputStream(new File(file)))
  14. val obj = in.readObject().asInstanceOf[T]
  15. in.close()
  16. obj
  17. }
  18. }
  19.  
  20. case class Stats(
  21. app: String,
  22. unit: ChronoUnit,
  23. private var _startupDurations: List[Long]
  24. ) {
  25. def startupDurations = _startupDurations.sorted
  26.  
  27. def startupDurations_=(durations: List[Long]) = _startupDurations = durations
  28.  
  29. @transient lazy val summary: LongSummaryStatistics = {
  30. _startupDurations.asJava.stream()
  31. .collect(summarizingLong(identity[Long]))
  32. }
  33. }
  34.  
  35. "SerializationUtil" should "(de)serialize Stats" in {
  36. val file = SerializationUtil.write(newStats())
  37. val state = SerializationUtil.read[Stats](file)
  38.  
  39. verifyStats(state)
  40. }
  41.  
  42. java.io.NotSerializableException: org.scalatest.Assertions$AssertionsHelper
  43. at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
  44.  
  45. trait SerializableRunnable[T] extends scala.Serializable with ((T) => Unit)
Add Comment
Please, Sign In to add comment