CORDA-1662 - Post backport fixes

This commit is contained in:
Katelyn Baker 2018-07-13 15:03:10 +01:00
parent 61b8bb4c6b
commit a47348d52a
6 changed files with 129 additions and 45 deletions

View File

@ -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.serialization.internal.amqp.testutils.AMQPTestUtilsKt;
import net.corda.serialization.internal.amqp.testutils.TestSerializationContext;
import net.corda.nodeapi.internal.serialization.amqp.testutils.AMQPTestUtilsKt;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@ -25,6 +23,7 @@ public class JavaEvolutionTests {
public String getWord() { return word; }
}
*/
// Class as it exists now with the newly added element
static class N1 {
private String word;
@ -69,14 +68,12 @@ public class JavaEvolutionTests {
// Uncomment to regenerate the base state of the test
/*
N1 n = new N1("potato");
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(
n, TestSerializationContext.testSerializationContext));
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(n));
*/
N1 n2 = new DeserializationInput(factory).deserialize(
new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)),
N1.class,
TestSerializationContext.testSerializationContext);
N1.class);
assertEquals(n2.getWord(), "potato");
assertNull(n2.getWibble());
}
@ -86,15 +83,12 @@ public class JavaEvolutionTests {
// Uncomment to regenerate the base state of the test
/*
N2 n = new N2("This is only a test");
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(
n, TestSerializationContext.testSerializationContext));
AMQPTestUtilsKt.writeTestResource(this, new SerializationOutput(factory).serialize(n));
*/
exception.expect(NotSerializableException.class);
new DeserializationInput(factory).deserialize(
new SerializedBytes<>(AMQPTestUtilsKt.readTestResource(this)),
N2.class,
TestSerializationContext.testSerializationContext);
N2.class);
}
}

View File

@ -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

View File

@ -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()
}
}

View File

@ -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)