edu.rice.cs.drjava.model.junit
Class DefaultJUnitModel

java.lang.Object
  extended by edu.rice.cs.drjava.model.junit.DefaultJUnitModel
All Implemented Interfaces:
JUnitModel, JUnitModelCallback

public class DefaultJUnitModel
extends Object
implements JUnitModel, JUnitModelCallback

Manages unit testing via JUnit.

Version:
$Id: DefaultJUnitModel.java 5456 2011-11-18 07:01:53Z rcartwright $

Field Summary
private  CompilerModel _compilerModel
          The compiler model.
private  boolean _forceTestSuffix
          State flag to record if test classes in projects must end in "Test"
private  SwingDocument _junitDoc
          The document used to display JUnit test results.
private  JUnitErrorModel _junitErrorModel
          The error model containing all current JUnit errors.
private  MainJVM _jvm
          RMI interface to a secondary JVM for running tests.
private static Log _log
          log for use in debugging
private  GlobalModel _model
          The global model to which the JUnitModel belongs
private  JUnitEventNotifier _notifier
          Manages listeners to this model.
private  boolean _testInProgress
          State flag to prevent starting new tests on top of old ones and to prevent resetting interactions after compilation is forced by unit testing.
 
Constructor Summary
DefaultJUnitModel(MainJVM jvm, CompilerModel compilerModel, GlobalModel model)
          Main constructor.
 
Method Summary
private  void _notifyCompileBeforeJUnit(CompilerListener testAfterCompile, List<OpenDefinitionsDocument> outOfSync)
          Helper method to notify JUnitModel listeners that all open files must be compiled before JUnit is run.
private  void _notifyJUnitEnded()
          Helper method to notify JUnitModel listeners that JUnit test suite execution has just ended.
private  void _notifyJUnitStarted()
          Helper method to notify JUnitModel listeners that JUnit test suite execution has started.
private  void _notifyNonTestCase(boolean testAll, boolean didCompileFail)
          Helper method to notify JUnitModel listeners that JUnit aborted before any tests could be run.
private  void _rawJUnitOpenDefDocs(List<OpenDefinitionsDocument> lod, boolean allTests)
          Runs all TestCases in the document list lod; assumes all documents have been compiled.
 void addListener(JUnitListener listener)
          Add a JUnitListener to the model.
 void classFileError(ClassFileError e)
          Called to indicate that an illegal class file was encountered
private  String getCanonicalPath(File f)
           
 Iterable<File> getClassPath()
          Returns the current classpath in use by the JUnit JVM.
 File getFileForClassName(String className)
          Called when the JUnitTestManager wants to open a file that is not currently open.
 SwingDocument getJUnitDocument()
          Used only for testing.
 JUnitErrorModel getJUnitErrorModel()
          Gets the JUnitErrorModel, which contains error info for the last test run.
 boolean isTestInProgress()
           
 void junit(OpenDefinitionsDocument doc)
          Runs JUnit on the current document.
 void junitAll()
          Creates a JUnit test suite over all currently open documents and runs it.
 void junitDocs(List<OpenDefinitionsDocument> lod)
          Runs JUnit over a list of documents.
 void junitJVMReady()
          Called when the JVM used for unit tests has registered.
private  void junitOpenDefDocs(List<OpenDefinitionsDocument> lod, boolean allTests)
          Ensures that all documents have been compiled since their last modification and then delegates the actual testing to _rawJUnitOpenTestDocs.
 void junitProject()
          Creates a JUnit test suite over all currently open documents and runs it.
 void nonTestCase(boolean isTestAll, boolean didCompileFail)
          Called from the JUnitTestManager if its given className is not a test case.
 void removeAllListeners()
          Removes all JUnitListeners from this model.
 void removeListener(JUnitListener listener)
          Remove a JUnitListener from the model.
 void resetJUnitErrors()
          Resets the junit error state to have no errors.
 void setForceTestSuffix(boolean b)
          set the forceTestSuffix flag that forces class names in projects to end in "Test
 void testEnded(String testName, boolean wasSuccessful, boolean causedError)
          Called when a particular test has ended.
 void testStarted(String testName)
          Called when a particular test is started.
 void testSuiteEnded(JUnitError[] errors)
          Called when a full suite of tests has finished running.
 void testSuiteStarted(int numTests)
          Called to indicate that a suite of tests has started running.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_log

