jdk/test/tools/launcher/ArgsFileTest.java
author henryjen
Wed, 08 Jul 2015 23:26:48 -0700
changeset 32267 4e96a9ee01b1
child 43325 033e4cced1dc
permissions -rw-r--r--
8027634: Support @argfiles for java command-line tool Reviewed-by: ksrini, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32267
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     1
/*
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     4
 *
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     8
 *
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    13
 * accompanied this code).
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    14
 *
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    18
 *
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    21
 * questions.
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    22
 */
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    23
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    24
/**
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    25
 * @test
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    26
 * @bug 8027634
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    27
 * @summary Argument parsing from file
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    28
 * @build TestHelper
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    29
 * @run main ArgsFileTest
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    30
 */
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    31
import java.io.File;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    32
import java.io.IOException;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    33
import java.util.ArrayList;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    34
import java.util.Arrays;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    35
import java.util.Collections;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    36
import java.util.HashMap;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    37
import java.util.List;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    38
import java.util.Map;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    39
import java.util.regex.Matcher;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    40
import java.util.regex.Pattern;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    41
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    42
public class ArgsFileTest extends TestHelper {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    43
    private static File testJar = null;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    44
    private static Map<String, String> env = new HashMap<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    45
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    46
    static void init() throws IOException {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    47
        if  (testJar != null) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    48
            return;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    49
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    50
        testJar = new File("test.jar");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    51
        StringBuilder tsrc = new StringBuilder();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    52
        tsrc.append("public static void main(String... args) {\n");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    53
        tsrc.append("   for (String x : args) {\n");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    54
        tsrc.append("        System.out.println(x);\n");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    55
        tsrc.append("   }\n");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    56
        tsrc.append("}\n");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    57
        createJar(testJar, new File("Foo"), tsrc.toString());
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    58
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    59
        env.put(JLDEBUG_KEY, "true");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    60
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    61
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    62
    private File createArgFile(String fname, List<String> lines) throws IOException {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    63
        File argFile = new File(fname);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    64
        argFile.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    65
        createAFile(argFile, lines);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    66
        return argFile;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    67
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    68
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    69
    private void verifyOptions(List<String> args, TestResult tr) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    70
        if (args.isEmpty()) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    71
            return;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    72
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    73
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    74
        int i = 1;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    75
        for (String x : args) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    76
            tr.matches(".*argv\\[" + i + "\\] = " + Pattern.quote(x) + ".*");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    77
            i++;
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    78
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    79
        if (! tr.testStatus) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    80
            System.out.println(tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    81
            throw new RuntimeException("test fails");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    82
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    83
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    84
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    85
    private void verifyUserArgs(List<String> args, TestResult tr, int index) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    86
        if (javaCmd != TestHelper.javaCmd) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    87
            tr.contains("\tFirst application arg index: 1");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    88
        } else {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    89
            tr.contains("\tFirst application arg index: " + index);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    90
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    91
            for (String arg: args) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    92
                tr.matches("^" + Pattern.quote(arg) + "$");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    93
            }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    94
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    95
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    96
        if (! tr.testStatus) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    97
            System.out.println(tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    98
            throw new RuntimeException("test fails");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
    99
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   100
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   101
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   102
    @Test
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   103
    public void expandAll() throws IOException {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   104
        List<String> lines = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   105
        lines.add("-Xmx32m");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   106
        lines.add("-Xint");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   107
        File argFile1 = createArgFile("argFile1", lines);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   108
        lines = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   109
        lines.add("-jar");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   110
        lines.add("test.jar");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   111
        lines.add("uarg1 @uarg2 @@uarg3 -uarg4 uarg5");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   112
        File argFile2 = createArgFile("argFile2", lines);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   113
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   114
        TestResult tr = doExec(env, javaCmd, "@argFile1", "@argFile2");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   115
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   116
        List<String> appArgs = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   117
        appArgs.add("uarg1");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   118
        appArgs.add("@uarg2");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   119
        appArgs.add("@@uarg3");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   120
        appArgs.add("-uarg4");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   121
        appArgs.add("uarg5");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   122
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   123
        List<String> options = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   124
        options.add("-Xmx32m");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   125
        options.add("-Xint");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   126
        options.add("-jar");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   127
        options.add("test.jar");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   128
        options.addAll(appArgs);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   129
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   130
        verifyOptions(options, tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   131
        verifyUserArgs(appArgs, tr, 5);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   132
        argFile1.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   133
        argFile2.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   134
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   135
        File cpFile = createArgFile("cpFile", Arrays.asList("-cp", "test.jar"));
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   136
        List<String> appCmd = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   137
        appCmd.add("Foo");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   138
        appCmd.addAll(appArgs);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   139
        File appFile = createArgFile("appFile", appCmd);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   140
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   141
        tr = doExec(env, javaCmd, "@cpFile", "@appFile");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   142
        verifyOptions(Arrays.asList("-cp", "test.jar", "Foo",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   143
                "uarg1", "@uarg2", "@@uarg3", "-uarg4", "uarg5"), tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   144
        verifyUserArgs(appArgs, tr, 4);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   145
        cpFile.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   146
        appFile.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   147
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   148
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   149
    @Test
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   150
    public void escapeArg() throws IOException {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   151
        List<String> lines = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   152
        lines.add("-Xmx32m");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   153
        lines.add("-Xint");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   154
        File argFile1 = createArgFile("argFile1", lines);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   155
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   156
        TestResult tr = doExec(env, javaCmd, "-cp", "@@arg", "-cp", "@",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   157
                "-cp", "@@@cp", "@argFile1", "@@@@Main@@@@", "-version");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   158
        List<String> options = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   159
        options.add("-cp");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   160
        options.add("@arg");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   161
        options.add("-cp");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   162
        options.add("@");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   163
        options.add("-cp");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   164
        options.add("@@cp");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   165
        options.add("-Xmx32m");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   166
        options.add("-Xint");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   167
        options.add("@@@Main@@@@");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   168
        options.add("-version");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   169
        verifyOptions(options, tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   170
        verifyUserArgs(Collections.emptyList(), tr, options.size());
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   171
        argFile1.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   172
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   173
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   174
    @Test
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   175
    public void killSwitch() throws IOException {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   176
        List<String> lines = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   177
        lines.add("-Xmx32m");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   178
        lines.add("-Xint");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   179
        File argFile1 = createArgFile("argFile1", lines);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   180
        lines = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   181
        lines.add("-jar");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   182
        lines.add("test.jar");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   183
        lines.add("uarg1 @uarg2 @@uarg3 -uarg4 uarg5");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   184
        File argFile2 = createArgFile("argFile2", lines);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   185
        File argKill = createArgFile("argKill",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   186
            Collections.singletonList("-Xdisable-@files"));
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   187
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   188
        TestResult tr = doExec(env, javaCmd, "@argFile1", "-Xdisable-@files", "@argFile2");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   189
        List<String> options = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   190
        options.add("-Xmx32m");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   191
        options.add("-Xint");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   192
        options.add("-Xdisable-@files");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   193
        options.add("@argFile2");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   194
        verifyOptions(options, tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   195
        // Main class is @argFile2
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   196
        verifyUserArgs(Collections.emptyList(), tr, 5);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   197
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   198
        // Specify in file is same as specify inline
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   199
        tr = doExec(env, javaCmd, "@argFile1", "@argKill", "@argFile2");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   200
        verifyOptions(options, tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   201
        // Main class is @argFile2
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   202
        verifyUserArgs(Collections.emptyList(), tr, 5);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   203
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   204
        // multiple is fine, once on is on.
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   205
        tr = doExec(env, javaCmd, "@argKill", "@argFile1", "-Xdisable-@files", "@argFile2");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   206
        options = Arrays.asList("-Xdisable-@files", "@argFile1",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   207
                "-Xdisable-@files", "@argFile2");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   208
        verifyOptions(options, tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   209
        verifyUserArgs(Collections.emptyList(), tr, 3);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   210
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   211
        // after main class, becoming an user application argument
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   212
        tr = doExec(env, javaCmd, "@argFile2", "@argKill");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   213
        options = Arrays.asList("-jar", "test.jar", "uarg1", "@uarg2", "@@uarg3",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   214
                "-uarg4", "uarg5", "@argKill");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   215
        verifyOptions(options, tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   216
        verifyUserArgs(Arrays.asList("uarg1", "@uarg2", "@@uarg3",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   217
                "-uarg4", "uarg5", "@argKill"), tr, 3);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   218
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   219
        argFile1.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   220
        argFile2.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   221
        argKill.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   222
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   223
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   224
    @Test
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   225
    public void userApplication() throws IOException {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   226
        List<String> lines = new ArrayList<>();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   227
        lines.add("-Xmx32m");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   228
        lines.add("-Xint");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   229
        File vmArgs = createArgFile("vmArgs", lines);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   230
        File jarOpt = createArgFile("jarOpt", Arrays.asList("-jar"));
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   231
        File cpOpt = createArgFile("cpOpt", Arrays.asList("-cp"));
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   232
        File jarArg = createArgFile("jarArg", Arrays.asList("test.jar"));
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   233
        File userArgs = createArgFile("userArgs", Arrays.asList("-opt", "arg", "--longopt"));
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   234
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   235
        TestResult tr = doExec(env, javaCmd,
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   236
                "@vmArgs", "@jarOpt", "test.jar", "-opt", "arg", "--longopt");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   237
        verifyOptions(Arrays.asList(
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   238
                "-Xmx32m", "-Xint", "-jar", "test.jar", "-opt", "arg", "--longopt"), tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   239
        verifyUserArgs(Arrays.asList("-opt", "arg", "--longopt"), tr, 5);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   240
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   241
        tr = doExec(env, javaCmd, "@jarOpt", "@jarArg", "@vmArgs");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   242
        verifyOptions(Arrays.asList("-jar", "test.jar", "@vmArgs"), tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   243
        verifyUserArgs(Arrays.asList("@vmArgs"), tr, 3);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   244
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   245
        tr = doExec(env, javaCmd, "-cp", "@jarArg", "@vmArgs", "Foo", "@userArgs");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   246
        verifyOptions(Arrays.asList("-cp", "test.jar", "-Xmx32m", "-Xint",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   247
                "Foo", "@userArgs"), tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   248
        verifyUserArgs(Arrays.asList("@userArgs"), tr, 6);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   249
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   250
        tr = doExec(env, javaCmd, "@cpOpt", "@jarArg", "@vmArgs", "Foo", "@userArgs");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   251
        verifyOptions(Arrays.asList("-cp", "test.jar", "-Xmx32m", "-Xint",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   252
                "Foo", "@userArgs"), tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   253
        verifyUserArgs(Arrays.asList("@userArgs"), tr, 6);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   254
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   255
        tr = doExec(env, javaCmd, "@cpOpt", "test.jar", "@vmArgs", "Foo", "@userArgs");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   256
        verifyOptions(Arrays.asList("-cp", "test.jar", "-Xmx32m", "-Xint",
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   257
                "Foo", "@userArgs"), tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   258
        verifyUserArgs(Arrays.asList("@userArgs"), tr, 6);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   259
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   260
        vmArgs.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   261
        jarOpt.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   262
        cpOpt.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   263
        jarArg.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   264
        userArgs.delete();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   265
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   266
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   267
    // test with missing file
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   268
    @Test
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   269
    public void missingFileNegativeTest() throws IOException {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   270
        TestResult tr = doExec(javaCmd, "@" + "missing.cmd");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   271
        tr.checkNegative();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   272
        tr.contains("Error: could not open `missing.cmd'");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   273
        if (!tr.testStatus) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   274
            System.out.println(tr);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   275
            throw new RuntimeException("test fails");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   276
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   277
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   278
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   279
    public static void main(String... args) throws Exception {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   280
        init();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   281
        ArgsFileTest a = new ArgsFileTest();
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   282
        a.run(args);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   283
        if (testExitValue > 0) {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   284
            System.out.println("Total of " + testExitValue + " failed");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   285
            System.exit(1);
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   286
        } else {
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   287
            System.out.println("All tests pass");
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   288
        }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   289
    }
4e96a9ee01b1 8027634: Support @argfiles for java command-line tool
henryjen
parents:
diff changeset
   290
}