001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test;
018:
019: import java.lang.reflect.Method;
020: import java.util.Vector;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024:
025: /**
026: * A simple extension to Junit <code>TestCase</code> allowing for
027: * {@link #beforeTestCase()} and {@link #afterTestCase()} callbacks.
028: * <p/>
029: * Note, the callbacks will only work if running the whole test case
030: * and not just one test.
031: *
032: * @author kimchy
033: */
034: public abstract class ExtendedTestCase extends TestCase {
035:
036: private static int testCount = 0;
037:
038: private static int totalTestCount = -1;
039:
040: private static boolean disableAfterTestCase = false;
041:
042: protected ExtendedTestCase() {
043: super ();
044: }
045:
046: protected ExtendedTestCase(String name) {
047: super (name);
048: }
049:
050: public void runBare() throws Throwable {
051: Throwable exception = null;
052: if (totalTestCount == -1) {
053: totalTestCount = countTotalTests();
054: }
055: if (testCount == 0) {
056: beforeTestCase();
057: }
058: testCount++;
059: try {
060: super .runBare();
061: } catch (Throwable running) {
062: exception = running;
063: }
064: if (testCount == totalTestCount) {
065: totalTestCount = -1;
066: testCount = 0;
067: if (!disableAfterTestCase) {
068: try {
069: afterTestCase();
070: } catch (Exception afterTestCase) {
071: if (exception == null)
072: exception = afterTestCase;
073: }
074: } else {
075: disableAfterTestCase = false;
076: }
077: }
078: if (exception != null)
079: throw exception;
080: }
081:
082: protected static void disableAfterTestCase() {
083: disableAfterTestCase = true;
084: }
085:
086: /**
087: * Called before any tests within this test case.
088: *
089: * @throws Exception
090: */
091: protected void beforeTestCase() throws Exception {
092:
093: }
094:
095: /**
096: * Called after all the tests within the test case
097: * have executed.
098: *
099: * @throws Exception
100: */
101: protected void afterTestCase() throws Exception {
102:
103: }
104:
105: private int countTotalTests() {
106: int count = 0;
107: Class super Class = getClass();
108: Vector names = new Vector();
109: while (Test.class.isAssignableFrom(super Class)) {
110: Method[] methods = super Class.getDeclaredMethods();
111: for (int i = 0; i < methods.length; i++) {
112: Method method = methods[i];
113: String name = method.getName();
114: if (names.contains(name))
115: continue;
116: names.addElement(name);
117: if (isTestMethod(method)) {
118: count++;
119: }
120: }
121: super Class = super Class.getSuperclass();
122: }
123: return count;
124: }
125:
126: private boolean isTestMethod(Method m) {
127: String name = m.getName();
128: Class[] parameters = m.getParameterTypes();
129: Class returnType = m.getReturnType();
130: return parameters.length == 0 && name.startsWith("test")
131: && returnType.equals(Void.TYPE);
132: }
133:
134: }
|