test/jdk/tools/jpackage/share/LicenseTest.java
branchJDK-8200758-branch
changeset 58304 7a61351edad2
parent 58303 88453b906981
child 58416 f09bf58c1f17
--- a/test/jdk/tools/jpackage/share/LicenseTest.java	Tue Sep 24 13:50:40 2019 -0400
+++ b/test/jdk/tools/jpackage/share/LicenseTest.java	Tue Sep 24 13:57:28 2019 -0400
@@ -25,6 +25,7 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.List;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -64,38 +65,61 @@
  * @summary jpackage with --license-file
  * @library ../helpers
  * @modules jdk.jpackage/jdk.jpackage.internal
- * @run main/othervm/timeout=360 -Xmx512m LicenseTest
+ * @run main/othervm/timeout=360 -Xmx512m LicenseTest testCommon
  */
+
+/*
+ * @test
+ * @summary jpackage with --license-file
+ * @library ../helpers
+ * @modules jdk.jpackage/jdk.jpackage.internal
+ * @requires (os.family == "linux")
+ * @run main/othervm/timeout=360 -Xmx512m LicenseTest testCustomDebianCopyright
+ * @run main/othervm/timeout=360 -Xmx512m LicenseTest testCustomDebianCopyrightSubst
+ */
+
 public class LicenseTest {
     public static void main(String[] args) {
         Test.run(args, () -> {
-            new PackageTest().configureHelloApp()
-            .addInitializer(cmd -> {
-                cmd.addArguments("--license-file", LICENSE_FILE);
-            })
-            .forTypes(PackageType.LINUX_DEB)
-            .addBundleVerifier(cmd -> {
-                verifyLicenseFileInLinuxPackage(cmd, debLicenseFile(cmd));
-            })
-            .addInstallVerifier(cmd -> {
-                verifyLicenseFileInstalledDebian(debLicenseFile(cmd));
-            })
-            .addUninstallVerifier(cmd -> {
-                verifyLicenseFileNotInstalledLinux(debLicenseFile(cmd));
-            })
-            .forTypes(PackageType.LINUX_RPM)
-            .addBundleVerifier(cmd -> {
-                verifyLicenseFileInLinuxPackage(cmd,rpmLicenseFile(cmd));
-            })
-            .addInstallVerifier(cmd -> {
-                verifyLicenseFileInstalledRpm(rpmLicenseFile(cmd));
-            })
-            .addUninstallVerifier(cmd -> {
-                verifyLicenseFileNotInstalledLinux(rpmLicenseFile(cmd));
-            })
-            .run();
+            String testFuncName = args[0];
+            Test.trace(String.format("Running %s...", testFuncName));
+            Test.getTestClass().getDeclaredMethod(testFuncName).invoke(null);
         });
-     }
+    }
+
+    public static void testCommon() {
+        new PackageTest().configureHelloApp()
+        .addInitializer(cmd -> {
+            cmd.addArguments("--license-file", LICENSE_FILE);
+        })
+        .forTypes(PackageType.LINUX)
+        .addBundleVerifier(cmd -> {
+            verifyLicenseFileInLinuxPackage(cmd, linuxLicenseFile(cmd));
+        })
+        .addInstallVerifier(cmd -> {
+            Test.assertReadableFileExists(linuxLicenseFile(cmd));
+        })
+        .addUninstallVerifier(cmd -> {
+            verifyLicenseFileNotInstalledLinux(linuxLicenseFile(cmd));
+        })
+        .forTypes(PackageType.LINUX_DEB)
+        .addInstallVerifier(cmd -> {
+            verifyLicenseFileInstalledDebian(debLicenseFile(cmd));
+        })
+        .forTypes(PackageType.LINUX_RPM)
+        .addInstallVerifier(cmd -> {
+            verifyLicenseFileInstalledRpm(rpmLicenseFile(cmd));
+        })
+        .run();
+    }
+
+    public static void testCustomDebianCopyright() {
+        new CustomDebianCopyrightTest().run();
+    }
+
+    public static void testCustomDebianCopyrightSubst() {
+        new CustomDebianCopyrightTest().withSubstitution(true).run();
+    }
 
     private static Path rpmLicenseFile(JPackageCommand cmd) {
         final Path licenseRoot = Path.of(
@@ -109,6 +133,20 @@
         return licensePath;
     }
 
+    private static Path linuxLicenseFile(JPackageCommand cmd) {
+        cmd.verifyIsOfType(PackageType.LINUX);
+        switch (cmd.packageType()) {
+            case LINUX_DEB:
+                return debLicenseFile(cmd);
+
+            case LINUX_RPM:
+                return rpmLicenseFile(cmd);
+
+            default:
+                return null;
+        }
+    }
+
     private static Path debLicenseFile(JPackageCommand cmd) {
         return cmd.appInstallationDirectory().resolve("share/doc/copyright");
     }
@@ -121,52 +159,121 @@
                         expectedLicensePath, LinuxHelper.getPackageName(cmd)));
     }
 
