|
1 | 1 | package de.mannodermaus.junit5.internal.runners
|
2 | 2 |
|
| 3 | +import android.os.Bundle |
3 | 4 | import androidx.test.platform.app.InstrumentationRegistry
|
4 | 5 | import de.mannodermaus.junit5.internal.discovery.GeneratedFilters
|
5 | 6 | import de.mannodermaus.junit5.internal.discovery.ParsedSelectors
|
6 | 7 | import de.mannodermaus.junit5.internal.discovery.PropertiesParser
|
7 | 8 | import de.mannodermaus.junit5.internal.discovery.ShardingFilter
|
8 | 9 | import org.junit.platform.engine.DiscoverySelector
|
9 | 10 | import org.junit.platform.engine.Filter
|
10 |
| -import org.junit.platform.engine.discovery.MethodSelector |
11 | 11 | import org.junit.platform.launcher.LauncherDiscoveryRequest
|
12 | 12 | import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder
|
13 | 13 |
|
14 | 14 | internal data class AndroidJUnit5RunnerParams(
|
15 |
| - private val selectors: List<DiscoverySelector> = emptyList(), |
| 15 | + private val arguments: Bundle = Bundle(), |
16 | 16 | private val filters: List<Filter<*>> = emptyList(),
|
17 | 17 | val environmentVariables: Map<String, String> = emptyMap(),
|
18 | 18 | val systemProperties: Map<String, String> = emptyMap(),
|
19 | 19 | private val configurationParameters: Map<String, String> = emptyMap()
|
20 | 20 | ) {
|
| 21 | + fun createSelectors(testClass: Class<*>): List<DiscoverySelector> { |
| 22 | + return ParsedSelectors.fromBundle(testClass, arguments) |
| 23 | + } |
21 | 24 |
|
22 |
| - fun createDiscoveryRequest(): LauncherDiscoveryRequest = |
23 |
| - LauncherDiscoveryRequestBuilder.request() |
24 |
| - .selectors(this.selectors) |
| 25 | + fun createDiscoveryRequest(selectors: List<DiscoverySelector>): LauncherDiscoveryRequest { |
| 26 | + return LauncherDiscoveryRequestBuilder.request() |
| 27 | + .selectors(selectors) |
25 | 28 | .filters(*this.filters.toTypedArray())
|
26 | 29 | .configurationParameters(this.configurationParameters)
|
27 | 30 | .build()
|
28 |
| - |
29 |
| - val isIsolatedMethodRun: Boolean |
30 |
| - get() = selectors.size == 1 && selectors.first() is MethodSelector |
| 31 | + } |
31 | 32 |
|
32 | 33 | val isParallelExecutionEnabled: Boolean
|
33 | 34 | get() = configurationParameters["junit.jupiter.execution.parallel.enabled"] == "true"
|
34 |
| -} |
35 |
| - |
36 |
| -private const val ARG_ENVIRONMENT_VARIABLES = "environmentVariables" |
37 |
| -private const val ARG_SYSTEM_PROPERTIES = "systemProperties" |
38 |
| -private const val ARG_CONFIGURATION_PARAMETERS = "configurationParameters" |
39 | 35 |
|
40 |
| -internal fun createRunnerParams(testClass: Class<*>): AndroidJUnit5RunnerParams { |
41 |
| - val instrumentation = InstrumentationRegistry.getInstrumentation() |
42 |
| - val arguments = InstrumentationRegistry.getArguments() |
| 36 | + internal companion object { |
| 37 | + fun create(): AndroidJUnit5RunnerParams { |
| 38 | + val instrumentation = InstrumentationRegistry.getInstrumentation() |
| 39 | + val arguments = InstrumentationRegistry.getArguments() |
43 | 40 |
|
44 |
| - // Parse environment variables & pass them to the JVM |
45 |
| - val environmentVariables = arguments.getString(ARG_ENVIRONMENT_VARIABLES) |
46 |
| - ?.run { PropertiesParser.fromString(this) } |
47 |
| - ?: emptyMap() |
| 41 | + // Parse environment variables & pass them to the JVM |
| 42 | + val environmentVariables = arguments.getString(ARG_ENVIRONMENT_VARIABLES) |
| 43 | + ?.run { PropertiesParser.fromString(this) } |
| 44 | + ?: emptyMap() |
48 | 45 |
|
49 |
| - // Parse system properties & pass them to the JVM |
50 |
| - val systemProperties = arguments.getString(ARG_SYSTEM_PROPERTIES) |
51 |
| - ?.run { PropertiesParser.fromString(this) } |
52 |
| - ?: emptyMap() |
| 46 | + // Parse system properties & pass them to the JVM |
| 47 | + val systemProperties = arguments.getString(ARG_SYSTEM_PROPERTIES) |
| 48 | + ?.run { PropertiesParser.fromString(this) } |
| 49 | + ?: emptyMap() |
53 | 50 |
|
54 |
| - // Parse configuration parameters |
55 |
| - val configurationParameters = arguments.getString(ARG_CONFIGURATION_PARAMETERS) |
56 |
| - ?.run { PropertiesParser.fromString(this) } |
57 |
| - ?: emptyMap() |
| 51 | + // Parse configuration parameters |
| 52 | + val configurationParameters = arguments.getString(ARG_CONFIGURATION_PARAMETERS) |
| 53 | + ?.run { PropertiesParser.fromString(this) } |
| 54 | + ?: emptyMap() |
58 | 55 |
|
59 |
| - // Parse the selectors to use from what's handed to the runner. |
60 |
| - val selectors = ParsedSelectors.fromBundle(testClass, arguments) |
| 56 | + // The user may apply test filters to their instrumentation tests through the Gradle plugin's DSL, |
| 57 | + // which aren't subject to the filtering imposed through adb. |
| 58 | + // A special resource file may be looked up at runtime, containing |
| 59 | + // the filters to apply by the AndroidJUnit5 runner. |
| 60 | + val filters = GeneratedFilters.fromContext(instrumentation.context) + |
| 61 | + listOfNotNull(ShardingFilter.fromArguments(arguments)) |
61 | 62 |
|
62 |
| - // The user may apply test filters to their instrumentation tests through the Gradle plugin's DSL, |
63 |
| - // which aren't subject to the filtering imposed through adb. |
64 |
| - // A special resource file may be looked up at runtime, containing |
65 |
| - // the filters to apply by the AndroidJUnit5 runner. |
66 |
| - val filters = GeneratedFilters.fromContext(instrumentation.context) + |
67 |
| - listOfNotNull(ShardingFilter.fromArguments(arguments)) |
68 |
| - |
69 |
| - return AndroidJUnit5RunnerParams( |
70 |
| - selectors, |
71 |
| - filters, |
72 |
| - environmentVariables, |
73 |
| - systemProperties, |
74 |
| - configurationParameters |
75 |
| - ) |
| 63 | + return AndroidJUnit5RunnerParams( |
| 64 | + arguments, |
| 65 | + filters, |
| 66 | + environmentVariables, |
| 67 | + systemProperties, |
| 68 | + configurationParameters |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
76 | 72 | }
|
| 73 | + |
| 74 | +private const val ARG_ENVIRONMENT_VARIABLES = "environmentVariables" |
| 75 | +private const val ARG_SYSTEM_PROPERTIES = "systemProperties" |
| 76 | +private const val ARG_CONFIGURATION_PARAMETERS = "configurationParameters" |
0 commit comments