PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/test/tools/launcher/Arrrghs.java

https://bitbucket.org/adoptopenjdk/jdk8-jdk
Java | 771 lines | 521 code | 94 blank | 156 comment | 59 complexity | e81a071cb8fd3846cd47bf788cb6f4bb MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, BSD-3-Clause
  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
  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. // use the jar cmd to create jars using the ? wildcard
  376. File jarFoo = new File(libDir, "Foo.jar");
  377. tr = doExec(jarCmd, "cvf", jarFoo.getAbsolutePath(), "lib" + File.separator + "F?o.class");
  378. if (!tr.isOK()) {
  379. System.out.println(tr);
  380. throw new RuntimeException("Error: creating jar with wildcards");
  381. }
  382. // now the litmus test!, this should work
  383. checkArgumentWildcard("a", "a");
  384. // test for basic expansion
  385. checkArgumentWildcard("lib\\F*java", "lib\\Fbo.java", "lib\\Foo.java");
  386. // basic expansion in quotes
  387. checkArgumentWildcard("\"lib\\F*java\"", "lib\\F*java");
  388. checkArgumentWildcard("lib\\**", "lib\\Fbo.class", "lib\\Fbo.java",
  389. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  390. checkArgumentWildcard("lib\\*?", "lib\\Fbo.class", "lib\\Fbo.java",
  391. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  392. checkArgumentWildcard("lib\\?*", "lib\\Fbo.class", "lib\\Fbo.java",
  393. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  394. checkArgumentWildcard("lib\\?", "lib\\?");
  395. // test for basic expansion
  396. checkArgumentWildcard("lib\\*java", "lib\\Fbo.java", "lib\\Foo.java");
  397. // basic expansion in quotes
  398. checkArgumentWildcard("\"lib\\*.java\"", "lib\\*.java");
  399. // suffix expansion
  400. checkArgumentWildcard("lib\\*.class", "lib\\Fbo.class", "lib\\Foo.class");
  401. // suffix expansion in quotes
  402. checkArgumentWildcard("\"lib\\*.class\"", "lib\\*.class");
  403. // check for ? expansion now
  404. checkArgumentWildcard("lib\\F?o.java", "lib\\Fbo.java", "lib\\Foo.java");
  405. // check ? in quotes
  406. checkArgumentWildcard("\"lib\\F?o.java\"", "lib\\F?o.java");
  407. // check ? as suffixes
  408. checkArgumentWildcard("lib\\F?o.????", "lib\\Fbo.java", "lib\\Foo.java");
  409. // check ? in a leading role
  410. checkArgumentWildcard("lib\\???.java", "lib\\Fbo.java", "lib\\Foo.java");
  411. checkArgumentWildcard("\"lib\\???.java\"", "lib\\???.java");
  412. // check ? prefixed with -
  413. checkArgumentWildcard("-?", "-?");
  414. // check * prefixed with -
  415. checkArgumentWildcard("-*", "-*");
  416. // check on empty directory
  417. checkArgumentWildcard("empty\\*", "empty\\*");
  418. checkArgumentWildcard("empty\\**", "empty\\**");
  419. checkArgumentWildcard("empty\\?", "empty\\?");
  420. checkArgumentWildcard("empty\\??", "empty\\??");
  421. checkArgumentWildcard("empty\\*?", "empty\\*?");
  422. checkArgumentWildcard("empty\\?*", "empty\\?*");
  423. }
  424. void doArgumentCheck(String inArgs, String... expArgs) {
  425. Map<String, String> env = new HashMap<>();
  426. env.put(JLDEBUG_KEY, "true");
  427. TestResult tr = doExec(env, javaCmd, inArgs);
  428. System.out.println(tr);
  429. int sindex = tr.testOutput.indexOf("Command line args:");
  430. if (sindex < 0) {
  431. System.out.println(tr);
  432. throw new RuntimeException("Error: no output");
  433. }
  434. sindex++; // skip over the tag
  435. List<String> gotList = new ArrayList<>();
  436. for (String x : tr.testOutput.subList(sindex, sindex + expArgs.length)) {
  437. String a[] = x.split("=");
  438. gotList.add(a[a.length - 1].trim());
  439. }
  440. List<String> expList = Arrays.asList(expArgs);
  441. if (!gotList.equals(expList)) {
  442. System.out.println(tr);
  443. System.out.println("Expected args:");
  444. System.out.println(expList);
  445. System.out.println("Obtained args:");
  446. System.out.println(gotList);
  447. throw new RuntimeException("Error: args do not match");
  448. }
  449. }
  450. /*
  451. * These tests are usually run on non-existent targets to check error results
  452. */
  453. @Test
  454. void testBasicErrorMessages() {
  455. // Tests for 5030233
  456. TestResult tr = doExec(javaCmd, "-cp");
  457. tr.checkNegative();
  458. tr.isNotZeroOutput();
  459. if (!tr.testStatus)
  460. System.out.println(tr);
  461. tr = doExec(javaCmd, "-classpath");
  462. tr.checkNegative();
  463. tr.isNotZeroOutput();
  464. if (!tr.testStatus)
  465. System.out.println(tr);
  466. tr = doExec(javaCmd, "-jar");
  467. tr.checkNegative();
  468. tr.isNotZeroOutput();
  469. if (!tr.testStatus)
  470. System.out.println(tr);
  471. tr = doExec(javacCmd, "-cp");
  472. tr.checkNegative();
  473. tr.isNotZeroOutput();
  474. if (!tr.testStatus)
  475. System.out.println(tr);
  476. // Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
  477. tr = doExec(javaCmd, "-X");
  478. tr.checkPositive();
  479. tr.isNotZeroOutput();
  480. if (!tr.testStatus)
  481. System.out.println(tr);
  482. tr = doExec(javaCmd, "-help");
  483. tr.checkPositive();
  484. tr.isNotZeroOutput();
  485. if (!tr.testStatus)
  486. System.out.println(tr);
  487. // 6753938, test for non-negative exit value for an incorrectly formed
  488. // command line, '% java'
  489. tr = doExec(javaCmd);
  490. tr.checkNegative();
  491. tr.isNotZeroOutput();
  492. if (!tr.testStatus)
  493. System.out.println(tr);
  494. // 6753938, test for non-negative exit value for an incorrectly formed
  495. // command line, '% java -Xcomp'
  496. tr = doExec(javaCmd, "-Xcomp");
  497. tr.checkNegative();
  498. tr.isNotZeroOutput();
  499. if (!tr.testStatus)
  500. System.out.println(tr);
  501. // 7151434, test for non-negative exit value for an incorrectly formed
  502. // command line, '% java -jar -W', note the bogus -W
  503. tr = doExec(javaCmd, "-jar", "-W");
  504. tr.checkNegative();
  505. tr.contains("Unrecognized option: -W");
  506. if (!tr.testStatus)
  507. System.out.println(tr);
  508. }
  509. /*
  510. * Tests various dispositions of the main method, these tests are limited
  511. * to English locales as they check for error messages that are localized.
  512. */
  513. @Test
  514. void testMainMethod() throws FileNotFoundException {
  515. if (!isEnglishLocale()) {
  516. return;
  517. }
  518. TestResult tr = null;
  519. // a missing class
  520. createJar("MIA", new File("some.jar"), new File("Foo"),
  521. (String[])null);
  522. tr = doExec(javaCmd, "-jar", "some.jar");
  523. tr.contains("Error: Could not find or load main class MIA");
  524. if (!tr.testStatus)
  525. System.out.println(tr);
  526. // use classpath to check
  527. tr = doExec(javaCmd, "-cp", "some.jar", "MIA");
  528. tr.contains("Error: Could not find or load main class MIA");
  529. if (!tr.testStatus)
  530. System.out.println(tr);
  531. // incorrect method access
  532. createJar(new File("some.jar"), new File("Foo"),
  533. "private static void main(String[] args){}");
  534. tr = doExec(javaCmd, "-jar", "some.jar");
  535. tr.contains("Error: Main method not found in class Foo");
  536. if (!tr.testStatus)
  537. System.out.println(tr);
  538. // use classpath to check
  539. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  540. tr.contains("Error: Main method not found in class Foo");
  541. if (!tr.testStatus)
  542. System.out.println(tr);
  543. // incorrect return type
  544. createJar(new File("some.jar"), new File("Foo"),
  545. "public static int main(String[] args){return 1;}");
  546. tr = doExec(javaCmd, "-jar", "some.jar");
  547. tr.contains("Error: Main method must return a value of type void in class Foo");
  548. if (!tr.testStatus)
  549. System.out.println(tr);
  550. // use classpath to check
  551. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  552. tr.contains("Error: Main method must return a value of type void in class Foo");
  553. if (!tr.testStatus)
  554. System.out.println(tr);
  555. // incorrect parameter type
  556. createJar(new File("some.jar"), new File("Foo"),
  557. "public static void main(Object[] args){}");
  558. tr = doExec(javaCmd, "-jar", "some.jar");
  559. tr.contains("Error: Main method not found in class Foo");
  560. if (!tr.testStatus)
  561. System.out.println(tr);
  562. // use classpath to check
  563. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  564. tr.contains("Error: Main method not found in class Foo");
  565. if (!tr.testStatus)
  566. System.out.println(tr);
  567. // incorrect method type - non-static
  568. createJar(new File("some.jar"), new File("Foo"),
  569. "public void main(String[] args){}");
  570. tr = doExec(javaCmd, "-jar", "some.jar");
  571. tr.contains("Error: Main method is not static in class Foo");
  572. if (!tr.testStatus)
  573. System.out.println(tr);
  574. // use classpath to check
  575. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  576. tr.contains("Error: Main method is not static in class Foo");
  577. if (!tr.testStatus)
  578. System.out.println(tr);
  579. // amongst a potpourri of kindred main methods, is the right one chosen ?
  580. createJar(new File("some.jar"), new File("Foo"),
  581. "void main(Object[] args){}",
  582. "int main(Float[] args){return 1;}",
  583. "private void main() {}",
  584. "private static void main(int x) {}",
  585. "public int main(int argc, String[] argv) {return 1;}",
  586. "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
  587. tr = doExec(javaCmd, "-jar", "some.jar");
  588. tr.contains("THE_CHOSEN_ONE");
  589. if (!tr.testStatus)
  590. System.out.println(tr);
  591. // use classpath to check
  592. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  593. tr.contains("THE_CHOSEN_ONE");
  594. if (!tr.testStatus)
  595. System.out.println(tr);
  596. // test for extraneous whitespace in the Main-Class attribute
  597. createJar(" Foo ", new File("some.jar"), new File("Foo"),
  598. "public static void main(String... args){}");
  599. tr = doExec(javaCmd, "-jar", "some.jar");
  600. tr.checkPositive();
  601. if (!tr.testStatus)
  602. System.out.println(tr);
  603. }
  604. /*
  605. * tests 6968053, ie. we turn on the -Xdiag (for now) flag and check if
  606. * the suppressed stack traces are exposed, ignore these tests for localized
  607. * locales, limiting to English only.
  608. */
  609. @Test
  610. void testDiagOptions() throws FileNotFoundException {
  611. if (!isEnglishLocale()) { // only english version
  612. return;
  613. }
  614. TestResult tr = null;
  615. // a missing class
  616. createJar("MIA", new File("some.jar"), new File("Foo"),
  617. (String[])null);
  618. tr = doExec(javaCmd, "-Xdiag", "-jar", "some.jar");
  619. tr.contains("Error: Could not find or load main class MIA");
  620. tr.contains("java.lang.ClassNotFoundException: MIA");
  621. if (!tr.testStatus)
  622. System.out.println(tr);
  623. // use classpath to check
  624. tr = doExec(javaCmd, "-Xdiag", "-cp", "some.jar", "MIA");
  625. tr.contains("Error: Could not find or load main class MIA");
  626. tr.contains("java.lang.ClassNotFoundException: MIA");
  627. if (!tr.testStatus)
  628. System.out.println(tr);
  629. // a missing class on the classpath
  630. tr = doExec(javaCmd, "-Xdiag", "NonExistentClass");
  631. tr.contains("Error: Could not find or load main class NonExistentClass");
  632. tr.contains("java.lang.ClassNotFoundException: NonExistentClass");
  633. if (!tr.testStatus)
  634. System.out.println(tr);
  635. }
  636. @Test
  637. static void testJreRestrictSearchFlag() {
  638. // test both arguments to ensure they exist
  639. TestResult tr = null;
  640. tr = doExec(javaCmd,
  641. "-no-jre-restrict-search", "-version");
  642. tr.checkPositive();
  643. if (!tr.testStatus)
  644. System.out.println(tr);
  645. tr = doExec(javaCmd,
  646. "-jre-restrict-search", "-version");
  647. tr.checkPositive();
  648. if (!tr.testStatus)
  649. System.out.println(tr);
  650. }
  651. /**
  652. * @param args the command line arguments
  653. * @throws java.io.FileNotFoundException
  654. */
  655. public static void main(String[] args) throws Exception {
  656. if (debug) {
  657. System.out.println("Starting Arrrghs tests");
  658. }
  659. Arrrghs a = new Arrrghs();
  660. a.run(args);
  661. if (testExitValue > 0) {
  662. System.out.println("Total of " + testExitValue + " failed");
  663. System.exit(1);
  664. } else {
  665. System.out.println("All tests pass");
  666. }
  667. }
  668. }