Skip to content

Commit 7f670df

Browse files
Test actual serialization
Infrastructure taken from scala/scala#5278
1 parent 51ed4eb commit 7f670df

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
case class Foo()
1+
case class Foo(s: String)
22

33
object Test {
4-
def main(args: Array[String]): Unit =
5-
assert(Foo().isInstanceOf[Serializable])
4+
def main(args: Array[String]): Unit = {
5+
val foo = Foo("bar")
6+
assert(foo.isInstanceOf[Serializable])
7+
assert(SerDes.serializeDeserialize[Foo](foo) == foo)
8+
}
9+
}
10+
11+
// From https://github.com/scala/scala/pull/5278
12+
object SerDes {
13+
import java.io._
14+
15+
def assertNotSerializable(a: AnyRef): Unit = {
16+
try {
17+
serialize(a)
18+
assert(false)
19+
} catch {
20+
case _: NotSerializableException => // okay
21+
}
22+
}
23+
24+
def serialize(obj: AnyRef): Array[Byte] = {
25+
val buffer = new ByteArrayOutputStream
26+
val out = new ObjectOutputStream(buffer)
27+
out.writeObject(obj)
28+
buffer.toByteArray
29+
}
30+
31+
def deserialize(a: Array[Byte]): AnyRef = {
32+
val in = new ObjectInputStream(new ByteArrayInputStream(a))
33+
in.readObject
34+
}
35+
36+
def serializeDeserialize[T <: AnyRef](obj: T) = deserialize(serialize(obj)).asInstanceOf[T]
637
}

0 commit comments

Comments
 (0)