001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.junit;
038:
039: import java.io.PrintStream;
040:
041: import junit.runner.*;
042: import junit.framework.*;
043: import junit.textui.TestRunner;
044:
045: /** DrJava's own testrunner. It updates the document in the JUnit pane as error and failure events are fired.
046: * @version $Id: JUnitTestRunner.java 4255 2007-08-28 19:17:37Z mgricken $
047: */
048: public class JUnitTestRunner extends TestRunner {
049:
050: /** Receives updates on the test suite's progress. */
051: private JUnitModelCallback _jmc;
052:
053: /** Used to tie the output of the ui textrunner to nothing. */
054: private PrintStream _writer;
055:
056: /** Class loader that uses DrJava's classpath. Overrides the super class' loader. */
057: private TestSuiteLoader _classLoader;
058:
059: /** The JUnit TestResult being accumulated. */
060: private TestResult _result;
061:
062: /** The current number of errors in the result. */
063: private int _errorCount;
064:
065: /** The current number of failures in the result. */
066: private int _failureCount;
067:
068: /** Standard constructor. */
069: public JUnitTestRunner(JUnitModelCallback jmc) {
070: super ();
071: _jmc = jmc;
072: _classLoader = new DrJavaTestSuiteLoader(jmc);
073: _writer = new PrintStream(System.out) {
074: public void print(String s) {
075: }
076:
077: public void println(String s) {
078: }
079:
080: public void println() {
081: }
082: };
083:
084: _errorCount = 0;
085: _failureCount = 0;
086: }
087:
088: public synchronized TestResult doRun(Test suite) {
089: // Reset all bookkeeping
090: _errorCount = 0;
091: _failureCount = 0;
092:
093: // Run the test
094: _result = createTestResult();
095: _result.addListener(this );
096: _jmc.testSuiteStarted(suite.countTestCases());
097: // long startTime = System.currentTimeMillis();
098: suite.run(_result);
099: // long endTime = System.currentTimeMillis();
100: // long runTime = endTime - startTime;
101: // fPrinter.print(result, runTime);
102: return _result;
103: }
104:
105: /** Overrides method in super class to always return a reloading test suite loader. */
106: public TestSuiteLoader getLoader() {
107: return _classLoader;
108: }
109:
110: /** Provides our own PrintStream which outputs to the appropriate document. */
111: protected PrintStream getWriter() {
112: return _writer;
113: }
114:
115: protected PrintStream writer() {
116: return getWriter();
117: }
118:
119: /** Called by JUnit when a test is started. */
120: public synchronized void startTest(Test test) {
121: _jmc.testStarted(test.toString());
122: }
123:
124: /** Called by JUnit when a test has finished. */
125: public synchronized void endTest(Test test) {
126: boolean error = false;
127: boolean failure = false;
128: if (_result.errorCount() > _errorCount) {
129: error = true;
130: _errorCount++;
131: }
132: if (_result.failureCount() > _failureCount) {
133: failure = true;
134: _failureCount++;
135: }
136: boolean success = !(failure || error);
137: _jmc.testEnded(test.toString(), success, failure);
138: }
139: }
|