|
| 1 | +package org.gradlex.javamodule.moduleinfo.test |
| 2 | + |
| 3 | +import org.gradlex.javamodule.moduleinfo.test.fixture.GradleBuild |
| 4 | +import org.gradlex.javamodule.moduleinfo.test.fixture.LegacyLibraries |
| 5 | +import spock.lang.Specification |
| 6 | + |
| 7 | +class OpensFunctionalTest extends Specification { |
| 8 | + |
| 9 | + @Delegate |
| 10 | + GradleBuild build = new GradleBuild() |
| 11 | + |
| 12 | + LegacyLibraries libs = new LegacyLibraries(false) |
| 13 | + |
| 14 | + def setup() { |
| 15 | + settingsFile << 'rootProject.name = "test-project"' |
| 16 | + buildFile << ''' |
| 17 | + plugins { |
| 18 | + id("application") |
| 19 | + id("org.gradlex.extra-java-module-info") |
| 20 | + } |
| 21 | + application { |
| 22 | + mainModule.set("org.gradle.sample.app") |
| 23 | + mainClass.set("org.gradle.sample.app.Main") |
| 24 | + } |
| 25 | + ''' |
| 26 | + file("src/main/java/module-info.java") << """ |
| 27 | + module org.gradle.sample.app { |
| 28 | + requires apache.commons.collections; |
| 29 | + } |
| 30 | + """ |
| 31 | + file("src/main/java/org/gradle/sample/app/Main.java") << """ |
| 32 | + package org.gradle.sample.app; |
| 33 | + |
| 34 | + public class Main { |
| 35 | + public static void main(String[] args) throws ClassNotFoundException { |
| 36 | + Class.forName("org.apache.commons.collections.buffer.BlockingBuffer").getDeclaredMethods()[0].setAccessible(true); |
| 37 | + Class.forName("org.apache.commons.collections.bag.HashBag").getDeclaredMethods()[0].setAccessible(true); |
| 38 | + } |
| 39 | + } |
| 40 | + """ |
| 41 | + } |
| 42 | + |
| 43 | + def "a module is open by default"() { |
| 44 | + given: |
| 45 | + buildFile << """ |
| 46 | + dependencies { |
| 47 | + implementation("commons-collections:commons-collections:3.2.2") |
| 48 | + } |
| 49 | + extraJavaModuleInfo { |
| 50 | + module(${libs.commonsCollections}, "apache.commons.collections") |
| 51 | + } |
| 52 | + """ |
| 53 | + |
| 54 | + expect: |
| 55 | + run() |
| 56 | + } |
| 57 | + |
| 58 | + def "a module can be closed"() { |
| 59 | + given: |
| 60 | + buildFile << """ |
| 61 | + dependencies { |
| 62 | + implementation("commons-collections:commons-collections:3.2.2") |
| 63 | + } |
| 64 | + extraJavaModuleInfo { |
| 65 | + module(${libs.commonsCollections}, "apache.commons.collections") { |
| 66 | + closeModule() |
| 67 | + } |
| 68 | + } |
| 69 | + """ |
| 70 | + |
| 71 | + expect: |
| 72 | + def out = failRun() |
| 73 | + out.output.contains('module apache.commons.collections does not "exports org.apache.commons.collections.buffer" to module org.gradle.sample.app') |
| 74 | + } |
| 75 | + |
| 76 | + def "a module is closed once it has an open package"() { |
| 77 | + given: |
| 78 | + |
| 79 | + buildFile << """ |
| 80 | + dependencies { |
| 81 | + implementation("commons-collections:commons-collections:3.2.2") |
| 82 | + } |
| 83 | + extraJavaModuleInfo { |
| 84 | + module(${libs.commonsCollections}, "apache.commons.collections") { |
| 85 | + opens("org.apache.commons.collections.buffer") |
| 86 | + } |
| 87 | + } |
| 88 | + """ |
| 89 | + |
| 90 | + expect: |
| 91 | + def out = failRun() |
| 92 | + out.output.contains('module apache.commons.collections does not "opens org.apache.commons.collections.bag" to module org.gradle.sample.app') |
| 93 | + } |
| 94 | +} |
0 commit comments