test/jdk/tools/jpackager/JPackagerCreateImageJVMArgsTest.java
author herrick
Thu, 08 Nov 2018 19:07:57 -0500
branchJDK-8200758-branch
changeset 57023 8bf89434f0a5
permissions -rw-r--r--
8213332: Create minimal automated tests for jpackager Submitten-by: almatvee Reviewed-by: herrick. kcr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57023
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     1
/*
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     4
 *
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     8
 *
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    13
 * accompanied this code).
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    14
 *
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    18
 *
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    21
 * questions.
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    22
 */
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    23
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    24
import java.io.File;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    25
import java.nio.file.Files;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    26
import java.util.ArrayList;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    27
import java.util.List;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    28
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    29
/*
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    30
 * @test
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    31
 * @summary jpackager create image with --jvm-args test
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    32
 * @build JPackagerHelper
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    33
 * @modules jdk.jpackager
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    34
 * @run main/othervm -Xmx512m JPackagerCreateImageJVMArgsTest
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    35
 */
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    36
public class JPackagerCreateImageJVMArgsTest {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    37
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    38
    private static final String app;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    39
    private static final String appOutput = "appOutput.txt";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    40
    private static final String appOutputPath;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    41
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    42
    private static final String[] CMD = {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    43
        "create-image",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    44
        "--input", "input",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    45
        "--output", "output",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    46
        "--name", "test",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    47
        "--main-jar", "hello.jar",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    48
        "--class", "Hello",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    49
        "--files", "hello.jar",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    50
        "--force",
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    51
        "--jvm-args", "TBD"};
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    52
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    53
    private static final String ARGUMENT1 = "-Dparam1=Some Param 1";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    54
    private static final String ARGUMENT2 = "-Dparam2=Some \"Param\" 2";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    55
    private static final String ARGUMENT3 =
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    56
            "-Dparam3=Some \"Param\" with \" 3";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    57
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    58
    private static final List<String> arguments = new ArrayList<>();
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    59
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    60
    static {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    61
        if (JPackagerHelper.isWindows()) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    62
            app = "output" + File.separator + "test"
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    63
                    + File.separator + "test.exe";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    64
            appOutputPath = "output" + File.separator + "test"
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    65
                    + File.separator + "app";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    66
        } else if (JPackagerHelper.isOSX()) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    67
            app = "output" + File.separator + "test.app"
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    68
                    + File.separator + "Contents" + File.separator + "MacOS"
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    69
                    + File.separator + "test"; appOutputPath = "output"
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    70
                    + File.separator + "test.app"
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    71
                    + File.separator + "Contents" + File.separator + "Java";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    72
        } else {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    73
            app = "output" + File.separator + "test" + File.separator + "test";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    74
            appOutputPath = "output"
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    75
                    + File.separator + "test" + File.separator + "app";
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    76
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    77
    }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    78
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    79
    private static void initArguments(boolean toolProvider) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    80
        if (arguments.isEmpty()) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    81
            arguments.add(ARGUMENT1);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    82
            arguments.add(ARGUMENT2);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    83
            arguments.add(ARGUMENT3);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    84
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    85
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    86
        String argumentsMap = JPackagerHelper.listToArgumentsMap(arguments,
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    87
                toolProvider);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    88
        CMD[CMD.length - 1] = argumentsMap;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    89
    }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    90
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    91
    private static void validateResult(String[] result, List<String> args)
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    92
            throws Exception {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    93
        if (result.length != (args.size() + 2)) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    94
            for (String r : result) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    95
                System.err.println(r.trim());
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    96
            }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    97
            throw new AssertionError("Unexpected number of lines: "
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    98
                    + result.length);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
    99
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   100
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   101
        if (!result[0].trim().equals("jpackager test application")) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   102
            throw new AssertionError("Unexpected result[0]: " + result[0]);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   103
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   104
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   105
        if (!result[1].trim().equals("args.length: 0")) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   106
            throw new AssertionError("Unexpected result[1]: " + result[1]);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   107
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   108
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   109
        int index = 2;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   110
        for (String arg : args) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   111
            if (!result[index].trim().equals(arg)) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   112
                throw new AssertionError("Unexpected result[" + index + "]: "
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   113
                    + result[index]);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   114
            }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   115
            index++;
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   116
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   117
    }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   118
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   119
    private static void validate(List<String> expectedArgs) throws Exception {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   120
        int retVal = JPackagerHelper.execute(null, app);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   121
        if (retVal != 0) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   122
            throw new AssertionError("Test application exited with error: "
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   123
                    + retVal);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   124
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   125
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   126
        File outfile = new File(appOutputPath + File.separator + appOutput);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   127
        if (!outfile.exists()) {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   128
            throw new AssertionError(appOutput + " was not created");
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   129
        }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   130
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   131
        String output = Files.readString(outfile.toPath());
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   132
        String[] result = output.split("\n");
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   133
        validateResult(result, expectedArgs);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   134
    }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   135
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   136
    private static void testCreateImage() throws Exception {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   137
        initArguments(false);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   138
        JPackagerHelper.executeCLI(true, CMD);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   139
        validate(arguments);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   140
    }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   141
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   142
    private static void testCreateImageToolProvider() throws Exception {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   143
        initArguments(true);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   144
        JPackagerHelper.executeToolProvider(true, CMD);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   145
        validate(arguments);
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   146
    }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   147
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   148
    public static void main(String[] args) throws Exception {
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   149
        JPackagerHelper.createHelloJar();
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   150
        testCreateImage();
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   151
        testCreateImageToolProvider();
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   152
    }
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   153
8bf89434f0a5 8213332: Create minimal automated tests for jpackager
herrick
parents:
diff changeset
   154
}