11package de .jjohannes .gradle .moduletesting ;
22
3+ import de .jjohannes .gradle .moduletesting .internal .ModuleInfoParser ;
4+ import de .jjohannes .gradle .moduletesting .internal .bridges .JavaModuleDependenciesBridge ;
5+ import de .jjohannes .gradle .moduletesting .internal .provider .WhiteboxTestCompileArgumentProvider ;
6+ import de .jjohannes .gradle .moduletesting .internal .provider .WhiteboxTestRuntimeArgumentProvider ;
37import org .gradle .api .Action ;
48import org .gradle .api .Project ;
59import org .gradle .api .artifacts .Configuration ;
610import org .gradle .api .artifacts .ConfigurationContainer ;
711import org .gradle .api .artifacts .dsl .DependencyHandler ;
12+ import org .gradle .api .plugins .jvm .JvmTestSuite ;
13+ import org .gradle .api .provider .Provider ;
814import org .gradle .api .tasks .SourceSet ;
915import org .gradle .api .tasks .SourceSetContainer ;
1016import org .gradle .api .tasks .TaskContainer ;
1319import org .gradle .api .tasks .testing .Test ;
1420import org .gradle .internal .jvm .JavaModuleDetector ;
1521import org .gradle .jvm .tasks .Jar ;
16- import org .gradle .language .base .plugins . LifecycleBasePlugin ;
22+ import org .gradle .testing .base .TestSuite ;
1723
1824import javax .inject .Inject ;
19- import java .util .List ;
20- import java .util .Map ;
2125
26+ @ SuppressWarnings ("UnstableApiUsage" )
2227public abstract class JavaModuleTestingExtension {
23- private static final Action <WhiteboxTestSet > NO_OP_ACTION = c -> {};
28+ private static final Action <WhiteboxJvmTestSuite > NO_OP_ACTION = c -> {};
2429
2530 private final Project project ;
2631 private final JavaModuleDetector moduleDetector ;
@@ -31,63 +36,32 @@ public JavaModuleTestingExtension(Project project, JavaModuleDetector moduleDete
3136 this .moduleDetector = moduleDetector ;
3237 }
3338
34- public void blackboxTests (String testSourceSetName ) {
35- SourceSet sourceSet = configureSourceSetForTesting (testSourceSetName );
36- configureSourceSetForBlackbox (sourceSet );
37- }
38-
39- public void junit5WhiteboxTests (String testSourceSetName , String junit5Version ) {
40- junit5WhiteboxTests (testSourceSetName , junit5Version , NO_OP_ACTION );
41- }
42-
43- public void junit5WhiteboxTests (String testSourceSetName , String junit5Version , Action <WhiteboxTestSet > conf ) {
44- whiteboxTests (testSourceSetName , "org.junit.jupiter.api" , "org.junit.jupiter:junit-jupiter-api:" + junit5Version , conf );
45- project .getTasks ().named (testSourceSetName , Test .class ).configure (Test ::useJUnitPlatform );
46- }
47-
48- public void junit4WhiteboxTests (String testSourceSetName , String junit4Version ) {
49- junit4WhiteboxTests (testSourceSetName , junit4Version , NO_OP_ACTION );
39+ public void blackbox (TestSuite jvmTestSuite ) {
40+ if (jvmTestSuite instanceof JvmTestSuite ) {
41+ configureJvmTestSuiteForBlackbox ((JvmTestSuite ) jvmTestSuite );
42+ }
5043 }
5144
52- public void junit4WhiteboxTests (String testSourceSetName , String junit4Version , Action <WhiteboxTestSet > conf ) {
53- whiteboxTests (testSourceSetName , "junit" , "junit:junit:" + junit4Version , conf );
54- project .getTasks ().named (testSourceSetName , Test .class ).configure (Test ::useJUnit );
55- }
45+ public void whitebox (TestSuite jvmTestSuite ) {
46+ whitebox (jvmTestSuite , NO_OP_ACTION );
5647
57- private void whiteboxTests (String testSourceSetName , String testFrameworkModuleName , String testFrameworkGAV , Action <WhiteboxTestSet > conf ) {
58- WhiteboxTestSet whiteboxTestSet = new WhiteboxTestSet ();
59- conf .execute (whiteboxTestSet );
60- SourceSetContainer sourceSets = project .getExtensions ().getByType (SourceSetContainer .class );
61- SourceSet mainSourceSet = sourceSets .getByName (SourceSet .MAIN_SOURCE_SET_NAME );
62- SourceSet testSourceSet = configureSourceSetForTesting (testSourceSetName );
63- configureSourceSetForWhitebox (mainSourceSet , testSourceSet , testFrameworkModuleName , testFrameworkGAV , whiteboxTestSet .getTestRequires ());
6448 }
6549
66- private SourceSet configureSourceSetForTesting (String name ) {
67- SourceSetContainer sourceSets = project .getExtensions ().getByType (SourceSetContainer .class );
68- TaskContainer tasks = project .getTasks ();
69- SourceSet sourceSet = sourceSets .maybeCreate (name );
70-
71- TaskProvider <Test > testTask ;
72- if (!tasks .getNames ().contains (name )) {
73- testTask = tasks .register (name , Test .class , t -> {
74- t .setDescription ("Runs " + name + " tests." );
75- t .setGroup ("verification" );
76- });
77- tasks .named (LifecycleBasePlugin .CHECK_TASK_NAME , t -> {
78- t .dependsOn (testTask );
79- });
50+ public void whitebox (TestSuite jvmTestSuite , Action <WhiteboxJvmTestSuite > conf ) {
51+ if (jvmTestSuite instanceof JvmTestSuite ) {
52+ WhiteboxJvmTestSuite whiteboxJvmTestSuite = project .getObjects ().newInstance (WhiteboxJvmTestSuite .class );
53+ whiteboxJvmTestSuite .getSourcesUnderTest ().convention (project .getExtensions ().getByType (SourceSetContainer .class ).getByName (SourceSet .MAIN_SOURCE_SET_NAME ));
54+ conf .execute (whiteboxJvmTestSuite );
55+ configureJvmTestSuiteForWhitebox ((JvmTestSuite ) jvmTestSuite , whiteboxJvmTestSuite );
8056 }
81-
82- return sourceSet ;
8357 }
8458
85- private void configureSourceSetForBlackbox ( SourceSet sourceSet ) {
59+ private void configureJvmTestSuiteForBlackbox ( JvmTestSuite jvmTestSuite ) {
8660 ConfigurationContainer configurations = project .getConfigurations ();
87- DependencyHandler dependencies = project .getDependencies ();
8861 TaskContainer tasks = project .getTasks ();
8962
9063 TaskProvider <Jar > jarTask ;
64+ SourceSet sourceSet = jvmTestSuite .getSources ();
9165 if (!tasks .getNames ().contains (sourceSet .getJarTaskName ())) {
9266 jarTask = tasks .register (sourceSet .getJarTaskName (), Jar .class , t -> {
9367 t .getArchiveClassifier ().set (sourceSet .getName ());
@@ -98,61 +72,58 @@ private void configureSourceSetForBlackbox(SourceSet sourceSet) {
9872 }
9973
10074 tasks .named (sourceSet .getName (), Test .class , t -> {
75+ // Classpath consists only of Jars to include classes+resources in one place
10176 t .setClasspath (configurations .getByName (sourceSet .getRuntimeClasspathConfigurationName ()).plus (project .files (jarTask )));
77+ // Rest test classes dir to allow switching back from 'whitebox' to 'blackbox'
10278 t .setTestClassesDirs (sourceSet .getOutput ().getClassesDirs ());
10379 });
104- dependencies .add (sourceSet .getImplementationConfigurationName (), project );
10580 }
10681
107- private void configureSourceSetForWhitebox ( SourceSet mainSourceSet , SourceSet testSourceSet , String testFrameworkModuleName , String testFrameworkGAV , List < String > testRequires ) {
82+ private void configureJvmTestSuiteForWhitebox ( JvmTestSuite jvmTestSuite , WhiteboxJvmTestSuite whiteboxJvmTestSuite ) {
10883 ConfigurationContainer configurations = project .getConfigurations ();
10984 DependencyHandler dependencies = project .getDependencies ();
11085 TaskContainer tasks = project .getTasks ();
11186 ModuleInfoParser moduleInfoParser = new ModuleInfoParser (project .getLayout (), project .getProviders ());
11287
113- tasks .named (testSourceSet .getCompileJavaTaskName (), JavaCompile .class , t -> {
114- t .setClasspath (mainSourceSet .getOutput ().plus (configurations .getByName (testSourceSet .getCompileClasspathConfigurationName ())));
115- t .getOptions ().getCompilerArgumentProviders ().add (new WhiteboxTestCompileArgumentProvider (
116- mainSourceSet .getJava ().getSrcDirs (),
117- testSourceSet .getJava ().getSrcDirs (),
118- t ,
119- testFrameworkModuleName ,
120- testRequires ,
88+ SourceSet testSources = jvmTestSuite .getSources ();
89+ tasks .named (testSources .getCompileJavaTaskName (), JavaCompile .class , compileJava -> {
90+ SourceSet sourcesUnderTest = whiteboxJvmTestSuite .getSourcesUnderTest ().get ();
91+ compileJava .setClasspath (sourcesUnderTest .getOutput ().plus (configurations .getByName (testSources .getCompileClasspathConfigurationName ())));
92+ compileJava .getOptions ().getCompilerArgumentProviders ().add (new WhiteboxTestCompileArgumentProvider (
93+ sourcesUnderTest .getJava ().getSrcDirs (),
94+ testSources .getJava ().getSrcDirs (),
95+ compileJava ,
96+ whiteboxJvmTestSuite .getRequires (),
12197 moduleDetector ,
12298 moduleInfoParser ));
12399 });
124100
125- tasks .named (testSourceSet .getName (), Test .class , t -> {
126- t .setClasspath (configurations .getByName (testSourceSet .getRuntimeClasspathConfigurationName ()).plus (mainSourceSet .getOutput ()).plus (testSourceSet .getOutput ()));
101+ tasks .named (testSources .getName (), Test .class , test -> {
102+ SourceSet sourcesUnderTest = whiteboxJvmTestSuite .getSourcesUnderTest ().get ();
103+ test .setClasspath (configurations .getByName (testSources .getRuntimeClasspathConfigurationName ()).plus (sourcesUnderTest .getOutput ()).plus (testSources .getOutput ()));
127104
128105 // Add main classes here so that Gradle finds module-info.class and treats this as a test with module path
129- t .setTestClassesDirs (mainSourceSet .getOutput ().getClassesDirs ().plus (testSourceSet .getOutput ().getClassesDirs ()));
130-
131- t .getJvmArgumentProviders ().add (new WhiteboxTestRuntimeArgumentProvider (
132- mainSourceSet .getJava ().getSrcDirs (),
133- testSourceSet .getJava ().getClassesDirectory (),
134- testFrameworkModuleName ,
135- testRequires ,
106+ test .setTestClassesDirs (sourcesUnderTest .getOutput ().getClassesDirs ().plus (testSources .getOutput ().getClassesDirs ()));
107+
108+ test .getJvmArgumentProviders ().add (new WhiteboxTestRuntimeArgumentProvider (
109+ sourcesUnderTest .getJava ().getSrcDirs (),
110+ testSources .getJava ().getClassesDirectory (),
111+ sourcesUnderTest .getOutput ().getResourcesDir (),
112+ testSources .getOutput ().getResourcesDir (),
113+ whiteboxJvmTestSuite .getRequires (),
114+ whiteboxJvmTestSuite .getOpensTo (),
136115 moduleInfoParser
137116 ));
138117 });
139118
140- Configuration implementation = configurations .getByName (testSourceSet .getImplementationConfigurationName ());
141- Configuration runtimeOnly = configurations .getByName (testSourceSet .getRuntimeOnlyConfigurationName ());
142-
143- implementation .extendsFrom (configurations .getByName (mainSourceSet .getImplementationConfigurationName ()));
144- runtimeOnly .extendsFrom (configurations .getByName (mainSourceSet .getRuntimeOnlyConfigurationName ()));
145-
146- dependencies .add (implementation .getName (), testFrameworkGAV );
147-
119+ Configuration implementation = configurations .getByName (testSources .getImplementationConfigurationName ());
148120 implementation .withDependencies (d -> {
149- for (String requiresModuleName : testRequires ) {
150- Map <?, ?> gav = JavaModuleDependenciesBridge .gav (project , requiresModuleName );
121+ for (String requiresModuleName : whiteboxJvmTestSuite . getRequires (). get () ) {
122+ Provider < ?> gav = JavaModuleDependenciesBridge .gav (project , requiresModuleName );
151123 if (gav != null ) {
152- dependencies .add (implementation .getName (), gav );
124+ dependencies .addProvider (implementation .getName (), gav );
153125 }
154126 }
155127 });
156128 }
157-
158129}
0 commit comments