KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > autodoc > v1 > testserver > junit > JUnitTestListener


1 /*
2  * @(#)JUnitTestListener.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.autodoc.v1.testserver.junit;
30
31
32 import junit.framework.Test;
33 import junit.framework.TestListener;
34 import junit.framework.AssertionFailedError;
35
36 import org.apache.log4j.Logger;
37
38 import net.sourceforge.groboutils.autodoc.v1.testserver.MonitorFinder;
39 import net.sourceforge.groboutils.autodoc.v1.testserver.TestCorrelate;
40 import net.sourceforge.groboutils.autodoc.v1.testserver.TestData;
41 import net.sourceforge.groboutils.autodoc.v1.testserver.TestInfo;
42
43
44 /**
45  * A helper class to interface between the test framework and JUnit's
46  * <tt>TestListener</tt> interface.
47  *
48  *
49  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
50  * @since March 17, 2002
51  * @version $Date: 2003/06/10 01:08:41 $
52  */

53 public abstract class JUnitTestListener extends TestCorrelate
54         implements TestListener
55 {
56     private static final Logger LOG = Logger.getLogger( JUnitTestListener.class );
57     
58     private MonitorFinder finder;
59     
60     
61     /**
62      *
63      */

64     public JUnitTestListener( MonitorFinder finder )
65     {
66         super( null, finder );
67     }
68     
69     
70     //-------------------------------------------------------------------------
71
// abstract methods
72

73     
74     /**
75      * Called before the test starts, and after the test has been registered.
76      */

77     protected abstract void startTest( TestData data );
78     
79     
80     /**
81      * Called after the test ends. The implemented method must not send off
82      * the data - that is done by the JUnitTestListener implementation.
83      */

84     protected abstract void endTest( TestData data );
85     
86     
87     /**
88      * Called when the test in the data has an unexpected exception thrown.
89      */

90     protected abstract void addError( TestData data, Throwable JavaDoc t );
91     
92     
93     /**
94      * Called when the test in the data has an assertion error.
95      */

96     protected abstract void addFailure( TestData data, AssertionFailedError t );
97     
98     
99     
100     //-------------------------------------------------------------------------
101
// JUnit listener methods
102

103     /**
104      * An error occurred.
105      */

106     public void addError(Test test, Throwable JavaDoc t)
107     {
108         if (test == null)
109         {
110             LOG.error("JUnit passed null test to method addError()",t);
111             return;
112         }
113         
114         TestData td = getTestData( createTestInfo( test ) );
115         // null means the test was never registered.
116
if (td != null)
117         {
118             addError( td, t );
119         }
120         else
121         {
122             LOG.warn("JUnit called 'addError' without calling 'startTest'.",
123                 t );
124         }
125     }
126     
127     /**
128      * A failure occurred.
129      */

130     public void addFailure(Test test, AssertionFailedError t)
131     {
132         if (test == null)
133         {
134             LOG.error("JUnit passed null test to method addFailure()",t);
135             return;
136         }
137         
138         TestData td = getTestData( createTestInfo( test ) );
139         // null means the test was never registered.
140
if (td != null)
141         {
142             addFailure( td, t );
143         }
144         else
145         {
146             LOG.warn("JUnit called 'addFailure' without calling 'startTest'.",
147                 t );
148         }
149     }
150     
151     
152     /**
153      * A test ended.
154      */

155     public void endTest(Test test)
156     {
157         TestInfo ti = createTestInfo( test );
158         TestData td = getTestData( ti );
159         if (td != null)
160         {
161             endTest( td );
162             
163             // end off the test info
164
try
165             {
166                 getFinder().getMonitor().sendTestData( ti );
167             }
168             catch (IllegalStateException JavaDoc ise)
169             {
170                 LOG.warn( "Warning for test ["+test+"]: "+ise.getMessage(),
171                     ise );
172             }
173         }
174         else
175         {
176             LOG.warn( "Received an end message for test ["+test+
177                 "], but it was never added." );
178         }
179     }
180     
181     
182     /**
183      * A test started.
184      */

185     public void startTest(Test test)
186     {
187         TestInfo ti = createTestInfo( test );
188         
189         // start the test info
190
getFinder().getMonitor().addTestData( ti );
191         
192         startTest( getTestData( ti ) );
193     }
194     
195     
196     /**
197      * Creates a JUnitTestInfo instance.
198      */

199     protected TestInfo createTestInfo( Test test )
200     {
201         TestInfo ti = new JUnitTestInfo( test );
202         return ti;
203     }
204 }
205
206
Popular Tags