Skip to content

Commit b21aa53

Browse files
committed
Test serialization with commons-lang3
1 parent ecb819b commit b21aa53

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ lazy val jvm = project.in(file("jvm"))
244244
}
245245
baseDirectory.value / "src" / "test" / s
246246
},
247+
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.12.0" % "test",
247248
libraryDependencies += "org.scala-sbt" % "test-interface" % "1.0"
248249
)
249250

jvm/src/test/scala/org/scalacheck/SerializabilitySpecification.scala

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,26 @@
99

1010
package org.scalacheck
1111

12-
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream }
12+
import org.apache.commons.lang3.SerializationUtils
13+
import java.io.Serializable
1314

1415
import Prop.proved
1516

1617
import util.SerializableCanBuildFroms._
1718

1819
object SerializabilitySpecification extends Properties("Serializability") {
1920

20-
// adapted from https://github.com/milessabin/shapeless/blob/6b870335c219d59079b46eddff15028332c0c294/core/jvm/src/test/scala/shapeless/serialization.scala#L42-L62
21-
private def serializable[M](m: M): Boolean = {
22-
val baos = new ByteArrayOutputStream
23-
val oos = new ObjectOutputStream(baos)
24-
var ois: ObjectInputStream = null
25-
try {
26-
oos.writeObject(m)
27-
oos.close()
28-
val bais = new ByteArrayInputStream(baos.toByteArray)
29-
ois = new ObjectInputStream(bais)
30-
val m2 = ois.readObject() // just ensure we can read it back
31-
ois.close()
32-
true
33-
} catch {
34-
case thr: Throwable =>
35-
thr.printStackTrace
36-
false
37-
} finally {
38-
oos.close()
39-
if (ois != null) ois.close()
40-
}
21+
def serializable[M <: Serializable](m: M): Boolean = {
22+
val arr = serialize(m)
23+
null != arr && arr.nonEmpty
24+
}
25+
26+
def serialize[T <: Serializable](in: T): Array[Byte] = {
27+
SerializationUtils.serialize(in)
4128
}
4229

4330
def serializableArbitrary[T: Arbitrary](name: String) =
44-
property(s"Arbitrary[$name] serializability") = {
31+
property(s"Arbitrary[$name]") = {
4532
val arb = implicitly[Arbitrary[T]]
4633
assert(serializable(arb))
4734

@@ -53,7 +40,7 @@ object SerializabilitySpecification extends Properties("Serializability") {
5340
}
5441

5542
def serializableGen[T](name: String, gen: Gen[T]) =
56-
property(s"Gen[$name] serializability") = {
43+
property(name) = {
5744
assert(serializable(gen))
5845

5946
// forcing the calculation of a value, to trigger the initialization of any lazily initialized field
@@ -64,15 +51,15 @@ object SerializabilitySpecification extends Properties("Serializability") {
6451
}
6552

6653
def serializableCogen[T: Cogen](name: String) =
67-
property(s"Cogen[$name] serializability") = {
54+
property(s"Cogen[$name]") = {
6855
val gen = Cogen[T]
6956
assert(serializable(gen))
7057

7158
proved
7259
}
7360

7461
def serializableShrink[T: Shrink](name: String) =
75-
property(s"Shrink[$name] serializability") = {
62+
property(s"Shrink[$name]") = {
7663
val shrink = implicitly[Shrink[T]]
7764
assert(serializable(shrink))
7865

@@ -89,11 +76,11 @@ object SerializabilitySpecification extends Properties("Serializability") {
8976
serializableArbitrary[(Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int)]("Tuple22[Int]")
9077
serializableArbitrary[List[(String,Int)]]("List[(String,Int)]")
9178

92-
serializableGen("identifier", Gen.identifier)
93-
serializableGen("oneOf", Gen.oneOf(true, false))
94-
serializableGen("choose", Gen.choose(1, 10))
95-
serializableGen("function1", Gen.function1[Int, Int](Gen.choose(1, 10)))
96-
serializableGen("zip(String,Int)", Gen.zip(Arbitrary.arbitrary[String], Arbitrary.arbitrary[Int]))
79+
serializableGen("Gen.identifier", Gen.identifier)
80+
serializableGen("Gen.oneOf", Gen.oneOf(true, false))
81+
serializableGen("Gen.choose", Gen.choose(1, 10))
82+
serializableGen("Gen.function1", Gen.function1[Int, Int](Gen.choose(1, 10)))
83+
serializableGen("Gen.zip(String,Int)", Gen.zip(Arbitrary.arbitrary[String], Arbitrary.arbitrary[Int]))
9784

9885
serializableCogen[String]("String")
9986
serializableCogen[Int]("Int")
@@ -115,9 +102,8 @@ object SerializabilitySpecification extends Properties("Serializability") {
115102
serializableShrink[(Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int)]("Tuple22[Int])")
116103
serializableShrink[List[(String,Int)]]("List[(String,Int)]")
117104

118-
property("Seed serializability") = {
119-
assert(serializable(rng.Seed(1L)))
120-
proved
105+
property("Seed(1L)") = {
106+
serializable(rng.Seed(1L))
121107
}
122108

123109
}

0 commit comments

Comments
 (0)