Skip to content

Commit d37b04f

Browse files
committed
Add JavaByteSerialization based on SerializationTest
1 parent 1715b8a commit d37b04f

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package scala.xml
2+
3+
import java.io._
4+
5+
object JavaByteSerialization {
6+
def roundTrip[T](obj: T): T = {
7+
def serialize(in: T): Array[Byte] = {
8+
val bos = new ByteArrayOutputStream()
9+
val oos = new ObjectOutputStream(bos)
10+
oos.writeObject(in)
11+
oos.flush()
12+
bos.toByteArray()
13+
}
14+
15+
def deserialize(in: Array[Byte]): T = {
16+
val bis = new ByteArrayInputStream(in)
17+
val ois = new ObjectInputStream(bis)
18+
ois.readObject.asInstanceOf[T]
19+
}
20+
21+
deserialize(serialize(obj))
22+
}
23+
}
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,25 @@
11
package scala.xml
22

3-
import java.io._
4-
53
import org.junit.Assert.assertEquals
64
import org.junit.Test
75

86
class SerializationTest {
9-
def roundTrip[T](obj: T): T = {
10-
def serialize(in: T): Array[Byte] = {
11-
val bos = new ByteArrayOutputStream()
12-
val oos = new ObjectOutputStream(bos)
13-
oos.writeObject(in)
14-
oos.flush()
15-
bos.toByteArray()
16-
}
17-
18-
def deserialize(in: Array[Byte]): T = {
19-
val bis = new ByteArrayInputStream(in)
20-
val ois = new ObjectInputStream(bis)
21-
ois.readObject.asInstanceOf[T]
22-
}
23-
24-
deserialize(serialize(obj))
25-
}
26-
277
@Test
288
def xmlLiteral: Unit = {
299
val n = <node/>
30-
assertEquals(n, roundTrip(n))
10+
assertEquals(n, JavaByteSerialization.roundTrip(n))
3111
}
3212

3313
@Test
3414
def empty: Unit = {
35-
assertEquals(NodeSeq.Empty, roundTrip(NodeSeq.Empty))
15+
assertEquals(NodeSeq.Empty, JavaByteSerialization.roundTrip(NodeSeq.Empty))
3616
}
3717

3818
@Test
3919
def implicitConversion: Unit = {
4020
val parent = <parent><child></child><child/></parent>
4121
val children: Seq[Node] = parent.child
4222
val asNodeSeq: NodeSeq = children
43-
assertEquals(asNodeSeq, roundTrip(asNodeSeq))
23+
assertEquals(asNodeSeq, JavaByteSerialization.roundTrip(asNodeSeq))
4424
}
4525
}

0 commit comments

Comments
 (0)