Skip to content

Commit 848c8de

Browse files
author
Marcel Schnelle
committed
no message
1 parent 6352d56 commit 848c8de

File tree

1 file changed

+104
-27
lines changed

1 file changed

+104
-27
lines changed

android-junit5/src/main/groovy/de/mannodermaus/gradle/anj5/AndroidJUnitPlatformPlugin.groovy

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,19 @@ import org.gradle.util.GradleVersion
88
import org.junit.platform.console.ConsoleLauncher
99
import org.junit.platform.gradle.plugin.*
1010

11-
import java.lang.reflect.Method
12-
1311
/**
1412
* Android JUnit Platform plugin for Gradle.
1513
*
1614
* Unfortunately, because of the restricted visibility of the plugin's members,
1715
* a lot of its functionality in regards to setup & configuration needs to be duplicated
18-
* in this class, even though it extends the "pure-Java plugin" and shares a lot
16+
* in this class, even though it extends the "pure-Java plugin" and shares some
1917
* of its work.
2018
*/
2119
class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
2220

2321
private static final String EXTENSION_NAME = 'junitPlatform'
2422
private static final String TASK_NAME = 'junitPlatformTest'
2523

26-
/* Reflectively accessed because of restricted visibility in the super-class */
27-
private Method buildArgsMethod
28-
2924
/**
3025
* This method doesn't call through to super.apply().
3126
* This is intentional, and prevents clashing between our Android-specific extension
@@ -38,15 +33,6 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
3833
throw new ProjectConfigurationException("The android or android-library plugin must be applied to this project", null)
3934
}
4035

41-
// Obtain a handle to the private "buildArgs" method, since that cuts down
42-
// on the amount of code duplication going on in this class
43-
try {
44-
buildArgsMethod = JUnitPlatformPlugin.class.getDeclaredMethod("buildArgs", Object.class, Object.class, Object.class)
45-
buildArgsMethod.setAccessible(true)
46-
} catch (NoSuchMethodError error) {
47-
throw new ProjectConfigurationException("Unexpected missing junit-plugin class definition.", error)
48-
}
49-
5036
// Construct the platform extension (use our Android variant of the Extension class though)
5137
def junitExtension = project.extensions.create(EXTENSION_NAME, AndroidJUnit5PlatformExtension, project)
5238
junitExtension.extensions.create('selectors', SelectorsExtension)
@@ -107,41 +93,47 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
10793
def testedVariantScope = testedVariantData.scope
10894
def testedScopeJavaOutputs = testedVariantScope.hasProperty("javaOutputs") ? testedVariantScope.javaOutputs : testedVariantScope.javaOuptuts
10995

96+
// Collect the root directories for unit tests from the variant's scopes
97+
def testRootDirs = []
98+
testRootDirs.add(variantScope.javaOutputDir)
99+
testRootDirs.add(testedVariantScope.javaOutputDir)
100+
110101
// Setup classpath for this variant's tests and add the test task
111102
// (refer to com.android.build.gradle.tasks.factory.UnitTestConfigAction)
112103
// 1) Add the compiler's classpath
113-
def classpaths = new ArrayList<>()
104+
def classpath = []
114105
if (variantData.javacTask) {
115106
def javaCompiler = variant.javaCompiler
116-
classpaths.add(javaCompiler.classpath)
117-
classpaths.add(javaCompiler.outputs.files)
107+
classpath.add(javaCompiler.classpath)
108+
classpath.add(javaCompiler.outputs.files)
118109
} else {
119-
classpaths.add(testedScopeJavaOutputs)
120-
classpaths.add(scopeJavaOutputs)
110+
classpath.add(testedScopeJavaOutputs)
111+
classpath.add(scopeJavaOutputs)
121112
}
122113

123114
// 2) Add the testApk configuration
124115
def testApk = project.configurations.findByName("testApk")
125116
if (testApk != null) {
126-
classpaths.add(testApk)
117+
classpath.add(testApk)
127118
}
128119

129120
// 3) Add test resources
130-
classpaths.add(variantData.javaResourcesForUnitTesting)
131-
classpaths.add(testedVariantData.javaResourcesForUnitTesting)
121+
classpath.add(variantData.javaResourcesForUnitTesting)
122+
classpath.add(testedVariantData.javaResourcesForUnitTesting)
132123

133124
// 4) Add filtered boot classpath
134125
def globalScope = variantScope.globalScope
135-
classpaths.add(globalScope.androidBuilder.getBootClasspath(false).findAll { it.name != "android.jar" })
126+
classpath.add(globalScope.androidBuilder.getBootClasspath(false).findAll { it.name != "android.jar" })
136127

137128
// 5) Add mocked version of android.jar
138-
classpaths.add(globalScope.mockableAndroidJarFile)
129+
classpath.add(globalScope.mockableAndroidJarFile)
139130

140131
addJunitPlatformTask(
141132
project: project,
142133
junitExtension: junitExtension,
143134
nameSuffix: nameSuffix,
144-
classpath: project.files(classpaths),
135+
classpath: project.files(classpath),
136+
testRootDirs: testRootDirs,
145137
dependentTasks: Collections.singletonList("assemble${nameSuffix}UnitTest"))
146138
}
147139
}
@@ -150,6 +142,7 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
150142
Project project = map.project as Project
151143
def junitExtension = map.junitExtension
152144
def classpath = map.classpath
145+
def testRootDirs = map.testRootDirs
153146
String nameSuffix = map.getOrDefault("nameSuffix", "")
154147
def dependentTasks = map.dependentTasks
155148

@@ -194,7 +187,26 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
194187
junitTask.classpath = classpath + project.configurations.junitPlatform
195188

196189
junitTask.main = ConsoleLauncher.class.getName()
197-
junitTask.args buildArgsMethod.invoke(this, project, junitExtension, reportsDir)
190+
junitTask.args buildArgs(project, junitExtension, reportsDir, testRootDirs)
191+
192+
doFirst {
193+
project.logger.info("CLASS PATH ==")
194+
classpath.each {
195+
project.logger.info("$it")
196+
}
197+
project.logger.info("=============")
198+
project.logger.info("TASK ARGS ===")
199+
project.logger.info("${junitTask.args.join(" ")}")
200+
project.logger.info("=============")
201+
202+
// def rootDirs = []
203+
// project.sourceSets.each { sourceSet ->
204+
// rootDirs.add(sourceSet.output.classesDir)
205+
// rootDirs.add(sourceSet.output.resourcesDir)
206+
// rootDirs.addAll(sourceSet.output.dirs.files)
207+
// }
208+
// args.addAll(['--scan-class-path', rootDirs.join(File.pathSeparator)])
209+
}
198210
}
199211
}
200212

@@ -215,6 +227,71 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
215227
testTask.enabled = junitExtension.enableStandardTestTask
216228
}
217229

230+
private List<String> buildArgs(project, junitExtension, reportsDir, testRootDirs) {
231+
232+
def args = ['--hide-details']
233+
234+
addSelectors(project, junitExtension.selectors, testRootDirs, args)
235+
addFilters(junitExtension.filters, args)
236+
237+
args.add('--reports-dir')
238+
args.add(reportsDir.getAbsolutePath())
239+
240+
return args
241+
}
242+
243+
private void addFilters(filters, args) {
244+
filters.includeClassNamePatterns.each { pattern ->
245+
args.addAll(['-n', pattern])
246+
}
247+
filters.packages.include.each { includedPackage ->
248+
args.addAll(['--include-package',includedPackage])
249+
}
250+
filters.packages.exclude.each { excludedPackage ->
251+
args.addAll(['--exclude-package',excludedPackage])
252+
}
253+
filters.tags.include.each { tag ->
254+
args.addAll(['-t', tag])
255+
}
256+
filters.tags.exclude.each { tag ->
257+
args.addAll(['-T', tag])
258+
}
259+
filters.engines.include.each { engineId ->
260+
args.addAll(['-e', engineId])
261+
}
262+
filters.engines.exclude.each { engineId ->
263+
args.addAll(['-E', engineId])
264+
}
265+
}
266+
267+
private void addSelectors(project, selectors, testRootDirs, args) {
268+
if (selectors.empty) {
269+
args.addAll(['--scan-class-path', testRootDirs.join(File.pathSeparator)])
270+
} else {
271+
selectors.uris.each { uri ->
272+
args.addAll(['-u', uri])
273+
}
274+
selectors.files.each { file ->
275+
args.addAll(['-f', file])
276+
}
277+
selectors.directories.each { directory ->
278+
args.addAll(['-d', directory])
279+
}
280+
selectors.packages.each { aPackage ->
281+
args.addAll(['-p', aPackage])
282+
}
283+
selectors.classes.each { aClass ->
284+
args.addAll(['-c', aClass])
285+
}
286+
selectors.methods.each { method ->
287+
args.addAll(['-m', method])
288+
}
289+
selectors.resources.each { resource ->
290+
args.addAll(['-r', resource])
291+
}
292+
}
293+
}
294+
218295
private static boolean isAndroidProject(Project project) {
219296
return project.plugins.findPlugin("com.android.application") ||
220297
project.plugins.findPlugin("android") ||

0 commit comments

Comments
 (0)