9
9
10
10
package org .scalacheck
11
11
12
- import java .io .{ ByteArrayInputStream , ByteArrayOutputStream , ObjectInputStream , ObjectOutputStream }
12
+ import org .apache .commons .lang3 .SerializationUtils
13
+ import java .io .Serializable
13
14
14
15
import Prop .proved
15
16
16
17
import util .SerializableCanBuildFroms ._
17
18
18
19
object SerializabilitySpecification extends Properties (" Serializability" ) {
19
20
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)
41
28
}
42
29
43
30
def serializableArbitrary [T : Arbitrary ](name : String ) =
44
- property(s " Arbitrary[ $name] serializability " ) = {
31
+ property(s " Arbitrary[ $name] " ) = {
45
32
val arb = implicitly[Arbitrary [T ]]
46
33
assert(serializable(arb))
47
34
@@ -53,7 +40,7 @@ object SerializabilitySpecification extends Properties("Serializability") {
53
40
}
54
41
55
42
def serializableGen [T ](name : String , gen : Gen [T ]) =
56
- property(s " Gen[ $ name] serializability " ) = {
43
+ property(name) = {
57
44
assert(serializable(gen))
58
45
59
46
// forcing the calculation of a value, to trigger the initialization of any lazily initialized field
@@ -64,15 +51,15 @@ object SerializabilitySpecification extends Properties("Serializability") {
64
51
}
65
52
66
53
def serializableCogen [T : Cogen ](name : String ) =
67
- property(s " Cogen[ $name] serializability " ) = {
54
+ property(s " Cogen[ $name] " ) = {
68
55
val gen = Cogen [T ]
69
56
assert(serializable(gen))
70
57
71
58
proved
72
59
}
73
60
74
61
def serializableShrink [T : Shrink ](name : String ) =
75
- property(s " Shrink[ $name] serializability " ) = {
62
+ property(s " Shrink[ $name] " ) = {
76
63
val shrink = implicitly[Shrink [T ]]
77
64
assert(serializable(shrink))
78
65
@@ -89,11 +76,11 @@ object SerializabilitySpecification extends Properties("Serializability") {
89
76
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]" )
90
77
serializableArbitrary[List [(String ,Int )]](" List[(String,Int)]" )
91
78
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 ]))
97
84
98
85
serializableCogen[String ](" String" )
99
86
serializableCogen[Int ](" Int" )
@@ -115,9 +102,8 @@ object SerializabilitySpecification extends Properties("Serializability") {
115
102
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])" )
116
103
serializableShrink[List [(String ,Int )]](" List[(String,Int)]" )
117
104
118
- property(" Seed serializability" ) = {
119
- assert(serializable(rng.Seed (1L )))
120
- proved
105
+ property(" Seed(1L)" ) = {
106
+ serializable(rng.Seed (1L ))
121
107
}
122
108
123
109
}
0 commit comments