|
| 1 | +/* |
| 2 | + * Copyright 2023 Ihor Herasymenko. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.iherasymenko.jlink.test; |
| 17 | + |
| 18 | +import org.assertj.core.api.InstanceOfAssertFactories; |
| 19 | +import org.gradle.testkit.runner.BuildResult; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.util.spi.ToolProvider; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +class DedupLegalNoticesPluginFunctionalTest extends AbstractTestBase { |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + void setUp() throws IOException { |
| 34 | + ToolProvider javac = ToolProvider.findFirst("javac").orElseThrow(() -> new IllegalStateException("javac not found")); |
| 35 | + ToolProvider jmod = ToolProvider.findFirst("jmod").orElseThrow(() -> new IllegalStateException("jmod not found")); |
| 36 | + |
| 37 | + Path src = Files.createDirectories(build.projectDir.resolve("foo-module/src")); |
| 38 | + Path classes = Files.createDirectories(build.projectDir.resolve("foo-module/classes")); |
| 39 | + Path legal = Files.createDirectories(build.projectDir.resolve("foo-module/legal")); |
| 40 | + Path libs = Files.createDirectories(build.projectDir.resolve("libs")); |
| 41 | + |
| 42 | + Path moduleInfo = src.resolve("module-info.java"); |
| 43 | + Files.writeString(moduleInfo, "module foo {}"); |
| 44 | + Files.writeString(legal.resolve("LICENSE"), "DUMMY LICENSE TEXT"); |
| 45 | + |
| 46 | + Path fooJmod = libs.resolve("foo.jmod"); |
| 47 | + |
| 48 | + javac.run(System.out, System.err, "-d", classes.toString(), "--release", "11", moduleInfo.toString()); |
| 49 | + jmod.run(System.out, System.err, "create", "--class-path", classes.toString(), "--legal-notices", legal.toString(), fooJmod.toString()); |
| 50 | + |
| 51 | + build.settingsFile = """ |
| 52 | + rootProject.name = 'demo' |
| 53 | + dependencyResolutionManagement { |
| 54 | + repositories { |
| 55 | + mavenCentral() |
| 56 | + } |
| 57 | + } |
| 58 | + """; |
| 59 | + build.mainClass = """ |
| 60 | + package com.example.demo; |
| 61 | + |
| 62 | + public class DemoApplication { |
| 63 | + public static void main(String[] args) { |
| 64 | + |
| 65 | + } |
| 66 | + } |
| 67 | + """; |
| 68 | + build.moduleInfo = """ |
| 69 | + module demo.main { |
| 70 | + requires foo; |
| 71 | + } |
| 72 | + """; |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void dedup_legal_notices_error_if_not_same_content() throws IOException { |
| 77 | + build.buildFile = """ |
| 78 | + plugins { |
| 79 | + id 'application' |
| 80 | + id 'com.github.iherasymenko.jlink' |
| 81 | + } |
| 82 | + |
| 83 | + group = 'com.example' |
| 84 | + version = '0.0.1-SNAPSHOT' |
| 85 | + |
| 86 | + java { |
| 87 | + toolchain { |
| 88 | + languageVersion = JavaLanguageVersion.of(System.getenv().getOrDefault('TESTING_AGAINST_JDK', '21')) |
| 89 | + vendor = JvmVendorSpec.AZUL |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + application { |
| 94 | + mainClass = 'com.example.demo.DemoApplication' |
| 95 | + mainModule = 'demo.main' |
| 96 | + } |
| 97 | + |
| 98 | + dependencies { |
| 99 | + implementation files("libs/foo.jmod") |
| 100 | + } |
| 101 | + |
| 102 | + jlinkApplication { |
| 103 | + dedupLegalNoticesErrorIfNotSameContent = true |
| 104 | + } |
| 105 | + |
| 106 | + // Gradle does not recognize *.jmod files as modules ¯\\_(ツ)_/¯ |
| 107 | + tasks.withType(JavaCompile).configureEach { task -> |
| 108 | + task.doFirst { |
| 109 | + task.options.compilerArgs += ["--module-path", task.classpath.asPath] |
| 110 | + classpath = files() |
| 111 | + } |
| 112 | + } |
| 113 | + """; |
| 114 | + BuildResult buildResult = build.runner("image").buildAndFail(); |
| 115 | + assertThat(buildResult) |
| 116 | + .extracting(BuildResult::getOutput, InstanceOfAssertFactories.STRING) |
| 117 | + .contains("Error: /java.base/legal/java.base/LICENSE /foo/legal/foo/LICENSE contain different content"); |
| 118 | + assertThat(build.projectDir.resolve("build/images/demo/bin")).doesNotExist(); |
| 119 | + assertThat(build.projectDir.resolve("build/images/demo/release")).doesNotExist(); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void dedup_legal_notices_error_if_not_same_content_is_disabled_by_default() throws IOException { |
| 124 | + build.buildFile = """ |
| 125 | + plugins { |
| 126 | + id 'application' |
| 127 | + id 'com.github.iherasymenko.jlink' |
| 128 | + } |
| 129 | + |
| 130 | + group = 'com.example' |
| 131 | + version = '0.0.1-SNAPSHOT' |
| 132 | + |
| 133 | + java { |
| 134 | + toolchain { |
| 135 | + languageVersion = JavaLanguageVersion.of(System.getenv().getOrDefault('TESTING_AGAINST_JDK', '21')) |
| 136 | + vendor = JvmVendorSpec.AZUL |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + application { |
| 141 | + mainClass = 'com.example.demo.DemoApplication' |
| 142 | + mainModule = 'demo.main' |
| 143 | + } |
| 144 | + |
| 145 | + dependencies { |
| 146 | + implementation files("libs/foo.jmod") |
| 147 | + } |
| 148 | + |
| 149 | + // Gradle does not recognize *.jmod files as modules ¯\\_(ツ)_/¯ |
| 150 | + tasks.withType(JavaCompile).configureEach { task -> |
| 151 | + task.doFirst { |
| 152 | + task.options.compilerArgs += ["--module-path", task.classpath.asPath] |
| 153 | + classpath = files() |
| 154 | + } |
| 155 | + } |
| 156 | + """; |
| 157 | + build.runner("image").build(); |
| 158 | + assertThat(build.projectDir.resolve("build/images/demo/legal/foo/LICENSE")) |
| 159 | + .content() |
| 160 | + .isEqualTo("DUMMY LICENSE TEXT"); |
| 161 | + assertThat(build.projectDir.resolve("build/images/demo/legal/java.base/LICENSE")) |
| 162 | + .content() |
| 163 | + .startsWith("The GNU General Public License (GPL)"); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments