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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.util.HashMap;
  17. import java.util.Map;
  18. import junit.extensions.TestSetup;
  19. import junit.framework.Test;
  20. import junit.framework.TestSuite;
  21. import org.apache.commons.digester.Digester;
  22. import org.aspectj.tools.ajc.AjcTestCase;
  23. /**
  24. * Root class for all Test suites that are based on an AspectJ XML test suite
  25. * file. Extends AjcTestCase allowing a mix of programmatic and spec-file
  26. * driven testing. See org.aspectj.systemtest.incremental.IncrementalTests for
  27. * an example of this mixed style.
  28. * <p>The class org.aspectj.testing.MakeTestClass will generate a subclass of
  29. * this class for you, given a suite spec. file as input...</p>
  30. */
  31. public abstract class XMLBasedAjcTestCase extends AjcTestCase {
  32. private static Map testMap = new HashMap();
  33. private static boolean suiteLoaded = false;
  34. private AjcTest currentTest = null;
  35. public XMLBasedAjcTestCase() {
  36. }
  37. /**
  38. * You must define a suite() method in subclasses, and return
  39. * the result of calling this method. (Don't you hate static
  40. * methods in programming models). For example:
  41. * <pre>
  42. * public static Test suite() {
  43. * return XMLBasedAjcTestCase.loadSuite(MyTestCaseClass.class);
  44. * }
  45. * </pre>
  46. * @param testCaseClass
  47. * @return
  48. */
  49. public static Test loadSuite(Class testCaseClass) {
  50. TestSuite suite = new TestSuite(testCaseClass.getName());
  51. suite.addTestSuite(testCaseClass);
  52. TestSetup wrapper = new TestSetup(suite) {
  53. /* (non-Javadoc)
  54. * @see junit.extensions.TestSetup#setUp()
  55. */
  56. protected void setUp() throws Exception {
  57. super.setUp();
  58. suiteLoaded = false;
  59. }
  60. /* (non-Javadoc)
  61. * @see junit.extensions.TestSetup#tearDown()
  62. */
  63. protected void tearDown() throws Exception {
  64. super.tearDown();
  65. suiteLoaded = false;
  66. }
  67. };
  68. return wrapper;
  69. }
  70. /**
  71. * The file containing the XML specification for the tests.
  72. */
  73. protected abstract File getSpecFile();
  74. /*
  75. * Return a map from (String) test title -> AjcTest
  76. */
  77. private Map getSuiteTests() {
  78. return testMap;
  79. }
  80. /**
  81. * This helper method runs the test with the given title in the
  82. * suite spec file. All tests steps in given ajc-test execute
  83. * in the same sandbox.
  84. */
  85. protected void runTest(String title) {
  86. currentTest = (AjcTest) testMap.get(title);
  87. if (currentTest == null) {
  88. fail("No test '" + title + "' in suite.");
  89. }
  90. ajc.setShouldEmptySandbox(true);
  91. currentTest.runTest(this);
  92. }
  93. /**
  94. * Get the currently executing test. Useful for access to e.g.
  95. * AjcTest.getTitle() etc..
  96. */
  97. protected AjcTest getCurrentTest() {
  98. return currentTest;
  99. }
  100. /**
  101. * For use by the Digester. As the XML document is parsed, it creates instances
  102. * of AjcTest objects, which are added to this TestCase by the Digester by
  103. * calling this method.
  104. */
  105. public void addTest(AjcTest test) {
  106. testMap.put(test.getTitle(),test);
  107. }
  108. /*
  109. * The rules for parsing a suite spec file. The Digester using bean properties to match attributes
  110. * in the XML document to properties in the associated classes, so this simple implementation should
  111. * be very easy to maintain and extend should you ever need to.
  112. */
  113. private Digester getDigester() {
  114. Digester digester = new Digester();
  115. digester.push(this);
  116. digester.addObjectCreate("suite/ajc-test",AjcTest.class);
  117. digester.addSetProperties("suite/ajc-test");
  118. digester.addSetNext("suite/ajc-test","addTest","org.aspectj.testing.AjcTest");
  119. digester.addObjectCreate("suite/ajc-test/compile",CompileSpec.class);
  120. digester.addSetProperties("suite/ajc-test/compile");
  121. digester.addSetNext("suite/ajc-test/compile","addTestStep","org.aspectj.testing.ITestStep");
  122. digester.addObjectCreate("suite/ajc-test/run",RunSpec.class);
  123. digester.addSetProperties("suite/ajc-test/run","class","classToRun");
  124. digester.addSetNext("suite/ajc-test/run","addTestStep","org.aspectj.testing.ITestStep");
  125. digester.addObjectCreate("*/message",ExpectedMessageSpec.class);
  126. digester.addSetProperties("*/message");
  127. digester.addSetNext("*/message","addExpectedMessage","org.aspectj.testing.ExpectedMessageSpec");
  128. return digester;
  129. }
  130. /* (non-Javadoc)
  131. * @see org.aspectj.tools.ajc.AjcTestCase#setUp()
  132. */
  133. protected void setUp() throws Exception {
  134. super.setUp();
  135. if (!suiteLoaded) {
  136. testMap = new HashMap();
  137. System.out.println("LOADING SUITE: " + getSpecFile().getPath());
  138. Digester d = getDigester();
  139. try {
  140. InputStreamReader isr = new InputStreamReader(new FileInputStream(getSpecFile()));
  141. d.parse(isr);
  142. } catch (Exception ex) {
  143. fail("Unable to load suite " + getSpecFile().getPath() + " : " + ex);
  144. }
  145. suiteLoaded = true;
  146. }
  147. }
  148. }