-    private static void verifyLicenseFileInstalledRpm(Path licenseFile) {
-        Test.assertTrue(Files.isReadable(licenseFile), String.format(
-                "Check license file [%s] is readable", licenseFile));
-        try {
-            Test.assertTrue(Files.readAllLines(licenseFile).equals(
-                    Files.readAllLines(LICENSE_FILE)), String.format(
-                    "Check contents of package license file [%s] are the same as contents of source license file [%s]",
-                    licenseFile, LICENSE_FILE));
-        } catch (IOException ex) {
-            throw new RuntimeException(ex);
-        }
+    private static void verifyLicenseFileInstalledRpm(Path licenseFile) throws
+            IOException {
+        Test.assertStringListEquals(Files.readAllLines(LICENSE_FILE),
+                Files.readAllLines(licenseFile), String.format(
+                "Check contents of package license file [%s] are the same as contents of source license file [%s]",
+                licenseFile, LICENSE_FILE));
     }
 
-    private static void verifyLicenseFileInstalledDebian(Path licenseFile) {
-        Test.assertTrue(Files.isReadable(licenseFile), String.format(
-                "Check license file [%s] is readable", licenseFile));
-
-        Function<List<String>, List<String>> stripper = (lines) -> Arrays.asList(
-                String.join("\n", lines).stripTrailing().split("\n"));
+    private static void verifyLicenseFileInstalledDebian(Path licenseFile)
+            throws IOException {
 
-        try {
-            List<String> actualLines = Files.readAllLines(licenseFile).stream().dropWhile(
-                    line -> !line.startsWith("License:")).collect(
-                            Collectors.toList());
-            // Remove leading `License:` followed by the whitespace from the first text line.
-            actualLines.set(0, actualLines.get(0).split("\\s+", 2)[1]);
+        List<String> actualLines = Files.readAllLines(licenseFile).stream().dropWhile(
+                line -> !line.startsWith("License:")).collect(
+                        Collectors.toList());
+        // Remove leading `License:` followed by the whitespace from the first text line.
+        actualLines.set(0, actualLines.get(0).split("\\s+", 2)[1]);
 
-            actualLines = stripper.apply(actualLines);
+        actualLines = DEBIAN_COPYRIGT_FILE_STRIPPER.apply(actualLines);
 
-            Test.assertNotEquals(0, String.join("\n", actualLines).length(),
-                    "Check stripped license text is not empty");
+        Test.assertNotEquals(0, String.join("\n", actualLines).length(),
+                "Check stripped license text is not empty");
 
-            Test.assertTrue(actualLines.equals(
-                    stripper.apply(Files.readAllLines(LICENSE_FILE))),
-                    String.format(
-                            "Check subset of package license file [%s] is a match of the source license file [%s]",
-                            licenseFile, LICENSE_FILE));
-        } catch (IOException ex) {
-            throw new RuntimeException(ex);
-        }
+        Test.assertStringListEquals(DEBIAN_COPYRIGT_FILE_STRIPPER.apply(
+                Files.readAllLines(LICENSE_FILE)), actualLines, String.format(
+                "Check subset of package license file [%s] is a match of the source license file [%s]",
+                licenseFile, LICENSE_FILE));
     }
 
     private static void verifyLicenseFileNotInstalledLinux(Path licenseFile) {
-        Test.assertDirectoryExists(licenseFile.getParent(), false);
+        Test.assertPathExists(licenseFile.getParent(), false);
+    }
+
+    private static class CustomDebianCopyrightTest {
+        CustomDebianCopyrightTest() {
+            withSubstitution(false);
+        }
+
+        private List<String> licenseFileText(String copyright, String licenseText) {
+            List<String> lines = new ArrayList(List.of(
+                    String.format("Copyright=%s", copyright),
+                    "Foo",
+                    "Bar",
+                    "Buz"));
+            lines.addAll(List.of(licenseText.split("\\R", -1)));
+            return lines;
+        }
+
+        private List<String> licenseFileText() {
+            if (withSubstitution) {
+                return licenseFileText("APPLICATION_COPYRIGHT",
+                        "APPLICATION_LICENSE_TEXT");
+            } else {
+                return expetedLicenseFileText();
+            }
+        }
+
+        private List<String> expetedLicenseFileText() {
+            return licenseFileText(copyright, licenseText);
+        }
+
+        CustomDebianCopyrightTest withSubstitution(boolean v) {
+            withSubstitution = v;
+            // Different values just to make easy to figure out from the test log which test was executed.
+            if (v) {
+                copyright = "Duke (C)";
+                licenseText = "The quick brown fox\n jumps over the lazy dog";
+            } else {
+                copyright = "Java (C)";
+                licenseText = "How vexingly quick daft zebras jump!";
+            }
+            return this;
+        }
+
+        void run() {
+            final Path srcLicenseFile = Test.workDir().resolve("license");
+            new PackageTest().configureHelloApp().forTypes(PackageType.LINUX_DEB)
+            .addInitializer(cmd -> {
+                // Create source license file.
+                Files.write(srcLicenseFile, List.of(
+                        licenseText.split("\\R", -1)));
+
+                cmd.setFakeRuntime();
+                cmd.setArgumentValue("--name", String.format("%s%s",
+                        withSubstitution ? "CustomDebianCopyrightWithSubst" : "CustomDebianCopyright",
+                        cmd.name()));
+                cmd.addArguments("--license-file", srcLicenseFile);
+                cmd.addArguments("--copyright", copyright);
+                cmd.addArguments("--resource-dir", RESOURCE_DIR);
+
+                // Create copyright template file in a resource dir.
+                Files.createDirectories(RESOURCE_DIR);
+                Files.write(RESOURCE_DIR.resolve("copyright"),
+                        licenseFileText());
+            })
+            .addInstallVerifier(cmd -> {
+                Path installedLicenseFile = debLicenseFile(cmd);
+                Test.assertStringListEquals(expetedLicenseFileText(),
+                        DEBIAN_COPYRIGT_FILE_STRIPPER.apply(Files.readAllLines(
+                                installedLicenseFile)), String.format(
+                                "Check contents of package license file [%s] are the same as contents of source license file [%s]",
+                                installedLicenseFile, srcLicenseFile));
+            })
+            .run();
+        }
+
+        private boolean withSubstitution;
+        private String copyright;
+        private String licenseText;
+
+        private final Path RESOURCE_DIR = Test.workDir().resolve("resources");
     }
 
     private static final Path LICENSE_FILE = Test.TEST_SRC_ROOT.resolve(
             Path.of("resources", "license.txt"));
+
+    private static final Function<List<String>, List<String>> DEBIAN_COPYRIGT_FILE_STRIPPER = (lines) -> Arrays.asList(
+            String.join("\n", lines).stripTrailing().split("\n"));
 }