KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > compatibility > test > matrix > MatrixTestContainer


1 /*
2  *
3  * JBoss, the OpenSource webOS
4  *
5  * Distributable under LGPL license.
6  * See terms of license at gnu.org.
7  */

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 JavaDoc;
18 import java.util.logging.Logger JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * This class is a container for other tests used for the MatrixTest.
25  * For each version used by the Matrix verification we are containing a bunch of other tests.
26  *
27  * This class uses list of variables defined by the testSuite and they have to be in the System.getProperties()
28  * jbosstest.hometest = Contains where we are loading the testcases
29  * jbosstest.executionlist = A comma based list of .class files. Each file has to begin with ${jbosstest.hometest}
30  * jbosstest.versionname = The name of the version being tested
31  *
32  * @author clebert.suconic@jboss.com
33  */

34 public class MatrixTestContainer extends TestCase
35 {
36     static Logger JavaDoc log = Logger.getLogger("MatrixTestContainer");
37
38     /** Used to similuate tests while renaming its names. */
39     private static class DummyTestCase extends TestCase
40     {
41         DummyTestCase(String JavaDoc name)
42         {
43             super (name);
44         }
45     }
46
47     /** We need this proxy just to inform failures*/
48     private static class TestSuiteProxy extends TestSuite
49     {
50         ArrayList JavaDoc loadFailures;
51         public TestSuiteProxy(ArrayList JavaDoc loadFailures)
52         {
53             this.loadFailures=loadFailures;
54         }
55
56         public void run(TestResult testResult)
57         {
58             Iterator JavaDoc 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 JavaDoc className;
78         Throwable JavaDoc exception;
79
80         public LoadFailure(String JavaDoc className, Throwable JavaDoc exception)
81         {
82             this.className=className;
83             this.exception=exception;
84         }
85     }
86
87     /**
88      * One of the goals of this class also is to keep original classNames into testNames. So, you will realize several proxies existent here to
89      * keep these class names while executing method names.
90      */

91     static class TestProxy extends TestCase
92     {
93         Hashtable JavaDoc hashTests = new Hashtable JavaDoc();
94
95
96
97         public TestProxy(Test testcase, String JavaDoc name)
98         {
99             super(name);
100             this.testcase = testcase;
101         }
102
103         public int countTestCases()
104         {
105             return testcase.countTestCases();
106         }
107
108         /**
109          * Create a dummy test renaming its content
110          * @param test
111          * @return
112          */

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                     // if can't recover the name, don't create a proxy
129
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 JavaDoc 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 JavaDoc clazz) throws Exception JavaDoc
176     {
177         Method JavaDoc method = null;
178         try
179         {
180             method = clazz.getMethod("suite", null);
181         }
182         catch (Exception JavaDoc 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 JavaDoc baseName)
198     {
199         destination.addTest(new TestProxy(source,baseName));
200     }
201
202     public static Test suite()
203     {
204         try
205         {
206             String JavaDoc homedir = (String JavaDoc) System.getProperties().get("jbosstest.hometest");
207
208             String JavaDoc executionList = (String JavaDoc) System.getProperties().get("jbosstest.executionlist");
209
210             String JavaDoc[] tests = executionList.split(",");
211
212             ArrayList JavaDoc loadFailures = new ArrayList JavaDoc();
213
214             TestSuite suite = new TestSuiteProxy(loadFailures);
215
216             for (int classesCount = 0; classesCount < tests.length; classesCount++)
217             {
218                 String JavaDoc 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); // - ".class".length()
224

225                     Class JavaDoc clazz = Class.forName(testName);
226                     Test suiteTmp = createSuite(clazz);
227                     copySuite(suiteTmp, suite, testName + ":");
228                 } catch (Throwable JavaDoc 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 JavaDoc e)
241         {
242             e.printStackTrace();
243             return null;
244         }
245
246
247     }
248
249 }
250
Popular Tags