Merge pull request #17144 from colinrgodsey/15170-bytestringser-2

ByteString serialization
This commit is contained in:
Konrad Malawski 2015-04-08 11:44:03 +02:00
commit 8ad51fba39
2 changed files with 128 additions and 5 deletions

View file

@ -4,6 +4,8 @@
package akka.util
import java.io.{ ByteArrayInputStream, ObjectInputStream, ObjectOutputStream, ByteArrayOutputStream }
import org.scalatest.WordSpec
import org.scalatest.Matchers
import org.scalatest.prop.Checkers
@ -11,6 +13,8 @@ import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.apache.commons.codec.binary.Hex.{ encodeHex, decodeHex }
import scala.collection.mutable.Builder
import java.nio.{ ByteBuffer }
@ -56,6 +60,23 @@ class ByteStringSpec extends WordSpec with Matchers with Checkers {
} yield (xs, from, until)
}
def testSer(obj: AnyRef) = {
val os = new ByteArrayOutputStream
val bos = new ObjectOutputStream(os)
bos.writeObject(obj)
val arr = os.toByteArray
val is = new ObjectInputStream(new ByteArrayInputStream(arr))
is.readObject == obj
}
def hexFromSer(obj: AnyRef) = {
val os = new ByteArrayOutputStream
val bos = new ObjectOutputStream(os)
bos.writeObject(obj)
String valueOf encodeHex(os.toByteArray)
}
val arbitraryByteArray: Arbitrary[Array[Byte]] = Arbitrary { Gen.sized { n Gen.containerOfN[Array, Byte](n, arbitrary[Byte]) } }
implicit val arbitraryByteArraySlice: Arbitrary[ArraySlice[Byte]] = arbSlice(arbitraryByteArray)
val arbitraryShortArray: Arbitrary[Array[Short]] = Arbitrary { Gen.sized { n Gen.containerOfN[Array, Short](n, arbitrary[Short]) } }
@ -372,6 +393,22 @@ class ByteStringSpec extends WordSpec with Matchers with Checkers {
}
}
}
"serialize correctly" when {
"parsing regular ByteString1C as compat" in {
val oldSerd = "aced000573720021616b6b612e7574696c2e42797465537472696e672442797465537472696e67314336e9eed0afcfe4a40200015b000562797465737400025b427872001b616b6b612e7574696c2e436f6d7061637442797465537472696e67fa2925150f93468f0200007870757200025b42acf317f8060854e002000078700000000a74657374737472696e67"
val bs = ByteString("teststring", "UTF8")
val str = hexFromSer(bs)
require(oldSerd == str)
}
"given all types of ByteString" in {
check { bs: ByteString
testSer(bs)
}
}
}
}
"A ByteStringIterator" must {