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.

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 junit.framework.Test;
  13. import junit.framework.TestCase;
  14. import junit.framework.TestSuite;
  15. import junit.extensions.TestSetup;
  16. import java.lang.reflect.Method;
  17. import java.util.Iterator;
  18. import java.util.Map;
  19. import java.util.HashMap;
  20. import java.io.InputStreamReader;
  21. import java.io.FileInputStream;
  22. import org.apache.commons.digester.Digester;
  23. import org.aspectj.tools.ajc.Ajc;
  24. /**
  25. * Autowiring of XML test spec file as JUnit tests.
  26. * <p/>
  27. * Extend this class and implement the getSpecFile and the static suite() method.
  28. * All tests described in the XML spec file will be auto-registered as JUnit tests.
  29. * Any regular test() method will be registered as well.
  30. *
  31. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  32. */
  33. public abstract class AutowiredXMLBasedAjcTestCase extends XMLBasedAjcTestCase {
  34. private Map<String,AjcTest> testMap = new HashMap<String,AjcTest>();
  35. public void addTest(AjcTest test) {
  36. testMap.put(test.getTitle(), test);
  37. }
  38. /*
  39. * Return a map from (String) test title -> AjcTest
  40. */
  41. protected Map<String,AjcTest> getSuiteTests() {
  42. return testMap;
  43. }
  44. public static Test loadSuite(Class<?> testCaseClass) {
  45. TestSuite suite = new TestSuite(testCaseClass.getName());
  46. //suite.addTestSuite(testCaseClass);
  47. // wire the spec file
  48. try {
  49. final AutowiredXMLBasedAjcTestCase wired = (AutowiredXMLBasedAjcTestCase) testCaseClass.newInstance();
  50. System.out.println("LOADING SUITE: " + wired.getSpecFile().getPath());
  51. Digester d = wired.getDigester();
  52. try {
  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. }