You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AjcTaskTest.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC)
  4. * 2003 Contributors.
  5. * All rights reserved.
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Common Public License v1.0
  8. * which accompanies this distribution and is available at
  9. * http://www.eclipse.org/legal/cpl-v10.html
  10. *
  11. * Contributors:
  12. * Xerox/PARC initial implementation
  13. * ******************************************************************/
  14. package org.aspectj.tools.ant.taskdefs;
  15. import org.apache.tools.ant.*;
  16. import org.apache.tools.ant.types.Path;
  17. import org.apache.tools.ant.types.selectors.FilenameSelector;
  18. import org.aspectj.bridge.*;
  19. import org.aspectj.util.*;
  20. import java.io.*;
  21. import java.io.File;
  22. import java.util.Arrays;
  23. import java.util.jar.JarEntry;
  24. import java.util.jar.JarFile;
  25. import junit.framework.TestCase;
  26. /**
  27. * Some API tests, but mostly functional tests driving
  28. * the task execute using data in ../taskdefs/testdata.
  29. * This will re-run in forked mode for any nonfailing
  30. * compile if aspectjtools-dist is built into
  31. * ../aj-build/dist/tools/lib/aspectjtools.jar.
  32. */
  33. public class AjcTaskTest extends TestCase {
  34. private static final Class NO_EXCEPTION = null;
  35. private static final String NOFILE = "NOFILE";
  36. private static final File tempDir;
  37. private static final String aspectjtoolsJar;
  38. private static final String testdataDir;
  39. private static final StringBuffer MESSAGES = new StringBuffer();
  40. /** accept writable .class files */
  41. private static FileFilter PICK_CLASS_FILES;
  42. static {
  43. tempDir = new File("IncrementalAjcTaskTest-temp");
  44. String toolsPath = "../aj-build/dist/tools/lib/aspectjtools.jar";
  45. File toolsjar = new File(toolsPath);
  46. if (toolsjar.canRead()) {
  47. aspectjtoolsJar = toolsjar.getAbsolutePath();
  48. } else {
  49. aspectjtoolsJar = null;
  50. String s =
  51. "AjcTaskTest not forking - build aspectjtools-dist to get "
  52. + toolsPath;
  53. System.out.println(s);
  54. }
  55. File dir = new File("../taskdefs/testdata");
  56. if (dir.canRead() && dir.isDirectory()) {
  57. testdataDir = dir.getAbsolutePath();
  58. } else {
  59. testdataDir = null;
  60. }
  61. PICK_CLASS_FILES = new FileFilter() {
  62. public boolean accept(File file) {
  63. return (
  64. (null != file)
  65. && file.isFile()
  66. && file.canWrite()
  67. && file.getPath().endsWith(".class"));
  68. }
  69. };
  70. }
  71. /**
  72. * Check that aspectjtools are found on the classpath,
  73. * reporting any errors to System.err.
  74. *
  75. * Run multiple times with different classpaths.
  76. * This should find variants
  77. * aspectjtools.jar,
  78. * aspectj-tools.jar,
  79. * aspectj-tools-1.1.jar, and
  80. * aspectjtools-1.0.6.jar
  81. * but not
  82. * aspectjrt.jar or
  83. * aspectj/tools.jar.
  84. * XXX use testing aspect to stub out
  85. * <code>System.getProperty("java.class.path")</code>
  86. * @param args a String[], first is expected path, if any
  87. */
  88. public static void main(String[] args) {
  89. java.io.File toolsjar = AjcTask.findAspectjtoolsJar();
  90. if ((null == args) || (0 == args.length)) {
  91. if (null != toolsjar) {
  92. System.err.println("FAIL - not expected: " + toolsjar);
  93. }
  94. } else if ("-help".equals(args[0])) {
  95. System.out.println(
  96. "java "
  97. + AjcTaskTest.class.getName()
  98. + " <expectedPathToAspectjtoolsJar>");
  99. } else if (null == toolsjar) {
  100. System.err.println("FAIL - expected: " + args[0]);
  101. } else {
  102. String path = toolsjar.getAbsolutePath();
  103. if (!path.equals(args[0])) {
  104. System.err.println(
  105. "FAIL - expected: " + args[0] + " actual: " + path);
  106. }
  107. }
  108. }
  109. public static void collectMessage(String s) {
  110. MESSAGES.append(s);
  111. }
  112. private static void deleteTempDir() {
  113. if ((null != tempDir) && tempDir.exists()) {
  114. FileUtil.deleteContents(tempDir);
  115. tempDir.delete();
  116. // when tempDir not used...
  117. if (null != testdataDir) {
  118. File dataDir = new File(testdataDir);
  119. if (dataDir.canRead()) {
  120. FileUtil.deleteContents(dataDir, PICK_CLASS_FILES, false);
  121. }
  122. }
  123. }
  124. }
  125. private static final File getTempDir() {
  126. return tempDir;
  127. }
  128. public AjcTaskTest(String name) {
  129. super(name);
  130. }
  131. public void tearDown() {
  132. deleteTempDir();
  133. MESSAGES.setLength(0);
  134. }
  135. public void testNullDestDir() {
  136. AjcTask task = getTask(NOFILE, null);
  137. String[] cmd = task.makeCommand();
  138. for (int i = 0; i < cmd.length; i++) {
  139. assertTrue(!"-d".equals(cmd[i]));
  140. }
  141. }
  142. public void testOutputRequirement() {
  143. AjcTask task = getTask("default.lst");
  144. checkRun(task, null);
  145. // copyInJars now just emits warning b/c unused
  146. task = getTask("default.lst", null);
  147. task.setCopyInjars(true);
  148. checkRun(task, null);
  149. // sourceRootCopyFilter requires destDir
  150. task = getTask("default.lst", null);
  151. task.setSourceRootCopyFilter("**/*.java");
  152. checkRun(task, "sourceRoot");
  153. }
  154. public void testSourceRootCopyFilter() {
  155. // sourceRootCopyFilter works..
  156. File destDir = getTempDir();
  157. assertTrue(
  158. "unable to create " + destDir,
  159. destDir.canRead() || destDir.mkdirs());
  160. AjcTask task = getTask("sourceroot", destDir);
  161. task.setSourceRootCopyFilter("doNotCopy,**/*.txt");
  162. File file = new File(destDir, "Default.java").getAbsoluteFile();
  163. assertTrue(file + ".canRead() prematurely", !file.canRead());
  164. checkRun(task, null);
  165. // got expected resources
  166. assertTrue(file + ".canRead() failed", file.canRead());
  167. File pack = new File(destDir, "pack");
  168. file = new File(pack, "Pack.java").getAbsoluteFile();
  169. assertTrue(file + ".canRead() failed", file.canRead());
  170. file = new File(pack, "includeme").getAbsoluteFile();
  171. assertTrue(file + ".canRead() failed", file.canRead());
  172. // didn't get unexpected resources
  173. file = new File(pack, "something.txt");
  174. assertTrue(file + ".canRead() passed", !file.canRead());
  175. file = new File(destDir, "doNotCopy");
  176. assertTrue(file + ".canRead() passed", !file.canRead());
  177. file = new File(destDir, "skipTxtFiles.txt");
  178. assertTrue(file + ".canRead() passed", !file.canRead());
  179. }
  180. public void testInpathDirCopyFilter() {
  181. // inpathDirCopyFilter works with output directory
  182. File destDir = getTempDir();
  183. assertTrue(
  184. "unable to create " + destDir,
  185. destDir.canRead() || destDir.mkdirs());
  186. AjcTask task = getTask(NOFILE, destDir);
  187. Project p = task.getProject();
  188. Path indirs = new Path(p);
  189. File dir = new File(testdataDir, "inpathDirs").getAbsoluteFile();
  190. indirs.addExisting(new Path(p, new File(dir, "inpathDirOne").getAbsolutePath()));
  191. indirs.addExisting(new Path(p, new File(dir, "inpathDirTwo").getAbsolutePath()));
  192. task.setInpath(indirs);
  193. task.setInpathDirCopyFilter("doNotCopy,**/*.txt");
  194. File file = new File(destDir, "Default.java").getAbsoluteFile();
  195. assertTrue(file + ".canRead() prematurely", !file.canRead());
  196. checkRun(task, null);
  197. // got expected resources
  198. File pack = new File(destDir, "pack");
  199. file = new File(pack, "includeme").getAbsoluteFile();
  200. assertTrue(file + ".canRead() failed", file.canRead());
  201. file = new File(pack, "Pack.class").getAbsoluteFile();
  202. assertTrue(file + ".canRead() failed", file.canRead());
  203. file = new File(destDir, "copyMe.htm").getAbsoluteFile();
  204. assertTrue(file + ".canRead() failed", file.canRead());
  205. file = new File(destDir, "Default.class").getAbsoluteFile();
  206. assertTrue(file + ".canRead() failed", file.canRead());
  207. // didn't get unexpected resources
  208. file = new File(pack, "something.txt");
  209. assertTrue(file + ".canRead() passed", !file.canRead());
  210. file = new File(destDir, "doNotCopy");
  211. assertTrue(file + ".canRead() passed", !file.canRead());
  212. file = new File(destDir, "skipTxtFiles.txt");
  213. assertTrue(file + ".canRead() passed", !file.canRead());
  214. }
  215. public void testInpathDirCopyFilterWithJar() throws IOException {
  216. // inpathDirCopyFilter works with output jar
  217. File destDir = getTempDir();
  218. assertTrue(
  219. "unable to create " + destDir,
  220. destDir.canRead() || destDir.mkdirs());
  221. AjcTask task = getTask(NOFILE, null);
  222. File destJar = new File(destDir, "testInpathDirCopyFilterWithJar-out.jar");
  223. task.setOutjar(destJar);
  224. Project p = task.getProject();
  225. Path indirs = new Path(p);
  226. File dir = new File(testdataDir, "inpathDirs").getAbsoluteFile();
  227. indirs.addExisting(new Path(p, new File(dir, "inpathDirOne").getAbsolutePath()));
  228. indirs.addExisting(new Path(p, new File(dir, "inpathDirTwo").getAbsolutePath()));
  229. task.setInpath(indirs);
  230. task.setInpathDirCopyFilter("doNotCopy,**/*.txt,**/*.class");
  231. checkRun(task, null);
  232. JarFile jarFile = new JarFile(destJar);
  233. String[] expected = {"copyMe.htm", "pack/includeme",
  234. "pack/Pack.class", "Default.class"};
  235. String[] unexpected = {"doNotCopy", "skipTxtFiles.txt", "pack/something.txt"};
  236. for (int i = 0; i < expected.length; i++) {
  237. JarEntry entry = jarFile.getJarEntry(expected[i]);
  238. assertTrue(expected[i] + " not found", null != entry);
  239. }
  240. for (int i = 0; i < unexpected.length; i++) {
  241. JarEntry entry = jarFile.getJarEntry(unexpected[i]);
  242. assertTrue(unexpected[i] + " found", null == entry);
  243. }
  244. }
  245. public void testInpathDirCopyFilterError() {
  246. // inpathDirCopyFilter fails with no output directory or jar iff specified
  247. AjcTask task = getTask(NOFILE, null);
  248. Project p = task.getProject();
  249. Path indirs = new Path(p);
  250. File dir = new File(testdataDir, "inpathDirs").getAbsoluteFile();
  251. indirs.addExisting(new Path(p, new File(dir, "inpathDirOne").getAbsolutePath()));
  252. indirs.addExisting(new Path(p, new File(dir, "inpathDirTwo").getAbsolutePath()));
  253. task.setInpath(indirs);
  254. task.setInpathDirCopyFilter("doNotCopy,**/*.txt,**/*.class");
  255. // expecting error
  256. checkRun(task, "inpathDirCopyFilter");
  257. }
  258. private void checkRun(AjcTask task, String exceptionString) {
  259. try {
  260. task.execute();
  261. assertTrue(null == exceptionString);
  262. } catch (BuildException e) {
  263. if (null == exceptionString) {
  264. assertTrue("unexpected " + e.getMessage(), false);
  265. } else {
  266. String m = e.getMessage();
  267. if (null == m) {
  268. assertTrue("not " + exceptionString, false);
  269. } else if (-1 == m.indexOf(exceptionString)) {
  270. assertEquals(exceptionString, e.getMessage());
  271. }
  272. }
  273. }
  274. }
  275. public void testCommandEditor() {
  276. String className = VerboseCommandEditor.class.getName();
  277. System.setProperty(AjcTask.COMMAND_EDITOR_NAME, className);
  278. assertEquals(
  279. className,
  280. System.getProperty(AjcTask.COMMAND_EDITOR_NAME));
  281. AjcTask task = getTask(NOFILE);
  282. task.setCommandEditor(new VerboseCommandEditor());
  283. String[] cmd = task.makeCommand();
  284. assertEquals(VerboseCommandEditor.VERBOSE, cmd[0]);
  285. task = getTask(NOFILE);
  286. task.setCommandEditorClass(VerboseCommandEditor.class.getName());
  287. cmd = task.makeCommand();
  288. assertEquals(VerboseCommandEditor.VERBOSE, cmd[0]);
  289. }
  290. // public void testStaticCommandEditor() {
  291. // // XXX need to test COMMAND_EDITOR, but can't require property when run
  292. // }
  293. public void testLimitTo() {
  294. int numArgs = 100;
  295. String arg = "123456789";
  296. String[] args = new String[numArgs];
  297. for (int i = 0; i < args.length; i++) {
  298. args[i] = arg;
  299. }
  300. // no limit
  301. int max = numArgs * (arg.length() + 1);
  302. Location location = new Location("AjcTaskTest.java");
  303. String[] newArgs = AjcTask.GuardedCommand.limitTo(args, max, location);
  304. assertTrue("same", args == newArgs);
  305. // limited - read file and verify arguments
  306. max--;
  307. newArgs = AjcTask.GuardedCommand.limitTo(args, max, location);
  308. assertTrue("not same", args != newArgs);
  309. assertTrue("not null", null != newArgs);
  310. String label = "newArgs " + Arrays.asList(newArgs);
  311. assertTrue("size 2" + label, 2 == newArgs.length);
  312. assertEquals("-argfile", newArgs[0]);
  313. File file = new File(newArgs[1]);
  314. assertTrue("readable newArgs[1]" + label, file.canRead());
  315. FileReader fin = null;
  316. try {
  317. fin = new FileReader(file);
  318. BufferedReader reader = new BufferedReader(fin);
  319. String line;
  320. int i = 0;
  321. while (null != (line = reader.readLine())) {
  322. assertEquals(i + ": ", args[i++], line);
  323. }
  324. assertEquals("num entries", i, args.length);
  325. } catch (IOException e) {
  326. assertTrue("IOException " + e.getMessage(), false);
  327. } finally {
  328. if (null != fin) {
  329. try {
  330. fin.close();
  331. } catch (IOException e) {
  332. // ignore
  333. }
  334. }
  335. file.delete();
  336. }
  337. }
  338. public void testFindAspectjtoolsJar() {
  339. File toolsJar = AjcTask.findAspectjtoolsJar();
  340. if (null != toolsJar) {
  341. assertNull("tools jar found?: " + toolsJar, toolsJar);
  342. }
  343. // not found when unit testing b/c not on system classpath
  344. // so just checking for exceptions.
  345. // XXX need aspect to stub out System.getProperty(..)
  346. }
  347. private void checkContains(String[] cmd, String option, boolean contains) {
  348. for (int i = 0; i < cmd.length; i++) {
  349. if (option.equals(cmd[i])) {
  350. if (contains) {
  351. return;
  352. } else {
  353. assertTrue(
  354. "not expecting " + option + " in " + Arrays.asList(cmd),
  355. false);
  356. }
  357. }
  358. }
  359. if (contains) {
  360. assertTrue(
  361. "expecting " + option + " in " + Arrays.asList(cmd),
  362. false);
  363. }
  364. }
  365. protected AjcTask getTask(String input) {
  366. return getTask(input, getTempDir());
  367. }
  368. protected AjcTask getTask(String input, File destDir) {
  369. AjcTask task = new AjcTask();
  370. Project p = new Project();
  371. task.setProject(p);
  372. if (null != destDir) {
  373. task.setDestdir(destDir);
  374. }
  375. if (NOFILE.equals(input)) {
  376. // add nothing
  377. } else if (input.endsWith(".lst")) {
  378. if (-1 != input.indexOf(",")) {
  379. throw new IllegalArgumentException(
  380. "lists not supported: " + input);
  381. } else if (null == testdataDir) {
  382. throw new Error("testdata not found - run in ../taskdefs");
  383. } else {
  384. String path = testdataDir + File.separator + input;
  385. task.setArgfiles(new Path(task.getProject(), path));
  386. }
  387. } else if ((input.endsWith(".java") || input.endsWith(".aj"))) {
  388. FilenameSelector fns = new FilenameSelector();
  389. fns.setName(input);
  390. task.addFilename(fns);
  391. } else {
  392. String path = testdataDir + File.separator + input;
  393. task.setSourceRoots(new Path(task.getProject(), path));
  394. }
  395. task.setClasspath(new Path(p, "../lib/test/aspectjrt.jar"));
  396. return task;
  397. }
  398. /** used in testMessageHolderClassName */
  399. public static class InfoHolder extends MessageHandler {
  400. public InfoHolder() {
  401. }
  402. public boolean handleMessage(IMessage message) {
  403. if (0 == IMessage.INFO.compareTo(message.getKind())) {
  404. AjcTaskTest.collectMessage(message.getMessage());
  405. }
  406. return true;
  407. }
  408. }
  409. /** used in testMessageHolderClassName */
  410. public static class Holder extends MessageHandler {
  411. public Holder() {
  412. }
  413. public boolean handleMessage(IMessage message) {
  414. IMessage.Kind kind = message.getKind();
  415. if (IMessage.ERROR.isSameOrLessThan(kind)) {
  416. String m = kind.toString();
  417. AjcTaskTest.collectMessage(m.substring(0, 1));
  418. }
  419. return true;
  420. }
  421. }
  422. public void testMessageHolderClassName() {
  423. AjcTask task = getTask("compileError.lst");
  424. task.setFailonerror(false);
  425. MESSAGES.setLength(0);
  426. runTest(
  427. task,
  428. null,
  429. MessageHolderChecker.ONE_ERROR,
  430. Holder.class.getName());
  431. String result = MESSAGES.toString();
  432. MESSAGES.setLength(0);
  433. assertEquals("messages", "e", result);
  434. }
  435. // TODO skipped test - works locally but not on build machine?
  436. public void skip_testMessageHolderClassWithDoneSignal() {
  437. AjcTask task = getTask("default.lst");
  438. task.setFailonerror(false);
  439. String DONE = "This is a unique message, not confused with others.";
  440. task.setXDoneSignal(DONE);
  441. MESSAGES.setLength(0);
  442. runTest(
  443. task,
  444. null,
  445. MessageHolderChecker.INFOS,
  446. InfoHolder.class.getName());
  447. final String result = MESSAGES.toString();
  448. String temp = new String(result);
  449. MESSAGES.setLength(0);
  450. if (!temp.endsWith(DONE)) {
  451. if (temp.length() > 20) {
  452. temp = "..." + temp.substring(temp.length()-20, temp.length());
  453. }
  454. assertTrue(DONE + " is not suffix of \"" + temp + "\"", false);
  455. }
  456. // exactly one such message
  457. temp = new String(result);
  458. temp = temp.substring(0, temp.length()-DONE.length());
  459. if (temp.endsWith(DONE)) {
  460. temp = new String(result);
  461. if (temp.length() > 20) {
  462. temp = "..." + temp.substring(temp.length()-20, temp.length());
  463. }
  464. assertTrue(DONE + " signalled twice: \"" + temp + "\"", false);
  465. }
  466. }
  467. public void testDefaultListForkedNoTools() {
  468. AjcTask task = getTask("default.lst");
  469. task.setFork(true);
  470. boolean passed = false;
  471. try {
  472. runTest(task, BuildException.class, MessageHolderChecker.NONE);
  473. passed = true;
  474. } finally {
  475. if (!passed) {
  476. String m =
  477. "AjcTaskTest.testDefaultListForkedNoTools()"
  478. + " fails if aspectjtools.jar is on the classpath";
  479. System.err.println(m);
  480. }
  481. }
  482. }
  483. public void testDefaultListForkedIncremental() {
  484. AjcTask task = getTask("default.lst");
  485. task.setFork(true);
  486. task.setIncremental(true);
  487. runTest(task, BuildException.class, MessageHolderChecker.NONE);
  488. }
  489. /** failonerror should default to true, unlike other booleans */
  490. public void testCompileErrorFailOnErrorDefault() {
  491. AjcTask task = getTask("compileError.lst");
  492. runTest(task, BuildException.class, MessageHolderChecker.ONE_ERROR);
  493. }
  494. public void testDefaultList() {
  495. AjcTask task = getTask("default.lst");
  496. runTest(task, NO_EXCEPTION, MessageHolderChecker.INFOS);
  497. }
  498. public void testCompileErrorList() {
  499. AjcTask task = getTask("compileError.lst");
  500. task.setFailonerror(false);
  501. runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR);
  502. }
  503. public void testShowWeaveInfo() {
  504. AjcTask task = getTask("showweaveinfo.lst");
  505. task.setShowWeaveInfo(true);
  506. MessageHandler mh = new MessageHandler(false);
  507. mh.dontIgnore(IMessage.WEAVEINFO);
  508. MessageHolderChecker mhc = new MessageHolderChecker(0,0,0,0,MessageHolderChecker.IGNORE);
  509. mhc.weaveinfos = 2; // Expect 2 weaving messages
  510. runTest(task,NO_EXCEPTION,mhc);
  511. mhc.weaveinfos = MessageHolderChecker.IGNORE;
  512. }
  513. public void testCompileWarningList() {
  514. AjcTask task = getTask("compileWarning.lst");
  515. runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_WARNING);
  516. }
  517. public void testNoSuchFileList() {
  518. AjcTask task = getTask("NoSuchFile.lst");
  519. task.setFailonerror(false);
  520. runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR_ONE_ABORT);
  521. }
  522. public void testVersions() {
  523. String[] inputs = AjcTask.TARGET_INPUTS;
  524. for (int i = 0; i < inputs.length; i++) {
  525. AjcTask task = getTask(NOFILE);
  526. task.setTarget(inputs[i]);
  527. String[] cmd = task.makeCommand();
  528. checkContains(cmd, "-target", true);
  529. checkContains(cmd, inputs[i], true);
  530. }
  531. inputs = AjcTask.SOURCE_INPUTS;
  532. for (int i = 0; i < inputs.length; i++) {
  533. AjcTask task = getTask(NOFILE);
  534. task.setSource(inputs[i]);
  535. String[] cmd = task.makeCommand();
  536. checkContains(cmd, "-source", true);
  537. checkContains(cmd, inputs[i], true);
  538. }
  539. inputs = AjcTask.COMPLIANCE_INPUTS;
  540. for (int i = 0; i < inputs.length; i++) {
  541. AjcTask task = getTask(NOFILE);
  542. task.setCompliance(inputs[i]);
  543. String[] cmd = task.makeCommand();
  544. checkContains(cmd, inputs[i], true);
  545. }
  546. }
  547. public void testClasspath() {
  548. AjcTask task = getTask(NOFILE);
  549. String[] cmd = task.makeCommand();
  550. checkContains(cmd, "-bootclasspath", false);
  551. String classpath = null;
  552. for (int i = 0; i < cmd.length; i++) {
  553. if ("-classpath".equals(cmd[i])) {
  554. classpath = cmd[i + 1];
  555. break;
  556. }
  557. }
  558. assertTrue(
  559. "expecting aspectj in classpath",
  560. (-1 != classpath.indexOf("aspectjrt.jar")));
  561. }
  562. // ---------------------------------------- sourcefile
  563. // XXX need to figure out how to specify files directly programmatically
  564. // public void testDefaultFile() {
  565. // AjcTask task = getTask("testdata/Default.java");
  566. // runTest(task, NO_EXCEPTION, MessageHolderChecker.INFOS);
  567. // }
  568. public void testNoFile() {
  569. AjcTask task = getTask(NOFILE);
  570. task.setFailonerror(false);
  571. runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR_ONE_ABORT);
  572. }
  573. public void testCompileErrorFile() {
  574. AjcTask task = getTask("compileError.lst");
  575. task.setFailonerror(false);
  576. runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR);
  577. }
  578. public void testCompileWarningFile() {
  579. AjcTask task = getTask("compileWarning.lst");
  580. task.setFailonerror(false);
  581. runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_WARNING);
  582. }
  583. public void testNoSuchFile() {
  584. AjcTask task = getTask("NoSuchFile.lst");
  585. task.setFailonerror(false);
  586. runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR_ONE_ABORT);
  587. }
  588. public void testDefaultFileComplete() {
  589. AjcTask task = getTask("default.lst");
  590. task.setDebugLevel("none");
  591. task.setDeprecation(true);
  592. task.setFailonerror(false);
  593. task.setNoExit(true); // ok to override Ant?
  594. task.setNoImportError(true);
  595. task.setNowarn(true);
  596. task.setXNoweave(true);
  597. task.setPreserveAllLocals(true);
  598. task.setProceedOnError(true);
  599. task.setReferenceInfo(true);
  600. task.setSource("1.3");
  601. task.setTarget("1.1");
  602. task.setTime(true);
  603. task.setVerbose(true);
  604. task.setXlint("info");
  605. runTest(task, NO_EXCEPTION, MessageHolderChecker.INFOS);
  606. }
  607. public void testXOptions() {
  608. String[] xopts = new String[] {
  609. "serializableAspects",
  610. "lazyTjp",
  611. "reweavable",
  612. "reweavable:compress",
  613. "noInline"
  614. };
  615. for (int i = 0; i < xopts.length; i++) {
  616. AjcTask task = getTask(NOFILE);
  617. task.setX(xopts[i]);
  618. String[] cmd = task.makeCommand();
  619. checkContains(cmd,"-X" + xopts[i],true);
  620. }
  621. }
  622. protected void runTest(
  623. AjcTask task,
  624. Class exceptionType,
  625. MessageHolderChecker checker,
  626. String messageHolderClass) {
  627. task.setMessageHolderClass(messageHolderClass);
  628. runTest(task, exceptionType, checker, (MessageHandler) null);
  629. }
  630. protected void runTest(
  631. AjcTask task,
  632. Class exceptionType,
  633. MessageHolderChecker checker) {
  634. MessageHandler holder = new MessageHandler();
  635. task.setMessageHolder(holder);
  636. runTest(task, exceptionType, checker, holder);
  637. }
  638. protected void runTest(
  639. AjcTask task,
  640. Class exceptionType,
  641. MessageHolderChecker checker,
  642. MessageHandler holder) {
  643. Throwable thrown = null;
  644. // re-run forked iff tools.jar and expect to pass
  645. boolean rerunForked =
  646. ((null != aspectjtoolsJar)
  647. && (null == exceptionType)
  648. && ((null == checker) || !checker.expectFail()));
  649. String label = "same-vm ";
  650. while (true) { // same vm, then perhaps forked
  651. try {
  652. task.execute();
  653. } catch (Throwable t) {
  654. thrown = t;
  655. } finally {
  656. deleteTempDir();
  657. }
  658. if (null == exceptionType) {
  659. if (null != thrown) {
  660. assertTrue(label + "thrown: " + render(thrown), false);
  661. }
  662. } else if (null == thrown) {
  663. assertTrue(
  664. label + "expected " + exceptionType.getName(),
  665. false);
  666. } else if (!(exceptionType.isAssignableFrom(thrown.getClass()))) {
  667. assertTrue(
  668. label
  669. + "expected "
  670. + exceptionType.getName()
  671. + " got "
  672. + render(thrown),
  673. false);
  674. }
  675. if (null != holder) {
  676. if (null == checker) {
  677. checker = MessageHolderChecker.NONE;
  678. }
  679. checker.check(holder, label);
  680. }
  681. if (!rerunForked) {
  682. break;
  683. } else {
  684. label = "other-vm ";
  685. rerunForked = false;
  686. // can't reset without losing values...
  687. task.setFork(true);
  688. task.setFailonerror(true);
  689. task.setForkclasspath(
  690. new Path(task.getProject(), aspectjtoolsJar));
  691. }
  692. }
  693. }
  694. protected String render(Throwable thrown) {
  695. return LangUtil.renderException(thrown);
  696. }
  697. static class MessageHolderChecker { // XXX export to testing-utils
  698. /** use as value to ignore results */
  699. static int IGNORE = Integer.MIN_VALUE;
  700. static MessageHolderChecker NONE =
  701. new MessageHolderChecker(0, 0, 0, 0, 0);
  702. /** any number (0+) of info messages */
  703. static MessageHolderChecker INFOS =
  704. new MessageHolderChecker(0, 0, 0, 0, IGNORE);
  705. /** one error, any number of info messages */
  706. static MessageHolderChecker ONE_ERROR =
  707. new MessageHolderChecker(0, 0, 1, 0, IGNORE);
  708. static MessageHolderChecker ONE_ERROR_ONE_ABORT =
  709. new MessageHolderChecker(1, 0, 1, 0, IGNORE);
  710. /** one warning, any number of info messages */
  711. static MessageHolderChecker ONE_WARNING =
  712. new MessageHolderChecker(0, 0, 0, 1, IGNORE);
  713. int aborts, fails, errors, warnings, infos;
  714. int weaveinfos;
  715. public MessageHolderChecker(
  716. int aborts,
  717. int fails,
  718. int errors,
  719. int warnings,
  720. int infos) {
  721. this.aborts = aborts;
  722. this.fails = fails;
  723. this.errors = errors;
  724. this.warnings = warnings;
  725. this.infos = infos;
  726. this.weaveinfos = IGNORE;
  727. }
  728. public boolean expectFail() {
  729. return (0 < (aborts + fails + errors));
  730. }
  731. public void check(IMessageHolder holder, String label) {
  732. boolean failed = true;
  733. try {
  734. check(holder, aborts, IMessage.ABORT);
  735. check(holder, fails, IMessage.FAIL);
  736. check(holder, errors, IMessage.ERROR);
  737. check(holder, warnings, IMessage.WARNING);
  738. check(holder, infos, IMessage.INFO);
  739. check(holder, weaveinfos, IMessage.WEAVEINFO);
  740. failed = false;
  741. } finally {
  742. if (failed) {
  743. MessageUtil.print(System.err, holder, label + "failed?");
  744. }
  745. }
  746. }
  747. private void check(
  748. IMessageHolder holder,
  749. int num,
  750. IMessage.Kind kind) {
  751. if (num != IGNORE) {
  752. int actual = holder.numMessages(kind, false);
  753. if (num != actual) {
  754. if (actual > 0) {
  755. MessageUtil.print(
  756. System.err,
  757. holder,
  758. kind + " expected " + num + " got " + actual);
  759. }
  760. assertEquals(kind.toString(), num, actual);
  761. }
  762. }
  763. }
  764. }
  765. }
  766. class SnoopingCommandEditor implements ICommandEditor {
  767. private static final String[] NONE = new String[0];
  768. String[] lastCommand;
  769. public String[] editCommand(String[] command) {
  770. lastCommand = (String[]) LangUtil.safeCopy(command, NONE);
  771. return command;
  772. }
  773. public String[] lastCommand() {
  774. return (String[]) LangUtil.safeCopy(lastCommand, NONE);
  775. }
  776. }
  777. class VerboseCommandEditor implements ICommandEditor {
  778. public static final String VERBOSE = "-verbose";
  779. public String[] editCommand(String[] command) {
  780. for (int i = 0; i < command.length; i++) {
  781. if (VERBOSE.equals(command[i])) {
  782. return command;
  783. }
  784. }
  785. String[] result = new String[1 + command.length];
  786. result[0] = VERBOSE;
  787. System.arraycopy(result, 1, command, 0, command.length);
  788. return result;
  789. }
  790. }
  791. class AppendingCommandEditor implements ICommandEditor {
  792. private static String[] NONE = new String[0];
  793. public static ICommandEditor VERBOSE =
  794. new AppendingCommandEditor(new String[] { "-verbose" }, NONE);
  795. public static ICommandEditor INVALID =
  796. new AppendingCommandEditor(NONE, new String[] { "-invalidOption" });
  797. final String[] prefix;
  798. final String[] suffix;
  799. public AppendingCommandEditor(String[] prefix, String[] suffix) {
  800. this.prefix = prefix;
  801. this.suffix = suffix;
  802. }
  803. public String[] editCommand(String[] command) {
  804. int len = command.length + prefix.length + suffix.length;
  805. String[] result = new String[len];
  806. System.arraycopy(result, 0, prefix, 0, prefix.length);
  807. System.arraycopy(result, prefix.length, command, 0, command.length);
  808. System.arraycopy(
  809. result,
  810. prefix.length + command.length,
  811. suffix,
  812. 0,
  813. suffix.length);
  814. return result;
  815. }
  816. }