From e2d7a2a75945b72c964975e4667297f0e0984bf2 Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Wed, 11 Oct 2017 19:03:03 -0400 Subject: [PATCH 1/6] Add JavaByteSerialization based on SerializationTest --- .../scala/xml/JavaByteSerialization.scala | 23 ++++++++++++++++ .../scala/scala/xml/SerializationTest.scala | 26 +++---------------- 2 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 jvm/src/test/scala/scala/xml/JavaByteSerialization.scala diff --git a/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala new file mode 100644 index 000000000..401213a49 --- /dev/null +++ b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala @@ -0,0 +1,23 @@ +package scala.xml + +import java.io._ + +object JavaByteSerialization { + def roundTrip[T](obj: T): T = { + def serialize(in: T): Array[Byte] = { + val bos = new ByteArrayOutputStream() + val oos = new ObjectOutputStream(bos) + oos.writeObject(in) + oos.flush() + bos.toByteArray() + } + + def deserialize(in: Array[Byte]): T = { + val bis = new ByteArrayInputStream(in) + val ois = new ObjectInputStream(bis) + ois.readObject.asInstanceOf[T] + } + + deserialize(serialize(obj)) + } +} diff --git a/jvm/src/test/scala/scala/xml/SerializationTest.scala b/jvm/src/test/scala/scala/xml/SerializationTest.scala index 6a63eae84..0bea7e43d 100644 --- a/jvm/src/test/scala/scala/xml/SerializationTest.scala +++ b/jvm/src/test/scala/scala/xml/SerializationTest.scala @@ -1,38 +1,18 @@ package scala.xml -import java.io._ - import org.junit.Assert.assertEquals import org.junit.Test class SerializationTest { - def roundTrip[T](obj: T): T = { - def serialize(in: T): Array[Byte] = { - val bos = new ByteArrayOutputStream() - val oos = new ObjectOutputStream(bos) - oos.writeObject(in) - oos.flush() - bos.toByteArray() - } - - def deserialize(in: Array[Byte]): T = { - val bis = new ByteArrayInputStream(in) - val ois = new ObjectInputStream(bis) - ois.readObject.asInstanceOf[T] - } - - deserialize(serialize(obj)) - } - @Test def xmlLiteral: Unit = { val n = - assertEquals(n, roundTrip(n)) + assertEquals(n, JavaByteSerialization.roundTrip(n)) } @Test def empty: Unit = { - assertEquals(NodeSeq.Empty, roundTrip(NodeSeq.Empty)) + assertEquals(NodeSeq.Empty, JavaByteSerialization.roundTrip(NodeSeq.Empty)) } @Test @@ -40,6 +20,6 @@ class SerializationTest { val parent = val children: Seq[Node] = parent.child val asNodeSeq: NodeSeq = children - assertEquals(asNodeSeq, roundTrip(asNodeSeq)) + assertEquals(asNodeSeq, JavaByteSerialization.roundTrip(asNodeSeq)) } } From 830b8b9020c8d3c6f2e5016ec5a641e11bf50cb6 Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Wed, 11 Oct 2017 19:18:33 -0400 Subject: [PATCH 2/6] Make serialization test methods available --- .../scala/xml/JavaByteSerialization.scala | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala index 401213a49..c8fc145b9 100644 --- a/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala +++ b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala @@ -4,20 +4,20 @@ import java.io._ object JavaByteSerialization { def roundTrip[T](obj: T): T = { - def serialize(in: T): Array[Byte] = { - val bos = new ByteArrayOutputStream() - val oos = new ObjectOutputStream(bos) - oos.writeObject(in) - oos.flush() - bos.toByteArray() - } + deserialize[T](serialize[T](obj)) + } - def deserialize(in: Array[Byte]): T = { - val bis = new ByteArrayInputStream(in) - val ois = new ObjectInputStream(bis) - ois.readObject.asInstanceOf[T] - } + def serialize[T](in: T): Array[Byte] = { + val bos = new ByteArrayOutputStream() + val oos = new ObjectOutputStream(bos) + oos.writeObject(in) + oos.flush() + bos.toByteArray() + } - deserialize(serialize(obj)) + def deserialize[T](in: Array[Byte]): T = { + val bis = new ByteArrayInputStream(in) + val ois = new ObjectInputStream(bis) + ois.readObject.asInstanceOf[T] } } From 01253265e4ee49c3022d13dd4a216e832bbc581d Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Wed, 11 Oct 2017 21:42:16 -0400 Subject: [PATCH 3/6] Add base64 to JavaByteSerialization --- jvm/src/test/scala/scala/xml/JavaByteSerialization.scala | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala index c8fc145b9..0e1acddf4 100644 --- a/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala +++ b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala @@ -1,6 +1,7 @@ package scala.xml import java.io._ +import java.util.Base64 object JavaByteSerialization { def roundTrip[T](obj: T): T = { @@ -20,4 +21,12 @@ object JavaByteSerialization { val ois = new ObjectInputStream(bis) ois.readObject.asInstanceOf[T] } + + def base64Encode[T](in: T): String = { + Base64.getEncoder.encodeToString(serialize[T](in)) + } + + def base64Decode[T](in: String): T = { + deserialize[T](Base64.getDecoder.decode(in)) + } } From cf74f331eb87ee637c1e1e4d2f8fa7eb5d3335c5 Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Wed, 11 Oct 2017 21:42:53 -0400 Subject: [PATCH 4/6] Add an XPath and a base64 test of serialization --- jvm/src/test/scala/scala/xml/SerializationTest.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/jvm/src/test/scala/scala/xml/SerializationTest.scala b/jvm/src/test/scala/scala/xml/SerializationTest.scala index 0bea7e43d..eb8f716ea 100644 --- a/jvm/src/test/scala/scala/xml/SerializationTest.scala +++ b/jvm/src/test/scala/scala/xml/SerializationTest.scala @@ -15,6 +15,11 @@ class SerializationTest { assertEquals(NodeSeq.Empty, JavaByteSerialization.roundTrip(NodeSeq.Empty)) } + @Test + def unmatched: Unit = { + assertEquals(NodeSeq.Empty, JavaByteSerialization.roundTrip( \ "HTML")) + } + @Test def implicitConversion: Unit = { val parent = @@ -22,4 +27,11 @@ class SerializationTest { val asNodeSeq: NodeSeq = children assertEquals(asNodeSeq, JavaByteSerialization.roundTrip(asNodeSeq)) } + + @Test + def base64Encode: Unit = { + val str = JavaByteSerialization.base64Encode(NodeSeq.Empty) + assertEquals("rO0ABXNy", str.take(8)) + assertEquals("AHhweA==", str.takeRight(8)) + } } From 1f93e3cfbbfb1a61c7805987994b6f062fa831e1 Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Thu, 12 Oct 2017 09:31:54 -0400 Subject: [PATCH 5/6] Add test dependency Apache commons-lang3 --- build.sbt | 1 + 1 file changed, 1 insertion(+) diff --git a/build.sbt b/build.sbt index 4ecf29d24..5757d2c10 100644 --- a/build.sbt +++ b/build.sbt @@ -41,6 +41,7 @@ lazy val xml = crossProject.in(file(".")) libraryDependencies += "junit" % "junit" % "4.11" % "test", libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test", + libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.6" % "test", libraryDependencies += ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").exclude("org.scala-lang.modules", s"scala-xml_${scalaVersion.value}") ) .jsSettings( From e443d2c9dd574da0d614e3d3a05a43354d1674c7 Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Thu, 12 Oct 2017 09:37:58 -0400 Subject: [PATCH 6/6] Use SerializationUtils from apache.commons.lang3 --- .../scala/xml/JavaByteSerialization.scala | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala index 0e1acddf4..2d9f286d0 100644 --- a/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala +++ b/jvm/src/test/scala/scala/xml/JavaByteSerialization.scala @@ -1,32 +1,27 @@ package scala.xml -import java.io._ +import java.io.Serializable import java.util.Base64 +import org.apache.commons.lang3.SerializationUtils object JavaByteSerialization { - def roundTrip[T](obj: T): T = { - deserialize[T](serialize[T](obj)) + def roundTrip[T <: Serializable](obj: T): T = { + SerializationUtils.roundtrip(obj) } - def serialize[T](in: T): Array[Byte] = { - val bos = new ByteArrayOutputStream() - val oos = new ObjectOutputStream(bos) - oos.writeObject(in) - oos.flush() - bos.toByteArray() + def serialize[T <: Serializable](in: T): Array[Byte] = { + SerializationUtils.serialize(in) } - def deserialize[T](in: Array[Byte]): T = { - val bis = new ByteArrayInputStream(in) - val ois = new ObjectInputStream(bis) - ois.readObject.asInstanceOf[T] + def deserialize[T <: Serializable](in: Array[Byte]): T = { + SerializationUtils.deserialize(in) } - def base64Encode[T](in: T): String = { + def base64Encode[T <: Serializable](in: T): String = { Base64.getEncoder.encodeToString(serialize[T](in)) } - def base64Decode[T](in: String): T = { + def base64Decode[T <: Serializable](in: String): T = { deserialize[T](Base64.getDecoder.decode(in)) } }