001: /*
002: * Copyright 2006-2007, Unitils.org
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: package org.unitils;
017:
018: import junit.framework.AssertionFailedError;
019: import static org.apache.commons.lang.StringUtils.isEmpty;
020: import static org.apache.commons.lang.StringUtils.rightPad;
021: import org.unitils.core.TestListener;
022:
023: import java.lang.reflect.Method;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: /**
028: * Test listener that records all method invocations.
029: */
030: public class TracingTestListener extends TestListener {
031:
032: public static final String BEFORE_ALL = "beforeAll";
033: public static final String BEFORE_TEST_CLASS = "beforeTestClass";
034: public static final String BEFORE_TEST_SET_UP = "beforeTestSetUp";
035: public static final String BEFORE_TEST_METHOD = "beforeTestMethod";
036: public static final String AFTER_TEST_METHOD = "afterTestMethod";
037: public static final String AFTER_TEST_TEAR_DOWN = "afterTestTearDown";
038: public static final String AFTER_TEST_CLASS = "afterTestClass";
039: public static final String AFTER_ALL = "afterAll";
040:
041: public static final String TEST_BEFORE_CLASS = "testBeforeClass";
042: public static final String TEST_SET_UP = "testSetUp";
043: public static final String TEST_METHOD = "testMethod";
044: public static final String TEST_TEAR_DOWN = "testTearDown";
045: public static final String TEST_AFTER_CLASS = "testAfterClass";
046:
047: private static final String TEST = "[Test]";
048: private static final String UNITILS = "[Unitils]";
049:
050: /* List that will contain a string representation of each method call */
051: private List<String> callList;
052:
053: private String exceptionMethod;
054:
055: private boolean throwAssertionFailedError;
056:
057: public TracingTestListener() {
058: this .callList = new ArrayList<String>();
059: }
060:
061: public List<String> getCallList() {
062: return callList;
063: }
064:
065: public String getCallListAsString() {
066: StringBuffer result = new StringBuffer();
067: for (String call : callList) {
068: result.append(call);
069: result.append('\n');
070: }
071: return result.toString();
072: }
073:
074: public void setExceptionMethod(String exceptionMethod,
075: boolean throwAssertionFailedError) {
076: this .exceptionMethod = exceptionMethod;
077: this .throwAssertionFailedError = throwAssertionFailedError;
078: }
079:
080: public void addTestInvocation(String invocation, Object test,
081: String testMethodName) {
082: callList.add(formatString(TEST, invocation, getClassName(test),
083: testMethodName));
084: throwExceptionIfRequested(invocation);
085: }
086:
087: @Override
088: public void beforeAll() {
089: callList.add(formatString(UNITILS, BEFORE_ALL, null, null));
090: throwExceptionIfRequested(BEFORE_ALL);
091: }
092:
093: @Override
094: public void beforeTestClass(Class<?> testClass) {
095: callList.add(formatString(UNITILS, BEFORE_TEST_CLASS,
096: getClassName(testClass), null));
097: throwExceptionIfRequested(BEFORE_TEST_CLASS);
098: }
099:
100: @Override
101: public void beforeTestSetUp(Object testObject) {
102: callList.add(formatString(UNITILS, BEFORE_TEST_SET_UP,
103: getClassName(testObject), null));
104: throwExceptionIfRequested(BEFORE_TEST_SET_UP);
105: }
106:
107: @Override
108: public void beforeTestMethod(Object testObject, Method testMethod) {
109: callList.add(formatString(UNITILS, BEFORE_TEST_METHOD,
110: getClassName(testObject), testMethod.getName()));
111: throwExceptionIfRequested(BEFORE_TEST_METHOD);
112: }
113:
114: @Override
115: public void afterTestMethod(Object testObject, Method testMethod,
116: Throwable throwable) {
117: callList.add(formatString(UNITILS, AFTER_TEST_METHOD,
118: getClassName(testObject), testMethod.getName()));
119: throwExceptionIfRequested(AFTER_TEST_METHOD);
120: }
121:
122: @Override
123: public void afterTestTearDown(Object testObject) {
124: callList.add(formatString(UNITILS, AFTER_TEST_TEAR_DOWN,
125: getClassName(testObject), null));
126: throwExceptionIfRequested(AFTER_TEST_TEAR_DOWN);
127: }
128:
129: @Override
130: public void afterTestClass(Class<?> testClass) {
131: callList.add(formatString(UNITILS, AFTER_TEST_CLASS,
132: getClassName(testClass), null));
133: throwExceptionIfRequested(AFTER_TEST_CLASS);
134: }
135:
136: @Override
137: public void afterAll() {
138: callList.add(formatString(UNITILS, AFTER_ALL, null, null));
139: throwExceptionIfRequested(AFTER_ALL);
140: }
141:
142: private String getClassName(Object object) {
143: if (object == null) {
144: return null;
145: }
146: String className = (object instanceof Class) ? ((Class) object)
147: .getName() : object.getClass().getName();
148: return className.substring(className.lastIndexOf('_') + 1);
149: }
150:
151: private void throwExceptionIfRequested(String exceptionMethod) {
152: if (this .exceptionMethod == null
153: || !this .exceptionMethod.equals(exceptionMethod)) {
154: return;
155: }
156: if (throwAssertionFailedError) {
157: throw new AssertionFailedError(exceptionMethod);
158: }
159: throw new RuntimeException(exceptionMethod);
160: }
161:
162: private String formatString(String type, String invocation,
163: String testClass, String testMethodName) {
164: String result = rightPad(type, 10);
165: result += rightPad(invocation, 17);
166: if (!isEmpty(testClass)) {
167: result += " - " + rightPad(testClass, 10);
168: }
169: return result.trim();
170: }
171: }
|