test/jdk/tools/jpackage/share/AddLauncherBase.java
branchJDK-8200758-branch
changeset 57446 5a5b85f00a63
parent 57445 405ddd80496e
child 57909 c7de06ed4b54
equal deleted inserted replaced
57445:405ddd80496e 57446:5a5b85f00a63
       
     1 /*
       
     2  * Copyright (c) 2018, 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 import java.io.BufferedWriter;
       
    25 import java.io.File;
       
    26 import java.io.FileWriter;
       
    27 import java.io.PrintWriter;
       
    28 import java.nio.file.Files;
       
    29 import java.util.ArrayList;
       
    30 import java.util.List;
       
    31 
       
    32 public class AddLauncherBase {
       
    33     private static final String app = JPackagePath.getApp();
       
    34     private static final String appOutput = JPackagePath.getAppOutputFile();
       
    35 
       
    36     // Note: quotes in argument for add launcher is not support by test
       
    37     private static final String ARGUMENT1 = "argument 1";
       
    38     private static final String ARGUMENT2 = "argument 2";
       
    39     private static final String ARGUMENT3 = "argument 3";
       
    40 
       
    41     private static final List<String> arguments = new ArrayList<>();
       
    42 
       
    43     private static final String PARAM1 = "-Dparam1=Some Param 1";
       
    44     private static final String PARAM2 = "-Dparam2=Some Param 2";
       
    45     private static final String PARAM3 = "-Dparam3=Some Param 3";
       
    46 
       
    47     private static final List<String> vmArguments = new ArrayList<>();
       
    48     private static final List<String> empty = new ArrayList<>();
       
    49 
       
    50     private static void validateResult(List<String> args, List<String> vmArgs)
       
    51             throws Exception {
       
    52         File outfile = new File(appOutput);
       
    53         if (!outfile.exists()) {
       
    54             throw new AssertionError(appOutput + " was not created");
       
    55         }
       
    56 
       
    57         String output = Files.readString(outfile.toPath());
       
    58         String[] result = output.split("\n");
       
    59 
       
    60         int expected = 2 + args.size() + vmArgs.size();
       
    61 
       
    62         if (result.length != expected) {
       
    63             throw new AssertionError("Unexpected number of lines: "
       
    64                     + result.length + " expected: " + expected + " - results: " + output);
       
    65         }
       
    66 
       
    67         if (!result[0].trim().endsWith("jpackage test application")) {
       
    68             throw new AssertionError("Unexpected result[0]: " + result[0]);
       
    69         }
       
    70 
       
    71         if (!result[1].trim().equals("args.length: " + args.size())) {
       
    72             throw new AssertionError("Unexpected result[1]: " + result[1]);
       
    73         }
       
    74 
       
    75         int index = 2;
       
    76         for (String arg : args) {
       
    77             if (!result[index].trim().equals(arg)) {
       
    78                 throw new AssertionError("Unexpected result["
       
    79                         + index + "]: " + result[index]);
       
    80             }
       
    81             index++;
       
    82         }
       
    83 
       
    84         for (String vmArg : vmArgs) {
       
    85             if (!result[index].trim().equals(vmArg)) {
       
    86                 throw new AssertionError("Unexpected result["
       
    87                         + index + "]: " + result[index]);
       
    88             }
       
    89             index++;
       
    90         }
       
    91     }
       
    92 
       
    93     private static void validate(boolean includeArgs, String name)
       
    94             throws Exception {
       
    95         int retVal = JPackageHelper.execute(null, app);
       
    96         if (retVal != 0) {
       
    97             throw new AssertionError("Test application " + app
       
    98                     + " exited with error: " + retVal);
       
    99         }
       
   100         validateResult(new ArrayList<>(), new ArrayList<>());
       
   101 
       
   102         String app2 = JPackagePath.getAppSL(name);
       
   103         retVal = JPackageHelper.execute(null, app2);
       
   104         if (retVal != 0) {
       
   105             throw new AssertionError("Test application " + app2
       
   106                     +  " exited with error: " + retVal);
       
   107         }
       
   108         if (includeArgs) {
       
   109             validateResult(arguments, vmArguments);
       
   110         } else {
       
   111             validateResult(empty, empty);
       
   112         }
       
   113     }
       
   114 
       
   115     public static void testCreateAppImage(String [] cmd) throws Exception {
       
   116         testCreateAppImage(cmd, true, "test2");
       
   117     }
       
   118 
       
   119     public static void testCreateAppImage(String [] cmd,
       
   120             boolean includeArgs, String name) throws Exception {
       
   121         JPackageHelper.executeCLI(true, cmd);
       
   122         validate(includeArgs, name);
       
   123     }
       
   124 
       
   125     public static void testCreateAppImageToolProvider(String [] cmd)
       
   126             throws Exception {
       
   127         testCreateAppImageToolProvider(cmd, true, "test2");
       
   128     }
       
   129 
       
   130     public static void testCreateAppImageToolProvider(String [] cmd,
       
   131             boolean includeArgs, String name) throws Exception {
       
   132         JPackageHelper.executeToolProvider(true, cmd);
       
   133         validate(includeArgs, name);
       
   134     }
       
   135 
       
   136     public static void createSLProperties() throws Exception {
       
   137         arguments.add(ARGUMENT1);
       
   138         arguments.add(ARGUMENT2);
       
   139         arguments.add(ARGUMENT3);
       
   140 
       
   141         String argumentsMap =
       
   142                 JPackageHelper.listToArgumentsMap(arguments, true);
       
   143 
       
   144         vmArguments.add(PARAM1);
       
   145         vmArguments.add(PARAM2);
       
   146         vmArguments.add(PARAM3);
       
   147 
       
   148         String vmArgumentsMap =
       
   149                 JPackageHelper.listToArgumentsMap(vmArguments, true);
       
   150 
       
   151         try (PrintWriter out = new PrintWriter(new BufferedWriter(
       
   152                 new FileWriter("sl.properties")))) {
       
   153             out.println("arguments=" + argumentsMap);
       
   154             out.println("java-options=" + vmArgumentsMap);
       
   155         }
       
   156 
       
   157         try (PrintWriter out = new PrintWriter(new BufferedWriter(
       
   158                 new FileWriter("m1.properties")))) {
       
   159             out.println("module=com.hello/com.hello.Hello");
       
   160             out.println("main-jar=");
       
   161         }
       
   162 
       
   163         try (PrintWriter out = new PrintWriter(new BufferedWriter(
       
   164                 new FileWriter("j1.properties")))) {
       
   165             out.println("main-jar hello.jar");
       
   166             out.println("main-class Hello");
       
   167         }
       
   168 
       
   169         
       
   170     }
       
   171 
       
   172 }