Path: blob/master/test/jdk/tools/jpackage/share/LicenseTest.java
41152 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.nio.file.Files;25import java.nio.file.Path;26import java.util.List;27import java.util.ArrayList;28import java.util.Arrays;29import java.util.function.Function;30import java.util.stream.Collectors;31import jdk.jpackage.test.JPackageCommand;32import jdk.jpackage.test.PackageType;33import jdk.jpackage.test.PackageTest;34import jdk.jpackage.test.LinuxHelper;35import jdk.jpackage.test.Executor;36import jdk.jpackage.test.TKit;3738/**39* Test --license-file parameter. Output of the test should be commonlicensetest*.*40* package bundle. The output package should provide the same functionality as41* the default package and also incorporate license information from42* test/jdk/tools/jpackage/resources/license.txt file from OpenJDK repo.43*44* deb:45*46* Package should install license file /opt/commonlicensetest/share/doc/copyright47* file.48*49* rpm:50*51* Package should install license file in52* %{_defaultlicensedir}/licensetest-1.0/license.txt file.53*54* Mac:55*56* Windows57*58* Installer should display license text matching contents of the license file59* during installation.60*/6162/*63* @test64* @summary jpackage with --license-file65* @library ../helpers66* @key jpackagePlatformPackage67* @build jdk.jpackage.test.*68* @compile LicenseTest.java69* @modules jdk.jpackage/jdk.jpackage.internal70* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main71* --jpt-run=LicenseTest.testCommon72*/7374/*75* @test76* @summary jpackage with --license-file77* @library ../helpers78* @key jpackagePlatformPackage79* @build jdk.jpackage.test.*80* @compile LicenseTest.java81* @requires (os.family == "linux")82* @requires (jpackage.test.SQETest == null)83* @modules jdk.jpackage/jdk.jpackage.internal84* @run main/othervm/timeout=1440 -Xmx512m jdk.jpackage.test.Main85* --jpt-run=LicenseTest.testCustomDebianCopyright86* --jpt-run=LicenseTest.testCustomDebianCopyrightSubst87* --jpt-run=LicenseTest.testLinuxLicenseInUsrTree88* --jpt-run=LicenseTest.testLinuxLicenseInUsrTree289* --jpt-run=LicenseTest.testLinuxLicenseInUsrTree390* --jpt-run=LicenseTest.testLinuxLicenseInUsrTree491*/9293public class LicenseTest {94public static void testCommon() {95PackageTest test = new PackageTest().configureHelloApp()96.addInitializer(cmd -> {97cmd.addArguments("--license-file", TKit.createRelativePathCopy(98LICENSE_FILE));99});100101initLinuxLicenseVerifier(test.forTypes(PackageType.LINUX));102103test.run();104}105106public static void testLinuxLicenseInUsrTree() {107testLinuxLicenseInUsrTree("/usr");108}109110public static void testLinuxLicenseInUsrTree2() {111testLinuxLicenseInUsrTree("/usr/local");112}113114public static void testLinuxLicenseInUsrTree3() {115testLinuxLicenseInUsrTree("/usr/foo");116}117118public static void testLinuxLicenseInUsrTree4() {119testLinuxLicenseInUsrTree("/usrbuz");120}121122public static void testCustomDebianCopyright() {123new CustomDebianCopyrightTest().run();124}125126public static void testCustomDebianCopyrightSubst() {127new CustomDebianCopyrightTest().withSubstitution(true).run();128}129130private static PackageTest initLinuxLicenseVerifier(PackageTest test) {131return test132.addBundleVerifier(cmd -> {133verifyLicenseFileInLinuxPackage(cmd, linuxLicenseFile(cmd));134})135.addInstallVerifier(cmd -> {136verifyLicenseFileInstalledLinux(cmd);137})138.addUninstallVerifier(cmd -> {139verifyLicenseFileNotInstalledLinux(linuxLicenseFile(cmd));140});141}142143private static void testLinuxLicenseInUsrTree(String installDir) {144PackageTest test = new PackageTest()145.forTypes(PackageType.LINUX)146.configureHelloApp()147.addInitializer(cmd -> {148cmd.setFakeRuntime();149cmd.addArguments("--license-file", TKit.createRelativePathCopy(150LICENSE_FILE));151cmd.addArguments("--install-dir", installDir);152});153154initLinuxLicenseVerifier(test);155156test.run();157}158159private static Path rpmLicenseFile(JPackageCommand cmd) {160final Path licenseRoot = Path.of(161new Executor()162.setExecutable("rpm")163.addArguments("--eval", "%{_defaultlicensedir}")164.executeAndGetFirstLineOfOutput());165166final Path licensePath = licenseRoot.resolve(String.format("%s-%s",167LinuxHelper.getPackageName(cmd), cmd.version())).resolve(168LICENSE_FILE.getFileName());169170return licensePath;171}172173private static Path debLicenseFile(JPackageCommand cmd) {174Path installDir = cmd.appInstallationDirectory();175176if (installDir.equals(Path.of("/")) || installDir.startsWith("/usr")) {177// Package is in '/usr' tree178return Path.of("/usr/share/doc/", LinuxHelper.getPackageName(cmd),179"copyright");180}181182return installDir.resolve("share/doc/copyright");183}184185private static Path linuxLicenseFile(JPackageCommand cmd) {186cmd.verifyIsOfType(PackageType.LINUX);187final Path licenseFile;188switch (cmd.packageType()) {189case LINUX_DEB:190licenseFile = debLicenseFile(cmd);191break;192193case LINUX_RPM:194licenseFile = rpmLicenseFile(cmd);195break;196197default:198throw new IllegalArgumentException();199}200201return cmd.pathToUnpackedPackageFile(licenseFile);202}203204private static void verifyLicenseFileInLinuxPackage(JPackageCommand cmd,205Path expectedLicensePath) {206TKit.assertTrue(LinuxHelper.getPackageFiles(cmd).filter(path -> path.equals(207expectedLicensePath)).findFirst().orElse(null) != null,208String.format("Check license file [%s] is in %s package",209expectedLicensePath, LinuxHelper.getPackageName(cmd)));210}211212private static void verifyLicenseFileInstalledRpm(Path licenseFile) throws213IOException {214TKit.assertStringListEquals(Files.readAllLines(LICENSE_FILE),215Files.readAllLines(licenseFile), String.format(216"Check contents of package license file [%s] are the same as contents of source license file [%s]",217licenseFile, LICENSE_FILE));218}219220private static void verifyLicenseFileInstalledDebian(Path licenseFile)221throws IOException {222223List<String> actualLines = Files.readAllLines(licenseFile).stream().dropWhile(224line -> !line.startsWith("License:")).collect(225Collectors.toList());226// Remove leading `License:` followed by the whitespace from the first text line.227actualLines.set(0, actualLines.get(0).split("\\s+", 2)[1]);228229actualLines = DEBIAN_COPYRIGT_FILE_STRIPPER.apply(actualLines);230231TKit.assertNotEquals(0, String.join("\n", actualLines).length(),232"Check stripped license text is not empty");233234TKit.assertStringListEquals(DEBIAN_COPYRIGT_FILE_STRIPPER.apply(235Files.readAllLines(LICENSE_FILE)), actualLines, String.format(236"Check subset of package license file [%s] is a match of the source license file [%s]",237licenseFile, LICENSE_FILE));238}239240private static void verifyLicenseFileInstalledLinux(JPackageCommand cmd)241throws IOException {242243final Path licenseFile = linuxLicenseFile(cmd);244TKit.assertReadableFileExists(licenseFile);245246switch (cmd.packageType()) {247case LINUX_DEB:248verifyLicenseFileInstalledDebian(licenseFile);249break;250251case LINUX_RPM:252verifyLicenseFileInstalledRpm(licenseFile);253break;254255default:256throw new IllegalArgumentException();257}258}259260private static void verifyLicenseFileNotInstalledLinux(Path licenseFile) {261TKit.assertPathExists(licenseFile.getParent(), false);262}263264private static class CustomDebianCopyrightTest {265CustomDebianCopyrightTest() {266withSubstitution(false);267}268269private List<String> licenseFileText(String copyright, String licenseText) {270List<String> lines = new ArrayList(List.of(271String.format("Copyright=%s", copyright),272"Foo",273"Bar",274"Buz"));275lines.addAll(List.of(licenseText.split("\\R", -1)));276return lines;277}278279private List<String> licenseFileText() {280if (withSubstitution) {281return licenseFileText("APPLICATION_COPYRIGHT",282"APPLICATION_LICENSE_TEXT");283} else {284return expetedLicenseFileText();285}286}287288private List<String> expetedLicenseFileText() {289return licenseFileText(copyright, licenseText);290}291292CustomDebianCopyrightTest withSubstitution(boolean v) {293withSubstitution = v;294// Different values just to make easy to figure out from the test log which test was executed.295if (v) {296copyright = "Duke (C)";297licenseText = "The quick brown fox\n jumps over the lazy dog";298} else {299copyright = "Java (C)";300licenseText = "How vexingly quick daft zebras jump!";301}302return this;303}304305void run() {306final Path srcLicenseFile = TKit.workDir().resolve("license");307new PackageTest().forTypes(PackageType.LINUX_DEB).configureHelloApp()308.addInitializer(cmd -> {309// Create source license file.310Files.write(srcLicenseFile, List.of(311licenseText.split("\\R", -1)));312313cmd.setFakeRuntime();314cmd.setArgumentValue("--name", String.format("%s%s",315withSubstitution ? "CustomDebianCopyrightWithSubst" : "CustomDebianCopyright",316cmd.name()));317cmd.addArguments("--license-file", srcLicenseFile);318cmd.addArguments("--copyright", copyright);319cmd.addArguments("--resource-dir", RESOURCE_DIR);320321// Create copyright template file in a resource dir.322Files.createDirectories(RESOURCE_DIR);323Files.write(RESOURCE_DIR.resolve("copyright"),324licenseFileText());325})326.addInstallVerifier(cmd -> {327Path installedLicenseFile = linuxLicenseFile(cmd);328TKit.assertStringListEquals(expetedLicenseFileText(),329DEBIAN_COPYRIGT_FILE_STRIPPER.apply(Files.readAllLines(330installedLicenseFile)), String.format(331"Check contents of package license file [%s] are the same as contents of source license file [%s]",332installedLicenseFile, srcLicenseFile));333})334.run();335}336337private boolean withSubstitution;338private String copyright;339private String licenseText;340341private final Path RESOURCE_DIR = TKit.workDir().resolve("resources");342}343344private static final Path LICENSE_FILE = TKit.TEST_SRC_ROOT.resolve(345Path.of("resources", "license.txt"));346347private static final Function<List<String>, List<String>> DEBIAN_COPYRIGT_FILE_STRIPPER = (lines) -> Arrays.asList(348String.join("\n", lines).stripTrailing().split("\n"));349}350351352