/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test * @bug 8027634 * @summary Argument parsing from file * @build TestHelper * @run main ArgsFileTest */ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ArgsFileTest extends TestHelper { private File createFile(List lines) throws IOException { File argFile = new File("argfile"); argFile.delete(); createAFile(argFile, lines); return argFile; } private void verifyOutput(List args, TestResult tr, int start_idx) { if (args.isEmpty()) { return; } if (start_idx <= 0) { String arg = Pattern.quote(args.get(0)); String line = tr.findInOutput(".*argv\\[(\\d+)\\] = " + arg + ".*"); if (line == null) { System.out.println(tr); throw new RuntimeException("test fails"); } Matcher m = Pattern.compile(".*argv\\[(\\d+)\\] = " + arg + ".*").matcher(line); if (!m.matches()) { System.out.println(tr); throw new RuntimeException("test fails"); } start_idx = Integer.parseInt(m.group(1)) + 1; args = args.subList(1, args.size()); } int i = start_idx; for (String x : args) { tr.matches(".*argv\\[" + i + "\\] = " + Pattern.quote(x) + ".*"); i++; } if (! tr.testStatus) { System.out.println(tr); throw new RuntimeException("test fails"); } } static String[] cases[][] = { { { "# a couple of -X flags", "-Xmx32m", "-XshowSettings", "# add -version", "-version" }, { "-Xmx32m", "-XshowSettings", "-version" } }, { { "-cp \"c:\\\\java lib\\\\all", "-Xmx32m -XshowSettings", "-version" }, { "-cp", "c:\\java lib\\all", "-Xmx32m", "-XshowSettings", "-version" } } }; public Object[][] loadCases() { int cnt = cases.length; Object[][] rv = new Object[cnt + 2][2]; for (int i = 0; i < cnt; i++) { rv[i][0] = Arrays.asList(cases[i][0]); rv[i][1] = Arrays.asList(cases[i][1]); } // long lines String bcp = "-Xbootclasspath/a:"; String ver = "-version"; // a token 8192 long char[] data = new char[8192 - bcp.length()]; Arrays.fill(data, 'O'); List scratch = new ArrayList<>(); scratch.add("-Xmx32m"); scratch.add(bcp + String.valueOf(data)); scratch.add(ver); rv[cnt][0] = scratch; rv[cnt++][1] = scratch; data = new char[8192 + 1024]; Arrays.fill(data, 'O'); scratch = new ArrayList<>(); scratch.add(bcp + String.valueOf(data)); scratch.add(ver); rv[cnt][0] = scratch; rv[cnt][1] = scratch; return rv; } // ensure the arguments in the file are read in correctly public void getArguments(List lines, List args) throws IOException { File argFile = createFile(lines); String fname = "@" + argFile.getName(); Map env = new HashMap<>(); env.put(JLDEBUG_KEY, "true"); TestResult tr = doExec(env, javaCmd, fname); tr.checkPositive(); verifyOutput(args, tr, 1); argFile.delete(); } @Test public void runner() throws IOException { Object[][] allcases = loadCases(); for (Object[] test: allcases) { getArguments((List) test[0], (List) test[1]); } } @Test public void variousTools() throws IOException { List lines = Arrays.asList(cases[0][0]); List args = Arrays.asList(cases[0][1]); File argFile = createFile(lines); String fname = "@" + argFile.getName(); Map env = new HashMap<>(); env.put(JLDEBUG_KEY, "true"); // Without -J, @argfile should not expand for javac TestResult tr = doExec(env, javacCmd, fname); verifyOutput(Collections.singletonList(fname), tr, -1); // With -J@argfile, @argfile is expanded tr = doExec(env, javacCmd, "-J" + fname); verifyOutput(args, tr, -1); tr = doExec(env, javaCmd, "-Xint", fname, "-Dlast.arg"); List scratch = new ArrayList<>(); scratch.add("-Xint"); scratch.addAll(args); scratch.add("-Dlast.arg"); verifyOutput(scratch, tr, 1); } // test with missing file @Test void missingFileNegativeTest() throws IOException { TestResult tr = doExec(javaCmd, "@" + "missing.cmd"); tr.checkNegative(); tr.contains("Error: could not open `missing.cmd'"); if (!tr.testStatus) { System.out.println(tr); throw new RuntimeException("test fails"); } } public static void main(String... args) throws Exception { ArgsFileTest a = new ArgsFileTest(); a.run(args); if (testExitValue > 0) { System.out.println("Total of " + testExitValue + " failed"); System.exit(1); } else { System.out.println("All tests pass"); } } }