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.

XMLBasedAjcTestCase.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer,
  11. * ******************************************************************/
  12. package org.aspectj.testing;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FilenameFilter;
  16. import java.io.InputStreamReader;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.Stack;
  20. import junit.extensions.TestSetup;
  21. import junit.framework.Test;
  22. import junit.framework.TestSuite;
  23. import org.apache.commons.digester.Digester;
  24. import org.aspectj.tools.ajc.AjcTestCase;
  25. import org.aspectj.tools.ajc.CompilationResult;
  26. import org.aspectj.util.FileUtil;
  27. /**
  28. * Root class for all Test suites that are based on an AspectJ XML test suite
  29. * file. Extends AjcTestCase allowing a mix of programmatic and spec-file
  30. * driven testing. See org.aspectj.systemtest.incremental.IncrementalTests for
  31. * an example of this mixed style.
  32. * <p>The class org.aspectj.testing.MakeTestClass will generate a subclass of
  33. * this class for you, given a suite spec. file as input...</p>
  34. */
  35. public abstract class XMLBasedAjcTestCase extends AjcTestCase {
  36. private static Map testMap = new HashMap();
  37. private static boolean suiteLoaded = false;
  38. private AjcTest currentTest = null;
  39. private Stack clearTestAfterRun = new Stack();
  40. public XMLBasedAjcTestCase() {
  41. }
  42. /**
  43. * You must define a suite() method in subclasses, and return
  44. * the result of calling this method. (Don't you hate static
  45. * methods in programming models). For example:
  46. * <pre>
  47. * public static Test suite() {
  48. * return XMLBasedAjcTestCase.loadSuite(MyTestCaseClass.class);
  49. * }
  50. * </pre>
  51. * @param testCaseClass
  52. * @return
  53. */
  54. public static Test loadSuite(Class testCaseClass) {
  55. TestSuite suite = new TestSuite(testCaseClass.getName());
  56. suite.addTestSuite(testCaseClass);
  57. TestSetup wrapper = new TestSetup(suite) {
  58. /* (non-Javadoc)
  59. * @see junit.extensions.TestSetup#setUp()
  60. */
  61. protected void setUp() throws Exception {
  62. super.setUp();
  63. suiteLoaded = false;
  64. }
  65. /* (non-Javadoc)
  66. * @see junit.extensions.TestSetup#tearDown()
  67. */
  68. protected void tearDown() throws Exception {
  69. super.tearDown();
  70. suiteLoaded = false;
  71. }
  72. };
  73. return wrapper;
  74. }
  75. /**
  76. * The file containing the XML specification for the tests.
  77. */
  78. protected abstract File getSpecFile();
  79. /*
  80. * Return a map from (String) test title -> AjcTest
  81. */
  82. protected Map getSuiteTests() {
  83. return testMap;
  84. }
  85. /**
  86. * This helper method runs the test with the given title in the
  87. * suite spec file. All tests steps in given ajc-test execute
  88. * in the same sandbox.
  89. */
  90. protected void runTest(String title) {
  91. currentTest = (AjcTest) testMap.get(title);
  92. final boolean clearTest = clearTestAfterRun();
  93. if (currentTest == null) {
  94. if (clearTest) {
  95. System.err.println("test already run: " + title);
  96. return;
  97. } else {
  98. fail("No test '" + title + "' in suite.");
  99. }
  100. }
  101. boolean run = currentTest.runTest(this);
  102. assertTrue("Test not run",run);
  103. if (clearTest) {
  104. testMap.remove(title);
  105. }
  106. }
  107. /**
  108. * Get the currently executing test. Useful for access to e.g.
  109. * AjcTest.getTitle() etc..
  110. */
  111. protected AjcTest getCurrentTest() {
  112. return currentTest;
  113. }
  114. /**
  115. * For use by the Digester. As the XML document is parsed, it creates instances
  116. * of AjcTest objects, which are added to this TestCase by the Digester by
  117. * calling this method.
  118. */
  119. public void addTest(AjcTest test) {
  120. testMap.put(test.getTitle(),test);
  121. }
  122. protected final void pushClearTestAfterRun(boolean val) {
  123. clearTestAfterRun.push(val ? Boolean.FALSE: Boolean.TRUE);
  124. }
  125. protected final boolean popClearTestAfterRun() {
  126. return clearTest(true);
  127. }
  128. protected final boolean clearTestAfterRun() {
  129. return clearTest(false);
  130. }
  131. private boolean clearTest(boolean pop) {
  132. if (clearTestAfterRun.isEmpty()) {
  133. return false;
  134. }
  135. boolean result = ((Boolean) clearTestAfterRun.peek()).booleanValue();
  136. if (pop) {
  137. clearTestAfterRun.pop();
  138. }
  139. return result;
  140. }
  141. /*
  142. * The rules for parsing a suite spec file. The Digester using bean properties to match attributes
  143. * in the XML document to properties in the associated classes, so this simple implementation should
  144. * be very easy to maintain and extend should you ever need to.
  145. */
  146. protected Digester getDigester() {
  147. Digester digester = new Digester();
  148. digester.push(this);
  149. digester.addObjectCreate("suite/ajc-test",AjcTest.class);
  150. digester.addSetProperties("suite/ajc-test");
  151. digester.addSetNext("suite/ajc-test","addTest","org.aspectj.testing.AjcTest");
  152. digester.addObjectCreate("suite/ajc-test/compile",CompileSpec.class);
  153. digester.addSetProperties("suite/ajc-test/compile");
  154. digester.addSetNext("suite/ajc-test/compile","addTestStep","org.aspectj.testing.ITestStep");
  155. digester.addObjectCreate("suite/ajc-test/run",RunSpec.class);
  156. digester.addSetProperties("suite/ajc-test/run","class","classToRun");
  157. digester.addSetProperties("suite/ajc-test/run","ltw","ltwFile");
  158. digester.addSetProperties("suite/ajc-test/run","xlintfile","xlintFile");
  159. digester.addSetProperties("suite/ajc-test/run/stderr","ordered","orderedStderr");
  160. digester.addSetNext("suite/ajc-test/run","addTestStep","org.aspectj.testing.ITestStep");
  161. digester.addObjectCreate("*/message",ExpectedMessageSpec.class);
  162. digester.addSetProperties("*/message");
  163. digester.addSetNext("*/message","addExpectedMessage","org.aspectj.testing.ExpectedMessageSpec");
  164. digester.addObjectCreate("suite/ajc-test/weave",WeaveSpec.class);
  165. digester.addSetProperties("suite/ajc-test/weave");
  166. digester.addSetNext("suite/ajc-test/weave","addTestStep","org.aspectj.testing.ITestStep");
  167. digester.addObjectCreate("suite/ajc-test/ant",AntSpec.class);
  168. digester.addSetProperties("suite/ajc-test/ant");
  169. digester.addSetNext("suite/ajc-test/ant","addTestStep","org.aspectj.testing.ITestStep");
  170. digester.addObjectCreate("suite/ajc-test/ant/stderr",OutputSpec.class);
  171. digester.addSetProperties("suite/ajc-test/ant/stderr");
  172. digester.addSetNext("suite/ajc-test/ant/stderr","addStdErrSpec","org.aspectj.testing.OutputSpec");
  173. digester.addObjectCreate("suite/ajc-test/ant/stdout",OutputSpec.class);
  174. digester.addSetProperties("suite/ajc-test/ant/stdout");
  175. digester.addSetNext("suite/ajc-test/ant/stdout","addStdOutSpec","org.aspectj.testing.OutputSpec");
  176. digester.addObjectCreate("suite/ajc-test/run/stderr",OutputSpec.class);
  177. digester.addSetProperties("suite/ajc-test/run/stderr");
  178. digester.addSetNext("suite/ajc-test/run/stderr","addStdErrSpec","org.aspectj.testing.OutputSpec");
  179. digester.addObjectCreate("suite/ajc-test/run/stdout",OutputSpec.class);
  180. digester.addSetProperties("suite/ajc-test/run/stdout");
  181. digester.addSetNext("suite/ajc-test/run/stdout","addStdOutSpec","org.aspectj.testing.OutputSpec");
  182. digester.addObjectCreate("*/line",OutputLine.class);
  183. digester.addSetProperties("*/line");
  184. digester.addSetNext("*/line","addLine","org.aspectj.testing.OutputLine");
  185. return digester;
  186. }
  187. /* (non-Javadoc)
  188. * @see org.aspectj.tools.ajc.AjcTestCase#setUp()
  189. */
  190. protected void setUp() throws Exception {
  191. super.setUp();
  192. if (!suiteLoaded) {
  193. testMap = new HashMap();
  194. System.out.println("LOADING SUITE: " + getSpecFile().getPath());
  195. Digester d = getDigester();
  196. try {
  197. InputStreamReader isr = new InputStreamReader(new FileInputStream(getSpecFile()));
  198. d.parse(isr);
  199. } catch (Exception ex) {
  200. fail("Unable to load suite " + getSpecFile().getPath() + " : " + ex);
  201. }
  202. suiteLoaded = true;
  203. }
  204. }
  205. protected long nextIncrement(boolean doWait) {
  206. long time = System.currentTimeMillis();
  207. if (doWait) {
  208. try {
  209. Thread.sleep(1000);
  210. } catch (InterruptedException intEx) {}
  211. }
  212. return time;
  213. }
  214. protected void copyFile(String from, String to) throws Exception {
  215. String dir = getCurrentTest().getDir();
  216. FileUtil.copyFile(new File(dir + File.separator + from),
  217. new File(ajc.getSandboxDirectory(),to));
  218. }
  219. protected void copyFileAndDoIncrementalBuild(String from, String to) throws Exception {
  220. copyFile(from,to);
  221. CompilationResult result = ajc.doIncrementalCompile();
  222. assertNoMessages(result,"Expected clean compile from test '" + getCurrentTest().getTitle() + "'");
  223. }
  224. protected void copyFileAndDoIncrementalBuild(String from, String to, MessageSpec expectedResults) throws Exception {
  225. String dir = getCurrentTest().getDir();
  226. FileUtil.copyFile(new File(dir + File.separator + from),
  227. new File(ajc.getSandboxDirectory(),to));
  228. CompilationResult result = ajc.doIncrementalCompile();
  229. assertMessages(result,"Test '" + getCurrentTest().getTitle() + "' did not produce expected messages",expectedResults);
  230. }
  231. protected void deleteFile(String file) {
  232. new File(ajc.getSandboxDirectory(),file).delete();
  233. }
  234. protected void deleteFileAndDoIncrementalBuild(String file, MessageSpec expectedResult) throws Exception {
  235. deleteFile(file);
  236. CompilationResult result = ajc.doIncrementalCompile();
  237. assertMessages(result,"Test '" + getCurrentTest().getTitle() + "' did not produce expected messages",expectedResult);
  238. }
  239. protected void deleteFileAndDoIncrementalBuild(String file) throws Exception {
  240. deleteFileAndDoIncrementalBuild(file,MessageSpec.EMPTY_MESSAGE_SET);
  241. }
  242. protected void assertAdded(String file) {
  243. assertTrue("File " + file + " should have been added",
  244. new File(ajc.getSandboxDirectory(),file).exists());
  245. }
  246. protected void assertDeleted(String file) {
  247. assertFalse("File " + file + " should have been deleted",
  248. new File(ajc.getSandboxDirectory(),file).exists());
  249. }
  250. protected void assertUpdated(String file, long sinceTime) {
  251. File f = new File(ajc.getSandboxDirectory(),file);
  252. assertTrue("File " + file + " should have been updated",f.lastModified() > sinceTime);
  253. }
  254. public static class CountingFilenameFilter implements FilenameFilter {
  255. private String suffix;
  256. private int count;
  257. public CountingFilenameFilter (String s) {
  258. this.suffix = s;
  259. }
  260. public boolean accept(File dir, String name) {
  261. if (name.endsWith(suffix)) count++;
  262. return false;
  263. }
  264. public int getCount() {
  265. return count;
  266. }
  267. }
  268. }