Skip to content

Commit

Permalink
chore: Rename class Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
warnyul committed May 7, 2024
1 parent 9765491 commit ff68b26
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package tech.apter.junit.jupiter.robolectric.internal

import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNestedTest
import tech.apter.junit.jupiter.robolectric.internal.extensions.isJUnit5NestedTest
import tech.apter.junit.jupiter.robolectric.internal.extensions.nearestOuterNestedTestOrOuterMostDeclaringClass

internal fun robolectricClassLoaderFactory(testClass: Class<*>): ClassLoader {
val testClassForRunner = if (testClass.kotlin.isCompanion) {
testClass.declaringClass
} else if (testClass.isNestedTest) {
} else if (testClass.isJUnit5NestedTest) {
testClass
} else if (testClass.isNested) {
} else if (testClass.declaringClass != null) {
testClass.nearestOuterNestedTestOrOuterMostDeclaringClass()
} else {
testClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tech.apter.junit.jupiter.robolectric.internal.extensions
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.extension.ExtendWith
import tech.apter.junit.jupiter.robolectric.RobolectricExtension
import java.lang.reflect.Modifier

internal fun Class<*>.isExtendedWithRobolectric(): Boolean {
val isExtended = findAnnotations(ExtendWith::class.java).any { annotation ->
Expand All @@ -21,15 +22,15 @@ internal fun Class<*>.isExtendedWithRobolectric(): Boolean {
}
}

internal inline val Class<*>.isNested: Boolean get() = declaringClass != null
internal inline val Class<*>.isNestedTest: Boolean
get() = isNested && findAnnotations(type = Nested::class.java).isNotEmpty()
internal inline val Class<*>.isNonStaticInnerClass: Boolean
get() = declaringClass != null && !Modifier.isStatic(modifiers)

internal inline val Class<*>.isJUnit5NestedTest: Boolean
get() = isNonStaticInnerClass && findAnnotations(type = Nested::class.java).isNotEmpty()

internal tailrec fun Class<*>.nearestOuterNestedTestOrOuterMostDeclaringClass(): Class<*> {
return if (declaringClass == null || isNestedTest) {
return if (declaringClass == null || isJUnit5NestedTest) {
this
} else if (declaringClass.isNestedTest) {
declaringClass
} else {
declaringClass.nearestOuterNestedTestOrOuterMostDeclaringClass()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import org.robolectric.annotation.Config
import org.robolectric.pluginapi.config.GlobalConfigProvider
import org.robolectric.plugins.ConfigConfigurer
import org.robolectric.plugins.PackagePropertiesLoader
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass

internal class JUnit5ConfigConfigurer(
packagePropertiesLoader: PackagePropertiesLoader,
defaultConfigProvider: GlobalConfigProvider,
) : ConfigConfigurer(packagePropertiesLoader, defaultConfigProvider) {
override fun getConfigFor(testClass: Class<*>): Config? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -20,7 +20,7 @@ internal class JUnit5ConfigConfigurer(

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): Config? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package tech.apter.junit.jupiter.robolectric.internal.plugins
import org.robolectric.annotation.ConscryptMode
import org.robolectric.plugins.ConscryptModeConfigurer
import org.robolectric.plugins.PackagePropertiesLoader
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass
import java.util.Properties

internal class JUnit5ConscryptModeConfigurer(
systemProperties: Properties,
propertyFileLoader: PackagePropertiesLoader,
) : ConscryptModeConfigurer(systemProperties, propertyFileLoader) {
override fun getConfigFor(testClass: Class<*>): ConscryptMode.Mode? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -20,7 +20,7 @@ internal class JUnit5ConscryptModeConfigurer(

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): ConscryptMode.Mode? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package tech.apter.junit.jupiter.robolectric.internal.plugins

import org.robolectric.annotation.GetInstallerPackageNameMode
import org.robolectric.plugins.GetInstallerPackageNameModeConfigurer
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass

internal class JUnit5GetInstallerPackageNameModeConfigurer : GetInstallerPackageNameModeConfigurer() {
override fun getConfigFor(testClass: Class<*>): GetInstallerPackageNameMode.Mode? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -15,7 +15,7 @@ internal class JUnit5GetInstallerPackageNameModeConfigurer : GetInstallerPackage

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): GetInstallerPackageNameMode.Mode? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tech.apter.junit.jupiter.robolectric.internal.plugins
import org.robolectric.annotation.GraphicsMode
import org.robolectric.plugins.GraphicsModeConfigurer
import org.robolectric.plugins.PackagePropertiesLoader
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass
import java.util.Properties

internal class JUnit5GraphicsModeConfigurer(
Expand All @@ -12,7 +12,7 @@ internal class JUnit5GraphicsModeConfigurer(
) : GraphicsModeConfigurer(systemProperties, propertyFileLoader) {

override fun getConfigFor(testClass: Class<*>): GraphicsMode.Mode? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -21,7 +21,7 @@ internal class JUnit5GraphicsModeConfigurer(

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): GraphicsMode.Mode? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tech.apter.junit.jupiter.robolectric.internal.plugins
import org.robolectric.annotation.experimental.LazyApplication
import org.robolectric.plugins.LazyApplicationConfigurer
import org.robolectric.plugins.PackagePropertiesLoader
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass
import java.util.Properties

internal class JUnit5LazyApplicationConfigurer(
Expand All @@ -12,7 +12,7 @@ internal class JUnit5LazyApplicationConfigurer(
) : LazyApplicationConfigurer(systemProperties, propertyFileLoader) {

override fun getConfigFor(testClass: Class<*>): LazyApplication.LazyLoad? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -21,7 +21,7 @@ internal class JUnit5LazyApplicationConfigurer(

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): LazyApplication.LazyLoad? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package tech.apter.junit.jupiter.robolectric.internal.plugins
import org.robolectric.annotation.LooperMode
import org.robolectric.plugins.LooperModeConfigurer
import org.robolectric.plugins.PackagePropertiesLoader
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass
import java.util.Properties

internal class JUnit5LooperModeConfigurer(
systemProperties: Properties,
propertyFileLoader: PackagePropertiesLoader,
) : LooperModeConfigurer(systemProperties, propertyFileLoader) {
override fun getConfigFor(testClass: Class<*>): LooperMode.Mode? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -20,7 +20,7 @@ internal class JUnit5LooperModeConfigurer(

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): LooperMode.Mode? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package tech.apter.junit.jupiter.robolectric.internal.plugins
import org.robolectric.annotation.SQLiteMode
import org.robolectric.plugins.PackagePropertiesLoader
import org.robolectric.plugins.SQLiteModeConfigurer
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass
import java.util.Properties

internal class JUnit5SQLiteModeConfigurer(
systemProperties: Properties,
propertyFileLoader: PackagePropertiesLoader,
) : SQLiteModeConfigurer(systemProperties, propertyFileLoader) {
override fun getConfigFor(testClass: Class<*>): SQLiteMode.Mode? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -20,7 +20,7 @@ internal class JUnit5SQLiteModeConfigurer(

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): SQLiteMode.Mode? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package tech.apter.junit.jupiter.robolectric.internal.plugins
import org.robolectric.annotation.TextLayoutMode
import org.robolectric.plugins.PackagePropertiesLoader
import org.robolectric.plugins.TextLayoutModeConfigurer
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNested
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNonStaticInnerClass
import java.util.Properties

internal class JUnit5TextLayoutModeConfigurer(
systemProperties: Properties,
propertyFileLoader: PackagePropertiesLoader,
) : TextLayoutModeConfigurer(systemProperties, propertyFileLoader) {
override fun getConfigFor(testClass: Class<*>): TextLayoutMode.Mode? {
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
getConfigMergedWithDeclaringClassConfig(testClass)
} else {
super.getConfigFor(testClass)
Expand All @@ -20,7 +20,7 @@ internal class JUnit5TextLayoutModeConfigurer(

private fun getConfigMergedWithDeclaringClassConfig(testClass: Class<*>): TextLayoutMode.Mode? {
val config = super.getConfigFor(testClass)
return if (testClass.isNested) {
return if (testClass.isNonStaticInnerClass) {
val parentConfig = getConfigMergedWithDeclaringClassConfig(testClass.declaringClass)
config?.let { c -> return parentConfig?.let { p -> merge(p, c) } ?: c } ?: parentConfig
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.parallel.ExecutionMode
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode
import tech.apter.junit.jupiter.robolectric.internal.extensions.isNestedTest
import tech.apter.junit.jupiter.robolectric.internal.extensions.isJUnit5NestedTest

internal object TestClassValidator {

Expand All @@ -23,7 +23,7 @@ internal object TestClassValidator {

private fun validateNestedTestClassCanNotOverrideRuntimeSdk(testClass: Class<*>) {
val config = testClass.getAnnotation(Config::class.java)
if (testClass.isNestedTest && config != null && config.sdk.isNotEmpty()) {
if (testClass.isJUnit5NestedTest && config != null && config.sdk.isNotEmpty()) {
error("Robolectric runtime sdk cannot be used on nested test class: ${testClass.name}")
}
}
Expand All @@ -32,7 +32,7 @@ internal object TestClassValidator {
testClass: Class<*>,
vararg annotationsClasses: Class<out Annotation>,
) {
if (testClass.isNestedTest) {
if (testClass.isJUnit5NestedTest) {
annotationsClasses.forEach { annotationClass ->
val annotation = testClass.getAnnotation(annotationClass)
if (annotation != null) {
Expand All @@ -55,16 +55,16 @@ internal object TestClassValidator {
private fun validateTestWithNestedTestsCanNotBeExecutedConcurrently(
testClass: Class<*>,
) {
fun Class<*>.isDeclaredNestedTestClasses() = declaredClasses.any { it.isNestedTest }
fun Class<*>.isConcurrent() =
fun Class<*>.isDeclaredJUnit5NestedTestClasses() = declaredClasses.any { it.isJUnit5NestedTest }
fun Class<*>.isConcurrentExecutionEnabled() =
(
System.getProperty("junit.jupiter.execution.parallel.mode.classes.default") == "concurrent" &&
executionMode() != ExecutionMode.SAME_THREAD
) ||
executionMode() == ExecutionMode.CONCURRENT

if (testClass.declaringClass == null) {
if (testClass.isDeclaredNestedTestClasses() && testClass.isConcurrent()) {
if (testClass.isDeclaredJUnit5NestedTestClasses() && testClass.isConcurrentExecutionEnabled()) {
error(
"${testClass.simpleName} must be annotated with @Execution(ExecutionMode.SAME_THREAD). " +
"Because it declared nested test classes. Or" +
Expand All @@ -75,7 +75,7 @@ internal object TestClassValidator {
}

private fun validateNestedTestCanNotBeExecutedConcurrently(testClass: Class<*>) {
if (testClass.isNestedTest && testClass.executionMode() == ExecutionMode.CONCURRENT) {
if (testClass.isJUnit5NestedTest && testClass.executionMode() == ExecutionMode.CONCURRENT) {
error("Concurrent execution mode not allowed on test class: ${testClass.simpleName}")
}
}
Expand Down

0 comments on commit ff68b26

Please sign in to comment.