[KPM] IdeaKotlinProjectModelObjectGraphTest: Include subtypes when resolving nodes
KT-51386
This commit is contained in:
committed by
Space
parent
2be85610a3
commit
1e5d398d6e
+33
-39
@@ -23,8 +23,6 @@ import kotlin.test.assertTrue
|
|||||||
@RunWith(Parameterized::class)
|
@RunWith(Parameterized::class)
|
||||||
class IdeaKotlinProjectModelObjectGraphTest(private val node: KClass<*>, @Suppress("unused_parameter") clazzName: String) {
|
class IdeaKotlinProjectModelObjectGraphTest(private val node: KClass<*>, @Suppress("unused_parameter") clazzName: String) {
|
||||||
|
|
||||||
private val reflections = Reflections("org.jetbrains.kotlin")
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `test - node implements Serializable`() {
|
fun `test - node implements Serializable`() {
|
||||||
assertTrue(
|
assertTrue(
|
||||||
@@ -35,49 +33,37 @@ class IdeaKotlinProjectModelObjectGraphTest(private val node: KClass<*>, @Suppre
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `test - node implementations contain serialVersionUID`() {
|
fun `test - node implementations contain serialVersionUID`() {
|
||||||
val implementations = reflections.getSubTypesOf(node.java).plus(node.java)
|
if (!node.java.isInterface && !Modifier.isAbstract(node.java.modifiers)) {
|
||||||
.filter { subtype -> !subtype.isInterface && !Modifier.isAbstract(subtype.modifiers) }
|
val serialVersionUID = assertNotNull(
|
||||||
|
node.java.getDeclaredFieldOrNull("serialVersionUID"),
|
||||||
|
"Expected $node to declare 'serialVersionUID' field"
|
||||||
|
)
|
||||||
|
|
||||||
assertTrue(
|
assertTrue(
|
||||||
implementations.isNotEmpty(),
|
Modifier.isStatic(serialVersionUID.modifiers),
|
||||||
"No implementations found for $node"
|
"Expected $node to declare 'serialVersionUID' statically"
|
||||||
)
|
)
|
||||||
|
|
||||||
implementations.forEach { implementation -> assertNodeImplementationDefinesSerialVersionUID(implementation) }
|
assertTrue(
|
||||||
|
serialVersionUID.type.isPrimitive,
|
||||||
|
"Expected $node to declare primitive 'serialVersionUID'"
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
serialVersionUID.type, Long::class.javaPrimitiveType,
|
||||||
|
"Expected $node to declare 'serialVersionUID' of type Long"
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `test - node implementations are marked with InternalKotlinGradlePluginApi when data class`() {
|
fun `test - node implementations are marked with InternalKotlinGradlePluginApi when data class`() {
|
||||||
reflections.getSubTypesOf(node.java).plus(node.java)
|
if (node.isData && node.visibility == PUBLIC) {
|
||||||
.filter { subtype -> subtype.kotlin.isData && subtype.kotlin.visibility == PUBLIC }
|
assertTrue(
|
||||||
.forEach { dataClass ->
|
node.annotations.any { it.annotationClass.simpleName == "InternalKotlinGradlePluginApi" },
|
||||||
assertTrue(
|
"Expected $node to be annotated with '@InternalKotlinGradlePluginApi'"
|
||||||
dataClass.annotations.any { it.annotationClass.simpleName == "InternalKotlinGradlePluginApi" },
|
)
|
||||||
"Expected $dataClass to be annotated with '@InternalKotlinGradlePluginApi'"
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun assertNodeImplementationDefinesSerialVersionUID(implementationClass: Class<*>) {
|
|
||||||
val serialVersionUID = assertNotNull(
|
|
||||||
implementationClass.getDeclaredFieldOrNull("serialVersionUID"),
|
|
||||||
"Expected $implementationClass to declare 'serialVersionUID' field"
|
|
||||||
)
|
|
||||||
|
|
||||||
assertTrue(
|
|
||||||
Modifier.isStatic(serialVersionUID.modifiers),
|
|
||||||
"Expected $implementationClass to declare 'serialVersionUID' statically"
|
|
||||||
)
|
|
||||||
|
|
||||||
assertTrue(
|
|
||||||
serialVersionUID.type.isPrimitive,
|
|
||||||
"Expected $implementationClass to declare primitive 'serialVersionUID'"
|
|
||||||
)
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
serialVersionUID.type, Long::class.javaPrimitiveType,
|
|
||||||
"Expected $implementationClass to declare 'serialVersionUID' of type Long"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Class<*>.getDeclaredFieldOrNull(name: String): Field? {
|
private fun Class<*>.getDeclaredFieldOrNull(name: String): Field? {
|
||||||
@@ -89,6 +75,8 @@ class IdeaKotlinProjectModelObjectGraphTest(private val node: KClass<*>, @Suppre
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private val reflections = Reflections("org.jetbrains.kotlin")
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@Parameterized.Parameters(name = "{1}")
|
@Parameterized.Parameters(name = "{1}")
|
||||||
fun findNodes(): List<Array<Any>> {
|
fun findNodes(): List<Array<Any>> {
|
||||||
@@ -100,6 +88,12 @@ class IdeaKotlinProjectModelObjectGraphTest(private val node: KClass<*>, @Suppre
|
|||||||
children.forEach { child ->
|
children.forEach { child ->
|
||||||
if (classes.add(child)) {
|
if (classes.add(child)) {
|
||||||
resolveQueue.add(child)
|
resolveQueue.add(child)
|
||||||
|
if (child.java.isInterface || Modifier.isAbstract(child.java.modifiers)) {
|
||||||
|
val subtypes = reflections.getSubTypesOf(child.java).map { it.kotlin }
|
||||||
|
assertTrue(subtypes.isNotEmpty(), "Missing implementations for $child")
|
||||||
|
classes.addAll(subtypes)
|
||||||
|
resolveQueue.addAll(subtypes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user