-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #634 from k163377/fix-#295
Fix ReflectionCache to be serializable
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/kotlin/com/fasterxml/jackson/module/kotlin/JDKSerializabilityTestHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.fasterxml.jackson.module.kotlin | ||
|
||
import junit.framework.TestCase | ||
import java.io.ByteArrayInputStream | ||
import java.io.ByteArrayOutputStream | ||
import java.io.ObjectInputStream | ||
import java.io.ObjectOutputStream | ||
|
||
fun jdkSerialize(o: Any): ByteArray { | ||
val bytes = ByteArrayOutputStream(1000) | ||
val obOut = ObjectOutputStream(bytes) | ||
obOut.writeObject(o) | ||
obOut.close() | ||
return bytes.toByteArray() | ||
} | ||
|
||
fun <T> jdkDeserialize(raw: ByteArray): T? { | ||
val objIn = ObjectInputStream(ByteArrayInputStream(raw)) | ||
return try { | ||
@Suppress("UNCHECKED_CAST") | ||
objIn.readObject() as T | ||
} catch (e: ClassNotFoundException) { | ||
TestCase.fail("Missing class: " + e.message) | ||
null | ||
} finally { | ||
objIn.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/test/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCacheTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.fasterxml.jackson.module.kotlin | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertNotNull | ||
|
||
class ReflectionCacheTest { | ||
@Test | ||
fun serializeEmptyCache() { | ||
val cache = ReflectionCache(100) | ||
val serialized = jdkSerialize(cache) | ||
val deserialized = jdkDeserialize<ReflectionCache>(serialized) | ||
|
||
assertNotNull(deserialized) | ||
// Deserialized instance also do not raise exceptions | ||
deserialized.kotlinFromJava(ReflectionCacheTest::class.java.getDeclaredMethod("serializeEmptyCache")) | ||
} | ||
|
||
@Test | ||
fun serializeNotEmptyCache() { | ||
val method = ReflectionCacheTest::class.java.getDeclaredMethod("serializeNotEmptyCache") | ||
|
||
val cache = ReflectionCache(100).apply { kotlinFromJava(method) } | ||
val serialized = jdkSerialize(cache) | ||
val deserialized = jdkDeserialize<ReflectionCache>(serialized) | ||
|
||
assertNotNull(deserialized) | ||
// Deserialized instance also do not raise exceptions | ||
deserialized.kotlinFromJava(method) | ||
} | ||
} |