PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test/tools/launcher/Arrrghs.java

https://bitbucket.org/psandoz/lambda-jdk-pipeline-patches
Java | 757 lines | 507 code | 94 blank | 156 comment | 59 complexity | 9e552c671602ae482d0d79fc14278d79 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0
  1. /*
  2. * Copyright (c) 2007, 2012, 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
  27. * @summary Argument parsing validation.
  28. * @compile -XDignore.symbol.file Arrrghs.java
  29. * @run main 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. java.nio.file.Files.deleteIfExists(batFile.toPath());
  183. createFile(batFile, scratchpad);
  184. TestResult tr = doExec(batFile.getName());
  185. ArrayList<String> expList = new ArrayList<>();
  186. expList.add(javaCmd);
  187. expList.add("-version");
  188. expList.addAll(Arrays.asList(expArgs));
  189. List<String> gotList = new ArrayList<>();
  190. for (String x : tr.testOutput) {
  191. Matcher m = ArgPattern.matcher(x);
  192. if (m.matches()) {
  193. String a[] = x.split("=");
  194. gotList.add(a[a.length - 1].trim());
  195. }
  196. }
  197. if (!gotList.equals(expList)) {
  198. System.out.println(tr);
  199. System.out.println("Expected args:");
  200. System.out.println(expList);
  201. System.out.println("Obtained args:");
  202. System.out.println(gotList);
  203. throw new RuntimeException("Error: args do not match");
  204. }
  205. System.out.println("\'" + inArgs + "\'" + " - Test passed");
  206. }
  207. /*
  208. * This tests general quoting and are specific to Windows, *nixes
  209. * need not worry about this, these have been tested with Windows
  210. * implementation and those that are known to work are used against
  211. * the java implementation. Note that the ProcessBuilder gets in the
  212. * way when testing some of these arguments, therefore we need to
  213. * create and execute a .bat file containing the arguments.
  214. */
  215. @Test
  216. void testArgumentParsing() throws IOException {
  217. if (!isWindows)
  218. return;
  219. // no quotes
  220. checkArgumentParsing("a b c d", "a", "b", "c", "d");
  221. // single quotes
  222. checkArgumentParsing("\"a b c d\"", "a b c d");
  223. //double quotes
  224. checkArgumentParsing("\"\"a b c d\"\"", "a", "b", "c", "d");
  225. // triple quotes
  226. checkArgumentParsing("\"\"\"a b c d\"\"\"", "\"a b c d\"");
  227. // a literal within single quotes
  228. checkArgumentParsing("\"a\"b c d\"e\"", "ab", "c", "de");
  229. // a literal within double quotes
  230. checkArgumentParsing("\"\"a\"b c d\"e\"\"", "ab c de");
  231. // a literal quote
  232. checkArgumentParsing("a\\\"b", "a\"b");
  233. // double back-slash
  234. checkArgumentParsing("\"a b c d\\\\\"", "a b c d\\");
  235. // triple back-slash
  236. checkArgumentParsing("a\\\\\\\"b", "a\\\"b");
  237. // dangling quote
  238. checkArgumentParsing("\"a b c\"\"", "a b c\"");
  239. // expansions of white space separators
  240. checkArgumentParsing("a b", "a", "b");
  241. checkArgumentParsing("a\tb", "a", "b");
  242. checkArgumentParsing("a \t b", "a", "b");
  243. checkArgumentParsing("\"C:\\TEST A\\\\\"", "C:\\TEST A\\");
  244. checkArgumentParsing("\"\"C:\\TEST A\\\\\"\"", "C:\\TEST", "A\\");
  245. // MS Windows tests
  246. // triple back-slash
  247. checkArgumentParsing("a\\\\\\d", "a\\\\\\d");
  248. // triple back-slash in quotes
  249. checkArgumentParsing("\"a\\\\\\d\"", "a\\\\\\d");
  250. // slashes separating characters
  251. checkArgumentParsing("X\\Y\\Z", "X\\Y\\Z");
  252. checkArgumentParsing("\\X\\Y\\Z", "\\X\\Y\\Z");
  253. // literals within dangling quotes, etc.
  254. checkArgumentParsing("\"a b c\" d e", "a b c", "d", "e");
  255. checkArgumentParsing("\"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d");
  256. checkArgumentParsing("a\\\\\\c d\"e f\"g h", "a\\\\\\c", "de fg", "h");
  257. checkArgumentParsing("a\\\\\\\"b c d", "a\\\"b", "c", "d");
  258. checkArgumentParsing("a\\\\\\\\\"g c\" d e", "a\\\\g c", "d", "e");
  259. // treatment of back-slashes
  260. checkArgumentParsing("*\\", "*\\");
  261. checkArgumentParsing("*/", "*/");
  262. checkArgumentParsing(".\\*", ".\\*");
  263. checkArgumentParsing("./*", "./*");
  264. checkArgumentParsing("..\\..\\*", "..\\..\\*");
  265. checkArgumentParsing("../../*", "../../*");
  266. checkArgumentParsing("..\\..\\", "..\\..\\");
  267. checkArgumentParsing("../../", "../../");
  268. }
  269. private void initEmptyDir(File emptyDir) throws IOException {
  270. if (emptyDir.exists()) {
  271. recursiveDelete(emptyDir);
  272. }
  273. emptyDir.mkdir();
  274. }
  275. private void initDirWithJavaFiles(File libDir) throws IOException {
  276. if (libDir.exists()) {
  277. recursiveDelete(libDir);
  278. }
  279. libDir.mkdirs();
  280. ArrayList<String> scratchpad = new ArrayList<>();
  281. scratchpad.add("package lib;");
  282. scratchpad.add("public class Fbo {");
  283. scratchpad.add("public static void main(String... args){Foo.f();}");
  284. scratchpad.add("public static void f(){}");
  285. scratchpad.add("}");
  286. createFile(new File(libDir, "Fbo.java"), scratchpad);
  287. scratchpad.clear();
  288. scratchpad.add("package lib;");
  289. scratchpad.add("public class Foo {");
  290. scratchpad.add("public static void main(String... args){");
  291. scratchpad.add("for (String x : args) {");
  292. scratchpad.add("System.out.println(x);");
  293. scratchpad.add("}");
  294. scratchpad.add("Fbo.f();");
  295. scratchpad.add("}");
  296. scratchpad.add("public static void f(){}");
  297. scratchpad.add("}");
  298. createFile(new File(libDir, "Foo.java"), scratchpad);
  299. }
  300. void checkArgumentWildcard(String inArgs, String... expArgs) throws IOException {
  301. String[] in = {inArgs};
  302. checkArgumentWildcard(in, expArgs);
  303. // now add arbitrary arguments before and after
  304. String[] outInArgs = { "-Q", inArgs, "-R"};
  305. String[] outExpArgs = new String[expArgs.length + 2];
  306. outExpArgs[0] = "-Q";
  307. System.arraycopy(expArgs, 0, outExpArgs, 1, expArgs.length);
  308. outExpArgs[expArgs.length + 1] = "-R";
  309. checkArgumentWildcard(outInArgs, outExpArgs);
  310. }
  311. void checkArgumentWildcard(String[] inArgs, String[] expArgs) throws IOException {
  312. ArrayList<String> argList = new ArrayList<>();
  313. argList.add(javaCmd);
  314. argList.add("-cp");
  315. argList.add("lib" + File.separator + "*");
  316. argList.add("lib.Foo");
  317. argList.addAll(Arrays.asList(inArgs));
  318. String[] cmds = new String[argList.size()];
  319. argList.toArray(cmds);
  320. TestResult tr = doExec(cmds);
  321. if (!tr.isOK()) {
  322. System.out.println(tr);
  323. throw new RuntimeException("Error: classpath single entry wildcard entry");
  324. }
  325. ArrayList<String> expList = new ArrayList<>();
  326. expList.addAll(Arrays.asList(expArgs));
  327. List<String> gotList = new ArrayList<>();
  328. for (String x : tr.testOutput) {
  329. gotList.add(x.trim());
  330. }
  331. if (!gotList.equals(expList)) {
  332. System.out.println(tr);
  333. System.out.println("Expected args:");
  334. System.out.println(expList);
  335. System.out.println("Obtained args:");
  336. System.out.println(gotList);
  337. throw new RuntimeException("Error: args do not match");
  338. }
  339. System.out.print("\'");
  340. for (String x : inArgs) {
  341. System.out.print(x + " ");
  342. }
  343. System.out.println("\'" + " - Test passed");
  344. }
  345. /*
  346. * These tests are not expected to work on *nixes, and are ignored.
  347. */
  348. @Test
  349. void testWildCardArgumentProcessing() throws IOException {
  350. if (!isWindows)
  351. return;
  352. File cwd = new File(".");
  353. File libDir = new File(cwd, "lib");
  354. initDirWithJavaFiles(libDir);
  355. initEmptyDir(new File(cwd, "empty"));
  356. // test if javac (the command) can compile *.java
  357. TestResult tr = doExec(javacCmd, libDir.getName() + File.separator + "*.java");
  358. if (!tr.isOK()) {
  359. System.out.println(tr);
  360. throw new RuntimeException("Error: compiling java wildcards");
  361. }
  362. // use the jar cmd to create jars using the ? wildcard
  363. File jarFoo = new File(libDir, "Foo.jar");
  364. tr = doExec(jarCmd, "cvf", jarFoo.getAbsolutePath(), "lib" + File.separator + "F?o.class");
  365. if (!tr.isOK()) {
  366. System.out.println(tr);
  367. throw new RuntimeException("Error: creating jar with wildcards");
  368. }
  369. // now the litmus test!, this should work
  370. checkArgumentWildcard("a", "a");
  371. // test for basic expansion
  372. checkArgumentWildcard("lib\\F*java", "lib\\Fbo.java", "lib\\Foo.java");
  373. // basic expansion in quotes
  374. checkArgumentWildcard("\"lib\\F*java\"", "lib\\F*java");
  375. checkArgumentWildcard("lib\\**", "lib\\Fbo.class", "lib\\Fbo.java",
  376. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  377. checkArgumentWildcard("lib\\*?", "lib\\Fbo.class", "lib\\Fbo.java",
  378. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  379. checkArgumentWildcard("lib\\?*", "lib\\Fbo.class", "lib\\Fbo.java",
  380. "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
  381. checkArgumentWildcard("lib\\?", "lib\\?");
  382. // test for basic expansion
  383. checkArgumentWildcard("lib\\*java", "lib\\Fbo.java", "lib\\Foo.java");
  384. // basic expansion in quotes
  385. checkArgumentWildcard("\"lib\\*.java\"", "lib\\*.java");
  386. // suffix expansion
  387. checkArgumentWildcard("lib\\*.class", "lib\\Fbo.class", "lib\\Foo.class");
  388. // suffix expansion in quotes
  389. checkArgumentWildcard("\"lib\\*.class\"", "lib\\*.class");
  390. // check for ? expansion now
  391. checkArgumentWildcard("lib\\F?o.java", "lib\\Fbo.java", "lib\\Foo.java");
  392. // check ? in quotes
  393. checkArgumentWildcard("\"lib\\F?o.java\"", "lib\\F?o.java");
  394. // check ? as suffixes
  395. checkArgumentWildcard("lib\\F?o.????", "lib\\Fbo.java", "lib\\Foo.java");
  396. // check ? in a leading role
  397. checkArgumentWildcard("lib\\???.java", "lib\\Fbo.java", "lib\\Foo.java");
  398. checkArgumentWildcard("\"lib\\???.java\"", "lib\\???.java");
  399. // check ? prefixed with -
  400. checkArgumentWildcard("-?", "-?");
  401. // check * prefixed with -
  402. checkArgumentWildcard("-*", "-*");
  403. // check on empty directory
  404. checkArgumentWildcard("empty\\*", "empty\\*");
  405. checkArgumentWildcard("empty\\**", "empty\\**");
  406. checkArgumentWildcard("empty\\?", "empty\\?");
  407. checkArgumentWildcard("empty\\??", "empty\\??");
  408. checkArgumentWildcard("empty\\*?", "empty\\*?");
  409. checkArgumentWildcard("empty\\?*", "empty\\?*");
  410. }
  411. void doArgumentCheck(String inArgs, String... expArgs) {
  412. Map<String, String> env = new HashMap<>();
  413. env.put(JLDEBUG_KEY, "true");
  414. TestResult tr = doExec(env, javaCmd, inArgs);
  415. System.out.println(tr);
  416. int sindex = tr.testOutput.indexOf("Command line args:");
  417. if (sindex < 0) {
  418. System.out.println(tr);
  419. throw new RuntimeException("Error: no output");
  420. }
  421. sindex++; // skip over the tag
  422. List<String> gotList = new ArrayList<>();
  423. for (String x : tr.testOutput.subList(sindex, sindex + expArgs.length)) {
  424. String a[] = x.split("=");
  425. gotList.add(a[a.length - 1].trim());
  426. }
  427. List<String> expList = Arrays.asList(expArgs);
  428. if (!gotList.equals(expList)) {
  429. System.out.println(tr);
  430. System.out.println("Expected args:");
  431. System.out.println(expList);
  432. System.out.println("Obtained args:");
  433. System.out.println(gotList);
  434. throw new RuntimeException("Error: args do not match");
  435. }
  436. }
  437. /*
  438. * These tests are usually run on non-existent targets to check error results
  439. */
  440. @Test
  441. void testBasicErrorMessages() {
  442. // Tests for 5030233
  443. TestResult tr = doExec(javaCmd, "-cp");
  444. tr.checkNegative();
  445. tr.isNotZeroOutput();
  446. if (!tr.testStatus)
  447. System.out.println(tr);
  448. tr = doExec(javaCmd, "-classpath");
  449. tr.checkNegative();
  450. tr.isNotZeroOutput();
  451. if (!tr.testStatus)
  452. System.out.println(tr);
  453. tr = doExec(javaCmd, "-jar");
  454. tr.checkNegative();
  455. tr.isNotZeroOutput();
  456. if (!tr.testStatus)
  457. System.out.println(tr);
  458. tr = doExec(javacCmd, "-cp");
  459. tr.checkNegative();
  460. tr.isNotZeroOutput();
  461. if (!tr.testStatus)
  462. System.out.println(tr);
  463. // Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
  464. tr = doExec(javaCmd, "-X");
  465. tr.checkPositive();
  466. tr.isNotZeroOutput();
  467. if (!tr.testStatus)
  468. System.out.println(tr);
  469. tr = doExec(javaCmd, "-help");
  470. tr.checkPositive();
  471. tr.isNotZeroOutput();
  472. if (!tr.testStatus)
  473. System.out.println(tr);
  474. // 6753938, test for non-negative exit value for an incorrectly formed
  475. // command line, '% java'
  476. tr = doExec(javaCmd);
  477. tr.checkNegative();
  478. tr.isNotZeroOutput();
  479. if (!tr.testStatus)
  480. System.out.println(tr);
  481. // 6753938, test for non-negative exit value for an incorrectly formed
  482. // command line, '% java -Xcomp'
  483. tr = doExec(javaCmd, "-Xcomp");
  484. tr.checkNegative();
  485. tr.isNotZeroOutput();
  486. if (!tr.testStatus)
  487. System.out.println(tr);
  488. // 7151434, test for non-negative exit value for an incorrectly formed
  489. // command line, '% java -jar -W', note the bogus -W
  490. tr = doExec(javaCmd, "-jar", "-W");
  491. tr.checkNegative();
  492. tr.contains("Unrecognized option: -W");
  493. if (!tr.testStatus)
  494. System.out.println(tr);
  495. }
  496. /*
  497. * Tests various dispositions of the main method, these tests are limited
  498. * to English locales as they check for error messages that are localized.
  499. */
  500. @Test
  501. void testMainMethod() throws FileNotFoundException {
  502. if (!isEnglishLocale()) {
  503. return;
  504. }
  505. TestResult tr = null;
  506. // a missing class
  507. createJar("MIA", new File("some.jar"), new File("Foo"),
  508. (String[])null);
  509. tr = doExec(javaCmd, "-jar", "some.jar");
  510. tr.contains("Error: Could not find or load main class MIA");
  511. if (!tr.testStatus)
  512. System.out.println(tr);
  513. // use classpath to check
  514. tr = doExec(javaCmd, "-cp", "some.jar", "MIA");
  515. tr.contains("Error: Could not find or load main class MIA");
  516. if (!tr.testStatus)
  517. System.out.println(tr);
  518. // incorrect method access
  519. createJar(new File("some.jar"), new File("Foo"),
  520. "private static void main(String[] args){}");
  521. tr = doExec(javaCmd, "-jar", "some.jar");
  522. tr.contains("Error: Main method not found in class Foo");
  523. if (!tr.testStatus)
  524. System.out.println(tr);
  525. // use classpath to check
  526. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  527. tr.contains("Error: Main method not found in class Foo");
  528. if (!tr.testStatus)
  529. System.out.println(tr);
  530. // incorrect return type
  531. createJar(new File("some.jar"), new File("Foo"),
  532. "public static int main(String[] args){return 1;}");
  533. tr = doExec(javaCmd, "-jar", "some.jar");
  534. tr.contains("Error: Main method must return a value of type void in class Foo");
  535. if (!tr.testStatus)
  536. System.out.println(tr);
  537. // use classpath to check
  538. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  539. tr.contains("Error: Main method must return a value of type void in class Foo");
  540. if (!tr.testStatus)
  541. System.out.println(tr);
  542. // incorrect parameter type
  543. createJar(new File("some.jar"), new File("Foo"),
  544. "public static void main(Object[] args){}");
  545. tr = doExec(javaCmd, "-jar", "some.jar");
  546. tr.contains("Error: Main method not found in class Foo");
  547. if (!tr.testStatus)
  548. System.out.println(tr);
  549. // use classpath to check
  550. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  551. tr.contains("Error: Main method not found in class Foo");
  552. if (!tr.testStatus)
  553. System.out.println(tr);
  554. // incorrect method type - non-static
  555. createJar(new File("some.jar"), new File("Foo"),
  556. "public void main(String[] args){}");
  557. tr = doExec(javaCmd, "-jar", "some.jar");
  558. tr.contains("Error: Main method is not static in class Foo");
  559. if (!tr.testStatus)
  560. System.out.println(tr);
  561. // use classpath to check
  562. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  563. tr.contains("Error: Main method is not static in class Foo");
  564. if (!tr.testStatus)
  565. System.out.println(tr);
  566. // amongst a potpourri of kindred main methods, is the right one chosen ?
  567. createJar(new File("some.jar"), new File("Foo"),
  568. "void main(Object[] args){}",
  569. "int main(Float[] args){return 1;}",
  570. "private void main() {}",
  571. "private static void main(int x) {}",
  572. "public int main(int argc, String[] argv) {return 1;}",
  573. "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
  574. tr = doExec(javaCmd, "-jar", "some.jar");
  575. tr.contains("THE_CHOSEN_ONE");
  576. if (!tr.testStatus)
  577. System.out.println(tr);
  578. // use classpath to check
  579. tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
  580. tr.contains("THE_CHOSEN_ONE");
  581. if (!tr.testStatus)
  582. System.out.println(tr);
  583. // test for extraneous whitespace in the Main-Class attribute
  584. createJar(" Foo ", new File("some.jar"), new File("Foo"),
  585. "public static void main(String... args){}");
  586. tr = doExec(javaCmd, "-jar", "some.jar");
  587. tr.checkPositive();
  588. if (!tr.testStatus)
  589. System.out.println(tr);
  590. }
  591. /*
  592. * tests 6968053, ie. we turn on the -Xdiag (for now) flag and check if
  593. * the suppressed stack traces are exposed, ignore these tests for localized
  594. * locales, limiting to English only.
  595. */
  596. @Test
  597. void testDiagOptions() throws FileNotFoundException {
  598. if (!isEnglishLocale()) { // only english version
  599. return;
  600. }
  601. TestResult tr = null;
  602. // a missing class
  603. createJar("MIA", new File("some.jar"), new File("Foo"),
  604. (String[])null);
  605. tr = doExec(javaCmd, "-Xdiag", "-jar", "some.jar");
  606. tr.contains("Error: Could not find or load main class MIA");
  607. tr.contains("java.lang.ClassNotFoundException: MIA");
  608. if (!tr.testStatus)
  609. System.out.println(tr);
  610. // use classpath to check
  611. tr = doExec(javaCmd, "-Xdiag", "-cp", "some.jar", "MIA");
  612. tr.contains("Error: Could not find or load main class MIA");
  613. tr.contains("java.lang.ClassNotFoundException: MIA");
  614. if (!tr.testStatus)
  615. System.out.println(tr);
  616. // a missing class on the classpath
  617. tr = doExec(javaCmd, "-Xdiag", "NonExistentClass");
  618. tr.contains("Error: Could not find or load main class NonExistentClass");
  619. tr.contains("java.lang.ClassNotFoundException: NonExistentClass");
  620. if (!tr.testStatus)
  621. System.out.println(tr);
  622. }
  623. @Test
  624. static void testJreRestrictSearchFlag() {
  625. // test both arguments to ensure they exist
  626. TestResult tr = null;
  627. tr = doExec(javaCmd,
  628. "-no-jre-restrict-search", "-version");
  629. tr.checkPositive();
  630. if (!tr.testStatus)
  631. System.out.println(tr);
  632. tr = doExec(javaCmd,
  633. "-jre-restrict-search", "-version");
  634. tr.checkPositive();
  635. if (!tr.testStatus)
  636. System.out.println(tr);
  637. }
  638. /**
  639. * @param args the command line arguments
  640. * @throws java.io.FileNotFoundException
  641. */
  642. public static void main(String[] args) throws Exception {
  643. if (debug) {
  644. System.out.println("Starting Arrrghs tests");
  645. }
  646. Arrrghs a = new Arrrghs();
  647. a.run(args);
  648. if (testExitValue > 0) {
  649. System.out.println("Total of " + testExitValue + " failed");
  650. System.exit(1);
  651. } else {
  652. System.out.println("All tests pass");
  653. }
  654. }
  655. }