4
4
package org .ekrich .config .impl
5
5
6
6
import org .junit .Assert ._
7
- import org . ekrich . config . ConfigOrigin
7
+
8
8
import java .io .Reader
9
9
import java .io .StringReader
10
- import org .ekrich .config .ConfigParseOptions
11
- import org .ekrich .config .ConfigSyntax
12
- import org .ekrich .config .ConfigFactory
13
- import java .io .ByteArrayOutputStream
14
- import java .io .ObjectOutputStream
15
- import java .io .ByteArrayInputStream
16
- import java .io .ObjectInputStream
17
- import java .io .NotSerializableException
18
- import java .io .OutputStream
19
- import java .io .InputStream
20
- import scala .annotation .tailrec
21
10
import java .net .URL
22
11
import java .util .concurrent .Executors
23
12
import java .util .concurrent .Callable
24
- import org .ekrich .config ._
13
+
14
+ import scala .annotation .tailrec
15
+ import scala .jdk .CollectionConverters ._
16
+ import scala .language .implicitConversions
25
17
import scala .reflect .ClassTag
26
18
import scala .reflect .classTag
27
- import scala .jdk .CollectionConverters ._
28
- import language .implicitConversions
19
+
20
+ import org .ekrich .config ._
21
+ import org .ekrich .config .ConfigOrigin
22
+ import org .ekrich .config .ConfigFactory
23
+ import org .ekrich .config .ConfigParseOptions
24
+ import org .ekrich .config .ConfigSyntax
29
25
30
26
abstract trait TestUtils {
31
27
protected def intercept [E <: Throwable : ClassTag ](block : => Any ): E = {
@@ -151,20 +147,6 @@ abstract trait TestUtils {
151
147
a
152
148
}
153
149
154
- private def copyViaSerialize (o : java.io.Serializable ): AnyRef = {
155
- val byteStream = new ByteArrayOutputStream ()
156
- val objectStream = new ObjectOutputStream (byteStream)
157
- objectStream.writeObject(o)
158
- objectStream.close()
159
- val bytes = byteStream.toByteArray()
160
- // outputStringLiteral(bytes) // uncomment to print
161
- val inStream = new ByteArrayInputStream (bytes)
162
- val inObjectStream = new ObjectInputStream (inStream)
163
- val copy = inObjectStream.readObject()
164
- inObjectStream.close()
165
- copy
166
- }
167
-
168
150
def outputStringLiteral (bytes : Array [Byte ]): Unit = {
169
151
val hex = encodeLegibleBinary(bytes)
170
152
outputStringLiteral(hex)
@@ -180,142 +162,6 @@ abstract trait TestUtils {
180
162
}
181
163
}
182
164
183
- protected def checkSerializationCompat [T : ClassTag ](
184
- expectedHex : String ,
185
- o : T ,
186
- changedOK : Boolean = false
187
- ): Unit = {
188
- // be sure we can still deserialize the old one
189
- val inStream = new ByteArrayInputStream (decodeLegibleBinary(expectedHex))
190
- var failure : Option [Exception ] = None
191
- var inObjectStream : ObjectInputStream = null
192
- val deserialized =
193
- try {
194
- inObjectStream = new ObjectInputStream (inStream) // this can throw too
195
- inObjectStream.readObject()
196
- } catch {
197
- case e : Exception =>
198
- failure = Some (e)
199
- null
200
- } finally {
201
- if (inObjectStream != null )
202
- inObjectStream.close()
203
- }
204
-
205
- val why = failure
206
- .map({ e => " : " + e.getClass.getSimpleName + " : " + e.getMessage })
207
- .getOrElse(" " )
208
-
209
- val byteStream = new ByteArrayOutputStream ()
210
- val objectStream = new ObjectOutputStream (byteStream)
211
- objectStream.writeObject(o)
212
- objectStream.close()
213
- val hex = encodeLegibleBinary(byteStream.toByteArray())
214
- def showCorrectResult (): Unit = {
215
- if (expectedHex != hex) {
216
- System .err.println(
217
- " Correct result literal for " + o.getClass.getSimpleName + " serialization:"
218
- )
219
- System .err.println(
220
- " \"\" + "
221
- ) // line up all the lines by using empty string on first line
222
- outputStringLiteral(hex)
223
- }
224
- }
225
-
226
- try {
227
- assertEquals(
228
- " Can no longer deserialize the old format of " + o.getClass.getSimpleName + why,
229
- o,
230
- deserialized
231
- )
232
- assertFalse(failure.isDefined) // should have thrown if we had a failure
233
-
234
- if (! changedOK)
235
- assertEquals(
236
- o.getClass.getSimpleName + " serialization has changed (though we still deserialized the old serialization)" ,
237
- expectedHex,
238
- hex
239
- )
240
- } catch {
241
- case e : Throwable =>
242
- showCorrectResult()
243
- throw e
244
- }
245
- }
246
-
247
- // no ObjectOutputStream / Serialization on Scala.js
248
- protected def checkNotSerializable (o : AnyRef ): Unit = {
249
- val byteStream = new ByteArrayOutputStream ()
250
- val objectStream = new ObjectOutputStream (byteStream)
251
- val e = intercept[NotSerializableException ] {
252
- objectStream.writeObject(o)
253
- }
254
- objectStream.close()
255
- }
256
-
257
- protected def checkSerializable [T : ClassTag ](expectedHex : String , o : T ): T = {
258
- val t = checkSerializable(o)
259
- checkSerializationCompat(expectedHex, o)
260
- t
261
- }
262
-
263
- protected def checkSerializableOldFormat [T : ClassTag ](
264
- expectedHex : String ,
265
- o : T
266
- ): T = {
267
- val t = checkSerializable(o)
268
- checkSerializationCompat(expectedHex, o, changedOK = true )
269
- t
270
- }
271
-
272
- protected def checkSerializableNoMeaningfulEquals [T : ClassTag ](o : T ): T = {
273
- assertTrue(
274
- o.getClass.getSimpleName + " not an instance of Serializable" ,
275
- o.isInstanceOf [java.io.Serializable ]
276
- )
277
-
278
- val a = o.asInstanceOf [java.io.Serializable ]
279
-
280
- val b =
281
- try {
282
- copyViaSerialize(a)
283
- } catch {
284
- case nf : ClassNotFoundException =>
285
- throw new AssertionError (
286
- " failed to make a copy via serialization, " +
287
- " possibly caused by http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6446627" ,
288
- nf
289
- )
290
- case e : Exception =>
291
- e.printStackTrace(System .err)
292
- throw new AssertionError (" failed to make a copy via serialization" , e)
293
- }
294
-
295
- assertTrue(
296
- " deserialized type " + b.getClass.getSimpleName + " doesn't match serialized type " + a.getClass.getSimpleName,
297
- classTag[T ].runtimeClass.isAssignableFrom(b.getClass)
298
- )
299
-
300
- b.asInstanceOf [T ]
301
- }
302
-
303
- protected def checkSerializable [T : ClassTag ](o : T ): T = {
304
- checkEqualObjects(o, o)
305
-
306
- assertTrue(
307
- o.getClass.getSimpleName + " not an instance of Serializable" ,
308
- o.isInstanceOf [java.io.Serializable ]
309
- )
310
-
311
- val b = checkSerializableNoMeaningfulEquals(o)
312
-
313
- checkEqualObjects(o, b)
314
- checkEqualOrigins(o, b)
315
-
316
- b
317
- }
318
-
319
165
// origin() is not part of value equality but is serialized, so
320
166
// we check it separately
321
167
protected def checkEqualOrigins [T ](a : T , b : T ): Unit = (a, b) match {
@@ -984,33 +830,4 @@ abstract trait TestUtils {
984
830
problems.size
985
831
)
986
832
}
987
-
988
- protected def checkSerializableWithCustomSerializer [T : ClassTag ](o : T ): T = {
989
- val byteStream = new ByteArrayOutputStream ()
990
- val objectStream = new CustomObjectOutputStream (byteStream)
991
- objectStream.writeObject(o)
992
- objectStream.close()
993
- val inStream = new ByteArrayInputStream (byteStream.toByteArray)
994
- val inObjectStream = new CustomObjectInputStream (inStream)
995
- val copy = inObjectStream.readObject()
996
- inObjectStream.close()
997
- copy.asInstanceOf [T ]
998
- }
999
-
1000
- class CustomObjectOutputStream (out : OutputStream )
1001
- extends ObjectOutputStream (out) {
1002
- override def writeUTF (str : String ): Unit = {
1003
- val bytes = str.getBytes
1004
- writeLong(bytes.length)
1005
- write(bytes)
1006
- }
1007
- }
1008
-
1009
- class CustomObjectInputStream (in : InputStream ) extends ObjectInputStream (in) {
1010
- override def readUTF (): String = {
1011
- val bytes = new Array [Byte ](readLong().toByte)
1012
- read(bytes)
1013
- new String (bytes)
1014
- }
1015
- }
1016
833
}
0 commit comments