Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

XMLBasedAjcTestCase.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-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.InputStreamReader;
  16. import java.io.IOException;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.logging.Logger;
  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. import org.xml.sax.EntityResolver;
  28. import org.xml.sax.InputSource;
  29. import org.xml.sax.SAXException;
  30. import org.xml.sax.Locator;
  31. /**
  32. * Root class for all Test suites that are based on an AspectJ XML test suite
  33. * file. Extends AjcTestCase allowing a mix of programmatic and spec-file
  34. * driven testing. See org.aspectj.systemtest.incremental.IncrementalTests for
  35. * an example of this mixed style.
  36. * <p>The class org.aspectj.testing.MakeTestClass will generate a subclass of
  37. * this class for you, given a suite spec. file as input...</p>
  38. */
  39. public abstract class XMLBasedAjcTestCase extends AjcTestCase {
  40. private static Map testMap = new HashMap();
  41. private static boolean suiteLoaded = false;
  42. private AjcTest currentTest = null;
  43. public XMLBasedAjcTestCase() {
  44. }
  45. /**
  46. * You must define a suite() method in subclasses, and return
  47. * the result of calling this method. (Don't you hate static
  48. * methods in programming models). For example:
  49. * <pre>
  50. * public static Test suite() {
  51. * return XMLBasedAjcTestCase.loadSuite(MyTestCaseClass.class);
  52. * }
  53. * </pre>
  54. * @param testCaseClass
  55. * @return
  56. */
  57. public static Test loadSuite(Class testCaseClass) {
  58. try {
  59. TestSuite suite = new TestSuite(testCaseClass.getName());
  60. suite.addTestSuite(testCaseClass);
  61. TestSetup wrapper = new TestSetup(suite) {
  62. /* (non-Javadoc)
  63. * @see junit.extensions.TestSetup#setUp()
  64. */
  65. protected void setUp() throws Exception {
  66. super.setUp();
  67. suiteLoaded = false;
  68. }
  69. /* (non-Javadoc)
  70. * @see junit.extensions.TestSetup#tearDown()
  71. */
  72. protected void tearDown() throws Exception {
  73. super.tearDown();
  74. suiteLoaded = false;
  75. }
  76. };
  77. return wrapper;
  78. } catch (Throwable e) {
  79. e.printStackTrace();
  80. throw new RuntimeException(e);
  81. }
  82. }
  83. /**
  84. * The file containing the XML specification for the tests.
  85. */
  86. protected abstract File getSpecFile();
  87. /*
  88. * Return a map from (String) test title -> AjcTest
  89. */
  90. private Map getSuiteTests() {
  91. return testMap;
  92. }
  93. /**
  94. * This helper method runs the test with the given title in the
  95. * suite spec file. All tests steps in given ajc-test execute
  96. * in the same sandbox.
  97. */
  98. protected void runTest(String title) {
  99. currentTest = (AjcTest) testMap.get(title);
  100. if (currentTest == null) {
  101. fail("No test '" + title + "' in suite.");
  102. }
  103. ajc.setShouldEmptySandbox(true);
  104. currentTest.runTest(this);
  105. }
  106. /**
  107. * Get the currently executing test. Useful for access to e.g.
  108. * AjcTest.getTitle() etc..
  109. */
  110. protected AjcTest getCurrentTest() {
  111. return currentTest;
  112. }
  113. /**
  114. * For use by the Digester. As the XML document is parsed, it creates instances
  115. * of AjcTest objects, which are added to this TestCase by the Digester by
  116. * calling this method.
  117. */
  118. public void addTest(AjcTest test) {
  119. testMap.put(test.getTitle(),test);
  120. }
  121. /*
  122. * The rules for parsing a suite spec file. The Digester using bean properties to match attributes
  123. * in the XML document to properties in the associated classes, so this simple implementation should
  124. * be very easy to maintain and extend should you ever need to.
  125. */
  126. private Digester getDigester() {
  127. Digester digester = new Digester();/* {
  128. //ALEX
  129. public InputSource resolveEntity(String s, String s1) throws SAXException {
  130. System.out.println("**********XMLBasedAjcTestCase.resolveEntity");
  131. if (s1.indexOf("org.aspectj/tests/") > 0) {
  132. String newS1 = s1.substring(s1.indexOf("org.aspectj/tests/")+"org.aspectj/tests/".length());
  133. return new InputSource("tests/"+newS1);
  134. }
  135. return super.resolveEntity(s, s1);
  136. }
  137. };*/
  138. digester.push(this);
  139. digester.addObjectCreate("suite/ajc-test",AjcTest.class);
  140. digester.addSetProperties("suite/ajc-test");
  141. digester.addSetNext("suite/ajc-test","addTest","org.aspectj.testing.AjcTest");
  142. digester.addObjectCreate("suite/ajc-test/compile",CompileSpec.class);
  143. digester.addSetProperties("suite/ajc-test/compile");
  144. digester.addSetNext("suite/ajc-test/compile","addTestStep","org.aspectj.testing.ITestStep");
  145. digester.addObjectCreate("suite/ajc-test/run",RunSpec.class);
  146. digester.addSetProperties("suite/ajc-test/run","class","classToRun");
  147. digester.addSetNext("suite/ajc-test/run","addTestStep","org.aspectj.testing.ITestStep");
  148. digester.addObjectCreate("*/message",ExpectedMessageSpec.class);
  149. digester.addSetProperties("*/message");
  150. digester.addSetNext("*/message","addExpectedMessage","org.aspectj.testing.ExpectedMessageSpec");
  151. return digester;
  152. }
  153. /* (non-Javadoc)
  154. * @see org.aspectj.tools.ajc.AjcTestCase#setUp()
  155. */
  156. protected void setUp() throws Exception {
  157. super.setUp();
  158. if (!suiteLoaded) {
  159. testMap = new HashMap();
  160. System.out.println("LOADING SUITE: " + getSpecFile().getPath());
  161. Digester d = getDigester();
  162. d.getXMLReader().setEntityResolver(new EntityResolver() {
  163. public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  164. System.out.println("!!!!!!!!!XMLBasedAjcTestCase.resolveEntity");
  165. return null; //To change body of implemented methods use File | Settings | File Templates.
  166. }
  167. });
  168. try {
  169. InputStreamReader isr = new InputStreamReader(new FileInputStream(getSpecFile()));
  170. d.parse(isr);
  171. } catch (Exception ex) {
  172. //ALEX was: but painfull in IntelliJ single module styled config
  173. fail("Unable to load suite " + getSpecFile().getPath() + " : " + ex);
  174. // ALEX is below
  175. // ex.printStackTrace();
  176. // try {
  177. // InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(getSpecFile().getPath().substring(3))));
  178. // d.parse(isr);
  179. // } catch (Exception ex2) {
  180. // ex2.printStackTrace();
  181. // fail("Unable to load suite " + getSpecFile().getPath() + " : " + ex + " : " + ex2);
  182. // }
  183. }
  184. suiteLoaded = true;
  185. }
  186. }
  187. protected long nextIncrement(boolean doWait) {
  188. long time = System.currentTimeMillis();
  189. if (doWait) {
  190. try {
  191. Thread.sleep(1000);
  192. } catch (InterruptedException intEx) {}
  193. }
  194. return time;
  195. }
  196. protected void copyFile(String from, String to) throws Exception {
  197. String dir = getCurrentTest().getDir();
  198. FileUtil.copyFile(new File(dir + File.separator + from),
  199. new File(ajc.getSandboxDirectory(),to));
  200. }
  201. protected void copyFileAndDoIncrementalBuild(String from, String to) throws Exception {
  202. copyFile(from,to);
  203. CompilationResult result = ajc.doIncrementalCompile();
  204. assertNoMessages(result,"Expected clean compile from test '" + getCurrentTest().getTitle() + "'");
  205. }
  206. protected void copyFileAndDoIncrementalBuild(String from, String to, MessageSpec expectedResults) throws Exception {
  207. String dir = getCurrentTest().getDir();
  208. FileUtil.copyFile(new File(dir + File.separator + from),
  209. new File(ajc.getSandboxDirectory(),to));
  210. CompilationResult result = ajc.doIncrementalCompile();
  211. assertMessages(result,"Test '" + getCurrentTest().getTitle() + "' did not produce expected messages",expectedResults);
  212. }
  213. protected void deleteFile(String file) {
  214. new File(ajc.getSandboxDirectory(),file).delete();
  215. }
  216. protected void deleteFileAndDoIncrementalBuild(String file, MessageSpec expectedResult) throws Exception {
  217. deleteFile(file);
  218. CompilationResult result = ajc.doIncrementalCompile();
  219. assertMessages(result,"Test '" + getCurrentTest().getTitle() + "' did not produce expected messages",expectedResult);
  220. }
  221. protected void deleteFileAndDoIncrementalBuild(String file) throws Exception {
  222. deleteFileAndDoIncrementalBuild(file,MessageSpec.EMPTY_MESSAGE_SET);
  223. }
  224. protected void assertAdded(String file) {
  225. assertTrue("File " + file + " should have been added",
  226. new File(ajc.getSandboxDirectory(),file).exists());
  227. }
  228. protected void assertDeleted(String file) {
  229. assertFalse("File " + file + " should have been deleted",
  230. new File(ajc.getSandboxDirectory(),file).exists());
  231. }
  232. protected void assertUpdated(String file, long sinceTime) {
  233. File f = new File(ajc.getSandboxDirectory(),file);
  234. assertTrue("File " + file + " should have been updated",f.lastModified() > sinceTime);
  235. }
  236. }