test/jdk/tools/jpackage/share/jdk/jpackage/tests/BasicTest.java
branchJDK-8200758-branch
changeset 58648 3bf53ffa9ae7
parent 58466 47f0d21c7e8d
child 58671 3b578a5976df
equal deleted inserted replaced
58647:2c43b89b1679 58648:3bf53ffa9ae7
    26 import java.io.IOException;
    26 import java.io.IOException;
    27 import java.nio.file.Files;
    27 import java.nio.file.Files;
    28 import java.nio.file.Path;
    28 import java.nio.file.Path;
    29 import java.util.List;
    29 import java.util.List;
    30 import java.util.ArrayList;
    30 import java.util.ArrayList;
       
    31 import java.util.function.Function;
       
    32 import java.util.function.Predicate;
    31 import java.util.regex.Pattern;
    33 import java.util.regex.Pattern;
    32 import java.util.stream.Collectors;
    34 import java.util.stream.Collectors;
    33 import jdk.jpackage.test.Executor;
    35 import java.util.stream.Stream;
    34 import jdk.jpackage.test.JPackageCommand;
    36 import jdk.jpackage.test.*;
    35 import jdk.jpackage.test.TKit;
       
    36 import jdk.jpackage.test.JavaTool;
       
    37 import jdk.jpackage.test.HelloApp;
       
    38 import jdk.jpackage.test.Annotations.*;
    37 import jdk.jpackage.test.Annotations.*;
    39 
    38 
    40 /*
    39 /*
    41  * @test
    40  * @test
    42  * @summary jpackage basic testing
    41  * @summary jpackage basic testing
    43  * @library ../../../../helpers
    42  * @library ../../../../helpers
    44  * @build jdk.jpackage.test.*
    43  * @build jdk.jpackage.test.*
    45  * @modules jdk.jpackage
    44  * @modules jdk.jpackage/jdk.jpackage.internal
    46  * @compile BasicTest.java
    45  * @compile BasicTest.java
    47  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
    46  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
    48  *  --jpt-run=jdk.jpackage.tests.BasicTest
    47  *  --jpt-run=jdk.jpackage.tests.BasicTest
    49  */
    48  */
    50 
    49 
    51 public class BasicTest {
    50 public final class BasicTest {
    52     @Test
    51     @Test
    53     public void testNoArgs() {
    52     public void testNoArgs() {
    54         List<String> output = JPackageCommand.filterOutput(
    53         List<String> output = JPackageCommand.filterOutput(
    55                 getJPackageToolProvider().executeAndGetOutput());
    54                 getJPackageToolProvider().executeAndGetOutput());
    56         TKit.assertStringListEquals(List.of("Usage: jpackage <mode> <options>",
    55         TKit.assertStringListEquals(List.of("Usage: jpackage <mode> <options>",
    67         TKit.assertStringListEquals(List.of(System.getProperty("java.version")),
    66         TKit.assertStringListEquals(List.of(System.getProperty("java.version")),
    68                 output, "Check jpackage output");
    67                 output, "Check jpackage output");
    69     }
    68     }
    70 
    69 
    71     @Test
    70     @Test
       
    71     public void testHelp() {
       
    72         List<String> hOutput = getJPackageToolProvider()
       
    73                 .addArgument("-h").executeAndGetOutput();
       
    74         List<String> helpOutput = getJPackageToolProvider()
       
    75                 .addArgument("--help").executeAndGetOutput();
       
    76 
       
    77         TKit.assertStringListEquals(hOutput, helpOutput,
       
    78                 "Check -h and --help parameters produce the same output");
       
    79 
       
    80         final String windowsPrefix = "--win-";
       
    81         final String linuxPrefix = "--linux-";
       
    82         final String osxPrefix = "--mac-";
       
    83 
       
    84         final String expectedPrefix;
       
    85         final List<String> unexpectedPrefixes;
       
    86 
       
    87         if (TKit.isWindows()) {
       
    88             expectedPrefix = windowsPrefix;
       
    89             unexpectedPrefixes = List.of(osxPrefix, linuxPrefix);
       
    90         } else if (TKit.isLinux()) {
       
    91             expectedPrefix = linuxPrefix;
       
    92             unexpectedPrefixes = List.of(windowsPrefix, osxPrefix);
       
    93         } else if (TKit.isOSX()) {
       
    94             expectedPrefix = osxPrefix;
       
    95             unexpectedPrefixes = List.of(linuxPrefix,  windowsPrefix);
       
    96         } else {
       
    97             throw TKit.throwUnknownPlatformError();
       
    98         }
       
    99 
       
   100         Function<String, Predicate<String>> createPattern = (prefix) -> {
       
   101             return Pattern.compile("^  " + prefix).asPredicate();
       
   102         };
       
   103 
       
   104         Function<List<String>, Long> countStrings = (prefixes) -> {
       
   105             return hOutput.stream().filter(
       
   106                     prefixes.stream().map(createPattern).reduce(x -> false,
       
   107                             Predicate::or)).peek(TKit::trace).count();
       
   108         };
       
   109 
       
   110         TKit.trace("Check parameters in help text");
       
   111         TKit.assertNotEquals(0, countStrings.apply(List.of(expectedPrefix)),
       
   112                 "Check help text contains plaform specific parameters");
       
   113         TKit.assertEquals(0, countStrings.apply(unexpectedPrefixes),
       
   114                 "Check help text doesn't contain unexpected parameters");
       
   115     }
       
   116 
       
   117     @Test
       
   118     public void testVerbose() {
       
   119         JPackageCommand cmd = JPackageCommand.helloAppImage()
       
   120                 .setFakeRuntime().executePrerequisiteActions();
       
   121 
       
   122         List<String> expectedVerboseOutputStrings = new ArrayList<>();
       
   123         expectedVerboseOutputStrings.add("Creating app package:");
       
   124         if (TKit.isWindows()) {
       
   125             expectedVerboseOutputStrings.add("Result application bundle:");
       
   126             expectedVerboseOutputStrings.add(
       
   127                     "Succeeded in building Windows Application Image package");
       
   128         } else if (TKit.isLinux()) {
       
   129             expectedVerboseOutputStrings.add(
       
   130                     "Succeeded in building Linux Application Image package");
       
   131         } else if (TKit.isOSX()) {
       
   132             expectedVerboseOutputStrings.add("Preparing Info.plist:");
       
   133             expectedVerboseOutputStrings.add(
       
   134                     "Succeeded in building Mac Application Image package");
       
   135         } else {
       
   136             TKit.throwUnknownPlatformError();
       
   137         }
       
   138 
       
   139         TKit.deleteDirectoryContentsRecursive(cmd.outputDir());
       
   140         List<String> nonVerboseOutput = cmd.createExecutor().executeAndGetOutput();
       
   141 
       
   142         TKit.deleteDirectoryContentsRecursive(cmd.outputDir());
       
   143         List<String> verboseOutput = cmd.createExecutor().addArgument(
       
   144                 "--verbose").executeAndGetOutput();
       
   145 
       
   146         TKit.assertTrue(nonVerboseOutput.size() < verboseOutput.size(),
       
   147                 "Check verbose output is longer than regular");
       
   148 
       
   149         expectedVerboseOutputStrings.forEach(str -> {
       
   150             TKit.assertTextStream(str).label("regular output")
       
   151                     .predicate(String::contains).negate()
       
   152                     .apply(nonVerboseOutput.stream());
       
   153         });
       
   154 
       
   155         expectedVerboseOutputStrings.forEach(str -> {
       
   156             TKit.assertTextStream(str).label("verbose output")
       
   157                     .apply(verboseOutput.stream());
       
   158         });
       
   159     }
       
   160 
       
   161     @Test
    72     public void testNoName() {
   162     public void testNoName() {
    73         final String mainClassName = "Greetings";
   163         final String mainClassName = "Greetings";
    74 
   164 
    75         JPackageCommand cmd = new JPackageCommand()
   165         JPackageCommand cmd = JPackageCommand.helloAppImage(mainClassName)
    76                 .helloAppImage(mainClassName)
       
    77                 .removeArgumentWithValue("--name");
   166                 .removeArgumentWithValue("--name");
    78 
   167 
    79         Path expectedImageDir = cmd.outputDir().resolve(mainClassName);
   168         Path expectedImageDir = cmd.outputDir().resolve(mainClassName);
    80         if (TKit.isOSX()) {
   169         if (TKit.isOSX()) {
    81             expectedImageDir = expectedImageDir.getParent().resolve(
   170             expectedImageDir = expectedImageDir.getParent().resolve(
    82                     expectedImageDir.getFileName().toString() + ".app");
   171                     expectedImageDir.getFileName().toString() + ".app");
    83         }
   172         }
    84 
   173 
    85         cmd.executeAndAssertHelloAppImageCreated();
   174         cmd.executeAndAssertHelloAppImageCreated();
    86         TKit.assertEquals(expectedImageDir.toAbsolutePath().normalize().toString(),
   175         TKit.assertEquals(expectedImageDir.toAbsolutePath().normalize().toString(),
    87                 cmd.appImage().toAbsolutePath().normalize().toString(),
   176                 cmd.outputBundle().toAbsolutePath().normalize().toString(),
    88                 String.format(
   177                 String.format(
    89                         "Check [%s] directory is filled with application image data",
   178                         "Check [%s] directory is filled with application image data",
    90                         expectedImageDir));
   179                         expectedImageDir));
    91     }
   180     }
    92 
   181 
    93     @Test
   182     @Test
    94     public void testApp() {
   183      // Regular app
    95         new JPackageCommand()
   184     @Parameter("Hello")
    96         .helloAppImage()
   185     // Modular app
       
   186     @Parameter("com.other/com.other.Hello")
       
   187     public void testApp(String javaAppDesc) {
       
   188         JPackageCommand.helloAppImage(javaAppDesc)
    97         .executeAndAssertHelloAppImageCreated();
   189         .executeAndAssertHelloAppImageCreated();
    98     }
   190     }
    99 
   191 
   100     @Test
   192     @Test
   101     public void testModularApp() {
   193     public void testWhitespaceInPaths() {
   102         new JPackageCommand()
   194         JPackageCommand.helloAppImage("a/b c.jar:Hello")
   103         .helloAppImage("com.other/com.other.Hello")
   195         .setArgumentValue("--input", TKit.workDir().resolve("The quick brown fox"))
       
   196         .setArgumentValue("--dest", TKit.workDir().resolve("jumps over the lazy dog"))
   104         .executeAndAssertHelloAppImageCreated();
   197         .executeAndAssertHelloAppImageCreated();
   105     }
   198     }
   106 
   199 
   107     @Test
   200     @Test
   108     @Parameter("ALL-MODULE-PATH")
   201     @Parameter("ALL-MODULE-PATH")
   109     @Parameter("ALL-DEFAULT")
   202     @Parameter("ALL-DEFAULT")
   110     @Parameter("jdk.desktop,jdk.jartool")
   203     @Parameter("java.desktop")
   111     public void testAddModules(String addModulesArg) {
   204     @Parameter("java.desktop,jdk.jartool")
   112         JPackageCommand cmd = new JPackageCommand()
   205     @Parameter({ "java.desktop", "jdk.jartool" })
   113             .helloAppImage("com.other/com.other.Hello");
   206     public void testAddModules(String... addModulesArg) {
   114         if (!addModulesArg.isEmpty()) {
   207         JPackageCommand cmd = JPackageCommand
   115             cmd.addArguments("--add-modules", addModulesArg);
   208                 .helloAppImage("goodbye.jar:com.other/com.other.Hello");
   116         }
   209         Stream.of(addModulesArg).map(v -> Stream.of("--add-modules", v)).flatMap(
       
   210                 s -> s).forEachOrdered(cmd::addArgument);
   117         cmd.executeAndAssertHelloAppImageCreated();
   211         cmd.executeAndAssertHelloAppImageCreated();
   118     }
   212     }
   119 
   213 
   120     /**
   214     /**
   121      * Test --temp option. Doesn't make much sense for app image as temporary
   215      * Test --temp option. Doesn't make much sense for app image as temporary
   122      * directory is used only on Windows.
   216      * directory is used only on Windows. Test it in packaging mode.
   123      * @throws IOException
   217      * @throws IOException
   124      */
   218      */
   125 //    @Test
   219     @Test
   126     public void testTemp() throws IOException {
   220     public void testTemp() throws IOException {
   127         JPackageCommand cmd = new JPackageCommand().helloAppImage();
   221         JPackageCommand cmd = JPackageCommand.helloAppImage()
       
   222                 .removeArgumentWithValue("--package-type")
       
   223                 .addArguments("--verbose")
       
   224                 .setFakeRuntime();
       
   225 
   128         TKit.withTempDirectory("temp-root", tempDir -> {
   226         TKit.withTempDirectory("temp-root", tempDir -> {
   129             cmd.addArguments("--temp", tempDir);
   227             cmd.addArguments("--temp", tempDir);
   130 
   228 
   131             cmd.executeAndAssertHelloAppImageCreated();
   229             cmd.execute().assertExitCodeIsZero();
   132 
   230 
   133             // Check jpackage actually used the supplied directory.
   231             // Check jpackage actually used the supplied directory.
   134             TKit.assertNotEquals(0, tempDir.toFile().list().length,
   232             TKit.assertNotEquals(0, tempDir.toFile().list().length,
   135                     String.format(
   233                     String.format(
   136                             "Check jpackage wrote some data in the supplied temporary directory [%s]",
   234                             "Check jpackage wrote some data in the supplied temporary directory [%s]",
   142         });
   240         });
   143     }
   241     }
   144 
   242 
   145     @Test
   243     @Test
   146     public void testAtFile() throws IOException {
   244     public void testAtFile() throws IOException {
   147         JPackageCommand cmd = new JPackageCommand().helloAppImage();
   245         JPackageCommand cmd = JPackageCommand.helloAppImage();
   148 
   246 
   149         // Init options file with the list of options configured
   247         // Init options file with the list of options configured
   150         // for JPackageCommand instance.
   248         // for JPackageCommand instance.
   151         final Path optionsFile = TKit.workDir().resolve("options");
   249         final Path optionsFile = TKit.workDir().resolve("options");
   152         Files.write(optionsFile,
   250         Files.write(optionsFile,
   209             jlink.execute().assertExitCodeIsZero();
   307             jlink.execute().assertExitCodeIsZero();
   210 
   308 
   211             cmd.addArguments("--runtime-image", runtimeDir);
   309             cmd.addArguments("--runtime-image", runtimeDir);
   212             cmd.executeAndAssertHelloAppImageCreated();
   310             cmd.executeAndAssertHelloAppImageCreated();
   213 
   311 
   214             final Path appImageRuntimePath = cmd.appImage().resolve(
   312             final Path appImageRuntimePath = cmd.appRuntimeDirectory();
   215                     cmd.appRuntimeDirectoryInAppImage());
       
   216 
   313 
   217             //
   314             //
   218             // This is an overkill to list modules in jlink output as we have
   315             // This is an overkill to list modules in jlink output as we have
   219             // already verified that Java app is functional and thus app's runtime
   316             // already verified that Java app is functional and thus app's runtime
   220             // is likely to be OK, but let's double check.
   317             // is likely to be OK, but let's double check.
   249     private static Executor getJPackageToolProvider() {
   346     private static Executor getJPackageToolProvider() {
   250         return getToolProvider(JavaTool.JPACKAGE);
   347         return getToolProvider(JavaTool.JPACKAGE);
   251     }
   348     }
   252 
   349 
   253     private static Executor getToolProvider(JavaTool tool) {
   350     private static Executor getToolProvider(JavaTool tool) {
   254         return new Executor()
   351         return new Executor().dumpOutput().saveOutput().setToolProvider(tool);
   255                 .dumpOutput().saveOutput()
       
   256                 .setToolProvider(tool.asToolProvider());
       
   257     }
   352     }
   258 }
   353 }