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.

readme-writing-compiler-tests.html 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <html>
  2. <!-- <![CDATA[ putDataHere ]]> -->
  3. <title>Writing tests for the AspectJ compiler
  4. </title>
  5. <body>
  6. <h2>Writing tests for the AspectJ compiler
  7. </h2>
  8. The AspectJ project has a harness
  9. which reads test specification files and run tests.
  10. The tests are usually simple scenarios like
  11. "compile and run" or "compile expecting error",
  12. but may involve multiple files, incremental compilation,
  13. classpath or aspectpath entries, etc.
  14. This document shows how to write tests that can be run
  15. by the harness and suggests some patterns to use in
  16. test code, discussing
  17. <ul>
  18. <li><a href="#simple">Simple test definitions</a></li>
  19. <li><a href="#sourceFiles">Test source files</a></li>
  20. <li><a href="#incremental">Incremental tests</a></li>
  21. <li><a href="#verifying">Verifying test steps</a></li>
  22. <ul>
  23. <li><a href="#messages">Messages</a></li>
  24. <li><a href="#dirchanges">Changes in an output directory</a></li>
  25. <li><a href="#tester">Runtime behavior</a></li>
  26. </ul>
  27. <li><a href="#compilerOptions">Compiler Options</a></li>
  28. <li><a href="#background">Harness background</a></li>
  29. </ul>
  30. Most people just writing a test case need to know only the
  31. information in
  32. <a href="#simple">Simple test definitions</a> and
  33. <a href="#sourceFiles">Test source files</a>.
  34. <p>Related documents:
  35. <ul>
  36. <li>For information on running the harness, see
  37. <a href="readme-tests-module.html">
  38. readme-tests-module.html</a>
  39. </li>
  40. <li>For example test definition files, see
  41. <a href="ajcTests.xml">ajcTests.xml</a> and
  42. <a href="ajcTestsFailing.xml">ajcTestsFailing.xml</a>.
  43. </li>
  44. </ul>
  45. <a name="simple"></a>
  46. <h4>Simple Test definitions</h4>
  47. Test definitions are specified in XML files.
  48. Here is a simple example to compile <code>Main.java</code>
  49. and expect an error on line 10 in a file <code>ajcSample.xml</code>:
  50. <pre>
  51. &lt;!DOCTYPE suite SYSTEM "../tests/ajcTestSuite.dtd">
  52. &lt;suite>
  53. &lt;ajc-test dir="new" title="simple error test">
  54. &lt;compile files="Main.java">
  55. &lt;message kind="error" line="10"/>
  56. &lt;/compile>
  57. &lt;/ajc-test>
  58. &lt;/suite>
  59. </pre>
  60. <p>Here is an example to compile
  61. <code>pack/Aspect.java</code> and
  62. <code>pack2/Main.java</code> and
  63. run the main class:
  64. <pre>
  65. &lt;ajc-test dir="new" title="simple run test">
  66. &lt;compile files="pack/Aspect.java,pack1/Main.java"/>
  67. &lt;run class="pack1.Main"/>
  68. &lt;/ajc-test>
  69. </pre>
  70. The compile and run steps of a given ajc-test share a common
  71. sandbox, so (e.g.,) the run step knows to set its classpath using
  72. the classes directory generated by the compile step.
  73. <p>More complex compilations are discussed in
  74. <a href="#compilerOptions">Compiler Options</a> below.
  75. <a name="sourceFiles"></a>
  76. <h4>Test source files</h4>
  77. The <code>dir</code> attribute in the <code>ajc-test</code>
  78. element specifies a base test directory
  79. relative to the directory of the test specification file.
  80. All paths are specified relative to this base test directory.
  81. E.g., the last example used <code>dir="new"</code> and
  82. presumed the following directory structure:
  83. <pre>
  84. {some dir} # test specification directory
  85. {testDefinition}.xml
  86. new/ # test base directory
  87. pack/Aspect.java
  88. pack2/Main.java
  89. </pre>
  90. Test cases with only one file in the default package can often
  91. share directories (e.g., see the many files in <a href="new">new/</a>),
  92. but usually a test case has its own directory.
  93. <a name="incremental"></a>
  94. <h4>Incremental tests</h4>
  95. Incremental tests are more complex because they involve
  96. updating source files before recompiling.
  97. Here's an example that
  98. <ul>
  99. <li>compiles Main.java,
  100. </li><li>runs it,
  101. </li><li>updates the source to introduce an error,
  102. </li><li>incrementally compile it (and detect the error)
  103. </li><li>updates the source to fix the error,
  104. </li><li>incrementally compile it (successfully), and
  105. </li><li>runs it again.
  106. </li>
  107. </ul>
  108. <pre>
  109. &lt;ajc-test dir="new/incremental1" title="incremental test">
  110. &lt;compile staging="true"
  111. sourceroots="."
  112. options="-incremental" />
  113. &lt;run class="Main"/>
  114. &lt;inc-compile tag="20">
  115. &lt;message kind="error" line="15">
  116. &lt;/inc-compile>
  117. &lt;inc-compile tag="30"/>
  118. &lt;run class="Main"/>
  119. &lt;/ajc-test>
  120. </pre>
  121. To understand what's happening in this test would require
  122. looking at the source directory to see which files are tagged
  123. "20" and "30". But before walking through that, there's a
  124. variation of incremental building for AJDE. (The AJDE wrapper
  125. around the <code>ajc</code> compiler can also be driven by the
  126. test harness.)
  127. <p>
  128. In AJDE, incremental tests also involve the notion of "fresh builds",
  129. i.e., when the test reuses the same compiler and build configuration
  130. but rebuilds from scratch. In that case, there is still the
  131. question of whether source files should be updated; if not,
  132. the tag can have the special value "same". For example, if
  133. the last example had two more lines:
  134. <pre>
  135. ...
  136. &lt;inc-compile tag="30"/>
  137. &lt;run class="Main"/>
  138. &lt;inc-compile fresh="true" tag="same"/>
  139. &lt;run class="Main"/>
  140. &lt;/ajc-test>
  141. </pre>
  142. The test would complete by completely rebuilding the same files
  143. and then running the main class. This option has no effect on
  144. the normal (ajc) compiler, and requires specifying
  145. <code>-ajdeCompiler</code> to the harness or compile step
  146. as an argument.
  147. <p>
  148. To recap the attributes of note for setting up incremental tests:
  149. <ul>
  150. <li><code>compile</code> <code>staging="true"</code>:
  151. Incremental tests require staging, which copies the
  152. test source files to a temporary directory so they can be
  153. updated during the test without changing the actual sources.
  154. </li>
  155. <li><code>compile</code> <code>sourceroots="{..}"</code>:
  156. incremental mode only takes source files in the form of
  157. <code>-sourceroots</code> entries.
  158. </li>
  159. <li><code>compile</code> <code>options="-incremental{,..}"</code>:
  160. Specify the <code>-incremental</code> option to signal
  161. incremental mode to the compiler. If you do not include this,
  162. the harness will still run an incremental test, but the compiler
  163. will not do its additional checking it on input options.
  164. </li>
  165. <li><code>inc-compile</code> <code>tag="{##}"</code>:
  166. The required tag attribute is a suffix identifying
  167. files in the test source directory specifying how the sources should
  168. be changed before that incremental compilation step.
  169. If there is a prefixing suffix "delete", then the file is deleted;
  170. otherwise, the file is copied (with the effect either of updating
  171. an existing file or adding a new file).
  172. If the tag is "same", then no files are changed.
  173. </li>
  174. <li><code>inc-compile</code> <code>fresh="true"</code>:
  175. With the AJDE compiler, you can rebuild the current build
  176. configuration in its entirety. (By contrast, doing another
  177. &lt;compile> task would re-initialize the AJDE compiler.)
  178. This option is ignored unless <code>-ajdeCompiler</code>
  179. is passed to the harness on the command line or to the
  180. immediately preceding &lt;compile> task in the options.
  181. </li>
  182. </ul>
  183. <p>
  184. Now, to get back to the question of
  185. what exactly is happening in an incremental test. To do so,
  186. compare the tags with the files specified in
  187. the test source directory; the tagged files are the updates
  188. for that particular step. (By convention the tags are numeric
  189. and in order, but they need not be.)
  190. For example, here are some sources for the test above:
  191. <pre>
  192. {some dir}
  193. {testDefinition}.xml
  194. new/
  195. incremental1/
  196. DeleteMe.delete.30.java
  197. DeleteMe.java
  198. Main.20.java
  199. Main.30.java
  200. Main.java
  201. NewFile.30.java
  202. </pre>
  203. Comparing this with the test specification, you can see
  204. the harness will run one compile and two re-compiles:
  205. <ol>
  206. <li>Initially compile <code>Main.java</code> and <code>DeleteMe.java</code>
  207. <pre>
  208. &lt;compile staging="true"
  209. files="Main.java,DeleteMe.java"/>
  210. {some dir}
  211. {testDefinition}.xml
  212. new/
  213. incremental1/
  214. ...
  215. DeleteMe.java
  216. ...
  217. Main.java
  218. ...
  219. </pre>
  220. </li>
  221. <li>For incremental tag 20,
  222. update <code>Main.java</code> with the contents of
  223. <code>Main.20.java</code>
  224. and recompile, expecting an error on line 15:
  225. <pre>
  226. &lt;inc-compile tag="20">
  227. &lt;message kind="error" line="15">
  228. &lt;/inc-compile>
  229. {some dir}
  230. {testDefinition}.xml
  231. new/
  232. incremental1/
  233. ...
  234. Main.20.java
  235. ...
  236. </pre></li>
  237. <li>For incremental tag 30,
  238. delete <code>DeleteMe.java</code>,
  239. add <code>NewFile.java</code>,
  240. update <code>Main.java</code> with the contents of
  241. <code>Main.30.java</code>
  242. and recompile with no error or warning messages:
  243. <pre>
  244. &lt;inc-compile tag="30"/>
  245. {some dir}
  246. {testDefinition}.xml
  247. new/
  248. incremental1/
  249. DeleteMe.delete.30.java
  250. ...
  251. Main.30.java
  252. ...
  253. NewFile.30.java
  254. </pre>
  255. </li>
  256. </ol>
  257. <a name="verifying"></a>
  258. <h4>Verifying test steps</h4>
  259. As seen above, two ways to verify that a compile was successful
  260. are to run the corresponding class or check the compiler messages.
  261. More generally, the harness can verify compile/run test steps by detecting the
  262. following things and comparing against expected behavior:
  263. <p>
  264. <table border="1" cellpadding="1">
  265. <tr><th>Detect </th>
  266. <th>Evaluate</th>
  267. </tr>
  268. <tr><td>Exceptions </td>
  269. <td>signal failure </td>
  270. </tr>
  271. <tr><td>Result value </td>
  272. <td>heuristically compare with expected:
  273. compiles not expecting errors are expected
  274. to return a normal result status, and vice-versa.</td>
  275. </tr>
  276. <tr><td>Messages (e.g., compiler warnings and errors)</td>
  277. <td>Compare with expected messages</td>
  278. </tr>
  279. <tr><td>Directory changes (e.g., <code>.class</code> files created) </td>
  280. <td>Compare with expected changes</td>
  281. </tr>
  282. <tr><td>Runtime behavior</td>
  283. <td>Use <code>Tester</code> in test source code
  284. to signal events for comparison with expected events.</td>
  285. </tr>
  286. </table>
  287. <p>
  288. <a name="messages"></a>
  289. <h5>Messages</h5>
  290. In a test definition, a nested <code>message</code> element
  291. specifies a condition on the successful completion of the nesting
  292. ajc-test sub-element. In the earlier example, if
  293. the harness does not detect an error message on line 10 or if
  294. there are unexpected messages, then the compile step will be
  295. reported as failing:
  296. <pre>
  297. &lt;ajc-test dir="new" title="simple error test">
  298. &lt;compile files="Main.java">
  299. &lt;message kind="error" line="10"/>
  300. &lt;/compile>
  301. &lt;/ajc-test>
  302. </pre>
  303. Expected messages can be specified as sub-elements for the three
  304. <code>ajc-test</code> elements
  305. <code>compile</code>,
  306. <code>inc-compile</code>, and
  307. <code>run</code>.
  308. Messages require a kind (error or warning) and a line.
  309. To make specification easier, if an error is specified for a line,
  310. the harness accepts as expected any number of errors on that line.
  311. <p>
  312. Most messages fall into those categories.
  313. However, an IMessage also has a Throwable thrown, a String detail,
  314. and a List of ISourceLocation (essentially, "see also", to point
  315. to other relevant places in the code). The thrown element is not
  316. supported, but you can specify the others:
  317. <pre>
  318. &lt;ajc-test dir="new" title="simple error test">
  319. &lt;compile files="Main.java">
  320. &lt;message
  321. kind="error"
  322. line="10"
  323. file="Main.java"
  324. text="This join point should never happen!"
  325. detail="(Find the declaring code below.)">
  326. &lt;source line="12" file="Main.java"/>
  327. &lt;source line="14" file="Main.java"/>
  328. &lt;message>
  329. &lt;/compile>
  330. &lt;/ajc-test>
  331. </pre>
  332. This compiler-error test specifies a single error message triggered
  333. on line 10 of Main.java, with some text and details and two other
  334. source locations that are relevant, on lines 12 and 14 of the same
  335. file.
  336. <p>
  337. When specifying messages, be sure to provide enough detail that
  338. the harness can distinguish expected messages. For example, if you
  339. only specify the line number, then it will match the message in
  340. any file (if there is more than one). If there are two or more
  341. messages expected on a line, provide enough information
  342. to distinguish them. If you are using text or detail attributes,
  343. do not use one string that is a prefix of the other, since it
  344. will match either message, and the other message might not match.
  345. <p>
  346. The "info" messages are special in that they are normally ignored.
  347. To specify expected "info" messages, you have to list all the
  348. messages the compiler will issue, which can vary depending on
  349. the compiler settings. Use the option <code>^verbose<code> to
  350. force the compiler's <code>-verbose</code> option off.
  351. <p>
  352. By the same token, if you don't specify any extra source locations,
  353. then they will not be checked. If you think it is a bug if they
  354. are issued, then you have to specify one if them. (There is
  355. currently no way to specify that a message has no extra
  356. source locations.)
  357. <a name="dirchanges"></a>
  358. <h5>Changes in an output directory</h5>
  359. As with messages, specifying directory changes as a nested element
  360. operates as a condition on the successful completion of the
  361. nesting element. The harness will check for files added, removed,
  362. updated, or unchanged from a given directory. The directory is
  363. specified explicitly or using a token for the shared classes or
  364. run directory. For even more brevity, the harness supports a
  365. default suffix for the files.
  366. <p>
  367. Directory changes have been used only to validate changes in
  368. the classes directory.
  369. The current harness defaults to using the classes directory,
  370. and when using the classes directory uses <code>.class</code>
  371. as a default suffix.
  372. <p>
  373. Here's an example specification:
  374. <pre>
  375. &lt;ajc-test dir="new/dirchanges-test" title="dir-changes test">
  376. &lt;compile staging="true"
  377. files="Main.java,DeleteMe.java,Unchanged.java"/>
  378. &lt;inc-compile tag="20">
  379. &lt;dir-changes updated="Main"
  380. removed="DeleteMe"
  381. unchanged="Unchanged"/>
  382. &lt;/inc-compile>
  383. &lt;/ajc-test>
  384. </pre>
  385. It checks after a recompile that
  386. <ul>
  387. <li><code>Main.class</code> was updated</li>
  388. <li><code>DeleteMe.class</code> was deleted</li>
  389. <li><code>Unchanged.class</code> was not touched</li>
  390. </ul>
  391. <a name="tester"></a>
  392. <h5>Runtime behavior</h5>
  393. Code that tests aspects often falls into the pattern of comparing
  394. expected and actual events/signals. For example, to prove that
  395. before advice in fact ran before a particular method execution,
  396. you might generated and expect signals corresponding to
  397. <ol>
  398. <li>method-call</li>
  399. <li>before advice execution</li>
  400. <li>method-execution</li>
  401. </ol>
  402. The <code>Tester</code> utility class provides API's for signalling
  403. actual and expecting events and comparing the two.
  404. Typically, events are symbolized and compared as String.
  405. Here's a small sample test case that for the scenario above:
  406. <pre>
  407. import org.aspectj.testing.Tester;
  408. public class Main implements Runnable {
  409. public static void main(String[] args) {
  410. Tester.expectEvent("before advice");
  411. Tester.expectEvent("execute run");
  412. new Main().run();
  413. Tester.checkAllEvents();
  414. }
  415. public void run() {
  416. Tester.event("execute run");
  417. }
  418. }
  419. aspect A {
  420. before () : target(Runnable) && execution(void run()) {
  421. Tester.event("before advice");
  422. }
  423. }
  424. </pre>
  425. If either the advice or the method does not run,
  426. the harness will report a failure.
  427. <p>
  428. <code>Tester</code> also has methods that operate like
  429. JUnit assertions as idioms to detect differences in
  430. expected and actual values, signalling appropriately.
  431. <p>
  432. <code>Tester</code> is at
  433. <a href="../testing-client/src/org/aspectj/testing/Tester.java">
  434. ../testing-client/src/org/aspectj/testing/Tester.java</a>
  435. and is built into
  436. <a href="../lib/tests/testing-client.jar">
  437. ../lib/tests/testing-client.jar</a>
  438. which is included on the classpath by the compile and run steps.
  439. <p>You can write runtime test cases without using Tester;
  440. simply throw some exception from the main thread to signal failure.
  441. <a name="compilerOptions"></a>
  442. <h4>Compiler options</h4>
  443. The harness does not support all of the AspectJ 1.1 compiler options.
  444. Flags are mainly supported through the a comma-delimited list in
  445. the <code>options</code> attribute:
  446. <pre>
  447. &lt;ajc-test dir="new" title="lint test">
  448. &lt;compile files="LintTest.java"
  449. options="-Xlint,-emacssym,-source,1.4">
  450. &lt;message kind="warning" line="22">
  451. &lt;/compile>
  452. </pre>
  453. This should work even for complex single-arg options like
  454. <code>-g:none</code>, but will fail for comma-delimited single-arg options like
  455. <code>-g:lines,vars</code> because the comma delimiters
  456. are ambiguous (yes, a design bug!).
  457. <p>
  458. The <code>compile</code> element has the following attributes
  459. which handle most of the other compiler arguments:
  460. <ul>
  461. <li><code>files</code>: .aj and .java files are treated as source files,
  462. but .jar/zip files are extracted and passed to the compiler
  463. as <code>-injars</code>
  464. and readable directories are passed as <code>-inpath</code>.
  465. </li><li><code>classpath</code>: directories and jar files for the classpath
  466. </li><li><code>aspectpath</code>: binary aspects in jar files
  467. </li><li><code>argfiles</code>: argument list files
  468. </li><li><code>sourceroots</code>: root directories for source files
  469. </li><li><code>xlintfile</code>: override org.aspectj.weaver.XlintDefault.properties
  470. </li>
  471. </ul>
  472. Paths for these are all relative to the test base directory, and
  473. multiple entries are separated with commas.
  474. (Use only one entry for xlintfile.)
  475. <p>
  476. Here is a cooked example that uses all <code>compiler</code> attributes:
  477. <pre>
  478. &lt;ajc-test dir="new" title="attributes test">
  479. &lt;compile files="Main.java,injar.jar,some-directory"
  480. staging="true"
  481. options="-Xlint,-g:none"
  482. argfiles="debug.lst,aspects/test.lst"
  483. aspectpath="jars/requiredAspects.jar"
  484. xlintfile="ignore-all-but-typenotfound.properties"
  485. classpath="providedClassesDir,jars/required.jar"/>
  486. &lt;inc-compile tag="20"/>
  487. &lt;/ajc-test>
  488. </pre>
  489. <h5>Test-only compiler attributes</h5>
  490. The following attributes of the compiler entity dictate harness behavior:
  491. <ul>
  492. <li><u>badInput</u>:
  493. To test invalid input, set the compiler <code>badInput</code> attribute
  494. to "</code>true</code>". This prevents the harness from aborting a test
  495. because a specified input file was not found. (However, there is no way
  496. to specify bad input for a directory in the files attribute intended for
  497. -inpath, because the harness selects readable directories.)
  498. </li>
  499. <li><u>includeClassesDir</u>:
  500. Set this in order to include the output classes directory explicitly
  501. on the classpath.
  502. </li>
  503. <li><u>reuseCompiler</u>:
  504. Set this to re-use a compiler from a previous compiler run.
  505. (This tests statefulness of a compiler across non-incremental runs.)
  506. </li>
  507. </ul>
  508. <h5>Unsupported compiler options</h5>
  509. The harness does not support the following AspectJ compiler
  510. options: <code>-outjar {file}, -log {file}</code>.
  511. (<code>-d {dir}</code> is used but specification is not supported.)
  512. <a name="background"></a>
  513. <h4>Background information on the Harness</h4>
  514. To make the test specifications as terse as possible,
  515. harness components for
  516. <code>inc-compile</code> and <code>run</code> elements
  517. use information set up earlier by <code>compile</code>,
  518. some of which is only implicit.
  519. When a test is run, the harness creates a staging directory
  520. for temporary files and a sandbox component for sharing information
  521. between test components, particularly classpath entries
  522. shared between the compile and run components.
  523. The compile and run components share classpath information
  524. through the sandbox, adding default libraries:
  525. <ul>
  526. <li>Aside from any explicit classpath entries,
  527. <code>compile</code> always includes the jars
  528. <a href="../lib/tests/aspecjrt.jar">
  529. ../lib/tests/aspecjrt.jar</a> and
  530. <a href="../lib/tests/testing-client.jar">
  531. ../lib/tests/testing-client.jar</a>
  532. on the compile classpath.
  533. </li>
  534. <li><code>run</code> sets up its classpath as the compile
  535. classpath plus the compile output (classes) directory
  536. plus any entries on the aspectpath.
  537. </li>
  538. </ul>
  539. The harness provides some more advance behaviors,
  540. which you might see specified in the tests.
  541. For more information, see the API documentation for the harness
  542. (<a href="../testing-drivers/src/org/aspectj/testing/drivers/package.html">
  543. org/aspectj/testing/drivers/package.html</a>).
  544. <ul>
  545. <li><u>option dominance and overriding</u>:
  546. Both in test specifications and on the command line
  547. you can force compiler options on or off.
  548. (Forcing means that, e.g., even if an option is enabled in the
  549. test specification, it can be disabled from the command line.)
  550. These appear in the test specifications as options
  551. with prefixes
  552. '!' or '^' rather than '-' (e.g., '^emacssym' to force
  553. the emacssym option off, even in tests that specify it).
  554. </li>
  555. </ul>
  556. <hr>
  557. <small>last updated March 8, 2004 </small> <!-- CVS variable -->
  558. </body>
  559. </html>