-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ain/kotlin/tech/apter/junit/jupiter/robolectric/internal/validation/TestClassValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package tech.apter.junit.jupiter.robolectric.internal.validation | ||
|
||
import org.junit.jupiter.api.parallel.Execution | ||
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 | ||
|
||
internal class TestClassValidator(private val testClass: Class<*>) { | ||
|
||
fun validate() { | ||
validateParallelModeDefault() | ||
validateTestWithNestedTestsCanNotBeExecutedConcurrently(testClass) | ||
validateNestedTestCanNotBeExecutedConcurrently(testClass) | ||
validateNestedTestClassCanNotOverrideRuntimeSdk(testClass) | ||
validateNestedTestClassCanNotApplyAnnotations( | ||
testClass, | ||
LooperMode::class.java, | ||
GraphicsMode::class.java, | ||
) | ||
} | ||
|
||
private fun validateNestedTestClassCanNotOverrideRuntimeSdk(testClass: Class<*>) { | ||
val config = testClass.getAnnotation(Config::class.java) | ||
if (testClass.isNestedTest && config != null && config.sdk.isNotEmpty()) { | ||
error("Robolectric runtime sdk cannot be used on nested test class: ${testClass.name}") | ||
} | ||
} | ||
|
||
private fun validateNestedTestClassCanNotApplyAnnotations( | ||
testClass: Class<*>, | ||
vararg annotationsClasses: Class<out Annotation>, | ||
) { | ||
annotationsClasses.forEach { annotationClass -> | ||
val annotation = testClass.getAnnotation(annotationClass) | ||
if (annotation != null) { | ||
error("${annotationClass.simpleName} annotation cannot be used on a nested test class: ${testClass.name}") | ||
} | ||
} | ||
} | ||
|
||
private fun validateParallelModeDefault() { | ||
val parallelModeDefault = System.getProperty("junit.jupiter.execution.parallel.mode.default") | ||
if (parallelModeDefault == "concurrent") { | ||
error("junit.jupiter.execution.parallel.mode.default=concurrent is not supported with Robolectric") | ||
} | ||
} | ||
|
||
private fun validateTestWithNestedTestsCanNotBeExecutedConcurrently( | ||
testClass: Class<*>, | ||
) { | ||
fun Class<*>.isDeclaredNestedTestClasses() = declaredClasses.any { it.isNestedTest } | ||
fun Class<*>.isConcurrent() = | ||
(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()) { | ||
error( | ||
"${testClass.simpleName} must be annotated with @Execution(ExecutionMode.SAME_THREAD). " + | ||
"Because it declared nested test classes. " + | ||
"Or system property junit.jupiter.execution.parallel.mode.classes.default=same_thread must be set" | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun validateNestedTestCanNotBeExecutedConcurrently(testClass: Class<*>) { | ||
if (testClass.isNestedTest && testClass.executionMode() == ExecutionMode.CONCURRENT) { | ||
error("Concurrent execution mode not allowed on test class: ${testClass.simpleName}") | ||
} | ||
} | ||
|
||
private fun Class<*>.executionMode() = getAnnotation(Execution::class.java)?.value | ||
} |
30 changes: 30 additions & 0 deletions
30
...in/kotlin/tech/apter/junit/jupiter/robolectric/internal/validation/TestMethodValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tech.apter.junit.jupiter.robolectric.internal.validation | ||
|
||
import org.junit.jupiter.api.parallel.Execution | ||
import org.junit.jupiter.api.parallel.ExecutionMode | ||
import org.robolectric.annotation.Config | ||
import java.lang.reflect.Method | ||
|
||
internal class TestMethodValidator(private val testMethod: Method) { | ||
|
||
fun validate() { | ||
validateTestMethodCanNotBeExecutedConcurrently(testMethod) | ||
validateTestMethodsCanNotOverrideRuntimeSdk(testMethod) | ||
} | ||
|
||
private fun validateTestMethodsCanNotOverrideRuntimeSdk(testMethod: Method) { | ||
val config = testMethod.getAnnotation(Config::class.java) | ||
if (config != null && config.sdk.isNotEmpty()) { | ||
error( | ||
"Robolectric runtime sdk cannot be overwritten on a test method: " + | ||
"${testMethod.declaringClass.simpleName}::${testMethod.name}" | ||
) | ||
} | ||
} | ||
|
||
private fun validateTestMethodCanNotBeExecutedConcurrently(testMethod: Method) { | ||
if (testMethod.getAnnotation(Execution::class.java)?.value == ExecutionMode.CONCURRENT) { | ||
error("${testMethod.name} cannot be annotated @Execution(ExecutionMode.CONCURRENT)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters