mirror of
https://github.com/corda/corda.git
synced 2025-01-14 08:49:47 +00:00
CORDA-1662 - Post backport fixes
This commit is contained in:
parent
61b8bb4c6b
commit
a47348d52a
@ -1,9 +1,7 @@
|
|||||||
package net.corda.serialization.internal.amqp;
|
package net.corda.nodeapi.internal.serialization.amqp;
|
||||||
|
|
||||||
import kotlin.Suppress;
|
|
||||||
import net.corda.core.serialization.SerializedBytes;
|
import net.corda.core.serialization.SerializedBytes;
|
||||||
import net.corda.serialization.internal.amqp.testutils.AMQPTestUtilsKt;
|
import net.corda.nodeapi.internal.serialization.amqp.testutils.AMQPTestUtilsKt;
|
||||||
import net.corda.serialization.internal.amqp.testutils.TestSerializationContext;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
@ -25,6 +23,7 @@ public class JavaEvolutionTests {
|
|||||||
public String getWord() { return word; }
|
public String getWord() { return word; }
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Class as it exists now with the newly added element
|
// Class as it exists now with the newly added element
|
||||||
static class N1 {
|
static class N1 {
|
||||||
private String word;
|
private String word;
|
||||||
@ -69,14 +68,12 @@ public class JavaEvolutionTests {
|
|||||||
// Uncomment to regenerate the base state of the test
|
// Uncomment to regenerate the base state of the test
|
||||||
/*
|
/*
|
||||||
N1 n = new N1("potato");
|
N1 n = new N1("potato");
|
||||||
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(
|
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(n));
|
||||||
n, TestSerializationContext.testSerializationContext));
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
N1 n2 = new DeserializationInput(factory).deserialize(
|
N1 n2 = new DeserializationInput(factory).deserialize(
|
||||||
new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)),
|
new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)),
|
||||||
N1.class,
|
N1.class);
|
||||||
TestSerializationContext.testSerializationContext);
|
|
||||||
assertEquals(n2.getWord(), "potato");
|
assertEquals(n2.getWord(), "potato");
|
||||||
assertNull(n2.getWibble());
|
assertNull(n2.getWibble());
|
||||||
}
|
}
|
||||||
@ -86,15 +83,12 @@ public class JavaEvolutionTests {
|
|||||||
// Uncomment to regenerate the base state of the test
|
// Uncomment to regenerate the base state of the test
|
||||||
/*
|
/*
|
||||||
N2 n = new N2("This is only a test");
|
N2 n = new N2("This is only a test");
|
||||||
|
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(n));
|
||||||
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(
|
|
||||||
n, TestSerializationContext.testSerializationContext));
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
exception.expect(NotSerializableException.class);
|
exception.expect(NotSerializableException.class);
|
||||||
new DeserializationInput(factory).deserialize(
|
new DeserializationInput(factory).deserialize(
|
||||||
new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)),
|
new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)),
|
||||||
N2.class,
|
N2.class);
|
||||||
TestSerializationContext.testSerializationContext);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,32 +0,0 @@
|
|||||||
package net.corda.nodeapi.internal.serialization.amqp
|
|
||||||
|
|
||||||
import org.apache.qpid.proton.codec.Data
|
|
||||||
import net.corda.nodeapi.internal.serialization.AllWhitelist
|
|
||||||
import net.corda.nodeapi.internal.serialization.EmptyWhitelist
|
|
||||||
|
|
||||||
fun testDefaultFactory() = SerializerFactory(AllWhitelist, ClassLoader.getSystemClassLoader())
|
|
||||||
fun testDefaultFactoryNoEvolution() = SerializerFactory(AllWhitelist, ClassLoader.getSystemClassLoader(),
|
|
||||||
EvolutionSerializerGetterTesting())
|
|
||||||
fun testDefaultFactoryWithWhitelist() = SerializerFactory(EmptyWhitelist, ClassLoader.getSystemClassLoader())
|
|
||||||
|
|
||||||
class TestSerializationOutput(
|
|
||||||
private val verbose: Boolean,
|
|
||||||
serializerFactory: SerializerFactory = testDefaultFactory())
|
|
||||||
: SerializationOutput(serializerFactory) {
|
|
||||||
|
|
||||||
override fun writeSchema(schema: Schema, data: Data) {
|
|
||||||
if (verbose) println(schema)
|
|
||||||
super.writeSchema(schema, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun writeTransformSchema(transformsSchema: TransformsSchema, data: Data) {
|
|
||||||
if(verbose) {
|
|
||||||
println ("Writing Transform Schema")
|
|
||||||
println (transformsSchema)
|
|
||||||
}
|
|
||||||
super.writeTransformSchema(transformsSchema, data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun testName(): String = Thread.currentThread().stackTrace[2].methodName
|
|
||||||
|
|
@ -0,0 +1,106 @@
|
|||||||
|
package net.corda.nodeapi.internal.serialization.amqp.testutils
|
||||||
|
|
||||||
|
import net.corda.core.internal.copyTo
|
||||||
|
import net.corda.core.internal.div
|
||||||
|
|
||||||
|
import net.corda.core.serialization.SerializationContext
|
||||||
|
import net.corda.core.serialization.SerializedBytes
|
||||||
|
import net.corda.core.utilities.OpaqueBytes
|
||||||
|
import net.corda.nodeapi.internal.serialization.AllWhitelist
|
||||||
|
import net.corda.nodeapi.internal.serialization.EmptyWhitelist
|
||||||
|
import net.corda.nodeapi.internal.serialization.amqp.*
|
||||||
|
import net.corda.testing.common.internal.ProjectStructure
|
||||||
|
import org.apache.qpid.proton.codec.Data
|
||||||
|
import org.junit.Test
|
||||||
|
import java.io.File.separatorChar
|
||||||
|
import java.io.NotSerializableException
|
||||||
|
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
|
||||||
|
|
||||||
|
fun testDefaultFactory() = SerializerFactory(AllWhitelist, ClassLoader.getSystemClassLoader())
|
||||||
|
|
||||||
|
fun testDefaultFactoryNoEvolution(): SerializerFactory {
|
||||||
|
return SerializerFactory(
|
||||||
|
AllWhitelist,
|
||||||
|
ClassLoader.getSystemClassLoader(),
|
||||||
|
evolutionSerializerGetter = EvolutionSerializerGetterTesting())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testDefaultFactoryWithWhitelist() = SerializerFactory(EmptyWhitelist, ClassLoader.getSystemClassLoader())
|
||||||
|
|
||||||
|
class TestSerializationOutput(
|
||||||
|
private val verbose: Boolean,
|
||||||
|
serializerFactory: SerializerFactory = testDefaultFactory())
|
||||||
|
: SerializationOutput(serializerFactory) {
|
||||||
|
|
||||||
|
override fun writeSchema(schema: Schema, data: Data) {
|
||||||
|
if (verbose) println(schema)
|
||||||
|
super.writeSchema(schema, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun writeTransformSchema(transformsSchema: TransformsSchema, data: Data) {
|
||||||
|
if(verbose) {
|
||||||
|
println ("Writing Transform Schema")
|
||||||
|
println (transformsSchema)
|
||||||
|
}
|
||||||
|
super.writeTransformSchema(transformsSchema, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(NotSerializableException::class)
|
||||||
|
override fun <T : Any> serialize(obj: T): SerializedBytes<T> {
|
||||||
|
try {
|
||||||
|
return _serialize(obj)
|
||||||
|
} finally {
|
||||||
|
andFinally()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testName(): String {
|
||||||
|
val classLoader = Thread.currentThread().contextClassLoader
|
||||||
|
return Thread.currentThread().stackTrace.first {
|
||||||
|
try {
|
||||||
|
classLoader.loadClass(it.className).getMethod(it.methodName).isAnnotationPresent(Test::class.java)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}.methodName
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Any.testResourceName(): String = "${javaClass.simpleName}.${testName()}"
|
||||||
|
|
||||||
|
fun Any.writeTestResource(bytes: OpaqueBytes) {
|
||||||
|
val dir = ProjectStructure.projectRootDir / "node-api" / "src" / "test" / "resources" / javaClass.`package`.name.replace('.', separatorChar)
|
||||||
|
bytes.open().copyTo(dir / testResourceName(), REPLACE_EXISTING)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Any.readTestResource(): ByteArray = javaClass.getResourceAsStream(testResourceName()).readBytes()
|
||||||
|
|
||||||
|
@Throws(NotSerializableException::class)
|
||||||
|
inline fun <reified T : Any> DeserializationInput.deserializeAndReturnEnvelope(
|
||||||
|
bytes: SerializedBytes<T>,
|
||||||
|
context: SerializationContext? = null
|
||||||
|
) : ObjectAndEnvelope<T> {
|
||||||
|
return deserializeAndReturnEnvelope(bytes, T::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(NotSerializableException::class)
|
||||||
|
inline fun <reified T : Any> DeserializationInput.deserialize(
|
||||||
|
bytes: SerializedBytes<T>,
|
||||||
|
context: SerializationContext? = null
|
||||||
|
) : T = deserialize(bytes, T::class.java)
|
||||||
|
|
||||||
|
|
||||||
|
@Throws(NotSerializableException::class)
|
||||||
|
fun <T : Any> SerializationOutput.serializeAndReturnSchema(
|
||||||
|
obj: T, context: SerializationContext? = null
|
||||||
|
): BytesAndSchemas<T> = serializeAndReturnSchema(obj)
|
||||||
|
|
||||||
|
|
||||||
|
@Throws(NotSerializableException::class)
|
||||||
|
fun <T : Any> SerializationOutput.serialize(obj: T): SerializedBytes<T> {
|
||||||
|
try {
|
||||||
|
return _serialize(obj)
|
||||||
|
} finally {
|
||||||
|
andFinally()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package net.corda.nodeapi.internal.serialization.amqp.testutils
|
||||||
|
|
||||||
|
import net.corda.core.serialization.SerializationContext
|
||||||
|
import net.corda.nodeapi.internal.serialization.AllWhitelist
|
||||||
|
import net.corda.nodeapi.internal.serialization.SerializationContextImpl
|
||||||
|
import net.corda.nodeapi.internal.serialization.amqp.AmqpHeaderV1_0
|
||||||
|
|
||||||
|
val serializationProperties: MutableMap<Any, Any> = mutableMapOf()
|
||||||
|
|
||||||
|
val testSerializationContext = SerializationContextImpl(
|
||||||
|
preferredSerializationVersion = AmqpHeaderV1_0,
|
||||||
|
deserializationClassLoader = ClassLoader.getSystemClassLoader(),
|
||||||
|
whitelist = AllWhitelist,
|
||||||
|
properties = serializationProperties,
|
||||||
|
objectReferencesEnabled = false,
|
||||||
|
useCase = SerializationContext.UseCase.P2P)
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user