Arrrghs.java revision 2581
0N/A/*
2581N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
647N/A/**
647N/A * @test
2581N/A * @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938
667N/A * @summary Argument parsing validation.
1945N/A * @compile -XDignore.symbol.file Arrrghs.java TestHelper.java
647N/A * @run main Arrrghs
647N/A */
647N/A
176N/Aimport java.io.BufferedReader;
176N/Aimport java.io.File;
647N/Aimport java.io.FileNotFoundException;
176N/Aimport java.io.IOException;
176N/Aimport java.io.InputStream;
176N/Aimport java.io.InputStreamReader;
176N/Aimport java.util.Map;
0N/A
176N/Apublic class Arrrghs {
647N/A private Arrrghs(){}
0N/A /**
647N/A * This class provides various tests for arguments processing.
0N/A * A group of tests to ensure that arguments are passed correctly to
0N/A * a child java process upon a re-exec, this typically happens when
0N/A * a version other than the one being executed is requested by the user.
0N/A *
0N/A * History: these set of tests were part of Arrrghs.sh. The MKS shell
647N/A * implementations were notoriously buggy. Implementing these tests purely
0N/A * in Java is not only portable but also robust.
0N/A *
0N/A */
0N/A
0N/A // The version string to force a re-exec
0N/A final static String VersionStr = "-version:1.1+";
0N/A
0N/A // The Cookie or the pattern we match in the debug output.
0N/A final static String Cookie = "ReExec Args: ";
0N/A
0N/A /*
0N/A * SIGH, On Windows all strings are quoted, we need to unwrap it
0N/A */
0N/A private static String removeExtraQuotes(String in) {
647N/A if (TestHelper.isWindows) {
0N/A // Trim the string and remove the enclosed quotes if any.
0N/A in = in.trim();
0N/A if (in.startsWith("\"") && in.endsWith("\"")) {
0N/A return in.substring(1, in.length()-1);
0N/A }
0N/A }
0N/A return in;
0N/A }
0N/A
0N/A /*
0N/A * This method detects the cookie in the output stream of the process.
0N/A */
647N/A private static boolean detectCookie(InputStream istream,
647N/A String expectedArguments) throws IOException {
0N/A BufferedReader rd = new BufferedReader(new InputStreamReader(istream));
0N/A boolean retval = false;
0N/A
0N/A String in = rd.readLine();
0N/A while (in != null) {
647N/A if (TestHelper.debug) System.out.println(in);
0N/A if (in.startsWith(Cookie)) {
0N/A String detectedArgument = removeExtraQuotes(in.substring(Cookie.length()));
0N/A if (expectedArguments.equals(detectedArgument)) {
0N/A retval = true;
0N/A } else {
647N/A System.out.println("Error: Expected Arguments\t:'" +
647N/A expectedArguments + "'");
647N/A System.out.println(" Detected Arguments\t:'" +
647N/A detectedArgument + "'");
0N/A }
0N/A // Return the value asap if not in debug mode.
647N/A if (!TestHelper.debug) {
0N/A rd.close();
0N/A istream.close();
0N/A return retval;
0N/A }
0N/A }
0N/A in = rd.readLine();
0N/A }
0N/A return retval;
0N/A }
0N/A
647N/A private static boolean doTest0(ProcessBuilder pb, String expectedArguments) {
0N/A boolean retval = false;
0N/A try {
176N/A pb.redirectErrorStream(true);
0N/A Process p = pb.start();
0N/A retval = detectCookie(p.getInputStream(), expectedArguments);
0N/A p.waitFor();
0N/A p.destroy();
0N/A } catch (Exception ex) {
0N/A ex.printStackTrace();
0N/A throw new RuntimeException(ex.getMessage());
0N/A }
0N/A return retval;
0N/A }
0N/A
0N/A /**
0N/A * This method return true if the expected and detected arguments are the same.
0N/A * Quoting could cause dissimilar testArguments and expected arguments.
0N/A */
647N/A static int doTest(String testArguments, String expectedPattern) {
647N/A ProcessBuilder pb = new ProcessBuilder(TestHelper.javaCmd,
647N/A VersionStr, testArguments);
0N/A
0N/A Map<String, String> env = pb.environment();
0N/A env.put("_JAVA_LAUNCHER_DEBUG", "true");
647N/A return doTest0(pb, testArguments) ? 0 : 1;
0N/A }
0N/A
0N/A /**
0N/A * A convenience method for identical test pattern and expected arguments
0N/A */
647N/A static int doTest(String testPattern) {
647N/A return doTest(testPattern, testPattern);
647N/A }
647N/A
647N/A static void quoteParsingTests() {
647N/A /*
647N/A * Tests for 6214916
647N/A * These tests require that a JVM (any JVM) be installed in the system registry.
647N/A * If none is installed, skip this test.
647N/A */
647N/A TestHelper.TestResult tr =
647N/A TestHelper.doExec(TestHelper.javaCmd, VersionStr, "-version");
647N/A if (!tr.isOK()) {
647N/A System.err.println("Warning:Argument Passing Tests were skipped, " +
647N/A "no java found in system registry.");
647N/A return;
647N/A }
647N/A
647N/A // Basic test
647N/A TestHelper.testExitValue += doTest("-a -b -c -d");
647N/A
647N/A // Basic test with many spaces
647N/A TestHelper.testExitValue += doTest("-a -b -c -d");
647N/A
647N/A // Quoted whitespace does matter ?
647N/A TestHelper.testExitValue += doTest("-a \"\"-b -c\"\" -d");
647N/A
647N/A
647N/A // Escaped quotes outside of quotes as literals
647N/A TestHelper.testExitValue += doTest("-a \\\"-b -c\\\" -d");
647N/A
647N/A // Check for escaped quotes inside of quotes as literal
647N/A TestHelper.testExitValue += doTest("-a \"-b \\\"stuff\\\"\" -c -d");
647N/A
647N/A // A quote preceeded by an odd number of slashes is a literal quote
647N/A TestHelper.testExitValue += doTest("-a -b\\\\\\\" -c -d");
647N/A
647N/A // A quote preceeded by an even number of slashes is a literal quote
647N/A // see 6214916.
647N/A TestHelper.testExitValue += doTest("-a -b\\\\\\\\\" -c -d");
647N/A
647N/A // Make sure that whitespace doesn't interfere with the removal of the
647N/A // appropriate tokens. (space-tab-space preceeds -jre-restict-search).
647N/A TestHelper.testExitValue += doTest("-a -b \t -jre-restrict-search -c -d","-a -b -c -d");
647N/A
647N/A // Make sure that the mJRE tokens being stripped, aren't stripped if
647N/A // they happen to appear as arguments to the main class.
647N/A TestHelper.testExitValue += doTest("foo -version:1.1+");
647N/A
647N/A System.out.println("Completed arguments quoting tests with " +
647N/A TestHelper.testExitValue + " errors");
647N/A }
647N/A
647N/A /*
647N/A * These tests are usually run on non-existent targets to check error results
647N/A */
647N/A static void runBasicErrorMessageTests() {
647N/A // Tests for 5030233
647N/A TestHelper.TestResult tr = TestHelper.doExec(TestHelper.javaCmd, "-cp");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
647N/A System.out.println(tr);
647N/A
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-classpath");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
647N/A System.out.println(tr);
647N/A
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
647N/A System.out.println(tr);
647N/A
647N/A tr = TestHelper.doExec(TestHelper.javacCmd, "-cp");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
647N/A System.out.println(tr);
647N/A
647N/A // Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-X");
647N/A tr.checkPositive();
647N/A tr.isNotZeroOutput();
647N/A System.out.println(tr);
647N/A
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-help");
647N/A tr.checkPositive();
647N/A tr.isNotZeroOutput();
647N/A System.out.println(tr);
2581N/A
2581N/A // 6753938, test for non-negative exit value for an incorrectly formed
2581N/A // command line, '% java'
2581N/A tr = TestHelper.doExec(TestHelper.javaCmd);
2581N/A tr.checkNegative();
2581N/A tr.isNotZeroOutput();
2581N/A System.out.println(tr);
2581N/A
2581N/A // 6753938, test for non-negative exit value for an incorrectly formed
2581N/A // command line, '% java -Xcomp'
2581N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-Xcomp");
2581N/A tr.checkNegative();
2581N/A tr.isNotZeroOutput();
2581N/A System.out.println(tr);
647N/A }
647N/A
647N/A /*
647N/A * A set of tests which tests various dispositions of the main method.
647N/A */
647N/A static void runMainMethodTests() throws FileNotFoundException {
647N/A TestHelper.TestResult tr = null;
647N/A
647N/A // a missing class
653N/A TestHelper.createJar("MIA", new File("some.jar"), new File("Foo"),
653N/A (String[])null);
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
667N/A tr.contains("Error: Could not find main class MIA");
667N/A tr.contains("java.lang.NoClassDefFoundError: MIA");
647N/A System.out.println(tr);
647N/A // use classpath to check
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "MIA");
647N/A tr.contains("Error: Could not find main class MIA");
667N/A tr.contains("java.lang.NoClassDefFoundError: MIA");
647N/A System.out.println(tr);
647N/A
647N/A // incorrect method access
647N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
647N/A "private static void main(String[] args){}");
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
647N/A tr.contains("Error: Main method not found in class Foo");
647N/A System.out.println(tr);
647N/A // use classpath to check
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("Error: Main method not found in class Foo");
647N/A System.out.println(tr);
647N/A
647N/A // incorrect return type
647N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
647N/A "public static int main(String[] args){return 1;}");
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
647N/A tr.contains("Error: Main method must return a value of type void in class Foo");
647N/A System.out.println(tr);
647N/A // use classpath to check
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("Error: Main method must return a value of type void in class Foo");
647N/A System.out.println(tr);
647N/A
647N/A // incorrect parameter type
647N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
647N/A "public static void main(Object[] args){}");
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
647N/A tr.contains("Error: Main method not found in class Foo");
647N/A System.out.println(tr);
647N/A // use classpath to check
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("Error: Main method not found in class Foo");
647N/A System.out.println(tr);
647N/A
647N/A // incorrect method type - non-static
647N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
653N/A "public void main(String[] args){}");
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
653N/A tr.contains("Error: Main method is not static in class Foo");
647N/A System.out.println(tr);
647N/A // use classpath to check
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
653N/A tr.contains("Error: Main method is not static in class Foo");
647N/A System.out.println(tr);
647N/A
647N/A // amongst a potpourri of kindred main methods, is the right one chosen ?
647N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
647N/A "void main(Object[] args){}",
647N/A "int main(Float[] args){return 1;}",
647N/A "private void main() {}",
647N/A "private static void main(int x) {}",
647N/A "public int main(int argc, String[] argv) {return 1;}",
647N/A "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
647N/A tr.contains("THE_CHOSEN_ONE");
647N/A System.out.println(tr);
647N/A // use classpath to check
647N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("THE_CHOSEN_ONE");
647N/A System.out.println(tr);
653N/A
653N/A // test for extraneous whitespace in the Main-Class attribute
653N/A TestHelper.createJar(" Foo ", new File("some.jar"), new File("Foo"),
653N/A "public static void main(String... args){}");
653N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
653N/A tr.checkPositive();
653N/A System.out.println(tr);
0N/A }
0N/A
0N/A /**
0N/A * @param args the command line arguments
647N/A * @throws java.io.FileNotFoundException
0N/A */
647N/A public static void main(String[] args) throws FileNotFoundException {
647N/A if (TestHelper.debug) System.out.println("Starting Arrrghs tests");
667N/A quoteParsingTests();
667N/A runBasicErrorMessageTests();
667N/A runMainMethodTests();
667N/A if (TestHelper.testExitValue > 0) {
667N/A System.out.println("Total of " + TestHelper.testExitValue + " failed");
667N/A System.exit(1);
667N/A } else {
667N/A System.out.println("All tests pass");
667N/A }
0N/A }
0N/A }