KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > Result


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit;
8
9
10 import java.io.PrintWriter JavaDoc;
11 import java.io.StringWriter JavaDoc;
12
13 import org.apache.log4j.Logger;
14 import org.jdom.CDATA;
15 import org.jdom.Document;
16 import org.jdom.Element;
17
18 import com.inversoft.junit.internal.AssertionFailedErrorWrapper;
19 import com.inversoft.junit.internal.LocationExceptionWrapper;
20
21
22 /**
23  * The default implementation of the Result interface. This handles all
24  * exceptions and such
25  */

26 public class Result {
27     /**
28      * The logger for this class
29      */

30     private static final Logger logger = Logger.getLogger(Result.class);
31
32     /**
33      * The class name XML element name
34      */

35     public static final String JavaDoc XML_CLASS_NAME_ELEMENT = "className";
36
37     /**
38      * The message XML element name
39      */

40     public static final String JavaDoc XML_MESSAGE_ELEMENT = "message";
41
42     /**
43      * The stack trace XML element name
44      */

45     public static final String JavaDoc XML_STACK_TRACE_ELEMENT = "stackTrace";
46
47     /**
48      * The root XML element name
49      */

50     public static final String JavaDoc XML_ROOT_ELEMENT = "result";
51
52     /**
53      * The name of the junit error that might have been thrown on the test location side
54      */

55     public static final String JavaDoc JUNIT_ERROR_NAME = "junit.framework.AssertionFailedError";
56
57     /**
58      * The throwable that stores the test location side exception, if any
59      */

60     Throwable JavaDoc throwable;
61
62     /**
63      * Stores the status
64      */

65     boolean success;
66
67     /**
68      * Stores the class name of the Throwable (if any)
69      */

70     String JavaDoc className;
71
72     /**
73      * Stores the message of the Throwable (if any)
74      */

75     String JavaDoc message;
76
77     /**
78      * Stores the entire stack trace of the Throwable (if any)
79      */

80     String JavaDoc stackTrace;
81
82
83     /**
84      * Constructs a new default result that is a successful result.
85      */

86     public Result()
87     {
88         this.success = true;
89     }
90
91     /**
92      * Constructs a new default result that is an unsuccessful result and contains the
93      * given throwable (even if the throwable is null!)
94      */

95     public Result(Throwable JavaDoc throwable)
96     {
97         this.throwable = throwable;
98         this.success = false;
99         this.className = throwable.getClass().getName();
100         this.message = throwable.getMessage();
101
102         // Save the stack trace as text
103
StringWriter JavaDoc sw = new StringWriter JavaDoc();
104         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
105         throwable.printStackTrace(pw);
106         this.stackTrace = sw.toString();
107     }
108
109
110     /**
111      * Returns the result code or status which is based on the existance of an exception
112      * or not. If there was no exception, then this returns a Boolean.TRUE. Otherwise,
113      * this returns Boolean.FALSE.
114      *
115      * @return An Boolean containing the result status
116      */

117     public Object JavaDoc getStatus()
118     {
119         if (success) {
120             return Boolean.TRUE;
121         }
122
123         return Boolean.FALSE;
124     }
125
126     /**
127      * Returns the Throwable was thrown from the <code>test</code> method. This
128      * generally indictates a failure or assertion. If no Throwable exists, this
129      * returns null.
130      */

131     public Throwable JavaDoc getThrowable()
132     {
133         return throwable;
134     }
135
136     /**
137      * Returns whether or not the test executed at the test location was a success
138      */

139     public boolean isSuccessful()
140     {
141         return success;
142     }
143
144     /**
145      * Returns the class name of the Throwable (if any)
146      */

147     public String JavaDoc getClassName()
148     {
149         return className;
150     }
151
152     /**
153      * Returns the message of the Throwable (if any)
154      */

155     public String JavaDoc getMessage()
156     {
157         return message;
158     }
159
160     /**
161      * Returns the stack trace of the Throwable (if any)
162      */

163     public String JavaDoc getStackTrace()
164     {
165         return stackTrace;
166     }
167
168     /**
169      * This is a client-server method (usually HTTP specific) that converts this
170      * Result to XML for transmition across the wire.
171      *
172      * @return An XML representation of this Result as a JDOM Document
173      */

174     public Document toXML() {
175         Element root = new Element(XML_ROOT_ELEMENT);
176
177         if (success) {
178             return new Document(root);
179         }
180
181         Element classNameElem = new Element(XML_CLASS_NAME_ELEMENT);
182         Element messageElem = new Element(XML_MESSAGE_ELEMENT);
183         Element stackTraceElem = new Element(XML_STACK_TRACE_ELEMENT);
184
185         classNameElem.addContent(new CDATA(this.className));
186         messageElem.addContent(new CDATA(this.message));
187         stackTraceElem.addContent(new CDATA(this.stackTrace));
188
189         root.addContent(classNameElem);
190         root.addContent(messageElem);
191         root.addContent(stackTraceElem);
192
193         return new Document(root);
194     }
195
196     /**
197      * This is a client-server method (usually HTTP specific) that converts the given
198      * Document to a Result and changes this result instance to reflect the XML.
199      *
200      * @param document The XML representation of the Result as a JDOM Document
201      */

202     public void fromXML(Document document) {
203         Element rootElem = document.getRootElement();
204         Element classNameElem = rootElem.getChild(XML_CLASS_NAME_ELEMENT);
205         Element messageElem = rootElem.getChild(XML_MESSAGE_ELEMENT);
206         Element stackTraceElem = rootElem.getChild(XML_STACK_TRACE_ELEMENT);
207
208         if (classNameElem == null || messageElem == null || stackTraceElem == null) {
209             success = true;
210             return;
211         }
212
213         this.className = classNameElem.getText();
214         this.message = messageElem.getText();
215         this.stackTrace = stackTraceElem.getText();
216         success = false;
217
218         if (logger.isDebugEnabled()) {
219             logger.debug("className: " + this.className);
220             logger.debug("message: " + this.message);
221             logger.debug("stackTrace: " + this.stackTrace);
222         }
223
224         if (this.className.equals(JUNIT_ERROR_NAME)) {
225             throwable = new AssertionFailedErrorWrapper(this.message, this.stackTrace);
226         } else {
227             throwable = new LocationExceptionWrapper(this.message, this.stackTrace);
228         }
229     }
230 }
Popular Tags