test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java
branchJDK-8200758-branch
changeset 58036 f7f10023f7c0
child 58113 885b0543f6e4
equal deleted inserted replaced
57951:37e70d4679df 58036:f7f10023f7c0
       
     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 package jdk.jpackage.test;
       
    24 
       
    25 import java.io.File;
       
    26 import java.io.IOException;
       
    27 import java.nio.file.Files;
       
    28 import java.nio.file.Path;
       
    29 import java.util.ArrayList;
       
    30 import java.util.HashMap;
       
    31 import java.util.List;
       
    32 import java.util.ListIterator;
       
    33 import java.util.Map;
       
    34 import java.util.function.Function;
       
    35 import java.util.function.Supplier;
       
    36 
       
    37 /**
       
    38  * jpackage command line with prerequisite actions. Prerequisite actions can be
       
    39  * anything. The simplest is to compile test application and pack in a jar for
       
    40  * use on jpackage command line.
       
    41  */
       
    42 public final class JPackageCommand extends CommandArguments<JPackageCommand> {
       
    43 
       
    44     public JPackageCommand() {
       
    45         actions = new ArrayList<>();
       
    46     }
       
    47 
       
    48     static JPackageCommand createImmutable(JPackageCommand v) {
       
    49         JPackageCommand reply = new JPackageCommand();
       
    50         reply.immutable = true;
       
    51         reply.args.addAll(v.args);
       
    52         return reply;
       
    53     }
       
    54 
       
    55     public void setArgumentValue(String argName, String newValue) {
       
    56         String prevArg = null;
       
    57         ListIterator<String> it = args.listIterator();
       
    58         while (it.hasNext()) {
       
    59             String value = it.next();
       
    60             if (prevArg != null && prevArg.equals(argName)) {
       
    61                 if (newValue != null) {
       
    62                     it.set(newValue);
       
    63                 } else {
       
    64                     it.remove();
       
    65                     it.previous();
       
    66                     it.remove();
       
    67                 }
       
    68                 return;
       
    69             }
       
    70             prevArg = value;
       
    71         }
       
    72 
       
    73         if (newValue != null) {
       
    74             addArguments(argName, newValue);
       
    75         }
       
    76     }
       
    77 
       
    78     public <T> T getArgumentValue(String argName,
       
    79             Supplier<T> defaultValueSupplier,
       
    80             Function<String, T> stringConverter) {
       
    81         String prevArg = null;
       
    82         for (String arg : args) {
       
    83             if (prevArg != null && prevArg.equals(argName)) {
       
    84                 return stringConverter.apply(arg);
       
    85             }
       
    86             prevArg = arg;
       
    87         }
       
    88         if (defaultValueSupplier != null) {
       
    89             return defaultValueSupplier.get();
       
    90         }
       
    91         return null;
       
    92     }
       
    93 
       
    94     public String getArgumentValue(String argName,
       
    95             Supplier<String> defaultValueSupplier) {
       
    96         return getArgumentValue(argName, defaultValueSupplier,
       
    97                 (v) -> v);
       
    98     }
       
    99 
       
   100     public PackageType packageType() {
       
   101         return getArgumentValue("--package-type",
       
   102                 () -> PackageType.IMAGE,
       
   103                 (v) -> PACKAGE_TYPES.get(v));
       
   104     }
       
   105 
       
   106     public Path outputDir() {
       
   107         return getArgumentValue("--output",
       
   108                 () -> Test.defaultOutputDir(),
       
   109                 (v) -> Path.of(v));
       
   110     }
       
   111 
       
   112     public Path inputDir() {
       
   113         return getArgumentValue("--input",
       
   114                 () -> Test.defaultInputDir(),
       
   115                 (v) -> Path.of(v));
       
   116     }
       
   117 
       
   118     public String version() {
       
   119         return getArgumentValue("--version", () -> "1.0");
       
   120     }
       
   121 
       
   122     public String name() {
       
   123         return getArgumentValue("--name",
       
   124                 () -> getArgumentValue("--main-class", null));
       
   125     }
       
   126 
       
   127     public JPackageCommand setDefaultInputOutput() {
       
   128         verifyMutable();
       
   129         addArguments("--input", Test.defaultInputDir().toString());
       
   130         addArguments("--output", Test.defaultOutputDir().toString());
       
   131         return this;
       
   132     }
       
   133 
       
   134     public JPackageCommand setHelloApp() {
       
   135         verifyMutable();
       
   136         actions.add(new Runnable() {
       
   137             @Override
       
   138             public void run() {
       
   139                 String mainClass = "Hello";
       
   140                 Path jar = inputDir().resolve("hello.jar");
       
   141                 new JarBuilder()
       
   142                         .setOutputJar(jar.toFile())
       
   143                         .setMainClass(mainClass)
       
   144                         .addSourceFile(Test.TEST_SRC_ROOT.resolve(
       
   145                                 Path.of("apps", "image", mainClass + ".java")))
       
   146                         .create();
       
   147                 addArguments("--main-jar", jar.getFileName().toString());
       
   148                 addArguments("--main-class", mainClass);
       
   149             }
       
   150         });
       
   151         return this;
       
   152     }
       
   153 
       
   154     public JPackageCommand setPackageType(PackageType type) {
       
   155         verifyMutable();
       
   156         type.applyTo(this);
       
   157         return this;
       
   158     }
       
   159 
       
   160     public Path outputBundle() {
       
   161         switch (packageType()) {
       
   162             case LINUX_RPM:
       
   163             case LINUX_DEB:
       
   164                 return outputDir().resolve(LinuxHelper.getBundleName(this));
       
   165         }
       
   166         return null;
       
   167     }
       
   168 
       
   169     public Path launcherInstallationPath() {
       
   170         switch (packageType()) {
       
   171             case LINUX_RPM:
       
   172             case LINUX_DEB:
       
   173                 return LinuxHelper.getLauncherPath(this);
       
   174         }
       
   175         return null;
       
   176     }
       
   177 
       
   178     public Executor.Result execute() {
       
   179         verifyMutable();
       
   180         if (actions != null) {
       
   181             actions.stream().forEach(r -> r.run());
       
   182         }
       
   183         return new Executor()
       
   184                 .setExecutable(JavaTool.JPACKAGE)
       
   185                 .addArguments(args)
       
   186                 .execute();
       
   187     }
       
   188 
       
   189     static void verifyHelloApp(Path helloAppLauncher) {
       
   190         File outputFile = Test.workDir().resolve("appOutput.txt").toFile();
       
   191         new Executor()
       
   192                 .setDirectory(outputFile.getParentFile().toPath())
       
   193                 .setExecutable(helloAppLauncher.toString())
       
   194                 .execute()
       
   195                 .assertExitCodeIsZero();
       
   196 
       
   197         Test.assertTrue(outputFile.exists(), String.format(
       
   198                 "Check file [%s] exists", outputFile));
       
   199 
       
   200         List<String> output = null;
       
   201         try {
       
   202             output = Files.readAllLines(outputFile.toPath());
       
   203         } catch (IOException ex) {
       
   204             throw new RuntimeException(ex);
       
   205         }
       
   206         Test.assertEquals(2, output.size(), String.format(
       
   207                 "Check file [%s] contains %d text lines", outputFile, 2));
       
   208 
       
   209         Test.assertEquals("jpackage test application", output.get(0),
       
   210                 String.format(
       
   211                         "Check contents of the first text line in [%s] file",
       
   212                         outputFile));
       
   213 
       
   214         Test.assertEquals("args.length: 0", output.get(1), String.format(
       
   215                 "Check contents of the second text line in [%s] file",
       
   216                 outputFile));
       
   217     }
       
   218 
       
   219     @Override
       
   220     protected boolean isMutable() {
       
   221         return !immutable;
       
   222     }
       
   223 
       
   224     private final List<Runnable> actions;
       
   225     private boolean immutable;
       
   226 
       
   227     private final static Map<String, PackageType> PACKAGE_TYPES
       
   228             = new Supplier<Map<String, PackageType>>() {
       
   229                 @Override
       
   230                 public Map<String, PackageType> get() {
       
   231                     Map<String, PackageType> reply = new HashMap<>();
       
   232                     for (PackageType type : PackageType.values()) {
       
   233                         reply.put(type.getName(), type);
       
   234                     }
       
   235                     return reply;
       
   236                 }
       
   237             }.get();
       
   238 }