test/jdk/tools/jpackage/share/ArgumentsBase.java
branchJDK-8200758-branch
changeset 58416 f09bf58c1f17
parent 58415 73f8e557549a
child 58417 67ffaf3a2b75
equal deleted inserted replaced
58415:73f8e557549a 58416:f09bf58c1f17
     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.File;
       
    25 import java.nio.file.Files;
       
    26 import java.util.ArrayList;
       
    27 import java.util.List;
       
    28 
       
    29 public class ArgumentsBase {
       
    30 
       
    31     private static final String app = JPackagePath.getApp();
       
    32     private static final String appOutput = JPackagePath.getAppOutputFile();
       
    33 
       
    34     private static final String ARGUMENT1 = "argument";
       
    35     private static final String ARGUMENT2 = "Some Arguments";
       
    36     private static final String ARGUMENT3 = "Value \"with\" quotes";
       
    37 
       
    38     private static final String ARGUMENT_CMD1 = "test";
       
    39 
       
    40     private static final List<String> arguments = new ArrayList<>();
       
    41     private static final List<String> argumentsCmd = new ArrayList<>();
       
    42 
       
    43     public static void initArguments(boolean toolProvider, String[] cmd) {
       
    44         if (arguments.isEmpty()) {
       
    45             arguments.add(ARGUMENT1);
       
    46             arguments.add(ARGUMENT2);
       
    47             arguments.add(ARGUMENT3);
       
    48         }
       
    49 
       
    50         if (argumentsCmd.isEmpty()) {
       
    51             argumentsCmd.add(ARGUMENT_CMD1);
       
    52         }
       
    53 
       
    54         String argumentsMap
       
    55                 = JPackageHelper.listToArgumentsMap(arguments, toolProvider);
       
    56         cmd[cmd.length - 1] = argumentsMap;
       
    57     }
       
    58 
       
    59     private static void validateResult(String[] result, List<String> args)
       
    60             throws Exception {
       
    61         if (result.length != (args.size() + 2)) {
       
    62             throw new AssertionError(
       
    63                     "Unexpected number of lines: " + 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(
       
    78                         "Unexpected result[" + index + "]: " + result[index]);
       
    79             }
       
    80             index++;
       
    81         }
       
    82     }
       
    83 
       
    84     private static void validate(String arg, List<String> expectedArgs)
       
    85             throws Exception {
       
    86         int retVal;
       
    87 
       
    88         if (arg == null) {
       
    89             retVal = JPackageHelper.execute(null, app);
       
    90         } else {
       
    91             retVal = JPackageHelper.execute(null, app, arg);
       
    92         }
       
    93         if (retVal != 0) {
       
    94             throw new AssertionError("Test application exited with error: "
       
    95                     + retVal);
       
    96         }
       
    97 
       
    98         File outfile = new File(appOutput);
       
    99         if (!outfile.exists()) {
       
   100             throw new AssertionError(appOutput + " was not created");
       
   101         }
       
   102 
       
   103         String output = Files.readString(outfile.toPath());
       
   104         String[] result = output.split("\n");
       
   105         validateResult(result, expectedArgs);
       
   106     }
       
   107 
       
   108     public static void testCreateAppImage(String[] cmd) throws Exception {
       
   109         initArguments(false, cmd);
       
   110         JPackageHelper.executeCLI(true, cmd);
       
   111         validate(null, arguments);
       
   112         validate(ARGUMENT_CMD1, argumentsCmd);
       
   113     }
       
   114 
       
   115     public static void testCreateAppImageToolProvider(String[] cmd) throws Exception {
       
   116         initArguments(true, cmd);
       
   117         JPackageHelper.executeToolProvider(true, cmd);
       
   118         validate(null, arguments);
       
   119         validate(ARGUMENT_CMD1, argumentsCmd);
       
   120     }
       
   121 }