KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > NumberedTestCase


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: NumberedTestCase.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test;
46
47 import java.lang.reflect.InvocationTargetException JavaDoc;
48 import java.lang.reflect.Method JavaDoc;
49 import java.util.Iterator JavaDoc;
50
51 import junit.framework.Assert;
52 import junit.framework.Protectable;
53 import junit.framework.Test;
54 import junit.framework.TestResult;
55 /**
56  *
57  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
58  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
59  */

60 public class NumberedTestCase extends Assert implements Test{
61     
62     Method JavaDoc[] testMethods = new Method JavaDoc[]{};
63     protected static final String JavaDoc standardPrefix = "test##_";
64     
65     class MethodComparator implements java.util.Comparator JavaDoc {
66         
67         public int compare(Object JavaDoc o1, Object JavaDoc o2){
68                 Method JavaDoc m1 = (Method JavaDoc)o1;
69                 Method JavaDoc m2 = (Method JavaDoc)o2;
70                 return m1.getName().compareTo(m2.getName());
71         }
72         public boolean eqauls(Object JavaDoc other){
73             if(other instanceof MethodComparator)
74                 return true;
75             else
76                 return false;
77         }
78     }
79                         
80
81     public NumberedTestCase(){
82         try{
83             // Get all methods of the subclass
84
Method JavaDoc[] methods = getClass().getMethods();
85             java.util.TreeSet JavaDoc tm = new java.util.TreeSet JavaDoc(new MethodComparator());
86
87             // Add the ones that start with "test"
88
for (int i=0; i < methods.length; i++){
89                 if (methods[i].getName().startsWith("test")){
90                     tm.add(methods[i]);
91                 }
92             }
93             testMethods = new Method JavaDoc[tm.size()];
94             Iterator JavaDoc orderedMethods = tm.iterator();
95             for(int i=0;orderedMethods.hasNext();i++){
96                 testMethods[i]=(Method JavaDoc)orderedMethods.next();
97             }
98         } catch (Exception JavaDoc e){
99             throw new RuntimeException JavaDoc(e.getMessage());
100         }
101     }
102     
103     protected void setUp() throws Exception JavaDoc{
104     }
105
106     protected void tearDown() throws Exception JavaDoc{
107     }
108     
109     /**
110      * Counts the number of test cases that will be run by this test.
111      */

112     public int countTestCases() {
113         return testMethods.length;
114     }
115     
116     /**
117      * Runs a test and collects its result in a TestResult instance.
118      */

119     public void run(TestResult result) {
120         try{
121             setUp();
122         } catch (Exception JavaDoc e){
123             Test test = new Test(){
124                     public int countTestCases() {
125                         return 0;
126                     }
127                     public void run(TestResult result) {
128                     }
129                     public String JavaDoc toString(){
130                         return name()+".setUp()";
131                     }
132                 };
133         
134             result.addError(test, e);
135             return;
136         }
137         for (int i=0; i < testMethods.length; i++){
138             run(result, testMethods[i]);
139         }
140         try{
141             tearDown();
142         } catch (Exception JavaDoc e){
143             Test test = new Test(){
144                     public int countTestCases() {
145                         return 0;
146                     }
147                     public void run(TestResult result) {
148                     }
149                     public String JavaDoc toString(){
150                         return name()+".tearDown()";
151                     }
152                 };
153         
154             result.addError(test, e);
155             return;
156         }
157     }
158
159     protected void run(final TestResult result, final Method JavaDoc testMethod) {
160         Test test = createTest(testMethod);
161         result.startTest(test);
162         Protectable p= new Protectable() {
163             public void protect() throws Throwable JavaDoc {
164                 runTestMethod(testMethod);
165             }
166         };
167         result.runProtected(test, p);
168         result.endTest(test);
169     }
170     
171
172     protected Test createTest(final Method JavaDoc testMethod){
173         Test test = new Test(){
174             public int countTestCases() {return 1;}
175             public void run(TestResult result) {}
176             public String JavaDoc toString(){
177                 return createTestName(testMethod);
178             }
179         };
180         return test;
181     }
182
183     protected void runTestMethod(Method JavaDoc testMethod) throws Throwable JavaDoc {
184         try {
185             testMethod.invoke(this, new Class JavaDoc[0]);
186         } catch (InvocationTargetException JavaDoc e) {
187             e.fillInStackTrace();
188             throw e.getTargetException();
189         } catch (IllegalAccessException JavaDoc e) {
190             e.fillInStackTrace();
191             throw e;
192         }
193     }
194
195
196     public String JavaDoc toString(){
197         return name();
198     }
199
200     public String JavaDoc name(){
201         return "";
202     }
203     
204     protected String JavaDoc createTestName(Method JavaDoc testMethod){
205         return name() + removePrefix(testMethod.getName());
206     }
207
208     protected static String JavaDoc removePrefix(String JavaDoc name){
209         return removePrefix(standardPrefix, name);
210     }
211     
212     protected static String JavaDoc removePrefix(String JavaDoc prefix, String JavaDoc name){
213         return name.substring(prefix.length());
214     }
215 }
216
217
218
Popular Tags