KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ut > HAbortingResult


1 /**
2  * $Id: HAbortingResult.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 1997-2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.ut;
30
31 import junit.framework.AssertionFailedError;
32 import junit.framework.Test;
33 import junit.framework.TestResult;
34
35 /**
36  * Test Result collector that will automatically signal a suite to stop if the number
37  * of errors and/or failures hit some predefined limit. For example, can be setup
38  * to stop if any errors are detected.
39  *
40  * @since JWare/core 0.4
41  * @author ssmc, &copy;1997-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
42  * @version 0.5
43  * @.safety guarded once initialized
44  * @.group impl,test
45  **/

46
47 public class HAbortingResult extends TestResult
48 {
49     /**
50      * Create new aborting result that doesn't (no limits).
51      **/

52     public HAbortingResult()
53     {
54         super();
55     }
56
57
58     /**
59      * Create new aborting result that will allow at most <code>maxFailures</code>
60      * failures and <code>maxErrors</code> errors before signalling all tests should
61      * be stopped (to caller).
62      **/

63     public HAbortingResult(int maxFailures, int maxErrors)
64     {
65         super();
66         m_maxFailures= maxFailures;
67         m_maxErrors= maxErrors;
68     }
69
70
71
72     /**
73      * Tells this result the positions from which its should calculate
74      * stop criteria. If this result is being used across multiple distinct
75      * test suites, each suite should reset this result's start points
76      * when it starts.
77      * @param failure0th failure start point
78      * @param error0th error start point
79      * @.safety guarded
80      * @since JWare/AntX 0.4
81      **/

82     public void setStartPoints(int failure0th, int error0th)
83     {
84         m_0thFailure = failure0th; //NB: need a stack if ever nestable!
85
m_0thError = error0th;
86     }
87
88
89
90     /**
91      * Convenient alternative to {@linkplain #setStartPoints(int,int)
92      * setStartPoints(int,int)} that uses the current error and failure
93      * counts as the start points.
94      * @.safety guarded
95      * @since JWare/AntX 0.4
96      **/

97     public final void newStartPoints()
98     {
99         setStartPoints(fFailures.size(),fErrors.size());
100     }
101
102
103
104     /**
105      * Convenience to uninstall the previous call to
106      * {@linkplain #newStartPoints}.
107      * @since JWare/AntX 0.4
108      **/

109     public final void popStartPoints()
110     {
111         setStartPoints(0,0); //NB: need a stack if ever nestable!
112
}
113
114
115
116     /**
117      * Add error to this result; will signal stop to caller (suite|test) if
118      * setup to stop on any error. See junit.framework.TestResult#shouldStop.
119      **/

120     public synchronized void addError(Test test, Throwable JavaDoc t)
121     {
122         super.addError(test,t);
123         if (fErrors.size()-get0thError()>=m_maxErrors) {
124             this.stop();
125         }
126     }
127
128
129     /**
130      * Add a failure to this result; will signal stop to caller (suite|test)
131      * if a maximum failure-count has been specified.
132      **/

133     public synchronized void addFailure(Test test, AssertionFailedError t)
134     {
135         super.addFailure(test,t);
136         if (fFailures.size()-get0thFailure()>=m_maxFailures) {
137             this.stop();
138         }
139     }
140
141
142     /**
143      * Return the maximum number of failures recorded by this result before
144      * it signals a stop. Will return <code>Integer.MAX_VALUE</code> if
145      * no such limit.
146      **/

147     public int getMaximumFailures()
148     {
149         return m_maxFailures;
150     }
151
152
153     /**
154      * Set the maximum number of failures recorded by this result before
155      * it signals a stop.
156      **/

157     public void setMaximumFailures(int maxFailures)
158     {
159         m_maxFailures = maxFailures;
160     }
161
162
163     /**
164      * Return the maximum number of errors recorded by this result before
165      * it signals a stop. Will return <code>Integer.MAX_VALUE</code> if
166      * no such limit.
167      **/

168     public int getMaximumErrors()
169     {
170         return m_maxErrors;
171     }
172
173
174     /**
175      * Set the maximum number of errors recorded by this result before
176      * it signals a stop.
177      **/

178     public void setMaximumErrors(int maxErrors)
179     {
180         m_maxErrors = maxErrors;
181     }
182
183
184     /**
185      * Returns whether this result is configured to signal stop as soon
186      * as any failure or error occurs.
187      **/

188     public final boolean isFailQuick()
189     {
190         return getMaximumErrors()==1 && getMaximumFailures()==1;
191     }
192
193
194     /**
195      * Shortcut to ensure this result signal stop as-soon-as <em>any</em>
196      * failure or error is recorded.
197      **/

198     public final void setFailQuick()
199     {
200         setMaximumErrors(1);
201         setMaximumFailures(1);
202     }
203
204
205
206     private int get0thError()
207     {
208         return m_0thError; //NB: need stack if ever suites are nestable!
209
}
210     
211     private int get0thFailure()
212     {
213         return m_0thFailure;//NB: ditto!
214
}
215
216     /** max failures and max errors. **/
217     protected int m_maxFailures= Integer.MAX_VALUE;
218     protected int m_maxErrors= Integer.MAX_VALUE;
219     protected int m_0thError= 0;
220     protected int m_0thFailure= 0;
221 }
222
223 /* end-of-HAbortingResult.java */
224
225
Popular Tags