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

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