001: /*
002: * @(#)JUnitTestListener.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.autodoc.v1.testserver.junit;
030:
031: import junit.framework.Test;
032: import junit.framework.TestListener;
033: import junit.framework.AssertionFailedError;
034:
035: import org.apache.log4j.Logger;
036:
037: import net.sourceforge.groboutils.autodoc.v1.testserver.MonitorFinder;
038: import net.sourceforge.groboutils.autodoc.v1.testserver.TestCorrelate;
039: import net.sourceforge.groboutils.autodoc.v1.testserver.TestData;
040: import net.sourceforge.groboutils.autodoc.v1.testserver.TestInfo;
041:
042: /**
043: * A helper class to interface between the test framework and JUnit's
044: * <tt>TestListener</tt> interface.
045: *
046: *
047: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
048: * @since March 17, 2002
049: * @version $Date: 2003/06/10 01:08:41 $
050: */
051: public abstract class JUnitTestListener extends TestCorrelate implements
052: TestListener {
053: private static final Logger LOG = Logger
054: .getLogger(JUnitTestListener.class);
055:
056: private MonitorFinder finder;
057:
058: /**
059: *
060: */
061: public JUnitTestListener(MonitorFinder finder) {
062: super (null, finder);
063: }
064:
065: //-------------------------------------------------------------------------
066: // abstract methods
067:
068: /**
069: * Called before the test starts, and after the test has been registered.
070: */
071: protected abstract void startTest(TestData data);
072:
073: /**
074: * Called after the test ends. The implemented method must not send off
075: * the data - that is done by the JUnitTestListener implementation.
076: */
077: protected abstract void endTest(TestData data);
078:
079: /**
080: * Called when the test in the data has an unexpected exception thrown.
081: */
082: protected abstract void addError(TestData data, Throwable t);
083:
084: /**
085: * Called when the test in the data has an assertion error.
086: */
087: protected abstract void addFailure(TestData data,
088: AssertionFailedError t);
089:
090: //-------------------------------------------------------------------------
091: // JUnit listener methods
092:
093: /**
094: * An error occurred.
095: */
096: public void addError(Test test, Throwable t) {
097: if (test == null) {
098: LOG.error("JUnit passed null test to method addError()", t);
099: return;
100: }
101:
102: TestData td = getTestData(createTestInfo(test));
103: // null means the test was never registered.
104: if (td != null) {
105: addError(td, t);
106: } else {
107: LOG
108: .warn(
109: "JUnit called 'addError' without calling 'startTest'.",
110: t);
111: }
112: }
113:
114: /**
115: * A failure occurred.
116: */
117: public void addFailure(Test test, AssertionFailedError t) {
118: if (test == null) {
119: LOG.error("JUnit passed null test to method addFailure()",
120: t);
121: return;
122: }
123:
124: TestData td = getTestData(createTestInfo(test));
125: // null means the test was never registered.
126: if (td != null) {
127: addFailure(td, t);
128: } else {
129: LOG
130: .warn(
131: "JUnit called 'addFailure' without calling 'startTest'.",
132: t);
133: }
134: }
135:
136: /**
137: * A test ended.
138: */
139: public void endTest(Test test) {
140: TestInfo ti = createTestInfo(test);
141: TestData td = getTestData(ti);
142: if (td != null) {
143: endTest(td);
144:
145: // end off the test info
146: try {
147: getFinder().getMonitor().sendTestData(ti);
148: } catch (IllegalStateException ise) {
149: LOG.warn("Warning for test [" + test + "]: "
150: + ise.getMessage(), ise);
151: }
152: } else {
153: LOG.warn("Received an end message for test [" + test
154: + "], but it was never added.");
155: }
156: }
157:
158: /**
159: * A test started.
160: */
161: public void startTest(Test test) {
162: TestInfo ti = createTestInfo(test);
163:
164: // start the test info
165: getFinder().getMonitor().addTestData(ti);
166:
167: startTest(getTestData(ti));
168: }
169:
170: /**
171: * Creates a JUnitTestInfo instance.
172: */
173: protected TestInfo createTestInfo(Test test) {
174: TestInfo ti = new JUnitTestInfo(test);
175: return ti;
176: }
177: }
|