Skip to content

Commit a3f4ade

Browse files
committed
Add 'exportsTo' configuration option for whitebox tests
Resolves #67
1 parent 9276ebb commit a3f4ade

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Version 1.5
44
* [#47](https://github.com/gradlex-org/java-module-testing/issues/47) Add support for Classpath Test Suites
55
* [#51](https://github.com/gradlex-org/java-module-testing/issues/51) testCompileOnly extends compileOnly for Whitebox Test Suites
6+
* [#67](https://github.com/gradlex-org/java-module-testing/issues/67) Whitebox Test Suites: add `exportsTo` configuration option
67

78
## Version 1.4
89
* [#2](https://github.com/gradlex-org/java-module-testing/issues/2) New approach to split Module Path and Classpath for whitebox testing

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Whitebox Test Suites might require additional configuration, which can be done l
8080
javaModuleTesting.whitebox(testing.suites["test"]) {
8181
requires.add("org.junit.jupiter.api")
8282
// opensTo.add("org.junit.platform.commons") <-- opensTo 'org.junit.platform.commons' is done by default
83+
// exportsTo.add("...")
8384
}
8485
```
8586

src/main/java/org/gradlex/javamodule/testing/JavaModuleTestingExtension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ private void configureJvmTestSuiteForWhitebox(JvmTestSuite jvmTestSuite, Whitebo
250250
argumentProvider.testRequires(JavaModuleDependenciesBridge.getRuntimeClasspathModules(project, testSources));
251251
argumentProvider.testRequires(whiteboxJvmTestSuite.getRequires());
252252
argumentProvider.testOpensTo(whiteboxJvmTestSuite.getOpensTo());
253+
argumentProvider.testExportsTo(whiteboxJvmTestSuite.getExportsTo());
253254
});
254255

255256
Configuration implementation = configurations.getByName(testSources.getImplementationConfigurationName());

src/main/java/org/gradlex/javamodule/testing/WhiteboxJvmTestSuite.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ public interface WhiteboxJvmTestSuite {
5454
* @return modifiable list of addition '--add-opens'
5555
*/
5656
ListProperty<String> getOpensTo();
57+
58+
/**
59+
* Export all packages of this Whitebox Test Suite to a given Module
60+
* for access to public methods at runtime.
61+
*
62+
* @return modifiable list of addition '--add-exports'
63+
*/
64+
ListProperty<String> getExportsTo();
5765
}

src/main/java/org/gradlex/javamodule/testing/internal/provider/WhiteboxTestRuntimeArgumentProvider.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class WhiteboxTestRuntimeArgumentProvider implements CommandLineArgumentP
3838

3939
private final ListProperty<String> allTestRequires;
4040
private final ListProperty<String> allTestOpensTo;
41+
private final ListProperty<String> allTestExportsTo;
4142

4243
public WhiteboxTestRuntimeArgumentProvider(Set<File> mainSourceFolders,
4344
Provider<Directory> testClassesFolders, File resourcesUnderTest, File testResources,
@@ -50,6 +51,7 @@ public WhiteboxTestRuntimeArgumentProvider(Set<File> mainSourceFolders,
5051
this.moduleInfoParser = moduleInfoParser;
5152
this.allTestRequires = objects.listProperty(String.class);
5253
this.allTestOpensTo = objects.listProperty(String.class);
54+
this.allTestExportsTo = objects.listProperty(String.class);
5355
}
5456

5557
public void testRequires(Provider<List<String>> testRequires) {
@@ -60,12 +62,12 @@ public void testRequires(List<String> testRequires) {
6062
allTestRequires.addAll(testRequires);
6163
}
6264

63-
public void testOpensTo(Provider<List<String>> testRequires) {
64-
allTestOpensTo.addAll(testRequires);
65+
public void testOpensTo(Provider<List<String>> testOpensTo) {
66+
allTestOpensTo.addAll(testOpensTo);
6567
}
6668

67-
public void testOpensTo(List<String> testRequires) {
68-
allTestOpensTo.addAll(testRequires);
69+
public void testExportsTo(Provider<List<String>> testExportsTo) {
70+
allTestExportsTo.addAll(testExportsTo);
6971
}
7072

7173
@Override
@@ -96,6 +98,13 @@ public Iterable<String> asArguments() {
9698
}
9799
}
98100

101+
for (String packageName : allTestClassPackages) {
102+
for (String opensTo : allTestExportsTo.get()) {
103+
args.add("--add-exports");
104+
args.add(moduleName + "/" + packageName + "=" + opensTo);
105+
}
106+
}
107+
99108
// Patch into Module located in the 'main' classes folder: test classes, resources, test resources
100109
args.add("--patch-module");
101110
args.add(moduleName + "=" +

src/test/groovy/org/gradlex/javamodule/testing/test/CustomizationTest.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ class CustomizationTest extends Specification {
5353
result.task(":app:test").outcome == TaskOutcome.SUCCESS
5454
}
5555

56+
def "can customize whitebox test suites with exportsTo"() {
57+
given:
58+
def mainTest = file("app/src/test/java/org/example/app/test/MainTest.java")
59+
// make test public, so that 'exportsTo org.junit.platform.commons' is sufficient
60+
mainTest.text = mainTest.text.replace('void testApp()' , 'public void testApp()')
61+
62+
appBuildFile << '''
63+
javaModuleTesting.classpath(testing.suites["test"]) // reset default setup
64+
javaModuleTesting.whitebox(testing.suites["test"]) {
65+
requires.add("org.junit.jupiter.api")
66+
exportsTo.add("org.junit.platform.commons")
67+
}
68+
'''
69+
appModuleInfoFile << '''
70+
module org.example.app {
71+
}
72+
'''
73+
74+
when:
75+
def result = runTests()
76+
77+
then:
78+
result.output.contains('Main Module: org.example.app')
79+
result.output.contains('Test Module: org.example.app')
80+
result.task(":app:test").outcome == TaskOutcome.SUCCESS
81+
}
82+
83+
5684
def "repetitive blackbox calls on the same test suite have no effect"() {
5785
given:
5886
appBuildFile << '''

0 commit comments

Comments
 (0)