1 8 package org.jboss.test.compatibility.test.matrix; 9 10 import junit.framework.AssertionFailedError; 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestListener; 14 import junit.framework.TestResult; 15 import junit.framework.TestSuite; 16 17 import java.lang.reflect.Method ; 18 import java.util.logging.Logger ; 19 import java.util.Hashtable ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 23 34 public class MatrixTestContainer extends TestCase 35 { 36 static Logger log = Logger.getLogger("MatrixTestContainer"); 37 38 39 private static class DummyTestCase extends TestCase 40 { 41 DummyTestCase(String name) 42 { 43 super (name); 44 } 45 } 46 47 48 private static class TestSuiteProxy extends TestSuite 49 { 50 ArrayList loadFailures; 51 public TestSuiteProxy(ArrayList loadFailures) 52 { 53 this.loadFailures=loadFailures; 54 } 55 56 public void run(TestResult testResult) 57 { 58 Iterator iter = loadFailures.iterator(); 59 while (iter.hasNext()) 60 { 61 LoadFailure load = (LoadFailure)iter.next(); 62 TestCase test = new DummyTestCase(load.className); 63 testResult.startTest(test); 64 testResult.addError(test,load.exception); 65 } 66 67 loadFailures.clear(); 68 69 super.run(testResult); 70 } 71 72 73 } 74 75 private static class LoadFailure 76 { 77 String className; 78 Throwable exception; 79 80 public LoadFailure(String className, Throwable exception) 81 { 82 this.className=className; 83 this.exception=exception; 84 } 85 } 86 87 91 static class TestProxy extends TestCase 92 { 93 Hashtable hashTests = new Hashtable (); 94 95 96 97 public TestProxy(Test testcase, String name) 98 { 99 super(name); 100 this.testcase = testcase; 101 } 102 103 public int countTestCases() 104 { 105 return testcase.countTestCases(); 106 } 107 108 113 private Test createDummyTest(Test test) 114 { 115 Test dummyTest = (Test)hashTests.get(test); 116 if (dummyTest==null) 117 { 118 if (test instanceof TestCase) 119 { 120 dummyTest = new DummyTestCase(this.getName() + ":"+ ((TestCase)test).getName()); 121 } else 122 if (test instanceof TestSuite) 123 { 124 dummyTest = new DummyTestCase(this.getName() + ":"+ ((TestCase)test).getName()); 125 } 126 else 127 { 128 log.warning("Couldn't find a name for " + test.toString() + ", class=" + test.getClass().getName()); 130 131 dummyTest = new DummyTestCase(test.getClass().getName()); 132 } 133 134 hashTests.put(test,dummyTest); 135 } 136 137 return dummyTest; 138 } 139 140 public void run(final TestResult result) 141 { 142 TestResult subResult = new TestResult(); 143 subResult.addListener(new TestListener() 144 { 145 public void addError(Test subtest, Throwable throwable) 146 { 147 Test dummyTest = createDummyTest(subtest); 148 result.addError(dummyTest, throwable); 149 } 150 151 public void addFailure(Test subtest, AssertionFailedError assertionFailedError) 152 { 153 Test dummyTest = createDummyTest(subtest); 154 result.addFailure(dummyTest, assertionFailedError); 155 } 156 157 public void endTest(Test subtest) 158 { 159 Test dummyTest = createDummyTest(subtest); 160 result.endTest(dummyTest); 161 } 162 163 public void startTest(Test subtest) 164 { 165 Test dummyTest = createDummyTest(subtest); 166 result.startTest(dummyTest); 167 } 168 }); 169 testcase.run(subResult); 170 } 171 172 Test testcase; 173 } 174 175 private static Test createSuite(Class clazz) throws Exception 176 { 177 Method method = null; 178 try 179 { 180 method = clazz.getMethod("suite", null); 181 } 182 catch (Exception e) 183 { 184 } 185 186 if (method != null) 187 { 188 return (Test) method.invoke(null, null); 189 } else 190 { 191 TestSuite suiteTmp = new TestSuite(); 192 suiteTmp.addTestSuite(clazz); 193 return suiteTmp; 194 } 195 } 196 197 private static void copySuite(Test source, TestSuite destination, String baseName) 198 { 199 destination.addTest(new TestProxy(source,baseName)); 200 } 201 202 public static Test suite() 203 { 204 try 205 { 206 String homedir = (String ) System.getProperties().get("jbosstest.hometest"); 207 208 String executionList = (String ) System.getProperties().get("jbosstest.executionlist"); 209 210 String [] tests = executionList.split(","); 211 212 ArrayList loadFailures = new ArrayList (); 213 214 TestSuite suite = new TestSuiteProxy(loadFailures); 215 216 for (int classesCount = 0; classesCount < tests.length; classesCount++) 217 { 218 String testName = null; 219 try 220 { 221 testName = tests[classesCount].substring(homedir.length() + 1); 222 testName = testName.replace('/', '.'); 223 testName = testName.substring(0, testName.length() - 6); 225 Class clazz = Class.forName(testName); 226 Test suiteTmp = createSuite(clazz); 227 copySuite(suiteTmp, suite, testName + ":"); 228 } catch (Throwable e) 229 { 230 loadFailures.add(new LoadFailure(testName,e)); 231 log.info("Error Loading " + testName); 232 e.printStackTrace(); 233 log.warning(e.getMessage()); 234 } 235 } 236 237 log.info("All classes loaded, executing tests"); 238 239 return suite; 240 } catch (Exception e) 241 { 242 e.printStackTrace(); 243 return null; 244 } 245 246 247 } 248 249 } 250 | Popular Tags |