Skip to content

Commit 171a199

Browse files
authored
Merge pull request #166 from ashawley/serialization-rework
Rework serialization testing
2 parents 912f2bf + e443d2c commit 171a199

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

build.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ lazy val xml = crossProject.in(file("."))
4141

4242
libraryDependencies += "junit" % "junit" % "4.11" % "test",
4343
libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test",
44+
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.6" % "test",
4445
libraryDependencies += ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").exclude("org.scala-lang.modules", s"scala-xml_${scalaVersion.value}")
4546
)
4647
.jsSettings(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package scala.xml
2+
3+
import java.io.Serializable
4+
import java.util.Base64
5+
import org.apache.commons.lang3.SerializationUtils
6+
7+
object JavaByteSerialization {
8+
def roundTrip[T <: Serializable](obj: T): T = {
9+
SerializationUtils.roundtrip(obj)
10+
}
11+
12+
def serialize[T <: Serializable](in: T): Array[Byte] = {
13+
SerializationUtils.serialize(in)
14+
}
15+
16+
def deserialize[T <: Serializable](in: Array[Byte]): T = {
17+
SerializationUtils.deserialize(in)
18+
}
19+
20+
def base64Encode[T <: Serializable](in: T): String = {
21+
Base64.getEncoder.encodeToString(serialize[T](in))
22+
}
23+
24+
def base64Decode[T <: Serializable](in: String): T = {
25+
deserialize[T](Base64.getDecoder.decode(in))
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
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))
16+
}
17+
18+
@Test
19+
def unmatched: Unit = {
20+
assertEquals(NodeSeq.Empty, JavaByteSerialization.roundTrip(<xml/> \ "HTML"))
3621
}
3722

3823
@Test
3924
def implicitConversion: Unit = {
4025
val parent = <parent><child></child><child/></parent>
4126
val children: Seq[Node] = parent.child
4227
val asNodeSeq: NodeSeq = children
43-
assertEquals(asNodeSeq, roundTrip(asNodeSeq))
28+
assertEquals(asNodeSeq, JavaByteSerialization.roundTrip(asNodeSeq))
29+
}
30+
31+
@Test
32+
def base64Encode: Unit = {
33+
val str = JavaByteSerialization.base64Encode(NodeSeq.Empty)
34+
assertEquals("rO0ABXNy", str.take(8))
35+
assertEquals("AHhweA==", str.takeRight(8))
4436
}
4537
}

0 commit comments

Comments
 (0)