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 29KB

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