001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test;
017:
018: import java.lang.reflect.InvocationTargetException;
019: import java.lang.reflect.Method;
020: import java.util.Iterator;
021:
022: import junit.framework.Assert;
023: import junit.framework.Protectable;
024: import junit.framework.Test;
025: import junit.framework.TestResult;
026:
027: /**
028: *
029: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
030: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
031: */
032: public class NumberedTestCase extends Assert implements Test {
033:
034: Method[] testMethods = new Method[] {};
035: protected static final String standardPrefix = "test##_";
036:
037: class MethodComparator implements java.util.Comparator {
038:
039: public int compare(Object o1, Object o2) {
040: Method m1 = (Method) o1;
041: Method m2 = (Method) o2;
042: return m1.getName().compareTo(m2.getName());
043: }
044:
045: public boolean eqauls(Object other) {
046: if (other instanceof MethodComparator)
047: return true;
048: else
049: return false;
050: }
051: }
052:
053: public NumberedTestCase() {
054: try {
055: // Get all methods of the subclass
056: Method[] methods = getClass().getMethods();
057: java.util.TreeSet tm = new java.util.TreeSet(
058: new MethodComparator());
059:
060: // Add the ones that start with "test"
061: for (int i = 0; i < methods.length; i++) {
062: if (methods[i].getName().startsWith("test")) {
063: tm.add(methods[i]);
064: }
065: }
066: testMethods = new Method[tm.size()];
067: Iterator orderedMethods = tm.iterator();
068: for (int i = 0; orderedMethods.hasNext(); i++) {
069: testMethods[i] = (Method) orderedMethods.next();
070: }
071: } catch (Exception e) {
072: throw new RuntimeException(e.getMessage());
073: }
074: }
075:
076: protected void setUp() throws Exception {
077: }
078:
079: protected void tearDown() throws Exception {
080: }
081:
082: /**
083: * Counts the number of test cases that will be run by this test.
084: */
085: public int countTestCases() {
086: return testMethods.length;
087: }
088:
089: /**
090: * Runs a test and collects its result in a TestResult instance.
091: */
092: public void run(TestResult result) {
093: try {
094: setUp();
095: } catch (Exception e) {
096: e.printStackTrace();
097: Test test = new TestSetup();
098:
099: result.addError(test, e);
100: return;
101: }
102: for (int i = 0; i < testMethods.length; i++) {
103: run(result, testMethods[i]);
104: }
105: try {
106: tearDown();
107: } catch (Exception e) {
108: e.printStackTrace();
109: Test test = new TestTearDown();
110:
111: result.addError(test, e);
112: return;
113: }
114: }
115:
116: protected void run(final TestResult result, final Method testMethod) {
117: Test test = createTest(testMethod);
118: result.startTest(test);
119: Protectable p = new Protectable() {
120: public void protect() throws Throwable {
121: runTestMethod(testMethod);
122: }
123: };
124: result.runProtected(test, p);
125: result.endTest(test);
126: }
127:
128: protected Test createTest(final Method testMethod) {
129: Test test = new NamedTest(testMethod);
130: return test;
131: }
132:
133: protected void runTestMethod(Method testMethod) throws Throwable {
134: try {
135: testMethod.invoke(this , new Class[0]);
136: } catch (InvocationTargetException e) {
137: e.fillInStackTrace();
138: throw e.getTargetException();
139: } catch (IllegalAccessException e) {
140: e.fillInStackTrace();
141: throw e;
142: }
143: }
144:
145: public String toString() {
146: return name();
147: }
148:
149: public String name() {
150: return "";
151: }
152:
153: protected String createTestName(Method testMethod) {
154: return name() + removePrefix(testMethod.getName());
155: }
156:
157: protected static String removePrefix(String name) {
158: return removePrefix(standardPrefix, name);
159: }
160:
161: protected static String removePrefix(String prefix, String name) {
162: return name.substring(prefix.length());
163: }
164:
165: public class NamedTest implements Test {
166: private final Method testMethod;
167:
168: public NamedTest(Method testMethod) {
169: this .testMethod = testMethod;
170: }
171:
172: public String getName() {
173: return createTestName(testMethod);
174: }
175:
176: public int countTestCases() {
177: return 1;
178: }
179:
180: public void run(TestResult result) {
181: }
182:
183: public String toString() {
184: return getName();
185: }
186: }
187:
188: public class TestSetup implements Test {
189: public int countTestCases() {
190: return 0;
191: }
192:
193: public void run(TestResult result) {
194: }
195:
196: public String getName() {
197: return name() + ".setUp()";
198: }
199:
200: public String toString() {
201: return getName();
202: }
203: }
204:
205: public class TestTearDown implements Test {
206: public int countTestCases() {
207: return 0;
208: }
209:
210: public void run(TestResult result) {
211: }
212:
213: public String getName() {
214: return name() + ".tearDown()";
215: }
216:
217: public String toString() {
218: return getName();
219: }
220: }
221: }
|