KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > mbtf > v1 > engine > ErrorsImpl


1 /*
2  * @(#)ErrorsImpl.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.mbtf.v1.engine;
30
31
32 import net.sourceforge.groboutils.mbtf.v1.IError;
33 import net.sourceforge.groboutils.mbtf.v1.IErrors;
34 import net.sourceforge.groboutils.mbtf.v1.IPathHistory;
35 import net.sourceforge.groboutils.mbtf.v1.TestHaltRuntimeException;
36 import net.sourceforge.groboutils.mbtf.v1.TestFailRuntimeException;
37
38 import java.util.Vector JavaDoc;
39
40
41 /**
42  * Default implementation of IErrors. Designed so that the same object can
43  * be reused.
44  *
45  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
46  * @version $Date: 2003/02/10 22:52:26 $
47  * @since June 12, 2002
48  */

49 public class ErrorsImpl implements IErrors
50 {
51     private Vector JavaDoc errors = new Vector JavaDoc();
52     private Vector JavaDoc warnings = new Vector JavaDoc();
53     private boolean haltTests = false;
54     private boolean haltPath = false;
55     private IPathHistory ph;
56     
57     
58     /*
59      * Reset the implementation for reuse.
60     public void reset()
61     {
62         this.errors.removeAllElements();
63         this.warnings.removeAllElements();
64         this.haltTests = false;
65         this.haltPath = false;
66         this.ph = null;
67     }
68      */

69     
70     
71     public void setCurrentPathHistory( IPathHistory ph )
72     {
73         this.ph = ph.copy();
74     }
75     
76     
77     /**
78      * Immediately stop the path processing, and do not continue other paths
79      * for processing. This will throw a <tt>RuntimeException</tt>.
80      * <P>
81      * Halts should be a last-recourse to indicate that the system cannot be
82      * used for further testing.
83      *
84      * @param msg a human-readable error message.
85      * @exception TestHaltRuntimeException will always be generated.
86      */

87     public void halt( String JavaDoc msg )
88     {
89         addErrorType( msg, null );
90         this.haltPath = true;
91         this.haltTests = true;
92         
93         throw new TestHaltRuntimeException( this, msg );
94     }
95     
96     
97     /**
98      * Add a failure to the list of current errors. Validation methods that
99      * register failures will halt the current path's testing. This method
100      * will not throw an exception, so validation processing must leave the
101      * method on its own.
102      * <P>
103      * Failures should be registered when a non-recoverable error occurs in the
104      * system. The framework may still process other paths, though.
105      *
106      * @param msg a human-readable error message.
107      */

108     public void addFailure( String JavaDoc msg )
109     {
110         addFailure( msg, null );
111     }
112     
113     
114     /**
115      * Add a failure associated with a Throwable to the list of current errors.
116      * Validation methods that register failures will halt the current path's
117      * testing. This method will not throw an exception, so validation
118      * processing must leave the method on its own.
119      * <P>
120      * Failures should be registered when a non-recoverable error occurs in the
121      * system. The framework may still process other paths, though.
122      *
123      * @param msg a human-readable error message.
124      * @param t the exception associated with the error.
125      */

126     public void addFailure( String JavaDoc msg, Throwable JavaDoc t )
127     {
128         addErrorType( msg, t );
129         this.haltPath = true;
130     }
131     
132     
133     /**
134      * Add a failure to the list of current errors. Validation methods that
135      * register failures will halt the current path's testing. This method
136      * will not throw a <tt>TestFailRuntimeException</tt>, so validation
137      * processing must leave the method on its own.
138      * <P>
139      * Failures should be registered when a non-recoverable error occurs in the
140      * system. The framework may still process other paths, though.
141      *
142      * @param msg a human-readable error message.
143      * @exception TestFailRuntimeException allows for easy exiting of a
144      * burried method call stack.
145      */

146     public void fail( String JavaDoc msg )
147             throws TestFailRuntimeException
148     {
149         addFailure( msg );
150         throw new TestFailRuntimeException( this, msg );
151     }
152     
153     
154     /**
155      * Add a failure associated with a Throwable to the list of current errors.
156      * Validation methods that register failures will halt the current path's
157      * testing. This method will throw a <tt>TestFailRuntimeException</tt> to
158      * allow for an easy exit from a burried method call stack.
159      * <P>
160      * Failures should be registered when a non-recoverable error occurs in the
161      * system. The framework may still process other paths, though.
162      *
163      * @param msg a human-readable error message.
164      * @param t the exception associated with the error.
165      * @exception TestFailRuntimeException allows for easy exiting of a
166      * burried method call stack.
167      */

168     public void fail( String JavaDoc msg, Throwable JavaDoc t )
169             throws TestFailRuntimeException
170     {
171         addFailure( msg, t );
172         throw new TestFailRuntimeException( this, msg );
173     }
174     
175     
176     /**
177      * Add an error to the list of current errors. Validation methods that
178      * register errors will not halt the current path's testing, but the error
179      * will be listed in the report with the associated path where the error
180      * condition occured.
181      * <P>
182      * Errors should be registered when an error occurs in the system, but
183      * the system can continue processing the path.
184      *
185      * @param msg a human-readable error message.
186      */

187     public void addError( String JavaDoc msg )
188     {
189         addError( msg, null );
190     }
191     
192     
193     /**
194      * Add an error associated with a Throwable to the list of current errors.
195      * Validation methods that register errors will halt the current path's
196      * testing.
197      * <P>
198      * Errors should be registered when an error occurs in the system, but
199      * the system can continue processing the path.
200      *
201      * @param msg a human-readable error message.
202      * @param t the exception associated with the error.
203      */

204     public void addError( String JavaDoc msg, Throwable JavaDoc t )
205     {
206         addErrorType( msg, t );
207     }
208     
209     
210     /**
211      * Add a warning to the list of current warnings. Warnings will not
212      * halt the testing process, and will not register an error.
213      * <P>
214      * Warnings should be used when a questionable system state occurs, or if
215      * the tester wants to perform debugging.
216      *
217      * @param msg a human-readable message.
218      */

219     public void addWarning( String JavaDoc msg )
220     {
221         addMessage( this.warnings, msg, null );
222     }
223     
224     
225     /**
226      * Retrieve all registered errors.
227      */

228     public IError[] getErrors()
229     {
230         IError e[] = new IError[ this.errors.size() ];
231         this.errors.copyInto( e );
232         return e;
233     }
234     
235     
236     
237     
238     
239     //-------------------------------------------------------------------------
240

241     
242     public IError[] getWarnings()
243     {
244         IError e[] = new IError[ this.warnings.size() ];
245         this.warnings.copyInto( e );
246         return e;
247     }
248     
249     
250     public boolean isHaltPath()
251     {
252         return this.haltPath;
253     }
254     
255     
256     public boolean isHaltTests()
257     {
258         return this.haltTests;
259     }
260     
261     
262     public String JavaDoc toString()
263     {
264         IError[] e = getErrors();
265         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[ Registered Errors:\n");
266         for (int i = 0; i < e.length; ++i)
267         {
268             sb.append( e[i] );
269         }
270         
271         sb.append("Registered Warnings:\n");
272         e = getWarnings();
273         for (int i = 0; i < e.length; ++i)
274         {
275             sb.append( e[i] );
276         }
277         
278         sb.append( "\n]" );
279         return sb.toString();
280     }
281     
282     
283     //-------------------------------------------------------------------------
284

285     
286     public void addErrors( IErrors e )
287     {
288         addErrors( e.getErrors() );
289         if (e instanceof ErrorsImpl)
290         {
291             ErrorsImpl ei = (ErrorsImpl)e;
292             addWarnings( ei.getWarnings() );
293             if (ei.isHaltPath())
294             {
295                 this.haltPath = true;
296             }
297             if (ei.isHaltTests())
298             {
299                 this.haltTests = true;
300             }
301         }
302     }
303     
304     
305     public void addErrors( IError[] r )
306     {
307         if (r != null)
308         {
309             for (int i = 0; i < r.length; ++i)
310             {
311                 this.errors.addElement( r[i] );
312             }
313         }
314     }
315     
316     
317     public void addWarnings( IError[] r )
318     {
319         if (r != null)
320         {
321             for (int i = 0; i < r.length; ++i)
322             {
323                 this.warnings.addElement( r[i] );
324             }
325         }
326     }
327     
328     
329     
330     //-------------------------------------------------------------------------
331

332     
333     protected void addErrorType( String JavaDoc msg, Throwable JavaDoc t )
334     {
335         addMessage( this.errors, msg, t );
336     }
337     
338     
339     protected void addMessage( Vector JavaDoc v, String JavaDoc msg, Throwable JavaDoc t )
340     {
341         IError err = new ErrorImpl( msg, t, this.ph );
342         v.addElement( err );
343     }
344 }
345
346
Popular Tags