PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/tools/launcher/Arrrghs.java

https://bitbucket.org/screenconnect/openjdk8-jdk
Java | 781 lines | 528 code | 96 blank | 157 comment | 60 complexity | 8156439503cb3812daee85bd9658d3f3 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-3.0
  1. /*
  2. * Copyright (c) 2007, 2013, 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. /**
  24. * @test
  25. * @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938
  26. * 6894719 6968053 7151434 7146424 8007333 8077822
  27. * @summary Argument parsing validation.
  28. * @compile -XDignore.symbol.file Arrrghs.java
  29. * @run main/othervm Arrrghs
  30. */
  31. import java.io.BufferedReader;
  32. import java.io.File;
  33. import java.io.FileNotFoundException;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.util.ArrayList;
  38. import java.util.Arrays;
  39. import java.util.HashMap;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.regex.Matcher;
  43. import java.util.regex.Pattern;
  44. public class Arrrghs extends TestHelper {
  45. private Arrrghs(){}
  46. /**
  47. * This class provides various tests for arguments processing.
  48. * A group of tests to ensure that arguments are passed correctly to
  49. * a child java process upon a re-exec, this typically happens when
  50. * a version other than the one being executed is requested by the user.
  51. *
  52. * History: these set of tests were part of Arrrghs.sh. The MKS shell
  53. * implementations were notoriously buggy. Implementing these tests purely
  54. * in Java is not only portable but also robust.
  55. *
  56. */
  57. // The version string to force a re-exec
  58. final static String VersionStr = "-version:1.1+";
  59. // The Cookie or the pattern we match in the debug output.
  60. final static String Cookie = "ReExec Args: ";
  61. /*
  62. * SIGH, On Windows all strings are quoted, we need to unwrap it
  63. */
  64. private static String removeExtraQuotes(String in) {
  65. if (isWindows) {
  66. // Trim the string and remove the enclosed quotes if any.
  67. in = in.trim();
  68. if (in.startsWith("\"") && in.endsWith("\"")) {
  69. return in.substring(1, in.length()-1);
  70. }
  71. }
  72. return in;
  73. }
  74. /*
  75. * This method detects the cookie in the output stream of the process.
  76. */
  77. private boolean detectCookie(InputStream istream,
  78. String expectedArguments) throws IOException {
  79. BufferedReader rd = new BufferedReader(new InputStreamReader(istream));
  80. boolean retval = false;
  81. String in = rd.readLine();
  82. while (in != null) {
  83. if (debug) System.out.println(in);
  84. if (in.startsWith(Cookie)) {
  85. String detectedArgument = removeExtraQuotes(in.substring(Cookie.length()));
  86. if (expectedArguments.equals(detectedArgument)) {
  87. retval = true;
  88. } else {
  89. System.out.println("Error: Expected Arguments\t:'" +
  90. expectedArguments + "'");
  91. System.out.println(" Detected Arguments\t:'" +
  92. detectedArgument + "'");
  93. }
  94. // Return the value asap if not in debug mode.
  95. if (!debug) {
  96. rd.close();
  97. istream.close();
  98. return retval;
  99. }
  100. }
  101. in = rd.readLine();
  102. }
  103. return retval;
  104. }
  105. private boolean doReExecTest0(ProcessBuilder pb, String expectedArguments) {
  106. boolean retval = false;
  107. try {
  108. pb.redirectErrorStream(true);
  109. Process p = pb.start();
  110. retval = detectCookie(p.getInputStream(), expectedArguments);
  111. p.waitFor();
  112. p.destroy();
  113. } catch (Exception ex) {
  114. ex.printStackTrace();
  115. throw new RuntimeException(ex.getMessage());
  116. }
  117. return retval;
  118. }
  119. /**
  120. * This method returns true if the expected and detected arguments are the same.
  121. * Quoting could cause dissimilar testArguments and expected arguments.
  122. */
  123. int doReExecTest(String testArguments, String expectedPattern) {
  124. ProcessBuilder pb = new ProcessBuilder(javaCmd,
  125. VersionStr, testArguments);
  126. Map<String, String> env = pb.environment();
  127. env.put(JLDEBUG_KEY, "true");
  128. return doReExecTest0(pb, testArguments) ? 0 : 1;
  129. }
  130. /**
  131. * A convenience method for identical test pattern and expected arguments
  132. */
  133. int doReExecTest(String testPattern) {
  134. return doReExecTest(testPattern, testPattern);
  135. }
  136. @Test
  137. void testQuoteParsingThroughReExec() {
  138. /*
  139. * Tests for 6214916
  140. * These tests require that a JVM (any JVM) be installed in the system registry.
  141. * If none is installed, skip this test.
  142. */
  143. TestResult tr = doExec(javaCmd, VersionStr, "-version");
  144. if (!tr.isOK()) {
  145. System.err.println("Warning:Argument Passing Tests were skipped, " +
  146. "no java found in system registry.");
  147. return;
  148. }
  149. // Basic test
  150. testExitValue += doReExecTest("-a -b -c -d");
  151. // Basic test with many spaces
  152. testExitValue += doReExecTest("-a -b -c -d");
  153. // Quoted whitespace does matter ?
  154. testExitValue += doReExecTest("-a \"\"-b -c\"\" -d");
  155. // Escaped quotes outside of quotes as literals
  156. testExitValue += doReExecTest("-a \\\"-b -c\\\" -d");
  157. // Check for escaped quotes inside of quotes as literal
  158. testExitValue += doReExecTest("-a \"-b \\\"stuff\\\"\" -c -d");
  159. // A quote preceeded by an odd number of slashes is a literal quote
  160. testExitValue += doReExecTest("-a -b\\\\\\\" -c -d");
  161. // A quote preceeded by an even number of slashes is a literal quote
  162. // see 6214916.
  163. testExitValue += doReExecTest("-a -b\\\\\\\\\" -c -d");
  164. // Make sure that whitespace doesn't interfere with the removal of the
  165. // appropriate tokens. (space-tab-space preceeds -jre-restict-search).
  166. testExitValue += doReExecTest("-a -b \t -jre-restrict-search -c -d", "-a -b -c -d");
  167. // Make sure that the mJRE tokens being stripped, aren't stripped if
  168. // they happen to appear as arguments to the main class.
  169. testExitValue += doReExecTest("foo -version:1.1+");
  170. System.out.println("Completed arguments quoting tests with "
  171. + testExitValue + " errors");
  172. }
  173. // the pattern we hope to see in the output
  174. static final Pattern ArgPattern = Pattern.compile("\\s*argv\\[[0-9]*\\].*=.*");
  175. void checkArgumentParsing(String inArgs, String... expArgs) throws IOException {
  176. List<String> scratchpad = new ArrayList<>();
  177. scratchpad.add("set " + JLDEBUG_KEY + "=true");
  178. // GAK, -version needs to be added so that windows can flush its stderr
  179. // exiting the process prematurely can terminate the stderr.
  180. scratchpad.add(javaCmd + " -version " + inArgs);
  181. File batFile = new File("atest.bat");
  182. createAFile(batFile, scratchpad);
  183. TestResult tr = doExec(batFile.getName());
  184. ArrayList<String> expList = new ArrayList<>();
  185. expList.add(javaCmd);
  186. expList.add("-version");
  187. expList.addAll(Arrays.asList(expArgs));
  188. List<String> gotList = new ArrayList<>();
  189. for (String x : tr.testOutput) {
  190. Matcher m = ArgPattern.matcher(x);
  191. if (m.matches()) {
  192. String a[] = x.split("=");
  193. gotList.add(a[a.length - 1].trim());
  194. }
  195. }
  196. if (!gotList.equals(expList)) {
  197. System.out.println(tr);
  198. System.out.println("Expected args:");
  199. System.out.println(expList);
  200. System.out.println("Obtained args:");
  201. System.out.println(gotList);
  202. throw new RuntimeException("Error: args do not match");
  203. }
  204. System.out.println("\'" + inArgs + "\'" + " - Test passed");
  205. }
  206. /*
  207. * This tests general quoting and are specific to Windows, *nixes
  208. * need not worry about this, these have been tested with Windows
  209. * implementation and those that are known to work are used against
  210. * the java implementation. Note that the ProcessBuilder gets in the
  211. * way when testing some of these arguments, therefore we need to
  212. * create and execute a .bat file containing the arguments.
  213. */
  214. @Test
  215. void testArgumentParsing() throws IOException {
  216. if (!isWindows)
  217. return;
  218. // no quotes
  219. checkArgumentParsing("a b c d", "a", "b", "c", "d");
  220. // single quotes
  221. checkArgumentParsing("\"a b c d\"", "a b c d");
  222. //double quotes
  223. checkArgumentParsing("\"\"a b c d\"\"", "a", "b", "c", "d");
  224. // triple quotes
  225. checkArgumentParsing("\"\"\"a b c d\"\"\"", "\"a b c d\"");
  226. // a literal within single quotes
  227. checkArgumentParsing("\"a\"b c d\"e\"", "ab", "c", "de");
  228. // a literal within double quotes
  229. checkArgumentParsing("\"\"a\"b c d\"e\"\"", "ab c de");
  230. // a literal quote
  231. checkArgumentParsing("a\\\"b", "a\"b");
  232. // double back-slash
  233. checkArgumentParsing("\"a b c d\\\\\"", "a b c d\\");
  234. // triple back-slash
  235. checkArgumentParsing("a\\\\\\\"b", "a\\\"b");
  236. // dangling quote
  237. checkArgumentParsing("\"a b c\"\"", "a b c\"");
  238. // expansions of white space separators
  239. checkArgumentParsing("a b", "a", "b");
  240. checkArgumentParsing("a\tb", "a", "b");
  241. checkArgumentParsing("a \t b", "a", "b");
  242. checkArgumentParsing("\"C:\\TEST A\\\\\"", "C:\\TEST A\\");
  243. checkArgumentParsing("\"\"C:\\TEST A\\\\\"\"", "C:\\TEST", "A\\");
  244. // MS Windows tests
  245. // triple back-slash
  246. checkArgumentParsing("a\\\\\\d", "a\\\\\\d");
  247. // triple back-slash in quotes
  248. checkArgumentParsing("\"a\\\\\\d\"", "a\\\\\\d");
  249. // slashes separating characters
  250. checkArgumentParsing("X\\Y\\Z", "X\\Y\\Z");
  251. checkArgumentParsing("\\X\\Y\\Z", "\\X\\Y\\Z");
  252. // literals within dangling quotes, etc.
  253. checkArgumentParsing("\"a b c\" d e", "a b c", "d", "e");
  254. checkArgumentParsing("\"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d");
  255. checkArgumentParsing("a\\\\\\c d\"e f\"g h", "a\\\\\\c", "de fg", "h");
  256. checkArgumentParsing("a\\\\\\\"b c d", "a\\\"b", "c", "d");
  257. checkArgumentParsing("a\\\\\\\\\"g c\" d e", "a\\\\g c", "d", "e");
  258. // treatment of back-slashes
  259. checkArgumentParsing("*\\", "*\\");
  260. checkArgumentParsing("*/", "*/");
  261. checkArgumentParsing(".\\*", ".\\*");
  262. checkArgumentParsing("./*", "./*");
  263. checkArgumentParsing("..\\..\\*", "..\\..\\*");
  264. checkArgumentParsing("../../*", "../../*");
  265. checkArgumentParsing("..\\..\\", "..\\..\\");
  266. checkArgumentParsing("../../", "../../");
  267. checkArgumentParsing("a b\\ c", "a", "b\\", "c");
  268. // 2 back-slashes
  269. checkArgumentParsing("\\\\?", "\\\\?");
  270. // 3 back-slashes
  271. checkArgumentParsing("\\\\\\?", "\\\\\\?");
  272. // 4 back-slashes
  273. checkArgumentParsing("\\\\\\\\?", "\\\\\\\\?");
  274. // 5 back-slashes
  275. checkArgumentParsing("\\\\\\\\\\?", "\\\\\\\\\\?");
  276. // 6 back-slashes
  277. checkArgumentParsing("\\\\\\\\\\\\?", "\\\\\\\\\\\\?");
  278. // more treatment of mixed slashes
  279. checkArgumentParsing("f1/ f3\\ f4/", "f1/", "f3\\", "f4/");
  280. checkArgumentParsing("f1/ f2\' ' f3/ f4/", "f1/", "f2\'", "'", "f3/", "f4/");
  281. }
  282. private void initEmptyDir(File emptyDir) throws IOException {
  283. if (emptyDir.exists()) {
  284. recursiveDelete(emptyDir);
  285. }
  286. emptyDir.mkdir();
  287. }
  288. private void initDirWithJavaFiles(File libDir) throws IOException {
  289. if (libDir.exists()) {
  290. recursiveDelete(libDir);
  291. }
  292. libDir.mkdirs();
  293. ArrayList<String> scratchpad = new ArrayList<>();
  294. scratchpad.add("package lib;");
  295. scratchpad.add("public class Fbo {");
  296. scratchpad.add("public static void main(String... args){Foo.f();}");
  297. scratchpad.add("public static void f(){}");
  298. scratchpad.add("}");
  299. createFile(new File(libDir, "Fbo.java"), scratchpad);
  300. scratchpad.clear();
  301. scratchpad.add("package lib;");
  302. scratchpad.add("public class Foo {");
  303. scratchpad.add("public static void main(String... args){");
  304. scratchpad.add("for (String x : args) {");
  305. scratchpad.add("System.out.println(x);");
  306. scratchpad.add("}");
  307. scratchpad.add("Fbo.f();");
  308. scratchpad.add("}");
  309. scratchpad.add("public static void f(){}");
  310. scratchpad.add("}");
  311. createFile(new File(libDir, "Foo.java"), scratchpad);
  312. }
  313. void checkArgumentWildcard(String inArgs, String... expArgs) throws IOException {
  314. String[] in = {inArgs};
  315. checkArgumentWildcard(in, expArgs);
  316. // now add arbitrary arguments before and after
  317. String[] outInArgs = { "-Q", inArgs, "-R"};
  318. String[] outExpArgs = new String[expArgs.length + 2];
  319. outExpArgs[0] = "-Q";
  320. System.arraycopy(expArgs, 0, outExpArgs, 1, expArgs.length);
  321. outExpArgs[expArgs.length + 1] = "-R";
  322. checkArgumentWildcard(outInArgs, outExpArgs);
  323. }
  324. void checkArgumentWildcard(String[] inArgs, String[] expArgs) throws IOException {
  325. ArrayList<String> argList = new ArrayList<>();
  326. argList.add(javaCmd);
  327. argList.add("-cp");
  328. argList.add("lib" + File.separator + "*");
  329. argList.add("lib.Foo");
  330. argList.addAll(Arrays.asList(inArgs));
  331. String[] cmds = new String[argList.size()];
  332. argList.toArray(cmds);
  333. TestResult tr = doExec(cmds);
  334. if (!tr.isOK()) {
  335. System.out.println(tr);
  336. throw new RuntimeException("Error: classpath single entry wildcard entry");
  337. }
  338. ArrayList<String> expList = new ArrayList<>();
  339. expList.addAll(Arrays.asList(expArgs));
  340. List<String> gotList = new ArrayList<>();
  341. for (String x : tr.testOutput) {
  342. gotList.add(x.trim());
  343. }
  344. if (!gotList.equals(expList)) {
  345. System.out.println(tr);
  346. System.out.println("Expected args:");
  347. System.out.println(expList);
  348. System.out.println("Obtained args:");
  349. System.out.println(gotList);
  350. throw new RuntimeException("Error: args do not match");
  351. }
  352. System.out.print("\'");
  353. for (String x : inArgs) {
  354. System.out.print(x + " ");
  355. }
  356. System.out.println("\'" + " - Test passed");
  357. }
  358. /*
  359. * These tests are not expected to work on *nixes, and are ignored.
  360. */
  361. @Test
  362. void testWildCardArgumentProcessing() throws IOException {
  363. if (!isWindows)
  364. return;
  365. File cwd = new File(".");
  366. File libDir = new File(cwd, "lib");
  367. initDirWithJavaFiles(libDir);
  368. initEmptyDir(new File(cwd, "empty"));
  369. // test if javac (the command) can compile *.java
  370. TestResult tr = doExec(javacCmd, libDir.getName() + File.separator + "*.java");
  371. if (!tr.isOK()) {
  372. System.out.println(tr);
  373. throw new RuntimeException("Error: compiling java wildcards");
  374. }
  375. // test if javac (the command) can compile *.java with a vmoption
  376. tr = doExec(javacCmd, "-cp", ".",
  377. "-J-showversion", "-J-Dsomeproperty=foo",
  378. libDir.getName() + File.separator + "*.java");
  379. if (!tr.isOK()) {
  380. System.out.println(tr);
  381. throw new RuntimeException("Error: compiling java wildcards with vmoptions");
  382. }
  383. // use the jar cmd to create jars using the ? wildcard
  384. File jarFoo = new File(libDir, "Foo.jar");
  385. tr = doExec(jarCmd, "cvf", jarFoo.getAbsolutePath(), "lib" + File.separator + "F?o.class");
  386. if (!tr.isOK()) {
  387. System.out.println(tr);
  388. throw new RuntimeException("Error: creating jar with wildcards");
  389. }
  390. // now the litmus test!, this should work
  391. checkArgumentWildcard("a", "a");
  392. // test for basic expansion
  393. checkArgumentWildcard("lib\\F*java", "lib\\Fbo.java", "lib\\Foo.java");
  394. // basic expansion in quotes
  395. checkArgumentWildcard("\"lib\\F*java\"", "lib\\F*java");
  396. checkArgumentWildcard("lib\\**", "lib\\Fbo.class", "lib\\Fbo.java",
  397. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  398. checkArgumentWildcard("lib\\*?", "lib\\Fbo.class", "lib\\Fbo.java",
  399. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  400. checkArgumentWildcard("lib\\?*", "lib\\Fbo.class", "lib\\Fbo.java",
  401. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  402. checkArgumentWildcard("lib\\?", "lib\\?");
  403. // test for basic expansion
  404. checkArgumentWildcard("lib\\*java", "lib\\Fbo.java", "lib\\Foo.java");
  405. // basic expansion in quotes
  406. checkArgumentWildcard("\"lib\\*.java\"", "lib\\*.java");
  407. // suffix expansion
  408. checkArgumentWildcard("lib\\*.class", "lib\\Fbo.class", "lib\\Foo.class");
  409. // suffix expansion in quotes
  410. checkArgumentWildcard("\"lib\\*.class\"", "lib\\*.class");
  411. // check for ? expansion now
  412. checkArgumentWildcard("lib\\F?o.java", "lib\\Fbo.java", "lib\\Foo.java");
  413. // check ? in quotes
  414. checkArgumentWildcard("\"lib\\F?o.java\"", "lib\\F?o.java");
  415. // check ? as suffixes
  416. checkArgumentWildcard("lib\\F?o.????", "lib\\Fbo.java", "lib\\Foo.java");
  417. // check ? in a leading role
  418. checkArgumentWildcard("lib\\???.java", "lib\\Fbo.java", "lib\\Foo.java");
  419. checkArgumentWildcard("\"lib\\???.java\"", "lib\\???.java");
  420. // check ? prefixed with -
  421. checkArgumentWildcard("-?", "-?");
  422. // check * prefixed with -
  423. checkArgumentWildcard("-*", "-*");
  424. // check on empty directory
  425. checkArgumentWildcard("empty\\*", "empty\\*");
  426. checkArgumentWildcard("empty\\**", "empty\\**");
  427. checkArgumentWildcard("empty\\?", "empty\\?");
  428. checkArgumentWildcard("empty\\??", "empty\\??");
  429. checkArgumentWildcard("empty\\*?", "empty\\*?");
  430. checkArgumentWildcard("empty\\?*", "empty\\?*");
  431. }
  432. void doArgumentCheck(String inArgs, String... expArgs) {
  433. Map<String, String> env = new HashMap<>();
  434. env.put(JLDEBUG_KEY, "true");
  435. TestResult tr = doExec(env, javaCmd, inArgs);
  436. System.out.println(tr);
  437. int sindex = tr.testOutput.indexOf("Command line args:");
  438. if (sindex < 0) {
  439. System.out.println(tr);
  440. throw new RuntimeException("Error: no output");
  441. }
  442. sindex++; // skip over the tag
  443. List<String> gotList = new ArrayList<>();
  444. for (String x : tr.testOutput.subList(sindex, sindex + expArgs.length)) {
  445. String a[] = x.split("=");
  446. gotList.add(a[a.length - 1].trim());
  447. }
  448. List<String> expList = Arrays.asList(expArgs);
  449. if (!gotList.equals(expList)) {
  450. System.out.println(tr);
  451. System.out.println("Expected args:");
  452. System.out.println(expList);
  453. System.out.println("Obtained args:");
  454. System.out.println(gotList);
  455. throw new RuntimeException("Error: args do not match");
  456. }
  457. }
  458. /*
  459. * These tests are usually run on non-existent targets to check error results
  460. */
  461. @Test
  462. void testBasicErrorMessages() {
  463. // Tests for 5030233
  464. TestResult tr = doExec(javaCmd, "-cp");
  465. tr.checkNegative();
  466. tr.isNotZeroOutput();
  467. if (!tr.testStatus)
  468. System.out.println(tr);
  469. tr = doExec(javaCmd, "-classpath");
  470. tr.checkNegative();
  471. tr.isNotZeroOutput();
  472. if (!tr.testStatus)
  473. System.out.println(tr);
  474. tr = doExec(javaCmd, "-jar");
  475. tr.checkNegative();
  476. tr.isNotZeroOutput();
  477. if (!tr.testStatus)
  478. System.out.println(tr);
  479. tr = doExec(javacCmd, "-cp");
  480. tr.checkNegative();
  481. tr.isNotZeroOutput();
  482. if (!tr.testStatus)
  483. System.out.println(tr);
  484. // Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
  485. tr = doExec(javaCmd, "-X");
  486. tr.checkPositive();
  487. tr.isNotZeroOutput();
  488. if (!tr.testStatus)
  489. System.out.println(tr);
  490. tr = doExec(javaCmd, "-help");
  491. tr.checkPositive();
  492. tr.isNotZeroOutput();
  493. if (!tr.testStatus)
  494. System.out.println(tr);
  495. // 6753938, test for non-negative exit value for an incorrectly formed
  496. // command line, '% java'
  497. tr = doExec(javaCmd);
  498. tr.checkNegative();
  499. tr.isNotZeroOutput();
  500. if (!tr.testStatus)
  501. System.out.println(tr);
  502. // 6753938, test for non-negative exit value for an incorrectly formed
  503. // command line, '% java -Xcomp'
  504. tr = doExec(javaCmd, "-Xcomp");
  505. tr.checkNegative();
  506. tr.isNotZeroOutput();
  507. if (!tr.testStatus)
  508. System.out.println(tr);
  509. // 7151434, test for non-negative exit value for an incorrectly formed
  510. // command line, '% java -jar -W', note the bogus -W
  511. tr = doExec(javaCmd, "-jar", "-W");
  512. tr.checkNegative();
  513. tr.contains("Unrecognized option: -W");
  514. if (!tr.testStatus)
  515. System.out.println(tr);
  516. }
  517. /*
  518. * Tests various dispositions of the main method, these tests are limited
  519. * to English locales as they check for error messages that are localized.
  520. */
  521. @Test
  522. void testMainMethod() throws FileNotFoundException {
  523. if (!isEnglishLocale()) {
  524. return;
  525. }
  526. TestResult tr = null;
  527. // a missing class
  528. createJar("MIA", new File("some.jar"), new File("Foo"),
  529. (String[])null);
  530. tr = doExec(javaCmd, "-jar", "some.jar");
  531. tr.contains("Error: Could not find or load main class MIA");
  532. if (!tr.testStatus)
  533. System.out.println(tr);
  534. // use classpath to check
  535. tr = doExec(javaCmd, "-cp", "some.jar", "MIA");
  536. tr.contains("Error: Could not find or load main class MIA");
  537. if (!tr.testStatus)
  538. System.out.println(tr);
  539. // incorrect method access
  540. createJar(new File("some.jar"), new File("Foo"),
  541. "private static void main(String[] args){}");
  542. tr = doExec(javaCmd, "-jar", "some.jar");
  543. tr.contains("Error: Main method not found in class Foo");
  544. if (!tr.testStatus)
  545. System.out.println(tr);
  546. // use classpath to check
  547. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  548. tr.contains("Error: Main method not found in class Foo");
  549. if (!tr.testStatus)
  550. System.out.println(tr);
  551. // incorrect return type
  552. createJar(new File("some.jar"), new File("Foo"),
  553. "public static int main(String[] args){return 1;}");
  554. tr = doExec(javaCmd, "-jar", "some.jar");
  555. tr.contains("Error: Main method must return a value of type void in class Foo");
  556. if (!tr.testStatus)
  557. System.out.println(tr);
  558. // use classpath to check
  559. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  560. tr.contains("Error: Main method must return a value of type void in class Foo");
  561. if (!tr.testStatus)
  562. System.out.println(tr);
  563. // incorrect parameter type
  564. createJar(new File("some.jar"), new File("Foo"),
  565. "public static void main(Object[] args){}");
  566. tr = doExec(javaCmd, "-jar", "some.jar");
  567. tr.contains("Error: Main method not found in class Foo");
  568. if (!tr.testStatus)
  569. System.out.println(tr);
  570. // use classpath to check
  571. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  572. tr.contains("Error: Main method not found in class Foo");
  573. if (!tr.testStatus)
  574. System.out.println(tr);
  575. // incorrect method type - non-static
  576. createJar(new File("some.jar"), new File("Foo"),
  577. "public void main(String[] args){}");
  578. tr = doExec(javaCmd, "-jar", "some.jar");
  579. tr.contains("Error: Main method is not static in class Foo");
  580. if (!tr.testStatus)
  581. System.out.println(tr);
  582. // use classpath to check
  583. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  584. tr.contains("Error: Main method is not static in class Foo");
  585. if (!tr.testStatus)
  586. System.out.println(tr);
  587. // amongst a potpourri of kindred main methods, is the right one chosen ?
  588. createJar(new File("some.jar"), new File("Foo"),
  589. "void main(Object[] args){}",
  590. "int main(Float[] args){return 1;}",
  591. "private void main() {}",
  592. "private static void main(int x) {}",
  593. "public int main(int argc, String[] argv) {return 1;}",
  594. "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
  595. tr = doExec(javaCmd, "-jar", "some.jar");
  596. tr.contains("THE_CHOSEN_ONE");
  597. if (!tr.testStatus)
  598. System.out.println(tr);
  599. // use classpath to check
  600. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  601. tr.contains("THE_CHOSEN_ONE");
  602. if (!tr.testStatus)
  603. System.out.println(tr);
  604. // test for extraneous whitespace in the Main-Class attribute
  605. createJar(" Foo ", new File("some.jar"), new File("Foo"),
  606. "public static void main(String... args){}");
  607. tr = doExec(javaCmd, "-jar", "some.jar");
  608. tr.checkPositive();
  609. if (!tr.testStatus)
  610. System.out.println(tr);
  611. }
  612. /*
  613. * tests 6968053, ie. we turn on the -Xdiag (for now) flag and check if
  614. * the suppressed stack traces are exposed, ignore these tests for localized
  615. * locales, limiting to English only.
  616. */
  617. @Test
  618. void testDiagOptions() throws FileNotFoundException {
  619. if (!isEnglishLocale()) { // only english version
  620. return;
  621. }
  622. TestResult tr = null;
  623. // a missing class
  624. createJar("MIA", new File("some.jar"), new File("Foo"),
  625. (String[])null);
  626. tr = doExec(javaCmd, "-Xdiag", "-jar", "some.jar");
  627. tr.contains("Error: Could not find or load main class MIA");
  628. tr.contains("java.lang.ClassNotFoundException: MIA");
  629. if (!tr.testStatus)
  630. System.out.println(tr);
  631. // use classpath to check
  632. tr = doExec(javaCmd, "-Xdiag", "-cp", "some.jar", "MIA");
  633. tr.contains("Error: Could not find or load main class MIA");
  634. tr.contains("java.lang.ClassNotFoundException: MIA");
  635. if (!tr.testStatus)
  636. System.out.println(tr);
  637. // a missing class on the classpath
  638. tr = doExec(javaCmd, "-Xdiag", "NonExistentClass");
  639. tr.contains("Error: Could not find or load main class NonExistentClass");
  640. tr.contains("java.lang.ClassNotFoundException: NonExistentClass");
  641. if (!tr.testStatus)
  642. System.out.println(tr);
  643. }
  644. @Test
  645. static void testJreRestrictSearchFlag() {
  646. // test both arguments to ensure they exist
  647. TestResult tr = null;
  648. tr = doExec(javaCmd,
  649. "-no-jre-restrict-search", "-version");
  650. tr.checkPositive();
  651. if (!tr.testStatus)
  652. System.out.println(tr);
  653. tr = doExec(javaCmd,
  654. "-jre-restrict-search", "-version");
  655. tr.checkPositive();
  656. if (!tr.testStatus)
  657. System.out.println(tr);
  658. }
  659. /**
  660. * @param args the command line arguments
  661. * @throws java.io.FileNotFoundException
  662. */
  663. public static void main(String[] args) throws Exception {
  664. if (debug) {
  665. System.out.println("Starting Arrrghs tests");
  666. }
  667. Arrrghs a = new Arrrghs();
  668. a.run(args);
  669. if (testExitValue > 0) {
  670. System.out.println("Total of " + testExitValue + " failed");
  671. System.exit(1);
  672. } else {
  673. System.out.println("All tests pass");
  674. }
  675. }
  676. }