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