Skip to content

Commit c135316

Browse files
committed
Add JDK serializability test for ReflectionCache
1 parent d75498c commit c135316

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fasterxml.jackson.module.kotlin
2+
3+
import junit.framework.TestCase
4+
import java.io.ByteArrayInputStream
5+
import java.io.ByteArrayOutputStream
6+
import java.io.ObjectInputStream
7+
import java.io.ObjectOutputStream
8+
9+
fun jdkSerialize(o: Any): ByteArray {
10+
val bytes = ByteArrayOutputStream(1000)
11+
val obOut = ObjectOutputStream(bytes)
12+
obOut.writeObject(o)
13+
obOut.close()
14+
return bytes.toByteArray()
15+
}
16+
17+
fun <T> jdkDeserialize(raw: ByteArray): T? {
18+
val objIn = ObjectInputStream(ByteArrayInputStream(raw))
19+
return try {
20+
@Suppress("UNCHECKED_CAST")
21+
objIn.readObject() as T
22+
} catch (e: ClassNotFoundException) {
23+
TestCase.fail("Missing class: " + e.message)
24+
null
25+
} finally {
26+
objIn.close()
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fasterxml.jackson.module.kotlin
2+
3+
import org.junit.Test
4+
import kotlin.test.assertNotNull
5+
6+
class ReflectionCacheTest {
7+
@Test
8+
fun serializeEmptyCache() {
9+
val cache = ReflectionCache(100)
10+
val serialized = jdkSerialize(cache)
11+
val deserialized = jdkDeserialize<ReflectionCache>(serialized)
12+
13+
assertNotNull(deserialized)
14+
// Deserialized instance also do not raise exceptions
15+
deserialized.kotlinFromJava(ReflectionCacheTest::class.java.getDeclaredMethod("serializeEmptyCache"))
16+
}
17+
18+
@Test
19+
fun serializeNotEmptyCache() {
20+
val method = ReflectionCacheTest::class.java.getDeclaredMethod("serializeNotEmptyCache")
21+
22+
val cache = ReflectionCache(100).apply { kotlinFromJava(method) }
23+
val serialized = jdkSerialize(cache)
24+
val deserialized = jdkDeserialize<ReflectionCache>(serialized)
25+
26+
assertNotNull(deserialized)
27+
// Deserialized instance also do not raise exceptions
28+
deserialized.kotlinFromJava(method)
29+
}
30+
}

0 commit comments

Comments
 (0)