source: trunk/openjdk/jdk/test/tools/launcher/Arrrghs.java

Last change on this file was 278, checked in by dmik, 14 years ago

trunk: Merged in openjdk6 b22 from branches/vendor/oracle.

File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2007, 2008, 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
24import java.io.BufferedReader;
25import java.io.File;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.InputStreamReader;
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.List;
33import java.util.Map;
34import java.util.StringTokenizer;
35
36public class Arrrghs {
37
38 /**
39 * A group of tests to ensure that arguments are passed correctly to
40 * a child java process upon a re-exec, this typically happens when
41 * a version other than the one being executed is requested by the user.
42 *
43 * History: these set of tests were part of Arrrghs.sh. The MKS shell
44 * implementations are notoriously buggy. Implementing these tests purely
45 * in Java is not only portable but also robust.
46 *
47 */
48
49 /* Do not instantiate */
50 private Arrrghs() {}
51
52 static String javaCmd;
53
54 // The version string to force a re-exec
55 final static String VersionStr = "-version:1.1+";
56
57 // The Cookie or the pattern we match in the debug output.
58 final static String Cookie = "ReExec Args: ";
59
60 private static boolean _debug = Boolean.getBoolean("Arrrghs.Debug");
61 private static boolean isWindows = System.getProperty("os.name", "unknown").startsWith("Windows");
62 private static int exitValue = 0;
63
64 private static void doUsage(String message) {
65 if (message != null) System.out.println("Error: " + message);
66 System.out.println("Usage: Arrrghs path_to_java");
67 System.exit(1);
68 }
69
70 /*
71 * SIGH, On Windows all strings are quoted, we need to unwrap it
72 */
73 private static String removeExtraQuotes(String in) {
74 if (isWindows) {
75 // Trim the string and remove the enclosed quotes if any.
76 in = in.trim();
77 if (in.startsWith("\"") && in.endsWith("\"")) {
78 return in.substring(1, in.length()-1);
79 }
80 }
81 return in;
82 }
83
84 /*
85 * This method detects the cookie in the output stream of the process.
86 */
87 private static boolean detectCookie(InputStream istream, String expectedArguments) throws IOException {
88 BufferedReader rd = new BufferedReader(new InputStreamReader(istream));
89 boolean retval = false;
90
91 String in = rd.readLine();
92 while (in != null) {
93 if (_debug) System.out.println(in);
94 if (in.startsWith(Cookie)) {
95 String detectedArgument = removeExtraQuotes(in.substring(Cookie.length()));
96 if (expectedArguments.equals(detectedArgument)) {
97 retval = true;
98 } else {
99 System.out.println("Error: Expected Arguments\t:'" + expectedArguments + "'");
100 System.out.println(" Detected Arguments\t:'" + detectedArgument + "'");
101 }
102 // Return the value asap if not in debug mode.
103 if (!_debug) {
104 rd.close();
105 istream.close();
106 return retval;
107 }
108 }
109 in = rd.readLine();
110 }
111 return retval;
112 }
113
114 private static boolean doExec0(ProcessBuilder pb, String expectedArguments) {
115 boolean retval = false;
116 try {
117 pb.redirectErrorStream(true);
118 Process p = pb.start();
119 retval = detectCookie(p.getInputStream(), expectedArguments);
120 p.waitFor();
121 p.destroy();
122 } catch (Exception ex) {
123 ex.printStackTrace();
124 throw new RuntimeException(ex.getMessage());
125 }
126 return retval;
127 }
128
129 /**
130 * This method return true if the expected and detected arguments are the same.
131 * Quoting could cause dissimilar testArguments and expected arguments.
132 */
133 static boolean doExec(String testArguments, String expectedPattern) {
134 ProcessBuilder pb = new ProcessBuilder(javaCmd, VersionStr, testArguments);
135
136 Map<String, String> env = pb.environment();
137 env.put("_JAVA_LAUNCHER_DEBUG", "true");
138 return doExec0(pb, testArguments);
139 }
140
141 /**
142 * A convenience method for identical test pattern and expected arguments
143 */
144 static boolean doExec(String testPattern) {
145 return doExec(testPattern, testPattern);
146 }
147
148 /**
149 * @param args the command line arguments
150 */
151 public static void main(String[] args) {
152 if (args.length < 1 && args[0] == null) {
153 doUsage("Invalid number of arguments");
154 }
155
156 javaCmd = args[0];
157
158 if (!new File(javaCmd).canExecute()) {
159 if (isWindows && new File(javaCmd + ".exe").canExecute()) {
160 javaCmd = javaCmd + ".exe";
161 } else {
162 doUsage("The java executable must exist");
163 }
164 }
165
166
167 if (_debug) System.out.println("Starting Arrrghs tests");
168 // Basic test
169 if (!doExec("-a -b -c -d")) exitValue++;
170
171 // Basic test with many spaces
172 if (!doExec("-a -b -c -d")) exitValue++;
173
174 // Quoted whitespace does matter ?
175 if (!doExec("-a \"\"-b -c\"\" -d")) exitValue++;
176
177 // Escaped quotes outside of quotes as literals
178 if (!doExec("-a \\\"-b -c\\\" -d")) exitValue++;
179
180 // Check for escaped quotes inside of quotes as literal
181 if (!doExec("-a \"-b \\\"stuff\\\"\" -c -d")) exitValue++;
182
183 // A quote preceeded by an odd number of slashes is a literal quote
184 if (!doExec("-a -b\\\\\\\" -c -d")) exitValue++;
185
186 // A quote preceeded by an even number of slashes is a literal quote
187 // see 6214916.
188 if (!doExec("-a -b\\\\\\\\\" -c -d")) exitValue++;
189
190 // Make sure that whitespace doesn't interfere with the removal of the
191 // appropriate tokens. (space-tab-space preceeds -jre-restict-search).
192 if (!doExec("-a -b \t -jre-restrict-search -c -d","-a -b -c -d")) exitValue++;
193
194 // Make sure that the mJRE tokens being stripped, aren't stripped if
195 // they happen to appear as arguments to the main class.
196 if (!doExec("foo -version:1.1+")) exitValue++;
197
198 System.out.println("Completed Arrrghs arguments quoting/matching tests with " + exitValue + " errors");
199 System.exit(exitValue);
200 }
201}
Note: See TracBrowser for help on using the repository browser.