001: /********************************************************************************
002: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
003: * Copyright (c) 2004, Joerg and Kai Gellien
004: * All rights reserved.
005: *
006: * The Software is provided under the terms of the Common Public License 1.0
007: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of the authors or DDTUnit, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package junitx.ddtunit;
037:
038: import java.util.ArrayList;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.ListIterator;
042: import java.util.Vector;
043:
044: import junit.framework.AssertionFailedError;
045: import junit.framework.Protectable;
046: import junit.framework.Test;
047: import junit.framework.TestCase;
048: import junit.framework.TestFailure;
049: import junit.framework.TestListener;
050: import junit.framework.TestResult;
051: import junitx.ddtunit.util.DDTConfiguration;
052: import junitx.ddtunit.util.PrivilegedAccessor;
053:
054: /**
055: * Extends class {@link TestResult}of JUnit <br/>This class will contain extra
056: * results from test execution based on xml data testcases.
057: *
058: * @author jg
059: */
060: public class DDTTestResult extends TestResult {
061: /**
062: * Vector containing all caught failures during test execution
063: */
064: protected List<TestFailure> fMethodTestFailures;
065:
066: /**
067: * Vector containing all errors caught during test execution
068: */
069: protected List<TestFailure> fMethodTestErrors;
070:
071: /**
072: * Number of executed tests inside of a testmethod
073: */
074: protected int fRunMethodTests;
075:
076: /**
077: *
078: */
079: public DDTTestResult() {
080: super ();
081: fMethodTestFailures = new Vector<TestFailure>();
082: fMethodTestErrors = new Vector<TestFailure>();
083: fRunMethodTests = 0;
084:
085: if (DDTConfiguration.getInstance().isActiveRunMonitor()
086: && !this .cloneListeners().contains(
087: DDTRunMonitor.getInstance())) {
088: this .addListener(DDTRunMonitor.getInstance());
089: }
090: }
091:
092: /**
093: * Instanciate DDTTestResult by using JUnit TestResult as base info.
094: *
095: * @param result
096: * of JUnit TestResult
097: * @throws DDTException
098: */
099: public DDTTestResult(TestResult result) throws DDTException {
100: try {
101: PrivilegedAccessor
102: .setFieldValue(this , "fErrors", PrivilegedAccessor
103: .getFieldValue(result, "fErrors"));
104: PrivilegedAccessor.setFieldValue(this , "fFailures",
105: PrivilegedAccessor.getFieldValue(result,
106: "fFailures"));
107: PrivilegedAccessor.setFieldValue(this , "fListeners",
108: PrivilegedAccessor.getFieldValue(result,
109: "fListeners"));
110: this .fRunTests = ((Integer) PrivilegedAccessor
111: .getFieldValue(result, "fRunTests")).intValue();
112: fMethodTestFailures = new Vector<TestFailure>();
113: fMethodTestErrors = new Vector<TestFailure>();
114: fRunMethodTests = 0;
115: } catch (Exception e) {
116: e.printStackTrace();
117: throw new DDTException(
118: "Error on transforming TestResult to DDTResult", e);
119: }
120:
121: if (DDTConfiguration.getInstance().isActiveRunMonitor()
122: && !this .cloneListeners().contains(
123: DDTRunMonitor.getInstance())) {
124: this .addListener(DDTRunMonitor.getInstance());
125: }
126: }
127:
128: /**
129: * DOCUMENT ME!
130: *
131: * @param result
132: * DOCUMENT ME!
133: *
134: * @return DOCUMENT ME!
135: */
136: public TestResult copyContent(TestResult result) {
137: try {
138: PrivilegedAccessor.setFieldValue(result, "fErrors",
139: this .fErrors);
140: PrivilegedAccessor.setFieldValue(result, "fFailures",
141: this .fFailures);
142: PrivilegedAccessor.setFieldValue(result, "fListeners",
143: this .fListeners);
144: PrivilegedAccessor.setFieldValue(result, "fRunTests",
145: new Integer(this .fRunTests));
146: } catch (Exception e) {
147: e.printStackTrace();
148: throw new DDTException(
149: "Error on transforming TestResult to DDTResult", e);
150: }
151:
152: return result;
153: }
154:
155: /**
156: * DOCUMENT ME!
157: *
158: * @return DOCUMENT ME!
159: *
160: * @throws DDTException
161: * DOCUMENT ME!
162: */
163: TestResult createTestResult() throws DDTException {
164: TestResult result = new TestResult();
165:
166: try {
167: PrivilegedAccessor.setFieldValue(result, "fError",
168: this .fErrors);
169: PrivilegedAccessor.setFieldValue(result, "fFailures",
170: this .fFailures);
171: PrivilegedAccessor.setFieldValue(result, "fListeners",
172: this .fListeners);
173: PrivilegedAccessor.setFieldValue(result, "fRunTests",
174: new Integer(this .fRunTests));
175: } catch (Exception e) {
176: e.printStackTrace();
177: throw new DDTException(
178: "Error during conversion from DDTResult to TestResult");
179: }
180:
181: return result;
182: }
183:
184: /**
185: * Runs a TestCase.
186: *
187: * @param test
188: * to run
189: */
190: protected void run(final TestCase test) {
191: startTest(test);
192:
193: Protectable p = new Protectable() {
194: public void protect() throws Throwable {
195: test.runBare();
196: }
197: };
198:
199: runProtected(test, p);
200:
201: endTest(test);
202: }
203:
204: /**
205: * Runs a TestCase.
206: *
207: * @param test
208: * to run
209: * @param protectedRunner
210: * executes testmethod and and throws Throwable if any error
211: * occures
212: */
213: public void runProtected(final Test test,
214: Protectable protectedRunner) {
215: try {
216: protectedRunner.protect();
217: } catch (AssertionFailedError e) {
218: addFailure(test, e);
219: } catch (ThreadDeath e) { // don't catch ThreadDeath by accident
220: throw e;
221: } catch (Throwable e) {
222: addError(test, e);
223: }
224: }
225:
226: /**
227: * Add error of a method test execution for later statistic
228: *
229: * @param test
230: * to run
231: * @param testName
232: * of dataset actually processed in testmethod
233: * @param throwable
234: * caught during execution of test
235: *
236: * @see junitx.ddtunit.DDTTestListener#addMethodTestError(junit.framework.Test,
237: * java.lang.String, java.lang.Throwable)
238: */
239: public void addMethodTestError(Test test, String testName,
240: Throwable throwable) {
241: fMethodTestErrors.add(new DDTTestFailure(test, testName,
242: throwable));
243:
244: for (Iterator e = cloneListeners().listIterator(); e.hasNext();) {
245: TestListener listener = (TestListener) e.next();
246:
247: if (DDTTestListener.class.isInstance(listener)) {
248: ((DDTTestListener) listener).addMethodTestError(test,
249: testName, throwable);
250: }
251: }
252: }
253:
254: /**
255: * Add assertion failure of a method test execution for later statistic
256: *
257: * @param test
258: * to run
259: * @param testName
260: * of dataset actually processed in testmethod
261: * @param assertError
262: * caught during execution of test
263: *
264: * @see junitx.ddtunit.DDTTestListener#addMethodTestFailure(junit.framework.Test,
265: * java.lang.String, junit.framework.AssertionFailedError)
266: */
267: public void addMethodTestFailure(Test test, String testName,
268: AssertionFailedError assertError) {
269: fMethodTestFailures.add(new DDTTestFailure(test, testName,
270: assertError));
271:
272: for (Iterator e = cloneListeners().listIterator(); e.hasNext();) {
273: TestListener listener = (TestListener) e.next();
274:
275: if (DDTTestListener.class.isInstance(listener)) {
276: ((DDTTestListener) listener).addMethodTestFailure(test,
277: testName, assertError);
278: }
279: }
280: }
281:
282: /**
283: * Notify about end of method test
284: *
285: * @param test
286: * to run
287: * @param methodName
288: * of actually processed method
289: *
290: * @see junitx.ddtunit.DDTTestListener#endMethodTest(junitx.ddtunit.DDTTestCase,
291: * java.lang.String)
292: */
293: public void endMethodTest(DDTTestCase test, String methodName) {
294: for (Iterator e = cloneListeners().listIterator(); e.hasNext();) {
295: TestListener listener = (TestListener) e.next();
296:
297: if (DDTTestListener.class.isInstance(listener)) {
298: ((DDTTestListener) listener).endMethodTest(test,
299: methodName);
300: }
301: }
302: }
303:
304: /**
305: * Notify about start of method test
306: *
307: * @param test
308: * to run
309: * @param methodName
310: * of actually processed method
311: *
312: * @see junitx.ddtunit.DDTTestListener#startMethodTest(junitx.ddtunit.DDTTestCase,
313: * java.lang.String)
314: */
315: public void startMethodTest(DDTTestCase test, String methodName) {
316: final int count = 1;
317:
318: synchronized (this ) {
319: fRunMethodTests += count;
320: }
321:
322: for (Iterator e = cloneListeners().listIterator(); e.hasNext();) {
323: TestListener listener = (TestListener) e.next();
324:
325: if (DDTTestListener.class.isInstance(listener)) {
326: ((DDTTestListener) listener).startMethodTest(test,
327: methodName);
328: }
329: }
330: }
331:
332: /**
333: * Returns a copy of the TestListeners
334: *
335: * @return Vector of TestListeners
336: */
337: private synchronized List cloneListeners() {
338: List newListeners = new ArrayList();
339: newListeners.addAll(fListeners);
340: return (List) newListeners;
341: }
342:
343: /**
344: * @return number of xml based tests run
345: */
346: public int runMethodTestCount() {
347: return fRunMethodTests;
348: }
349:
350: /**
351: * @return number of method test failures were caught during execution
352: */
353: public int methodTestFailureCount() {
354: return fMethodTestFailures.size();
355: }
356:
357: /**
358: * @return number of method test errors were caught during execution
359: */
360: public int methodTestErrorCount() {
361: return fMethodTestErrors.size();
362: }
363:
364: public ListIterator<TestFailure> methodTestFailures() {
365: ListIterator<TestFailure> iter = this.fMethodTestFailures
366: .listIterator();
367: return iter;
368: }
369:
370: }
|