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.

AutowiredXMLBasedAjcTestCase.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * initial implementation Alexandre Vasseur
  10. *******************************************************************************/
  11. package org.aspectj.testing;
  12. import java.io.InputStreamReader;
  13. import java.lang.reflect.Method;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16. import java.util.Map;
  17. import org.apache.commons.digester.Digester;
  18. import org.aspectj.tools.ajc.Ajc;
  19. import junit.extensions.TestSetup;
  20. import junit.framework.Test;
  21. import junit.framework.TestCase;
  22. import junit.framework.TestSuite;
  23. /**
  24. * Autowiring of XML test spec file as JUnit tests.
  25. * <p/>
  26. * Extend this class and implement the getSpecFile and the static suite() method.
  27. * All tests described in the XML spec file will be auto-registered as JUnit tests.
  28. * Any regular test() method will be registered as well.
  29. *
  30. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  31. */
  32. public abstract class AutowiredXMLBasedAjcTestCase extends XMLBasedAjcTestCase {
  33. private Map<String,AjcTest> testMap = new HashMap<String,AjcTest>();
  34. public void addTest(AjcTest test) {
  35. testMap.put(test.getTitle(), test);
  36. }
  37. /*
  38. * Return a map from (String) test title -> AjcTest
  39. */
  40. protected Map<String,AjcTest> getSuiteTests() {
  41. return testMap;
  42. }
  43. public static Test loadSuite(Class<?> testCaseClass) {
  44. TestSuite suite = new TestSuite(testCaseClass.getName());
  45. //suite.addTestSuite(testCaseClass);
  46. // wire the spec file
  47. try {
  48. final AutowiredXMLBasedAjcTestCase wired = (AutowiredXMLBasedAjcTestCase) testCaseClass.newInstance();
  49. System.out.println("LOADING SUITE: " + wired.getSpecFile().getPath());
  50. Digester d = wired.getDigester();
  51. try {
  52. InputStreamReader isr = new InputStreamReader(wired.getSpecFile().openStream());
  53. // InputStreamReader isr = new InputStreamReader(new FileInputStream(wired.getSpecFile()));
  54. d.parse(isr);
  55. } catch (Exception ex) {
  56. fail("Unable to load suite " + wired.getSpecFile().getPath() + " : " + ex);
  57. }
  58. wired.ajc = new Ajc();
  59. Map<String,AjcTest> ajTests = wired.getSuiteTests();
  60. for (Iterator<Map.Entry<String,AjcTest>> iterator = ajTests.entrySet().iterator(); iterator.hasNext();) {
  61. final Map.Entry<String,AjcTest> entry = iterator.next();
  62. suite.addTest(
  63. new TestCase(entry.getKey().toString()) {
  64. protected void runTest() {
  65. entry.getValue().runTest(wired);
  66. }
  67. public String getName() {
  68. return entry.getKey();
  69. }
  70. }
  71. );
  72. }
  73. } catch (Throwable t) {
  74. final String message = t.toString();
  75. suite.addTest(
  76. new TestCase("error") {
  77. protected void runTest() {
  78. fail(message);
  79. }
  80. }
  81. );
  82. }
  83. // wire the test methods as well if any
  84. // this simple check avoids failure when no test.. method is found.
  85. // it could be refined to lookup in the hierarchy as well, and excluding private method as JUnit does.
  86. Method[] testMethods = testCaseClass.getDeclaredMethods();
  87. for (int i = 0; i < testMethods.length; i++) {
  88. Method testMethod = testMethods[i];
  89. if (testMethod.getName().startsWith("test")) {
  90. suite.addTestSuite(testCaseClass);
  91. break;
  92. }
  93. }
  94. TestSetup wrapper = new TestSetup(suite) {
  95. /* (non-Javadoc)
  96. * @see junit.extensions.TestSetup#setUp()
  97. */
  98. protected void setUp() throws Exception {
  99. super.setUp();
  100. //suiteLoaded = false;
  101. }
  102. /* (non-Javadoc)
  103. * @see junit.extensions.TestSetup#tearDown()
  104. */
  105. protected void tearDown() throws Exception {
  106. super.tearDown();
  107. //suiteLoaded = false;
  108. }
  109. };
  110. return wrapper;
  111. //return suite;
  112. }
  113. /* (non-Javadoc)
  114. * @see org.aspectj.tools.ajc.AjcTestCase#setUp()
  115. */
  116. protected void setUp() throws Exception {
  117. super.setUp();
  118. }
  119. /**
  120. * This helper method runs the test with the given title in the
  121. * suite spec file. All tests steps in given ajc-test execute
  122. * in the same sandbox.
  123. */
  124. protected void runTest(String title) {
  125. AjcTest currentTest = (AjcTest) testMap.get(title);
  126. if (currentTest == null) {
  127. fail("No test '" + title + "' in suite.");
  128. }
  129. ajc.setShouldEmptySandbox(true);
  130. currentTest.runTest(this);
  131. }
  132. }