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 JPackageCreateImageSecondaryLauncherBase {
  33     private static final String app = JPackagePath.getApp();
  34     private static final String app2 = JPackagePath.getAppSL();
  35     private static final String appOutput = JPackagePath.getAppOutputFile();
  36     private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
  37 
  38     // Note: quotes in argument for secondary launcher is not support by test
  39     private static final String ARGUMENT1 = "argument 1";
  40     private static final String ARGUMENT2 = "argument 2";
  41     private static final String ARGUMENT3 = "argument 3";
  42 
  43     private static final List<String> arguments = new ArrayList<>();
  44 
  45     private static final String PARAM1 = "-Dparam1=Some Param 1";
  46     private static final String PARAM2 = "-Dparam2=Some Param 2";
  47     private static final String PARAM3 = "-Dparam3=Some Param 3";
  48 
  49     private static final List<String> vmArguments = new ArrayList<>();
  50 
  51     private static void validateResult(List<String> args, List<String> vmArgs)
  52             throws Exception {
  53         File outfile = new File(appWorkingDir + File.separator + appOutput);
  54         if (!outfile.exists()) {
  55             throw new AssertionError(appOutput + " was not created");
  56         }
  57 
  58         String output = Files.readString(outfile.toPath());
  59         String[] result = output.split("\n");
  60 
  61         if (result.length != (args.size() + vmArgs.size() + 2)) {
  62             throw new AssertionError("Unexpected number of lines: "
  63                     + result.length);
  64         }
  65 
  66         if (!result[0].trim().equals("jpackage test application")) {
  67             throw new AssertionError("Unexpected result[0]: " + result[0]);
  68         }
  69 
  70         if (!result[1].trim().equals("args.length: " + args.size())) {
  71             throw new AssertionError("Unexpected result[1]: " + result[1]);
  72         }
  73 
  74         int index = 2;
  75         for (String arg : args) {
  76             if (!result[index].trim().equals(arg)) {
  77                 throw new AssertionError("Unexpected result[" + index + "]: "
  78                     + result[index]);
  79             }
  80             index++;
  81         }
  82 
  83         for (String vmArg : vmArgs) {
  84             if (!result[index].trim().equals(vmArg)) {
  85                 throw new AssertionError("Unexpected result[" + index + "]: "
  86                     + result[index]);
  87             }
  88             index++;
  89         }
  90     }
  91 
  92     private static void validate() throws Exception {
  93         int retVal = JPackageHelper.execute(null, app);
  94         if (retVal != 0) {
  95             throw new AssertionError("Test application exited with error: "
  96                     + retVal);
  97         }
  98         validateResult(new ArrayList<>(), new ArrayList<>());
  99 
 100         retVal = JPackageHelper.execute(null, app2);
 101         if (retVal != 0) {
 102             throw new AssertionError("Test application exited with error: "
 103                     + retVal);
 104         }
 105         validateResult(arguments, vmArguments);
 106     }
 107 
 108     public static void testCreateImage(String [] cmd) throws Exception {
 109         JPackageHelper.executeCLI(true, cmd);
 110         validate();
 111     }
 112 
 113     public static void testCreateImageToolProvider(String [] cmd) throws Exception {
 114         JPackageHelper.executeToolProvider(true, cmd);
 115         validate();
 116     }
 117 
 118     public static void createSLProperties() throws Exception {
 119         arguments.add(ARGUMENT1);
 120         arguments.add(ARGUMENT2);
 121         arguments.add(ARGUMENT3);
 122 
 123         String argumentsMap =
 124                 JPackageHelper.listToArgumentsMap(arguments, true);
 125 
 126         vmArguments.add(PARAM1);
 127         vmArguments.add(PARAM2);
 128         vmArguments.add(PARAM3);
 129 
 130         String vmArgumentsMap =
 131                 JPackageHelper.listToArgumentsMap(vmArguments, true);
 132 
 133         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 134                 new FileWriter("sl.properties")))) {
 135             out.println("name=test2");
 136             out.println("arguments=" + argumentsMap);
 137             out.println("jvm-args=" + vmArgumentsMap);
 138         }
 139     }
 140 
 141 }