jvmMain.dk.cachet.carp.test.serialization.SerializerRegistrationTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of carp.test-jvm Show documentation
Show all versions of carp.test-jvm Show documentation
Helper classes relied upon by test projects of all subsystems. E.g., to disable tests specified in common part of projects for the JavaScript runtime only.
package dk.cachet.carp.test.serialization
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.modules.SerializersModule
import org.reflections.Reflections
import java.lang.reflect.Modifier
import kotlin.jvm.internal.Reflection
import kotlin.test.*
/**
* Verifies whether all extending types of the interface or abstract class [T]
* are registered for polymorphic serialization in [serializersModule].
*
* It is assumed all extending classes are located in the same namespace.
*/
@ExperimentalSerializationApi
inline fun verifyTypesAreRegistered( serializersModule: SerializersModule )
{
val klass = T::class
check( klass.isAbstract )
val namespace = klass.java.`package`.name
val polymorphicSerializers = getPolymorphicSerializers( serializersModule )
val reflections = Reflections( namespace )
reflections
.getSubTypesOf( klass.java )
.filter { serializable ->
// Wrappers for unknown types are only used at runtime and don't need to be serializable.
serializable.interfaces.none { it.simpleName == "UnknownPolymorphicWrapper" } &&
// Only verify concrete types.
!Modifier.isAbstract( serializable.modifiers ) && !Modifier.isInterface( serializable.modifiers ) &&
// Ignore private types since they are not part of the API.
Modifier.isPublic( serializable.modifiers )
}
.forEach {
val kotlinClass = Reflection.createKotlinClass( it )
val serializer = polymorphicSerializers[ kotlinClass ]
assertNotNull( serializer, "No serializer registered for '$it'." )
}
}