Skip to content

Commit

Permalink
Add 'exportsTo' configuration option for whitebox tests
Browse files Browse the repository at this point in the history
Resolves #67
  • Loading branch information
jjohannes committed Nov 18, 2024
1 parent 9276ebb commit a3f4ade
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Version 1.5
* [#47](https://github.com/gradlex-org/java-module-testing/issues/47) Add support for Classpath Test Suites
* [#51](https://github.com/gradlex-org/java-module-testing/issues/51) testCompileOnly extends compileOnly for Whitebox Test Suites
* [#67](https://github.com/gradlex-org/java-module-testing/issues/67) Whitebox Test Suites: add `exportsTo` configuration option

## Version 1.4
* [#2](https://github.com/gradlex-org/java-module-testing/issues/2) New approach to split Module Path and Classpath for whitebox testing
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Whitebox Test Suites might require additional configuration, which can be done l
javaModuleTesting.whitebox(testing.suites["test"]) {
requires.add("org.junit.jupiter.api")
// opensTo.add("org.junit.platform.commons") <-- opensTo 'org.junit.platform.commons' is done by default
// exportsTo.add("...")
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ private void configureJvmTestSuiteForWhitebox(JvmTestSuite jvmTestSuite, Whitebo
argumentProvider.testRequires(JavaModuleDependenciesBridge.getRuntimeClasspathModules(project, testSources));
argumentProvider.testRequires(whiteboxJvmTestSuite.getRequires());
argumentProvider.testOpensTo(whiteboxJvmTestSuite.getOpensTo());
argumentProvider.testExportsTo(whiteboxJvmTestSuite.getExportsTo());
});

Configuration implementation = configurations.getByName(testSources.getImplementationConfigurationName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ public interface WhiteboxJvmTestSuite {
* @return modifiable list of addition '--add-opens'
*/
ListProperty<String> getOpensTo();

/**
* Export all packages of this Whitebox Test Suite to a given Module
* for access to public methods at runtime.
*
* @return modifiable list of addition '--add-exports'
*/
ListProperty<String> getExportsTo();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class WhiteboxTestRuntimeArgumentProvider implements CommandLineArgumentP

private final ListProperty<String> allTestRequires;
private final ListProperty<String> allTestOpensTo;
private final ListProperty<String> allTestExportsTo;

public WhiteboxTestRuntimeArgumentProvider(Set<File> mainSourceFolders,
Provider<Directory> testClassesFolders, File resourcesUnderTest, File testResources,
Expand All @@ -50,6 +51,7 @@ public WhiteboxTestRuntimeArgumentProvider(Set<File> mainSourceFolders,
this.moduleInfoParser = moduleInfoParser;
this.allTestRequires = objects.listProperty(String.class);
this.allTestOpensTo = objects.listProperty(String.class);
this.allTestExportsTo = objects.listProperty(String.class);
}

public void testRequires(Provider<List<String>> testRequires) {
Expand All @@ -60,12 +62,12 @@ public void testRequires(List<String> testRequires) {
allTestRequires.addAll(testRequires);
}

public void testOpensTo(Provider<List<String>> testRequires) {
allTestOpensTo.addAll(testRequires);
public void testOpensTo(Provider<List<String>> testOpensTo) {
allTestOpensTo.addAll(testOpensTo);
}

public void testOpensTo(List<String> testRequires) {
allTestOpensTo.addAll(testRequires);
public void testExportsTo(Provider<List<String>> testExportsTo) {
allTestExportsTo.addAll(testExportsTo);
}

@Override
Expand Down Expand Up @@ -96,6 +98,13 @@ public Iterable<String> asArguments() {
}
}

for (String packageName : allTestClassPackages) {
for (String opensTo : allTestExportsTo.get()) {
args.add("--add-exports");
args.add(moduleName + "/" + packageName + "=" + opensTo);
}
}

// Patch into Module located in the 'main' classes folder: test classes, resources, test resources
args.add("--patch-module");
args.add(moduleName + "=" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ class CustomizationTest extends Specification {
result.task(":app:test").outcome == TaskOutcome.SUCCESS
}

def "can customize whitebox test suites with exportsTo"() {
given:
def mainTest = file("app/src/test/java/org/example/app/test/MainTest.java")
// make test public, so that 'exportsTo org.junit.platform.commons' is sufficient
mainTest.text = mainTest.text.replace('void testApp()' , 'public void testApp()')

appBuildFile << '''
javaModuleTesting.classpath(testing.suites["test"]) // reset default setup
javaModuleTesting.whitebox(testing.suites["test"]) {
requires.add("org.junit.jupiter.api")
exportsTo.add("org.junit.platform.commons")
}
'''
appModuleInfoFile << '''
module org.example.app {
}
'''

when:
def result = runTests()

then:
result.output.contains('Main Module: org.example.app')
result.output.contains('Test Module: org.example.app')
result.task(":app:test").outcome == TaskOutcome.SUCCESS
}


def "repetitive blackbox calls on the same test suite have no effect"() {
given:
appBuildFile << '''
Expand Down

0 comments on commit a3f4ade

Please sign in to comment.