private static Log _log
log for use in debugging


_notifier

private final JUnitEventNotifier _notifier
Manages listeners to this model.


_jvm

private final MainJVM _jvm
RMI interface to a secondary JVM for running tests. Using a second JVM prevents interactions and tests from corrupting the state of DrJava.


_compilerModel

private final CompilerModel _compilerModel
The compiler model. It contains a lock used to prevent simultaneous test and compile. It also tracks the number errors in the last compilation, which is required information if junit forces compilation.


_model

private final GlobalModel _model
The global model to which the JUnitModel belongs


_junitErrorModel

private volatile JUnitErrorModel _junitErrorModel
The error model containing all current JUnit errors.


_testInProgress

private volatile boolean _testInProgress
State flag to prevent starting new tests on top of old ones and to prevent resetting interactions after compilation is forced by unit testing. This field is NOT REDUNDANT, it is used in junitJVMReady.


_forceTestSuffix

private boolean _forceTestSuffix
State flag to record if test classes in projects must end in "Test"


_junitDoc

private final SwingDocument _junitDoc
The document used to display JUnit test results. Used only for testing.

Constructor Detail

DefaultJUnitModel

public DefaultJUnitModel(MainJVM jvm,
                         CompilerModel compilerModel,
                         GlobalModel model)
Main constructor.

Parameters:
jvm - RMI interface to a secondary JVM for running tests
compilerModel - the CompilerModel, used only as a lock to prevent simultaneous test and compile
model - used only for getSourceFile
Method Detail

setForceTestSuffix

public void setForceTestSuffix(boolean b)
Description copied from interface: JUnitModel
set the forceTestSuffix flag that forces class names in projects to end in "Test

Specified by:
setForceTestSuffix in interface JUnitModel

isTestInProgress

public boolean isTestInProgress()

addListener

public void addListener(JUnitListener listener)
Add a JUnitListener to the model.

Specified by:
addListener in interface JUnitModel
Parameters:
listener - a listener that reacts to JUnit events

removeListener

public void removeListener(JUnitListener listener)
Remove a JUnitListener from the model. If the listener is not currently listening to this model, this method has no effect.

Specified by:
removeListener in interface JUnitModel
Parameters:
listener - a listener that reacts to JUnit events

removeAllListeners

public void removeAllListeners()
Removes all JUnitListeners from this model.

Specified by:
removeAllListeners in interface JUnitModel

getJUnitDocument

public SwingDocument getJUnitDocument()
Used only for testing.

Specified by:
getJUnitDocument in interface JUnitModel

junitAll

public void junitAll()
Creates a JUnit test suite over all currently open documents and runs it. If the class file associated with a file is not a test case, it is ignored.

Specified by:
junitAll in interface JUnitModel

junitProject

public void junitProject()
Creates a JUnit test suite over all currently open documents and runs it. If a class file associated with a source file is not a test case, it will be ignored. Synchronized against the compiler model to prevent testing and compiling at the same time, which would create invalid results.

Specified by:
junitProject in interface JUnitModel

junitDocs

public void junitDocs(List<OpenDefinitionsDocument> lod)
Description copied from interface: JUnitModel
Runs JUnit over a list of documents. Synchronized against the compiler model to prevent testing and compiling at the same time, which would create invalid results.

Specified by:
junitDocs in interface JUnitModel
Parameters:
lod - the list of documents that are to be run through JUnit testing.

junit

public void junit(OpenDefinitionsDocument doc)
           throws ClassNotFoundException,
                  IOException
Runs JUnit on the current document. Forces the user to compile all open documents before proceeding.

Specified by:
junit in interface JUnitModel
Parameters:
doc - the document to be run under JUnit
Throws:
ClassNotFoundException
IOException

junitOpenDefDocs

private void junitOpenDefDocs(List<OpenDefinitionsDocument> lod,
                              boolean allTests)
