001: /*
002: * @(#)ErrorsImpl.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.mbtf.v1.engine;
030:
031: import net.sourceforge.groboutils.mbtf.v1.IError;
032: import net.sourceforge.groboutils.mbtf.v1.IErrors;
033: import net.sourceforge.groboutils.mbtf.v1.IPathHistory;
034: import net.sourceforge.groboutils.mbtf.v1.TestHaltRuntimeException;
035: import net.sourceforge.groboutils.mbtf.v1.TestFailRuntimeException;
036:
037: import java.util.Vector;
038:
039: /**
040: * Default implementation of IErrors. Designed so that the same object can
041: * be reused.
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @version $Date: 2003/02/10 22:52:26 $
045: * @since June 12, 2002
046: */
047: public class ErrorsImpl implements IErrors {
048: private Vector errors = new Vector();
049: private Vector warnings = new Vector();
050: private boolean haltTests = false;
051: private boolean haltPath = false;
052: private IPathHistory ph;
053:
054: /*
055: * Reset the implementation for reuse.
056: public void reset()
057: {
058: this.errors.removeAllElements();
059: this.warnings.removeAllElements();
060: this.haltTests = false;
061: this.haltPath = false;
062: this.ph = null;
063: }
064: */
065:
066: public void setCurrentPathHistory(IPathHistory ph) {
067: this .ph = ph.copy();
068: }
069:
070: /**
071: * Immediately stop the path processing, and do not continue other paths
072: * for processing. This will throw a <tt>RuntimeException</tt>.
073: * <P>
074: * Halts should be a last-recourse to indicate that the system cannot be
075: * used for further testing.
076: *
077: * @param msg a human-readable error message.
078: * @exception TestHaltRuntimeException will always be generated.
079: */
080: public void halt(String msg) {
081: addErrorType(msg, null);
082: this .haltPath = true;
083: this .haltTests = true;
084:
085: throw new TestHaltRuntimeException(this , msg);
086: }
087:
088: /**
089: * Add a failure to the list of current errors. Validation methods that
090: * register failures will halt the current path's testing. This method
091: * will not throw an exception, so validation processing must leave the
092: * method on its own.
093: * <P>
094: * Failures should be registered when a non-recoverable error occurs in the
095: * system. The framework may still process other paths, though.
096: *
097: * @param msg a human-readable error message.
098: */
099: public void addFailure(String msg) {
100: addFailure(msg, null);
101: }
102:
103: /**
104: * Add a failure associated with a Throwable to the list of current errors.
105: * Validation methods that register failures will halt the current path's
106: * testing. This method will not throw an exception, so validation
107: * processing must leave the method on its own.
108: * <P>
109: * Failures should be registered when a non-recoverable error occurs in the
110: * system. The framework may still process other paths, though.
111: *
112: * @param msg a human-readable error message.
113: * @param t the exception associated with the error.
114: */
115: public void addFailure(String msg, Throwable t) {
116: addErrorType(msg, t);
117: this .haltPath = true;
118: }
119:
120: /**
121: * Add a failure to the list of current errors. Validation methods that
122: * register failures will halt the current path's testing. This method
123: * will not throw a <tt>TestFailRuntimeException</tt>, so validation
124: * processing must leave the method on its own.
125: * <P>
126: * Failures should be registered when a non-recoverable error occurs in the
127: * system. The framework may still process other paths, though.
128: *
129: * @param msg a human-readable error message.
130: * @exception TestFailRuntimeException allows for easy exiting of a
131: * burried method call stack.
132: */
133: public void fail(String msg) throws TestFailRuntimeException {
134: addFailure(msg);
135: throw new TestFailRuntimeException(this , msg);
136: }
137:
138: /**
139: * Add a failure associated with a Throwable to the list of current errors.
140: * Validation methods that register failures will halt the current path's
141: * testing. This method will throw a <tt>TestFailRuntimeException</tt> to
142: * allow for an easy exit from a burried method call stack.
143: * <P>
144: * Failures should be registered when a non-recoverable error occurs in the
145: * system. The framework may still process other paths, though.
146: *
147: * @param msg a human-readable error message.
148: * @param t the exception associated with the error.
149: * @exception TestFailRuntimeException allows for easy exiting of a
150: * burried method call stack.
151: */
152: public void fail(String msg, Throwable t)
153: throws TestFailRuntimeException {
154: addFailure(msg, t);
155: throw new TestFailRuntimeException(this , msg);
156: }
157:
158: /**
159: * Add an error to the list of current errors. Validation methods that
160: * register errors will not halt the current path's testing, but the error
161: * will be listed in the report with the associated path where the error
162: * condition occured.
163: * <P>
164: * Errors should be registered when an error occurs in the system, but
165: * the system can continue processing the path.
166: *
167: * @param msg a human-readable error message.
168: */
169: public void addError(String msg) {
170: addError(msg, null);
171: }
172:
173: /**
174: * Add an error associated with a Throwable to the list of current errors.
175: * Validation methods that register errors will halt the current path's
176: * testing.
177: * <P>
178: * Errors should be registered when an error occurs in the system, but
179: * the system can continue processing the path.
180: *
181: * @param msg a human-readable error message.
182: * @param t the exception associated with the error.
183: */
184: public void addError(String msg, Throwable t) {
185: addErrorType(msg, t);
186: }
187:
188: /**
189: * Add a warning to the list of current warnings. Warnings will not
190: * halt the testing process, and will not register an error.
191: * <P>
192: * Warnings should be used when a questionable system state occurs, or if
193: * the tester wants to perform debugging.
194: *
195: * @param msg a human-readable message.
196: */
197: public void addWarning(String msg) {
198: addMessage(this .warnings, msg, null);
199: }
200:
201: /**
202: * Retrieve all registered errors.
203: */
204: public IError[] getErrors() {
205: IError e[] = new IError[this .errors.size()];
206: this .errors.copyInto(e);
207: return e;
208: }
209:
210: //-------------------------------------------------------------------------
211:
212: public IError[] getWarnings() {
213: IError e[] = new IError[this .warnings.size()];
214: this .warnings.copyInto(e);
215: return e;
216: }
217:
218: public boolean isHaltPath() {
219: return this .haltPath;
220: }
221:
222: public boolean isHaltTests() {
223: return this .haltTests;
224: }
225:
226: public String toString() {
227: IError[] e = getErrors();
228: StringBuffer sb = new StringBuffer("[ Registered Errors:\n");
229: for (int i = 0; i < e.length; ++i) {
230: sb.append(e[i]);
231: }
232:
233: sb.append("Registered Warnings:\n");
234: e = getWarnings();
235: for (int i = 0; i < e.length; ++i) {
236: sb.append(e[i]);
237: }
238:
239: sb.append("\n]");
240: return sb.toString();
241: }
242:
243: //-------------------------------------------------------------------------
244:
245: public void addErrors(IErrors e) {
246: addErrors(e.getErrors());
247: if (e instanceof ErrorsImpl) {
248: ErrorsImpl ei = (ErrorsImpl) e;
249: addWarnings(ei.getWarnings());
250: if (ei.isHaltPath()) {
251: this .haltPath = true;
252: }
253: if (ei.isHaltTests()) {
254: this .haltTests = true;
255: }
256: }
257: }
258:
259: public void addErrors(IError[] r) {
260: if (r != null) {
261: for (int i = 0; i < r.length; ++i) {
262: this .errors.addElement(r[i]);
263: }
264: }
265: }
266:
267: public void addWarnings(IError[] r) {
268: if (r != null) {
269: for (int i = 0; i < r.length; ++i) {
270: this .warnings.addElement(r[i]);
271: }
272: }
273: }
274:
275: //-------------------------------------------------------------------------
276:
277: protected void addErrorType(String msg, Throwable t) {
278: addMessage(this .errors, msg, t);
279: }
280:
281: protected void addMessage(Vector v, String msg, Throwable t) {
282: IError err = new ErrorImpl(msg, t, this.ph);
283: v.addElement(err);
284: }
285: }
|