[KPM] Implement test to check if data classes are marked as internal

KT-51262
KT-51220
This commit is contained in:
sebastian.sellmair
2022-02-17 14:17:40 +01:00
committed by Space
parent fa13e38e91
commit 21d6c11bbb
2 changed files with 32 additions and 5 deletions
@@ -8,3 +8,11 @@ package org.jetbrains.kotlin.gradle.kpm.idea
import java.io.Serializable
interface IdeaKotlinFragmentDependency : Serializable
@Suppress("unused")
@InternalKotlinGradlePluginApi
class IdeaKotlinFragmentDependencyImpl: IdeaKotlinFragmentDependency {
companion object {
const val serialVersionUID = 0L
}
}
@@ -11,6 +11,8 @@ import org.reflections.Reflections
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import kotlin.reflect.KClass
import kotlin.reflect.KVisibility
import kotlin.reflect.KVisibility.PUBLIC
import kotlin.reflect.full.memberProperties
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -21,6 +23,8 @@ import kotlin.test.assertTrue
@RunWith(Parameterized::class)
class IdeaKotlinProjectModelObjectGraphTest(private val node: KClass<*>, @Suppress("unused_parameter") clazzName: String) {
private val reflections = Reflections("org.jetbrains.kotlin")
@Test
fun `test - node implements Serializable`() {
assertTrue(
@@ -31,12 +35,27 @@ class IdeaKotlinProjectModelObjectGraphTest(private val node: KClass<*>, @Suppre
@Test
fun `test - node implementations contain serialVersionUID`() {
val reflections = Reflections("org.jetbrains.kotlin")
reflections.getSubTypesOf(node.java).forEach { subtype ->
if (!subtype.isInterface && !Modifier.isAbstract(subtype.modifiers)) {
assertNodeImplementationDefinesSerialVersionUID(subtype)
val implementations = reflections.getSubTypesOf(node.java).plus(node.java)
.filter { subtype -> !subtype.isInterface && !Modifier.isAbstract(subtype.modifiers) }
assertTrue(
implementations.isNotEmpty(),
"No implementations found for $node"
)
implementations.forEach { implementation -> assertNodeImplementationDefinesSerialVersionUID(implementation) }
}
@Test
fun `test - node implementations are marked with InternalKotlinGradlePluginApi when data class`() {
reflections.getSubTypesOf(node.java).plus(node.java)
.filter { subtype -> subtype.kotlin.isData && subtype.kotlin.visibility == PUBLIC }
.forEach { dataClass ->
assertTrue(
dataClass.annotations.any { it.annotationClass.simpleName == "InternalKotlinGradlePluginApi" },
"Expected $dataClass to be annotated with '@InternalKotlinGradlePluginApi'"
)
}
}
}
private fun assertNodeImplementationDefinesSerialVersionUID(implementationClass: Class<*>) {