Source code: com/simscomputing/testbed/TestResult.java
1 package com.simscomputing.testbed;
2
3 import com.simscomputing.SoftwareFaultException;
4 import com.simscomputing.util.Debugger;
5
6 import java.io.ByteArrayOutputStream;
7
8 /**
9 For each test, a TestResult object indicates whether the test passed and
10 how long the test took to execute in milliseconds.
11
12 @author David Sims
13 @author Dave Lamy
14 @version $Revision: 1.1.1.1 $ $Date: 2000/02/21 21:22:34 $
15 */
16 public class TestResult implements Cloneable {
17 /** The name of the test case collection. */
18 // fixme private TestCaseCollection testCaseCollection;
19 // private String testCaseCollectionName;
20 private String testCaseCollectionClassName;
21
22 /** The name of the test case. */
23 private String testCaseName;
24
25 /** Whether the test passed */
26 private boolean passed;
27 /** How long the test took to run in milliseconds. */
28 private long executionTimeInMilliseconds;
29 private Throwable abnormalFailureException;
30 private String testFailureDescription;
31
32 /**!!! 10-04-99 Begin D Lamy code modification !!!**/
33
34 /** Stored stream of system.out data **/
35 private ByteArrayOutputStream outStream;
36 /** Stored stream of system.err data **/
37 private ByteArrayOutputStream errStream;
38
39 private boolean didNotRun = false;
40
41 public TestResult(TestCaseCollection testCaseCollection,
42 String testCaseName) {
43 didNotRun = true;
44 this.testCaseCollectionClassName = testCaseCollection.getClass().getName();
45 this.testCaseName = testCaseName;
46 } // constructor
47
48 public boolean didTestCaseRun() {
49 return !didNotRun;
50 } // didTestCaseRun
51
52 /**
53 Saves the results of running a test case.
54
55 @param String testName
56 @param boolean passed
57 @param long executionTimeInMilliseconds
58 @param outStream - a ByteArrayOutputStream that has all system.out data buffered in it
59 @param errStream - a ByteArrayOutputStream that has all system.err data buffered in it
60 @precondition passed ||
61 (failureException != null &&
62 failureException instanceof TestFailedException => failureException.getMessage() != null)
63 */
64 public TestResult(TestCaseCollection testCaseCollection,
65 String testCaseName,
66 boolean passed,
67 long executionTimeInMilliseconds,
68 Throwable failureException,
69 ByteArrayOutputStream outStream,
70 ByteArrayOutputStream errStream) {
71
72 Debugger.checkPrecondition(passed || failureException != null,
73 "if the test failed, failureException must not be null");
74
75 //fixme this.testCaseCollection = testCaseCollection;
76 this.testCaseCollectionClassName = testCaseCollection.getClass().getName();
77 this.testCaseName = testCaseName;
78 this.passed = passed;
79 this.executionTimeInMilliseconds = executionTimeInMilliseconds;
80 this.outStream = outStream;
81 this.errStream = errStream;
82
83 if (!passed) {
84 Debugger.checkPrecondition(failureException != null, "failureException must not be null");
85
86 if (failureException instanceof com.simscomputing.testbed.TestFailedException) {
87 testFailureDescription = ((TestFailedException) failureException).getMessage();
88 Debugger.checkPrecondition(testFailureDescription != null,
89 "failureDescription must not be null");
90 } // if
91 else {
92 this.abnormalFailureException = failureException;
93 } // else
94 } // if
95 } // constructor
96
97 /**!!! 10-04-99 End D Lamy code modification !!!**/
98
99
100
101 ///////////////
102 // accessors //
103 ///////////////
104
105 /**
106 Return the testName attribute.
107
108 @return String - testName
109 */
110 public String getTestCaseName() {
111 return testCaseName;
112 } // getTestName()
113
114 // public String getTestCaseCollectionName() {
115 // return testCaseCollectionName; // fixme: testCaseCollection.getClass().getName();
116 // } // getTestName()
117
118 public String getTestCaseCollectionClassName() {
119 return testCaseCollectionClassName;
120 } // getTestCaseCollectionClassName()
121
122 // fixme
123 // public TestCaseCollection hi() {
124 // return testCaseCollection;
125 // } // getTestCaseCollection()
126
127 // public Class getTestCaseCollection() {
128 // return testCaseCollection.getClass();
129 // } // getTestCaseCollection()
130
131 public String getLongName() {
132 return getTestCaseCollectionClassName() + "." + getTestCaseName();
133 } // getLongName()
134
135 /**
136 Return the passed attribute.
137
138 @return boolean - passed
139 */
140 public boolean didTestPass() {
141 return passed;
142 } // getTestResult()
143
144 public boolean didTestFail() {
145 return !didTestPass();
146 } // didTestFail()
147
148 public boolean didTestFailAbnormally() {
149 return abnormalFailureException != null;
150 } // didTestFailAbnormally()
151
152 /**
153 Accesses the failure exception, if any.
154
155 @return the exception that caused the test to fail, if any
156 */
157 public Throwable getTestFailureException() {
158 Debugger.checkPrecondition(didTestFailAbnormally(), "test must have failed abnormally");
159 return abnormalFailureException;
160 } // getFailureException()
161
162 public String getTestFailureDescription() {
163 Debugger.checkPrecondition(didTestFail() || !didTestFailAbnormally(),
164 "test must have failed but not abnormally");
165 return testFailureDescription;
166 } // getTestFailureDescription()
167
168 /**
169 Return the executionTimeInMilliseconds attribute.
170
171 @return long - executionTimeInMilliseconds
172 */
173 public long getExecutionTimeInMilliseconds() {
174 return executionTimeInMilliseconds;
175 } // getExecutionTimeInMilliseconds()
176
177 public Object clone() {
178 try {
179 return (TestResult) super.clone();
180 } // try
181 catch (CloneNotSupportedException e) {
182 throw new SoftwareFaultException("clone should work", e);
183 } // catch
184 } // clone()
185
186 /** !!! 10-04-99 Begin D Lamy code block !!! **/
187 public String getOutStreamData() {
188 return outStream.toString();
189 } // getOutStreamData
190
191 public String getErrStreamData() {
192 return errStream.toString();
193 } // getOutStreamData
194
195 /** !!! 10-04-99 End D Lamy code block !!! **/
196
197 } // class TestResult