test/jdk/tools/jpackage/share/jdk/jpackage/tests/BasicTest.java
branchJDK-8200758-branch
changeset 58416 f09bf58c1f17
child 58463 4e71249f291c
equal deleted inserted replaced
58415:73f8e557549a 58416:f09bf58c1f17
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package jdk.jpackage.tests;
       
    25 
       
    26 import java.io.IOException;
       
    27 import java.nio.file.Files;
       
    28 import java.nio.file.Path;
       
    29 import java.util.List;
       
    30 import java.util.ArrayList;
       
    31 import java.util.stream.Collectors;
       
    32 import jdk.jpackage.test.Executor;
       
    33 import jdk.jpackage.test.JPackageCommand;
       
    34 import jdk.jpackage.test.TKit;
       
    35 import jdk.jpackage.test.JavaTool;
       
    36 import jdk.jpackage.test.HelloApp;
       
    37 import jdk.jpackage.test.Annotations.*;
       
    38 
       
    39 /*
       
    40  * @test
       
    41  * @summary jpackage basic testing
       
    42  * @library ../../../../helpers
       
    43  * @build jdk.jpackage.test.*
       
    44  * @modules jdk.jpackage
       
    45  * @compile BasicTest.java
       
    46  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
       
    47  *  --jpt-run=jdk.jpackage.tests.BasicTest
       
    48  */
       
    49 
       
    50 public class BasicTest {
       
    51     @Test
       
    52     public void testNoArgs() {
       
    53         List<String> output = JPackageCommand.filterOutput(
       
    54                 createVanillaJPackageCommand().executeAndGetOutput());
       
    55         TKit.assertStringListEquals(List.of("Usage: jpackage <mode> <options>",
       
    56                 "Use jpackage --help (or -h) for a list of possible options"),
       
    57                 output, "Check jpackage output");
       
    58     }
       
    59 
       
    60     @Test
       
    61     public void testVersion() {
       
    62         List<String> output = JPackageCommand.filterOutput(
       
    63                 createVanillaJPackageCommand()
       
    64                         .addArgument("--version")
       
    65                         .executeAndGetOutput());
       
    66         TKit.assertStringListEquals(List.of(System.getProperty("java.version")),
       
    67                 output, "Check jpackage output");
       
    68     }
       
    69 
       
    70     @Test
       
    71     public void testNoName() {
       
    72         final String mainClassName = "Greetings";
       
    73 
       
    74         JPackageCommand cmd = new JPackageCommand()
       
    75                 .helloAppImage(mainClassName)
       
    76                 .removeArgumentWithValue("--name");
       
    77 
       
    78         Path expectedImageDir = cmd.outputDir().resolve(mainClassName);
       
    79         if (TKit.isOSX()) {
       
    80             expectedImageDir = expectedImageDir.getParent().resolve(
       
    81                     expectedImageDir.getFileName().toString() + ".app");
       
    82         }
       
    83 
       
    84         cmd.executeAndAssertHelloAppImageCreated();
       
    85         TKit.assertEquals(expectedImageDir.toAbsolutePath().normalize().toString(),
       
    86                 cmd.appImage().toAbsolutePath().normalize().toString(),
       
    87                 String.format(
       
    88                         "Check [%s] directory is filled with application image data",
       
    89                         expectedImageDir));
       
    90     }
       
    91 
       
    92     @Test
       
    93     public void testApp() {
       
    94         new JPackageCommand()
       
    95         .helloAppImage()
       
    96         .executeAndAssertHelloAppImageCreated();
       
    97     }
       
    98 
       
    99     @Test
       
   100     public void testModularApp() {
       
   101         new JPackageCommand()
       
   102         .helloAppImage("com.other/com.other.Hello")
       
   103         .executeAndAssertHelloAppImageCreated();
       
   104     }
       
   105 
       
   106     @Test
       
   107 //    @Parameter("ALL-MODULE-PATH") ; This test fails
       
   108     @Parameter("ALL-DEFAULT")
       
   109     public void testAddModules(String addModulesArg) {
       
   110         JPackageCommand cmd = new JPackageCommand()
       
   111             .helloAppImage("com.other/com.other.Hello");
       
   112         if (!addModulesArg.isEmpty()) {
       
   113             cmd.addArguments("--add-modules", addModulesArg);
       
   114         }
       
   115         cmd.executeAndAssertHelloAppImageCreated();
       
   116     }
       
   117 
       
   118     /**
       
   119      * Test --temp option. Doesn't make much sense for app image as temporary
       
   120      * directory is used only on Windows.
       
   121      * @throws IOException
       
   122      */
       
   123 //    @Test
       
   124     public void testTemp() throws IOException {
       
   125         JPackageCommand cmd = new JPackageCommand().helloAppImage();
       
   126         TKit.withTempDirectory("temp-root", tempDir -> {
       
   127             cmd.addArguments("--temp", tempDir);
       
   128 
       
   129             cmd.executeAndAssertHelloAppImageCreated();
       
   130 
       
   131             // Check jpackage actually used the supplied directory.
       
   132             TKit.assertNotEquals(0, tempDir.toFile().list().length,
       
   133                     String.format(
       
   134                             "Check jpackage wrote some data in the supplied temporary directory [%s]",
       
   135                             tempDir));
       
   136 
       
   137             // Temporary directory should not be empty,
       
   138             // jpackage should exit with error.
       
   139             cmd.execute().assertExitCodeIs(1);
       
   140         });
       
   141     }
       
   142 
       
   143     @Test
       
   144     public void testAtFile() throws IOException {
       
   145         JPackageCommand cmd = new JPackageCommand().helloAppImage();
       
   146 
       
   147         // Init options file with the list of options configured
       
   148         // for JPackageCommand instance.
       
   149         final Path optionsFile = TKit.workDir().resolve("options");
       
   150         Files.write(optionsFile,
       
   151                 List.of(String.join(" ", cmd.getAllArguments())));
       
   152 
       
   153         // Build app jar file.
       
   154         cmd.executePrerequisiteActions();
       
   155 
       
   156         // Make sure output directory is empty. Normally JPackageCommand would
       
   157         // do this automatically.
       
   158         TKit.deleteDirectoryContentsRecursive(cmd.outputDir());
       
   159 
       
   160         // Instead of running jpackage command through configured
       
   161         // JPackageCommand instance, run vanilla jpackage command with @ file.
       
   162         createVanillaJPackageCommand()
       
   163                 .addArgument(String.format("@%s", optionsFile))
       
   164                 .execute().assertExitCodeIsZero();
       
   165 
       
   166         // Verify output of jpackage command.
       
   167         cmd.assertImageCreated();
       
   168         HelloApp.executeLauncherAndVerifyOutput(cmd);
       
   169     }
       
   170 
       
   171     @Parameter("Hello")
       
   172     @Parameter("com.foo/com.foo.main.Aloha")
       
   173     @Test
       
   174     public void testJLinkRuntime(String javaAppDesc) {
       
   175         JPackageCommand cmd = new JPackageCommand().helloAppImage(javaAppDesc);
       
   176 
       
   177         // If `--module` parameter was set on jpackage command line, get its
       
   178         // value and extract module name.
       
   179         // E.g.: foo.bar2/foo.bar.Buz -> foo.bar2
       
   180         // Note: HelloApp class manages `--module` parameter on jpackage command line
       
   181         final String moduleName = cmd.getArgumentValue("--module", () -> null,
       
   182                 (v) -> v.split("/", 2)[0]);
       
   183 
       
   184         if (moduleName != null) {
       
   185             // Build module jar.
       
   186             cmd.executePrerequisiteActions();
       
   187         }
       
   188 
       
   189         TKit.withTempDirectory("runtime", runtimeDir -> {
       
   190             TKit.deleteDirectoryRecursive(runtimeDir, String.format(
       
   191                     "Delete [%s] output directory for jlink command", runtimeDir));
       
   192             Executor jlink = createJavaToolCommand(JavaTool.JLINK)
       
   193             .saveOutput(false)
       
   194             .addArguments(
       
   195                     "--add-modules", "java.base",
       
   196                     "--output", runtimeDir.toString(),
       
   197                     "--strip-debug",
       
   198                     "--no-header-files",
       
   199                     "--no-man-pages");
       
   200 
       
   201             if (moduleName != null) {
       
   202                 jlink.addArguments("--add-modules", moduleName, "--module-path",
       
   203                         Path.of(cmd.getArgumentValue("--module-path")).resolve(
       
   204                                 "hello.jar").toString());
       
   205             }
       
   206 
       
   207             jlink.execute().assertExitCodeIsZero();
       
   208 
       
   209             cmd.addArguments("--runtime-image", runtimeDir);
       
   210             cmd.executeAndAssertHelloAppImageCreated();
       
   211 
       
   212             final Path appImageRuntimePath = cmd.appImage().resolve(
       
   213                     cmd.appRuntimeDirectoryInAppImage());
       
   214 
       
   215             // This is an overkill to list modules in jlink output as we have
       
   216             // already verified that Java app is functional and thus app's runtime
       
   217             // is likely to be OK, but let's double check.
       
   218             List<String> moduleList = new Executor().dumpOutput().setExecutable(
       
   219                     appImageRuntimePath.resolve(
       
   220                             JPackageCommand.relativePathInRuntime(JavaTool.JAVA))).addArguments(
       
   221                             "--list-modules").executeAndGetOutput().stream().sorted().collect(
       
   222                                     Collectors.toList());
       
   223 
       
   224             List<String> expectedModules = new ArrayList<>();
       
   225             expectedModules.add(String.format("java.base@%s",
       
   226                     System.getProperty("java.version")));
       
   227             if (moduleName != null) {
       
   228                 expectedModules.add(moduleName);
       
   229             }
       
   230             expectedModules = expectedModules.stream().sorted().collect(
       
   231                     Collectors.toList());
       
   232 
       
   233             TKit.assertStringListEquals(expectedModules, moduleList,
       
   234                     String.format(
       
   235                             "Check modules in application image runtime directory at [%s]",
       
   236                             appImageRuntimePath));
       
   237         });
       
   238     }
       
   239 
       
   240     private static Executor createVanillaJPackageCommand() {
       
   241         return createJavaToolCommand(JavaTool.JPACKAGE);
       
   242     }
       
   243 
       
   244     private static Executor createJavaToolCommand(JavaTool tool) {
       
   245         Executor exec = new Executor().dumpOutput().saveOutput();
       
   246         if (new JPackageCommand().isWithToolProvider()) {
       
   247             exec.setToolProvider(tool.asToolProvider());
       
   248         } else {
       
   249             exec.setExecutable(tool);
       
   250         }
       
   251 
       
   252         return exec;
       
   253     }
       
   254 }