-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove need for Jupiter test discovery for API 26+ devices
This should pave the road for supporting non-Jupiter test engines for instrumentation tests
- Loading branch information
1 parent
13ccb68
commit a8772ea
Showing
10 changed files
with
212 additions
and
239 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
70 changes: 70 additions & 0 deletions
70
...n/runner/src/main/kotlin/de/mannodermaus/junit5/internal/dummy/JupiterTestMethodFinder.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,70 @@ | ||
package de.mannodermaus.junit5.internal.dummy | ||
|
||
import android.util.Log | ||
import de.mannodermaus.junit5.internal.LOG_TAG | ||
import org.junit.jupiter.api.RepeatedTest | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestFactory | ||
import org.junit.jupiter.api.TestTemplate | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import java.lang.reflect.Method | ||
import java.lang.reflect.Modifier | ||
|
||
/** | ||
* Algorithm to find all methods annotated with a JUnit Jupiter annotation | ||
* for devices running below API level 26 (i.e. those that cannot run Jupiter). | ||
* We're unable to rely on JUnit Platform's own reflection utilities since they rely on Java 8 stuff | ||
*/ | ||
internal object JupiterTestMethodFinder { | ||
private val jupiterTestAnnotations = listOf( | ||
Test::class.java, | ||
TestFactory::class.java, | ||
RepeatedTest::class.java, | ||
TestTemplate::class.java, | ||
ParameterizedTest::class.java, | ||
) | ||
|
||
fun find(cls: Class<*>): Set<Method> = cls.doFind(includeInherited = true) | ||
|
||
private fun Class<*>.doFind(includeInherited: Boolean): Set<Method> = buildSet { | ||
try { | ||
// Check each method in the Class for the presence | ||
// of the well-known list of JUnit Jupiter annotations. | ||
addAll(declaredMethods.filter(::isApplicableMethod)) | ||
|
||
// Recursively check non-private inner classes as well | ||
declaredClasses.filter(::isApplicableClass).forEach { inner -> | ||
addAll(inner.doFind(includeInherited = false)) | ||
} | ||
|
||
// Attach methods from inherited superclass or (for Java) implemented interfaces, too | ||
if (includeInherited) { | ||
addAll(superclass?.doFind(includeInherited = true).orEmpty()) | ||
interfaces.forEach { i -> addAll(i.doFind(includeInherited = true)) } | ||
} | ||
} catch (t: Throwable) { | ||
Log.w( | ||
LOG_TAG, | ||
"Encountered ${t.javaClass.simpleName} while finding Jupiter test methods for ${this@doFind.name}", | ||
t | ||
) | ||
} | ||
} | ||
|
||
private fun isApplicableMethod(method: Method): Boolean { | ||
// The method must not be static... | ||
if (Modifier.isStatic(method.modifiers)) return false | ||
|
||
// ...and have at least one of the recognized JUnit 5 annotations | ||
return hasJupiterAnnotation(method) | ||
} | ||
|
||
private fun hasJupiterAnnotation(method: Method): Boolean { | ||
return jupiterTestAnnotations.any { method.getAnnotation(it) != null } | ||
} | ||
|
||
private fun isApplicableClass(cls: Class<*>): Boolean { | ||
// A class must not be private to be considered | ||
return !Modifier.isPrivate(cls.modifiers) | ||
} | ||
} |
124 changes: 0 additions & 124 deletions
124
...ner/src/main/kotlin/de/mannodermaus/junit5/internal/extensions/JupiterTestMethodFinder.kt
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.