KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > junit > JUnitTestRunner


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.junit;
35
36 import java.io.PrintStream JavaDoc;
37
38 import junit.runner.*;
39 import junit.framework.*;
40 import junit.textui.TestRunner;
41
42 /** DrJava's own testrunner. It updates the document in the JUnit pane as error and failure events are fired.
43  * @version $Id: JUnitTestRunner.java 3587 2006-03-09 13:21:45Z rcartwright $
44  */

45 public class JUnitTestRunner extends TestRunner {
46   
47   /** Receives updates on the test suite's progress. */
48   private JUnitModelCallback _jmc;
49
50   /** Used to tie the output of the ui textrunner to nothing. */
51   private PrintStream JavaDoc _writer;
52
53   /** Class loader that uses DrJava's classpath. Overrides the super class' loader. */
54   private TestSuiteLoader _classLoader;
55
56   /** The JUnit TestResult being accumulated. */
57   private TestResult _result;
58
59   /** The current number of errors in the result. */
60   private int _errorCount;
61
62   /** The current number of failures in the result. */
63   private int _failureCount;
64
65   /** Standard constructor. */
66   public JUnitTestRunner(JUnitModelCallback jmc) {
67     super();
68     _jmc = jmc;
69     _classLoader = new DrJavaTestSuiteLoader(jmc);
70     _writer = new PrintStream JavaDoc(System.out) {
71       public void print(String JavaDoc s) { }
72       public void println(String JavaDoc s) { }
73       public void println() { }
74     };
75
76     _errorCount = 0;
77     _failureCount = 0;
78   }
79
80   public synchronized TestResult doRun(Test suite) {
81     // Reset all bookkeeping
82
_errorCount = 0;
83     _failureCount = 0;
84
85     // Run the test
86
_result = createTestResult();
87     _result.addListener(this);
88     _jmc.testSuiteStarted(suite.countTestCases());
89 // long startTime = System.currentTimeMillis();
90
suite.run(_result);
91 // long endTime = System.currentTimeMillis();
92
// long runTime = endTime - startTime;
93
// fPrinter.print(result, runTime);
94
return _result;
95   }
96
97   /** Overrides method in super class to always return a reloading test suite loader. */
98   public TestSuiteLoader getLoader() { return _classLoader; }
99
100   /** Provides our own PrintStream which outputs to the appropriate document. */
101   protected PrintStream JavaDoc getWriter() { return _writer; }
102
103   protected PrintStream JavaDoc writer() { return getWriter(); }
104
105   /** Called by JUnit when a test is started. */
106   public synchronized void startTest(Test test) { _jmc.testStarted(test.toString()); }
107
108   /** Called by JUnit when a test has finished. */
109   public synchronized void endTest(Test test) {
110     boolean error = false;
111     boolean failure = false;
112     if (_result.errorCount() > _errorCount) {
113       error = true;
114       _errorCount++;
115     }
116     if (_result.failureCount() > _failureCount) {
117       failure = true;
118       _failureCount++;
119     }
120     boolean success = ! (failure || error);
121     _jmc.testEnded(test.toString(), success, failure);
122   }
123 }
124
Popular Tags