test/jdk/tools/jpackage/share/LicenseTest.java
branchJDK-8200758-branch
changeset 58304 7a61351edad2
parent 58303 88453b906981
child 58416 f09bf58c1f17
equal deleted inserted replaced
58303:88453b906981 58304:7a61351edad2
    23 
    23 
    24 import java.io.IOException;
    24 import java.io.IOException;
    25 import java.nio.file.Files;
    25 import java.nio.file.Files;
    26 import java.nio.file.Path;
    26 import java.nio.file.Path;
    27 import java.util.List;
    27 import java.util.List;
       
    28 import java.util.ArrayList;
    28 import java.util.Arrays;
    29 import java.util.Arrays;
    29 import java.util.function.Function;
    30 import java.util.function.Function;
    30 import java.util.stream.Collectors;
    31 import java.util.stream.Collectors;
    31 import jdk.jpackage.test.JPackageCommand;
    32 import jdk.jpackage.test.JPackageCommand;
    32 import jdk.jpackage.test.PackageType;
    33 import jdk.jpackage.test.PackageType;
    62 /*
    63 /*
    63  * @test
    64  * @test
    64  * @summary jpackage with --license-file
    65  * @summary jpackage with --license-file
    65  * @library ../helpers
    66  * @library ../helpers
    66  * @modules jdk.jpackage/jdk.jpackage.internal
    67  * @modules jdk.jpackage/jdk.jpackage.internal
    67  * @run main/othervm/timeout=360 -Xmx512m LicenseTest
    68  * @run main/othervm/timeout=360 -Xmx512m LicenseTest testCommon
    68  */
    69  */
       
    70 
       
    71 /*
       
    72  * @test
       
    73  * @summary jpackage with --license-file
       
    74  * @library ../helpers
       
    75  * @modules jdk.jpackage/jdk.jpackage.internal
       
    76  * @requires (os.family == "linux")
       
    77  * @run main/othervm/timeout=360 -Xmx512m LicenseTest testCustomDebianCopyright
       
    78  * @run main/othervm/timeout=360 -Xmx512m LicenseTest testCustomDebianCopyrightSubst
       
    79  */
       
    80 
    69 public class LicenseTest {
    81 public class LicenseTest {
    70     public static void main(String[] args) {
    82     public static void main(String[] args) {
    71         Test.run(args, () -> {
    83         Test.run(args, () -> {
    72             new PackageTest().configureHelloApp()
    84             String testFuncName = args[0];
    73             .addInitializer(cmd -> {
    85             Test.trace(String.format("Running %s...", testFuncName));
    74                 cmd.addArguments("--license-file", LICENSE_FILE);
    86             Test.getTestClass().getDeclaredMethod(testFuncName).invoke(null);
    75             })
       
    76             .forTypes(PackageType.LINUX_DEB)
       
    77             .addBundleVerifier(cmd -> {
       
    78                 verifyLicenseFileInLinuxPackage(cmd, debLicenseFile(cmd));
       
    79             })
       
    80             .addInstallVerifier(cmd -> {
       
    81                 verifyLicenseFileInstalledDebian(debLicenseFile(cmd));
       
    82             })
       
    83             .addUninstallVerifier(cmd -> {
       
    84                 verifyLicenseFileNotInstalledLinux(debLicenseFile(cmd));
       
    85             })
       
    86             .forTypes(PackageType.LINUX_RPM)
       
    87             .addBundleVerifier(cmd -> {
       
    88                 verifyLicenseFileInLinuxPackage(cmd,rpmLicenseFile(cmd));
       
    89             })
       
    90             .addInstallVerifier(cmd -> {
       
    91                 verifyLicenseFileInstalledRpm(rpmLicenseFile(cmd));
       
    92             })
       
    93             .addUninstallVerifier(cmd -> {
       
    94                 verifyLicenseFileNotInstalledLinux(rpmLicenseFile(cmd));
       
    95             })
       
    96             .run();
       
    97         });
    87         });
    98      }
    88     }
       
    89 
       
    90     public static void testCommon() {
       
    91         new PackageTest().configureHelloApp()
       
    92         .addInitializer(cmd -> {
       
    93             cmd.addArguments("--license-file", LICENSE_FILE);
       
    94         })
       
    95         .forTypes(PackageType.LINUX)
       
    96         .addBundleVerifier(cmd -> {
       
    97             verifyLicenseFileInLinuxPackage(cmd, linuxLicenseFile(cmd));
       
    98         })
       
    99         .addInstallVerifier(cmd -> {
       
   100             Test.assertReadableFileExists(linuxLicenseFile(cmd));
       
   101         })
       
   102         .addUninstallVerifier(cmd -> {
       
   103             verifyLicenseFileNotInstalledLinux(linuxLicenseFile(cmd));
       
   104         })
       
   105         .forTypes(PackageType.LINUX_DEB)
       
   106         .addInstallVerifier(cmd -> {
       
   107             verifyLicenseFileInstalledDebian(debLicenseFile(cmd));
       
   108         })
       
   109         .forTypes(PackageType.LINUX_RPM)
       
   110         .addInstallVerifier(cmd -> {
       
   111             verifyLicenseFileInstalledRpm(rpmLicenseFile(cmd));
       
   112         })
       
   113         .run();
       
   114     }
       
   115 
       
   116     public static void testCustomDebianCopyright() {
       
   117         new CustomDebianCopyrightTest().run();
       
   118     }
       
   119 
       
   120     public static void testCustomDebianCopyrightSubst() {
       
   121         new CustomDebianCopyrightTest().withSubstitution(true).run();
       
   122     }
    99 
   123 
   100     private static Path rpmLicenseFile(JPackageCommand cmd) {
   124     private static Path rpmLicenseFile(JPackageCommand cmd) {
   101         final Path licenseRoot = Path.of(
   125         final Path licenseRoot = Path.of(
   102                 new Executor()
   126                 new Executor()
   103                 .setExecutable("rpm")
   127                 .setExecutable("rpm")
   107                 LinuxHelper.getPackageName(cmd), cmd.version())).resolve(
   131                 LinuxHelper.getPackageName(cmd), cmd.version())).resolve(
   108                 LICENSE_FILE.getFileName());
   132                 LICENSE_FILE.getFileName());
   109         return licensePath;
   133         return licensePath;
   110     }
   134     }
   111 
   135 
       
   136     private static Path linuxLicenseFile(JPackageCommand cmd) {
       
   137         cmd.verifyIsOfType(PackageType.LINUX);
       
   138         switch (cmd.packageType()) {
       
   139             case LINUX_DEB:
       
   140                 return debLicenseFile(cmd);
       
   141 
       
   142             case LINUX_RPM:
       
   143                 return rpmLicenseFile(cmd);
       
   144 
       
   145             default:
       
   146                 return null;
       
   147         }
       
   148     }
       
   149 
   112     private static Path debLicenseFile(JPackageCommand cmd) {
   150     private static Path debLicenseFile(JPackageCommand cmd) {
   113         return cmd.appInstallationDirectory().resolve("share/doc/copyright");
   151         return cmd.appInstallationDirectory().resolve("share/doc/copyright");
   114     }
   152     }
   115 
   153 
   116     private static void verifyLicenseFileInLinuxPackage(JPackageCommand cmd,
   154     private static void verifyLicenseFileInLinuxPackage(JPackageCommand cmd,
   119                 expectedLicensePath)).findFirst().orElse(null) != null,
   157                 expectedLicensePath)).findFirst().orElse(null) != null,
   120                 String.format("Check license file [%s] is in %s package",
   158                 String.format("Check license file [%s] is in %s package",
   121                         expectedLicensePath, LinuxHelper.getPackageName(cmd)));
   159                         expectedLicensePath, LinuxHelper.getPackageName(cmd)));
   122     }
   160     }
   123 
   161 
   124     private static void verifyLicenseFileInstalledRpm(Path licenseFile) {
   162     private static void verifyLicenseFileInstalledRpm(Path licenseFile) throws
   125         Test.assertTrue(Files.isReadable(licenseFile), String.format(
   163             IOException {
   126                 "Check license file [%s] is readable", licenseFile));
   164         Test.assertStringListEquals(Files.readAllLines(LICENSE_FILE),
   127         try {
   165                 Files.readAllLines(licenseFile), String.format(
   128             Test.assertTrue(Files.readAllLines(licenseFile).equals(
   166                 "Check contents of package license file [%s] are the same as contents of source license file [%s]",
   129                     Files.readAllLines(LICENSE_FILE)), String.format(
   167                 licenseFile, LICENSE_FILE));
   130                     "Check contents of package license file [%s] are the same as contents of source license file [%s]",
   168     }
   131                     licenseFile, LICENSE_FILE));
   169 
   132         } catch (IOException ex) {
   170     private static void verifyLicenseFileInstalledDebian(Path licenseFile)
   133             throw new RuntimeException(ex);
   171             throws IOException {
   134         }
   172 
   135     }
   173         List<String> actualLines = Files.readAllLines(licenseFile).stream().dropWhile(
   136 
   174                 line -> !line.startsWith("License:")).collect(
   137     private static void verifyLicenseFileInstalledDebian(Path licenseFile) {
   175                         Collectors.toList());
   138         Test.assertTrue(Files.isReadable(licenseFile), String.format(
   176         // Remove leading `License:` followed by the whitespace from the first text line.
   139                 "Check license file [%s] is readable", licenseFile));
   177         actualLines.set(0, actualLines.get(0).split("\\s+", 2)[1]);
   140 
   178 
   141         Function<List<String>, List<String>> stripper = (lines) -> Arrays.asList(
   179         actualLines = DEBIAN_COPYRIGT_FILE_STRIPPER.apply(actualLines);
   142                 String.join("\n", lines).stripTrailing().split("\n"));
   180 
   143 
   181         Test.assertNotEquals(0, String.join("\n", actualLines).length(),
   144         try {
   182                 "Check stripped license text is not empty");
   145             List<String> actualLines = Files.readAllLines(licenseFile).stream().dropWhile(
   183 
   146                     line -> !line.startsWith("License:")).collect(
   184         Test.assertStringListEquals(DEBIAN_COPYRIGT_FILE_STRIPPER.apply(
   147                             Collectors.toList());
   185                 Files.readAllLines(LICENSE_FILE)), actualLines, String.format(
   148             // Remove leading `License:` followed by the whitespace from the first text line.
   186                 "Check subset of package license file [%s] is a match of the source license file [%s]",
   149             actualLines.set(0, actualLines.get(0).split("\\s+", 2)[1]);
   187                 licenseFile, LICENSE_FILE));
   150 
       
   151             actualLines = stripper.apply(actualLines);
       
   152 
       
   153             Test.assertNotEquals(0, String.join("\n", actualLines).length(),
       
   154                     "Check stripped license text is not empty");
       
   155 
       
   156             Test.assertTrue(actualLines.equals(
       
   157                     stripper.apply(Files.readAllLines(LICENSE_FILE))),
       
   158                     String.format(
       
   159                             "Check subset of package license file [%s] is a match of the source license file [%s]",
       
   160                             licenseFile, LICENSE_FILE));
       
   161         } catch (IOException ex) {
       
   162             throw new RuntimeException(ex);
       
   163         }
       
   164     }
   188     }
   165 
   189 
   166     private static void verifyLicenseFileNotInstalledLinux(Path licenseFile) {
   190     private static void verifyLicenseFileNotInstalledLinux(Path licenseFile) {
   167         Test.assertDirectoryExists(licenseFile.getParent(), false);
   191         Test.assertPathExists(licenseFile.getParent(), false);
       
   192     }
       
   193 
       
   194     private static class CustomDebianCopyrightTest {
       
   195         CustomDebianCopyrightTest() {
       
   196             withSubstitution(false);
       
   197         }
       
   198 
       
   199         private List<String> licenseFileText(String copyright, String licenseText) {
       
   200             List<String> lines = new ArrayList(List.of(
       
   201                     String.format("Copyright=%s", copyright),
       
   202                     "Foo",
       
   203                     "Bar",
       
   204                     "Buz"));
       
   205             lines.addAll(List.of(licenseText.split("\\R", -1)));
       
   206             return lines;
       
   207         }
       
   208 
       
   209         private List<String> licenseFileText() {
       
   210             if (withSubstitution) {
       
   211                 return licenseFileText("APPLICATION_COPYRIGHT",
       
   212                         "APPLICATION_LICENSE_TEXT");
       
   213             } else {
       
   214                 return expetedLicenseFileText();
       
   215             }
       
   216         }
       
   217 
       
   218         private List<String> expetedLicenseFileText() {
       
   219             return licenseFileText(copyright, licenseText);
       
   220         }
       
   221 
       
   222         CustomDebianCopyrightTest withSubstitution(boolean v) {
       
   223             withSubstitution = v;
       
   224             // Different values just to make easy to figure out from the test log which test was executed.
       
   225             if (v) {
       
   226                 copyright = "Duke (C)";
       
   227                 licenseText = "The quick brown fox\n jumps over the lazy dog";
       
   228             } else {
       
   229                 copyright = "Java (C)";
       
   230                 licenseText = "How vexingly quick daft zebras jump!";
       
   231             }
       
   232             return this;
       
   233         }
       
   234 
       
   235         void run() {
       
   236             final Path srcLicenseFile = Test.workDir().resolve("license");
       
   237             new PackageTest().configureHelloApp().forTypes(PackageType.LINUX_DEB)
       
   238             .addInitializer(cmd -> {
       
   239                 // Create source license file.
       
   240                 Files.write(srcLicenseFile, List.of(
       
   241                         licenseText.split("\\R", -1)));
       
   242 
       
   243                 cmd.setFakeRuntime();
       
   244                 cmd.setArgumentValue("--name", String.format("%s%s",
       
   245                         withSubstitution ? "CustomDebianCopyrightWithSubst" : "CustomDebianCopyright",
       
   246                         cmd.name()));
       
   247                 cmd.addArguments("--license-file", srcLicenseFile);
       
   248                 cmd.addArguments("--copyright", copyright);
       
   249                 cmd.addArguments("--resource-dir", RESOURCE_DIR);
       
   250 
       
   251                 // Create copyright template file in a resource dir.
       
   252                 Files.createDirectories(RESOURCE_DIR);
       
   253                 Files.write(RESOURCE_DIR.resolve("copyright"),
       
   254                         licenseFileText());
       
   255             })
       
   256             .addInstallVerifier(cmd -> {
       
   257                 Path installedLicenseFile = debLicenseFile(cmd);
       
   258                 Test.assertStringListEquals(expetedLicenseFileText(),
       
   259                         DEBIAN_COPYRIGT_FILE_STRIPPER.apply(Files.readAllLines(
       
   260                                 installedLicenseFile)), String.format(
       
   261                                 "Check contents of package license file [%s] are the same as contents of source license file [%s]",
       
   262                                 installedLicenseFile, srcLicenseFile));
       
   263             })
       
   264             .run();
       
   265         }
       
   266 
       
   267         private boolean withSubstitution;
       
   268         private String copyright;
       
   269         private String licenseText;
       
   270 
       
   271         private final Path RESOURCE_DIR = Test.workDir().resolve("resources");
   168     }
   272     }
   169 
   273 
   170     private static final Path LICENSE_FILE = Test.TEST_SRC_ROOT.resolve(
   274     private static final Path LICENSE_FILE = Test.TEST_SRC_ROOT.resolve(
   171             Path.of("resources", "license.txt"));
   275             Path.of("resources", "license.txt"));
       
   276 
       
   277     private static final Function<List<String>, List<String>> DEBIAN_COPYRIGT_FILE_STRIPPER = (lines) -> Arrays.asList(
       
   278             String.join("\n", lines).stripTrailing().split("\n"));
   172 }
   279 }