Ensures that all documents have been compiled since their last modification and then delegates the actual testing to _rawJUnitOpenTestDocs.


_rawJUnitOpenDefDocs

private void _rawJUnitOpenDefDocs(List<OpenDefinitionsDocument> lod,
                                  boolean allTests)
Runs all TestCases in the document list lod; assumes all documents have been compiled. It finds the TestCase classes by searching the build directories for the documents. Note: caller must respond to thrown exceptions by invoking _junitUnitInterrupted (to run hourglassOff() and reset the unit testing UI).


_notifyJUnitStarted

private void _notifyJUnitStarted()
Helper method to notify JUnitModel listeners that JUnit test suite execution has started.


_notifyJUnitEnded

private void _notifyJUnitEnded()
Helper method to notify JUnitModel listeners that JUnit test suite execution has just ended.


_notifyCompileBeforeJUnit

private void _notifyCompileBeforeJUnit(CompilerListener testAfterCompile,
                                       List<OpenDefinitionsDocument> outOfSync)
Helper method to notify JUnitModel listeners that all open files must be compiled before JUnit is run.


_notifyNonTestCase

private void _notifyNonTestCase(boolean testAll,
                                boolean didCompileFail)
Helper method to notify JUnitModel listeners that JUnit aborted before any tests could be run.


getCanonicalPath

private String getCanonicalPath(File f)
                         throws IOException
Throws:
IOException

getJUnitErrorModel

public JUnitErrorModel getJUnitErrorModel()
Gets the JUnitErrorModel, which contains error info for the last test run.

Specified by:
getJUnitErrorModel in interface JUnitModel

resetJUnitErrors

public void resetJUnitErrors()
Resets the junit error state to have no errors.

Specified by:
resetJUnitErrors in interface JUnitModel

nonTestCase

public void nonTestCase(boolean isTestAll,
                        boolean didCompileFail)
Called from the JUnitTestManager if its given className is not a test case.

Specified by:
nonTestCase in interface JUnitModel
Specified by:
nonTestCase in interface JUnitModelCallback
Parameters:
isTestAll - whether or not it was a use of the test all button
didCompileFail - whether or not a compile before this JUnit attempt failed

classFileError

public void classFileError(ClassFileError e)
Called to indicate that an illegal class file was encountered

Specified by:
classFileError in interface JUnitModelCallback
Parameters:
e - the ClassFileObject describing the error.

testSuiteStarted

public void testSuiteStarted(int numTests)
Called to indicate that a suite of tests has started running.

Specified by:
testSuiteStarted in interface JUnitModelCallback
Parameters:
numTests - The number of tests in the suite to be run.

testStarted

public void testStarted(String testName)
Called when a particular test is started.

Specified by:
testStarted in interface JUnitModelCallback
Parameters:
testName - The name of the test being started.

testEnded

public void testEnded(String testName,
                      boolean wasSuccessful,
                      boolean causedError)
Called when a particular test has ended.

Specified by:
testEnded in interface JUnitModelCallback
Parameters:
testName - The name of the test that has ended.
wasSuccessful - Whether the test passed or not.
causedError - If not successful, whether the test caused an error or simply failed.

testSuiteEnded

public void testSuiteEnded(JUnitError[] errors)
Called when a full suite of tests has finished running. Does not necessarily run in event thread.

Specified by:
testSuiteEnded in interface JUnitModelCallback
Parameters:
errors - The array of errors from all failed tests in the suite.

getFileForClassName

public File getFileForClassName(String className)
Called when the JUnitTestManager wants to open a file that is not currently open.

Specified by:
getFileForClassName in interface JUnitModelCallback
Parameters:
className - the name of the class for which we want to find the file
Returns:
the file associated with the given class

getClassPath

public Iterable<File> getClassPath()
Returns the current classpath in use by the JUnit JVM.

Specified by:
getClassPath in interface JUnitModelCallback

junitJVMReady

public void junitJVMReady()
Called when the JVM used for unit tests has registered. Does not necessarily run in even thread.

Specified by:
junitJVMReady in interface JUnitModelCallback