0001: /*BEGIN_COPYRIGHT_BLOCK
0002: *
0003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
0004: * All rights reserved.
0005: *
0006: * Redistribution and use in source and binary forms, with or without
0007: * modification, are permitted provided that the following conditions are met:
0008: * * Redistributions of source code must retain the above copyright
0009: * notice, this list of conditions and the following disclaimer.
0010: * * Redistributions in binary form must reproduce the above copyright
0011: * notice, this list of conditions and the following disclaimer in the
0012: * documentation and/or other materials provided with the distribution.
0013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
0014: * names of its contributors may be used to endorse or promote products
0015: * derived from this software without specific prior written permission.
0016: *
0017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0028: *
0029: * This software is Open Source Initiative approved Open Source Software.
0030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
0031: *
0032: * This file is part of DrJava. Download the current version of this project
0033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
0034: *
0035: * END_COPYRIGHT_BLOCK*/
0036:
0037: package edu.rice.cs.drjava.model;
0038:
0039: import edu.rice.cs.drjava.DrJava;
0040: import edu.rice.cs.drjava.model.compiler.CompilerListener;
0041: import edu.rice.cs.drjava.model.repl.InteractionsDocument;
0042: import edu.rice.cs.drjava.model.junit.JUnitModel;
0043: import edu.rice.cs.util.FileOpenSelector;
0044: import edu.rice.cs.plt.io.IOUtil;
0045: import edu.rice.cs.util.FileOps;
0046: import edu.rice.cs.util.Log;
0047: import edu.rice.cs.util.OperationCanceledException;
0048: import edu.rice.cs.util.StringOps;
0049: import edu.rice.cs.util.UnexpectedException;
0050: import edu.rice.cs.util.classloader.ClassFileError;
0051: import edu.rice.cs.util.swing.Utilities;
0052: import edu.rice.cs.util.swing.AsyncTask;
0053: import edu.rice.cs.util.text.EditDocumentException;
0054: import edu.rice.cs.util.text.EditDocumentInterface;
0055:
0056: import javax.swing.text.BadLocationException;
0057: import java.io.File;
0058: import java.io.IOException;
0059: import java.util.regex.*;
0060: import java.util.List;
0061:
0062: /** Base class for tests over the {@link GlobalModel}.
0063: *
0064: * This class provides a number of convenience methods for testing the GlobalModel. It also contains a model instance
0065: * (reset in {@link #setUp} and a temporary directory that's created per test invocation (and subsequently cleaned in
0066: * {@link #tearDown}. This reduces the burden for such file management stuff in the test cases themselves.
0067: *
0068: * @version $Id: GlobalModelTestCase.java 4255 2007-08-28 19:17:37Z mgricken $
0069: */
0070: public abstract class GlobalModelTestCase extends MultiThreadedTestCase {
0071:
0072: protected static final Log _log = new Log("GlobalModel.txt", false);
0073:
0074: protected volatile DefaultGlobalModel _model;
0075: protected volatile File _tempDir;
0076: protected volatile OpenDefinitionsDocument _doc; // the working document in some shared set up routines
0077:
0078: protected static final String FOO_TEXT = "class DrJavaTestFoo {}";
0079: protected static final String BAR_TEXT = "class DrJavaTestBar {}";
0080: protected static final String BAZ_TEXT = "class DrJavaTestBaz extends DrJavaTestFoo { public static int x = 3; }";
0081: protected static final String FOO_MISSING_CLOSE_TEXT = "class DrJavaTestFoo {";
0082: protected static final String FOO_PACKAGE_AFTER_IMPORT = "import java.util.*;\npackage a;\n"
0083: + FOO_TEXT;
0084: protected static final String FOO_PACKAGE_INSIDE_CLASS = "class DrJavaTestFoo { package a; }";
0085: protected static final String FOO_PACKAGE_AS_FIELD = "class DrJavaTestFoo { int package; }";
0086: protected static final String FOO_PACKAGE_AS_FIELD_2 = "class DrJavaTestFoo { int package = 5; }";
0087: protected static final String FOO_PACKAGE_AS_PART_OF_FIELD = "class DrJavaTestFoo { int cur_package = 5; }";
0088:
0089: public GlobalModelTestCase() {
0090: _log.log("Constructing a " + this );
0091: }
0092:
0093: /** Setup for each test case, which does the following.
0094: * <OL>
0095: * <LI>
0096: * Creates a new GlobalModel in {@link #_model} for each test case run.
0097: * </LI>
0098: * <LI>
0099: * Creates a new temporary directory in {@link #_tempDir}.
0100: * </LI>
0101: * </OL>
0102: */
0103: public void setUp() throws Exception {
0104: _log.log("Setting up " + this );
0105: super .setUp();
0106: _model = new TestGlobalModel();
0107: _log.log("Global model created for " + this );
0108: DrJava.getConfig().resetToDefaults();
0109: String user = System.getProperty("user.name");
0110: _tempDir = /* IOUtil.createAndMarkTempDirectory */FileOps
0111: .createTempDirectory("DrJava-test-" + user /*, ""*/);
0112: // Wait until model has connected to slave JVM
0113: _log.log("Ensuring that interpreter is connected in " + this );
0114: _model._jvm.ensureInterpreterConnected();
0115: _log.log("Ensured that intepreter is connected in " + this );
0116: _model.setResetAfterCompile(false);
0117: _log.log("Completed (GlobalModelTestCase) set up of " + this );
0118:
0119: // _model.getOpenDefinitionsDocuments().get(0).saveFile(new FileSelector(new File(_tempDir, "blank document")));
0120: // super.setUp();
0121: }
0122:
0123: /** Teardown for each test case, which recursively deletes the temporary directory created in setUp. */
0124: public void tearDown() throws Exception {
0125: _log.log("Tearing down " + this );
0126: boolean ret = IOUtil.deleteRecursively(_tempDir);
0127: assertTrue("delete temp directory " + _tempDir, ret);
0128:
0129: _model.dispose();
0130: _tempDir = null;
0131: _model = null;
0132:
0133: super .tearDown();
0134: _log.log("Completed tear down of " + this );
0135: }
0136:
0137: /** Clear all old text and insert the given text. */
0138: protected void changeDocumentText(String s,
0139: OpenDefinitionsDocument doc) throws BadLocationException {
0140: doc.clear();
0141: assertLength(0, doc);
0142: doc.append(s, null);
0143: assertModified(true, doc);
0144: assertContents(s, doc);
0145: }
0146:
0147: /** Create a new temporary file in _tempDir. */
0148: protected File tempFile() throws IOException {
0149: File f = File.createTempFile("DrJava-test", ".java", _tempDir)
0150: .getCanonicalFile();
0151: // System.err.println("temp file created with name " + f);
0152: return f;
0153: }
0154:
0155: /** Create a new temporary file in _tempDir. Calls with the same int will return the same filename, while calls
0156: * with different ints will return different filenames.
0157: */
0158: protected File tempFile(int i) throws IOException {
0159: return File
0160: .createTempFile("DrJava-test" + i, ".java", _tempDir)
0161: .getCanonicalFile();
0162: }
0163:
0164: /** Create a new temporary directory in _tempDir. */
0165: protected File tempDirectory() throws IOException {
0166: return IOUtil.createAndMarkTempDirectory("DrJava-test", "",
0167: _tempDir);
0168: }
0169:
0170: protected File createFile(String name) {
0171: return new File(_tempDir, name);
0172: }
0173:
0174: /** Given a .java file and a class file name, returns the corresponding .class file. */
0175: protected File classForJava(File sourceFile, String className) {
0176: assertTrue(sourceFile.getName().endsWith(".java"));
0177: String cname = className + ".class";
0178: return new File(sourceFile.getParent(), cname);
0179: }
0180:
0181: /** Creates a new temporary file and writes the given text to it.
0182: * The File object for the new file is returned.
0183: */
0184: protected File writeToNewTempFile(String text) throws IOException {
0185: File temp = tempFile();
0186: IOUtil.writeStringToFile(temp, text);
0187: return temp;
0188: }
0189:
0190: /** Creates and returns a new document, makes sure newFile is fired, and then adds some text. When this method is
0191: * done newCount is reset to 0.
0192: * @return the new modified document
0193: */
0194: protected OpenDefinitionsDocument setupDocument(String text)
0195: throws BadLocationException {
0196: TestListener listener = new TestListener() {
0197: public synchronized void newFileCreated(
0198: OpenDefinitionsDocument doc) {
0199: newCount++;
0200: }
0201: };
0202:
0203: _model.addListener(listener);
0204:
0205: // Open a new document
0206: int numOpen = _model.getOpenDefinitionsDocuments().size();
0207: Utilities.invokeAndWait(new Runnable() {
0208: public void run() {
0209: _doc = _model.newFile();
0210: }
0211: });
0212:
0213: assertNumOpenDocs(numOpen + 1);
0214:
0215: listener.assertNewCount(1);
0216: assertLength(0, _doc);
0217: assertModified(false, _doc);
0218:
0219: changeDocumentText(text, _doc);
0220: assertModified(true, _doc);
0221: _model.removeListener(listener);
0222:
0223: return _doc;
0224: }
0225:
0226: /** Compiles a new file with the given text. The compile is expected to succeed and it is checked to make sure it
0227: * worked reasonably. This method does not return until the Interactions JVM has reset and is ready to use.
0228: * @param text Code for the class to be compiled
0229: * @param file File to save the class in
0230: * @return Document after it has been saved and compiled
0231: */
0232: protected synchronized OpenDefinitionsDocument doCompile(
0233: String text, File file) throws IOException,
0234: BadLocationException, InterruptedException {
0235:
0236: OpenDefinitionsDocument doc = setupDocument(text);
0237: doCompile(doc, file);
0238: return doc;
0239: }
0240:
0241: /** Saves to the given file, and then compiles the given document. The compile is expected to succeed and it is
0242: * checked to make sure it worked reasonably. This method does not return until the Interactions JVM has reset
0243: * and is ready to use.
0244: * @param doc Document containing the code to be compiled
0245: * @param file File to save the class in
0246: */
0247: protected void doCompile(OpenDefinitionsDocument doc, File file)
0248: throws IOException, InterruptedException {
0249: doc.saveFile(new FileSelector(file));
0250:
0251: // Perform a mindless interpretation to force interactions to reset (only to simplify this method)
0252: try {
0253: interpret("0");
0254: } catch (EditDocumentException e) {
0255: throw new UnexpectedException(e);
0256: }
0257: Utilities.clearEventQueue();
0258:
0259: _model.setResetAfterCompile(true);
0260: CompileShouldSucceedListener listener = new CompileShouldSucceedListener(
0261: true);
0262: _model.addListener(listener);
0263:
0264: listener.logCompileStart();
0265:
0266: doc.startCompile();
0267: Utilities.clearEventQueue();
0268:
0269: if (_model.getCompilerModel().getNumErrors() > 0)
0270: fail("compile failed: " + getCompilerErrorString());
0271:
0272: listener.waitCompileDone();
0273:
0274: listener.checkCompileOccurred();
0275: assertCompileErrorsPresent(false);
0276:
0277: listener.waitResetDone();
0278: Utilities.clearEventQueue();
0279: _model.removeListener(listener);
0280: }
0281:
0282: /** Returns a string with all compiler errors. */
0283: protected String getCompilerErrorString() {
0284: final StringBuilder buf = new StringBuilder();
0285: buf.append(" compiler error(s):\n");
0286: buf.append(_model.getCompilerModel().getCompilerErrorModel()
0287: .toString());
0288: return buf.toString();
0289: }
0290:
0291: /** Puts the given input into the interactions document and then interprets it, returning the result that was put
0292: * into the interactions document. This assumes the interactions document is in a state with no text after the
0293: * prompt. To be sure this is the case, you can reset interactions first. This method provides its own listener
0294: * to synchronized with the completion of the interaction.
0295: *
0296: * @param input text to interpret
0297: * @return The output from this interpretation, in String form, as it was printed to the interactions document.
0298: */
0299: protected String interpret(String input)
0300: throws EditDocumentException {
0301:
0302: InteractionsDocument interactionsDoc = _model
0303: .getInteractionsDocument();
0304: Utilities.clearEventQueue();
0305:
0306: interactionsDoc.setInProgress(false); // for some reason, the inProgress state can be true when interpret is invoked
0307: interactionsDoc.append(input,
0308: InteractionsDocument.DEFAULT_STYLE);
0309:
0310: Utilities.clearEventQueue();
0311:
0312: // skip the right length for the newline
0313: final int newLineLen = 1; // Was StringOps.EOL.length(); but Swing uses '\n' for newLine
0314: final int resultsStartLocation = interactionsDoc.getLength()
0315: + newLineLen;
0316:
0317: InteractionListener listener = new InteractionListener();
0318:
0319: _model.addListener(listener);
0320: listener.logInteractionStart();
0321: try {
0322: _model.interpretCurrentInteraction();
0323: listener.waitInteractionDone();
0324: } catch (InterruptedException ie) {
0325: throw new UnexpectedException(ie);
0326: }
0327: Utilities.clearEventQueue();
0328: _model.removeListener(listener);
0329:
0330: listener.assertInteractionStartCount(1);
0331: listener.assertInteractionEndCount(1);
0332:
0333: // skip the right length for the newline
0334: interactionsDoc.acquireReadLock();
0335: try {
0336: final int resultsEndLocation = interactionsDoc.getLength()
0337: - newLineLen - interactionsDoc.getPrompt().length();
0338:
0339: final int resultsLen = resultsEndLocation
0340: - resultsStartLocation;
0341: _log.log("resultsStartLoc = " + resultsStartLocation
0342: + " resultsEndLocation = " + resultsEndLocation);
0343: _log.log("Contents = '"
0344: + interactionsDoc.getDocText(0,
0345: resultsEndLocation + 1) + "'");
0346: if (resultsLen <= 0)
0347: return "";
0348: return interactionsDoc.getDocText(resultsStartLocation,
0349: resultsLen);
0350: } finally {
0351: interactionsDoc.releaseReadLock();
0352: }
0353: }
0354:
0355: /** Appends the input string to the interactions pane and interprets it. */
0356: protected void interpretIgnoreResult(String input)
0357: throws EditDocumentException {
0358: InteractionsDocument interactionsDoc = _model
0359: .getInteractionsDocument();
0360: interactionsDoc.append(input,
0361: InteractionsDocument.DEFAULT_STYLE);
0362: _model.interpretCurrentInteraction();
0363: }
0364:
0365: /** Asserts that the given string exists in the Interactions Document. */
0366: protected void assertInteractionsContains(String text)
0367: throws EditDocumentException {
0368: _assertInteractionContainsHelper(text, true);
0369: }
0370:
0371: /** Asserts that the given string does not exist in the Interactions Document. */
0372: protected void assertInteractionsDoesNotContain(String text)
0373: throws EditDocumentException {
0374: _assertInteractionContainsHelper(text, false);
0375: }
0376:
0377: private void _assertInteractionContainsHelper(String text,
0378: boolean shouldContain) throws EditDocumentException {
0379:
0380: String interactText = getInteractionsText();
0381: int contains = interactText.lastIndexOf(text);
0382: assertTrue("Interactions document should "
0383: + (shouldContain ? "" : "not ") + "contain:\n" + text
0384: + "\nActual contents of Interactions document:\n"
0385: + interactText, (contains != -1) == shouldContain);
0386: }
0387:
0388: /** Asserts that the text in the Interactions Document matches the given regex. */
0389: protected void assertInteractionsMatches(String regex)
0390: throws EditDocumentException {
0391: _assertInteractionMatchesHelper(regex, true);
0392: }
0393:
0394: /** Asserts that the text in the Interactions Document does NOT match the given regex. */
0395: protected void assertInteractionsDoesNotMatch(String regex)
0396: throws EditDocumentException {
0397: _assertInteractionMatchesHelper(regex, false);
0398: }
0399:
0400: private void _assertInteractionMatchesHelper(String regex,
0401: boolean shouldMatch) throws EditDocumentException {
0402:
0403: String interactText = getInteractionsText();
0404: boolean matches = Pattern.compile(regex,
0405: Pattern.MULTILINE | Pattern.DOTALL).matcher(
0406: interactText).matches();
0407: assertTrue("Interactions document should "
0408: + (shouldMatch ? "" : "not ") + "match:\n" + regex
0409: + "\nActual contents of Interactions document:\n"
0410: + interactText, matches == shouldMatch);
0411: }
0412:
0413: /** Returns the current contents of the interactions document */
0414: protected String getInteractionsText() throws EditDocumentException {
0415: InteractionsDocument doc = _model.getInteractionsDocument();
0416: return doc.getText();
0417: }
0418:
0419: protected void assertNumOpenDocs(int num) {
0420: assertEquals("number of open documents", num, _model
0421: .getOpenDefinitionsDocuments().size());
0422: }
0423:
0424: protected void assertModified(boolean b, OpenDefinitionsDocument doc) {
0425: assertEquals("document isModifiedSinceSave", b, doc
0426: .isModifiedSinceSave());
0427: }
0428:
0429: protected void assertLength(int len, OpenDefinitionsDocument doc) {
0430: assertEquals("document length", len, doc.getLength());
0431: }
0432:
0433: protected void assertContents(String s, OpenDefinitionsDocument doc)
0434: throws BadLocationException {
0435: assertEquals("document contents", s, doc.getText());
0436: }
0437:
0438: protected void assertCompileErrorsPresent(boolean b) {
0439: assertCompileErrorsPresent("", b);
0440: }
0441:
0442: protected void assertCompileErrorsPresent(String name, boolean b) {
0443: //CompilerError[] errors = _model.getCompileErrors();
0444: int numErrors = _model.getCompilerModel().getNumErrors();
0445:
0446: if (name.length() > 0)
0447: name += ": ";
0448:
0449: //StringBuffer buf = new StringBuffer();
0450: //for (int i = 0; i < errors.length; i++) {
0451: // buf.append("\nerror #" + i + ": " + errors[i]);
0452: //}
0453:
0454: assertEquals(name + "compile errors > 0? numErrors ="
0455: + numErrors, b, numErrors > 0);
0456: }
0457:
0458: // These exceptions are specially used only in this test case.
0459: // They are used to verify that the code blocks
0460: public static class OverwriteException extends RuntimeException {
0461: }
0462:
0463: public static class OpenWarningException extends RuntimeException {
0464: }
0465:
0466: public static class FileMovedWarningException extends
0467: RuntimeException {
0468: }
0469:
0470: public static class WarningFileSelector implements
0471: FileOpenSelector, FileSaveSelector {
0472: private volatile File _file;
0473:
0474: public WarningFileSelector(File f) {
0475: _file = f;
0476: }
0477:
0478: public File getFile() throws OperationCanceledException {
0479: return _file;
0480: }
0481:
0482: public File[] getFiles() throws OperationCanceledException {
0483: return new File[] { _file };
0484: }
0485:
0486: public boolean warnFileOpen(File f) {
0487: throw new OpenWarningException();
0488: }
0489:
0490: public boolean verifyOverwrite() {
0491: throw new OverwriteException();
0492: }
0493:
0494: public boolean shouldSaveAfterFileMoved(
0495: OpenDefinitionsDocument doc, File oldFile) {
0496: throw new FileMovedWarningException();
0497: }
0498: }
0499:
0500: /** This class is used by several test cases in Compile Tests that expect incorrect behavior concerning the saving
0501: * of files. This special FileSelector is included to ensure compliance with these test cases, for which the
0502: * intricacies of saving files are unimportant. The only FileSelector that honest-to-supreme-deity matters is
0503: * is DefaultGlobalModel.ConcreteOpenDefDoc, which is much more like WarningFileSelector
0504: */
0505:
0506: public static class FileSelector implements FileOpenSelector,
0507: FileSaveSelector {
0508: private volatile File _file1, _file2;
0509:
0510: public FileSelector(File f) {
0511: _file1 = f;
0512: }
0513:
0514: public FileSelector(File f1, File f2) {
0515: _file1 = f1;
0516: _file2 = f2;
0517: }
0518:
0519: public File getFile() throws OperationCanceledException {
0520: return _file1;
0521: }
0522:
0523: public File[] getFiles() throws OperationCanceledException {
0524: if (_file2 != null)
0525: return new File[] { _file1, _file2 };
0526: else
0527: return new File[] { _file1 };
0528: }
0529:
0530: public boolean warnFileOpen(File f) {
0531: return true;
0532: }
0533:
0534: public boolean verifyOverwrite() {
0535: return true;
0536: }
0537:
0538: public boolean shouldSaveAfterFileMoved(
0539: OpenDefinitionsDocument doc, File oldFile) {
0540: return true;
0541: }
0542: }
0543:
0544: public static class CancelingSelector implements FileOpenSelector,
0545: FileSaveSelector {
0546: public File getFile() throws OperationCanceledException {
0547: throw new OperationCanceledException();
0548: }
0549:
0550: public File[] getFiles() throws OperationCanceledException {
0551: throw new OperationCanceledException();
0552: }
0553:
0554: public boolean warnFileOpen(File f) {
0555: return true;
0556: }
0557:
0558: public boolean verifyOverwrite() {
0559: return true;
0560: }
0561:
0562: public boolean shouldSaveAfterFileMoved(
0563: OpenDefinitionsDocument doc, File oldFile) {
0564: return true;
0565: }
0566: }
0567:
0568: /** A GlobalModelListener for testing. By default it expects no events to be fired. To customize,
0569: * subclass and override one or more methods.
0570: */
0571: public static class TestListener implements GlobalModelListener {
0572: /** Remembers when this listener was created. */
0573: protected volatile Exception _startupTrace;
0574:
0575: protected volatile int fileNotFoundCount;
0576: protected volatile int newCount;
0577: protected volatile int openCount;
0578: protected volatile int closeCount;
0579: protected volatile int saveCount;
0580: protected volatile int canAbandonCount;
0581: protected volatile int quitFileCount;
0582: protected volatile int classFileErrorCount;
0583: protected volatile int compileStartCount;
0584: protected volatile int compileEndCount;
0585: protected volatile int runStartCount;
0586: protected volatile int junitStartCount;
0587: protected volatile int junitSuiteStartedCount;
0588: protected volatile int junitTestStartedCount;
0589: protected volatile int junitTestEndedCount;
0590: protected volatile int junitEndCount;
0591: protected volatile int interactionStartCount;
0592: protected volatile int interactionEndCount;
0593: protected volatile int interactionErrorCount;
0594: protected volatile int interpreterResettingCount;
0595: protected volatile int interpreterReadyCount;
0596: protected volatile int interpreterExitedCount;
0597: protected volatile int interpreterResetFailedCount;
0598: protected volatile int interpreterChangedCount;
0599: //protected int interactionCaretPositionChangedCount;
0600: protected volatile int consoleResetCount;
0601: protected volatile int saveBeforeCompileCount;
0602: //protected int saveBeforeRunCount;
0603: protected volatile int compileBeforeJUnitCount;
0604: protected volatile int saveBeforeJavadocCount;
0605: //protected int saveBeforeDebugCount;
0606: protected volatile int nonTestCaseCount;
0607: protected volatile int lastExitStatus;
0608: protected volatile int fileRevertedCount;
0609: protected volatile int shouldRevertFileCount;
0610: protected volatile int undoableEditCount;
0611: protected volatile int interactionIncompleteCount;
0612: protected volatile int filePathContainsPoundCount;
0613:
0614: public TestListener() {
0615: _startupTrace = new Exception();
0616: resetCounts();
0617: }
0618:
0619: public synchronized void resetCounts() {
0620: fileNotFoundCount = 0;
0621: newCount = 0;
0622: openCount = 0;
0623: closeCount = 0;
0624: saveCount = 0;
0625: canAbandonCount = 0;
0626: quitFileCount = 0;
0627: classFileErrorCount = 0;
0628: compileStartCount = 0;
0629: compileEndCount = 0;
0630: runStartCount = 0;
0631: junitStartCount = 0;
0632: junitSuiteStartedCount = 0;
0633: junitTestStartedCount = 0;
0634: junitTestEndedCount = 0;
0635: junitEndCount = 0;
0636: interactionStartCount = 0;
0637: interactionEndCount = 0;
0638: interactionErrorCount = 0;
0639: interpreterChangedCount = 0;
0640: //interactionCaretPositionChangedCount = 0;
0641: consoleResetCount = 0;
0642: interpreterResettingCount = 0;
0643: interpreterReadyCount = 0;
0644: interpreterExitedCount = 0;
0645: interpreterResetFailedCount = 0;
0646: saveBeforeCompileCount = 0;
0647: //saveBeforeRunCount = 0;
0648: compileBeforeJUnitCount = 0;
0649: saveBeforeJavadocCount = 0;
0650: //saveBeforeDebugCount = 0;
0651: nonTestCaseCount = 0;
0652: lastExitStatus = 0;
0653: fileRevertedCount = 0;
0654: shouldRevertFileCount = 0;
0655: undoableEditCount = 0;
0656: interactionIncompleteCount = 0;
0657: filePathContainsPoundCount = 0;
0658: }
0659:
0660: public void projectModified() {
0661: }
0662:
0663: public void projectOpened(File pfile, FileOpenSelector files) {
0664: }
0665:
0666: public void projectClosed() {
0667: }
0668:
0669: public void projectBuildDirChanged() {
0670: }
0671:
0672: public void projectWorkDirChanged() {
0673: }
0674:
0675: public void projectRunnableChanged() {
0676: }
0677:
0678: public void currentDirectoryChanged(File dir) {
0679: }
0680:
0681: /** Appends the stack trace from the listener's creation to the end of the given failure message. */
0682: public void listenerFail(String message) {
0683: String header = "\nTestListener creation stack trace:\n"
0684: + StringOps.getStackTrace(_startupTrace);
0685: MultiThreadedTestCase.listenerFail(message + header);
0686: }
0687:
0688: public void assertFileNotFoundCount(int i) {
0689: assertEquals("number of times fileNotFound fired", i,
0690: fileNotFoundCount);
0691: }
0692:
0693: public void assertAbandonCount(int i) {
0694: assertEquals("number of times canAbandon fired", i,
0695: canAbandonCount);
0696: }
0697:
0698: public void assertQuitFileCount(int i) {
0699: assertEquals("number of times quitFile fired", i,
0700: quitFileCount);
0701: }
0702:
0703: public void assertClassFileErrorCount(int i) {
0704: assertEquals("number of times classFileError fired", i,
0705: classFileErrorCount);
0706: }
0707:
0708: public void assertNewCount(int i) {
0709: assertEquals("number of times newFile fired", i, newCount);
0710: }
0711:
0712: public void assertOpenCount(int i) {
0713: assertEquals("number of times openFile fired", i, openCount);
0714: }
0715:
0716: public void assertCloseCount(int i) {
0717: assertEquals("number of times closeFile fired", i,
0718: closeCount);
0719: }
0720:
0721: public void assertSaveCount(int i) {
0722: assertEquals("number of times saveFile fired", i, saveCount);
0723: }
0724:
0725: public void assertJUnitStartCount(int i) {
0726: assertEquals("number of times junitStarted fired", i,
0727: junitStartCount);
0728: }
0729:
0730: public void assertJUnitSuiteStartedCount(int i) {
0731: assertEquals("number of times junitSuiteStarted fired", i,
0732: junitSuiteStartedCount);
0733: }
0734:
0735: public void assertJUnitTestStartedCount(int i) {
0736: assertEquals("number of times junitTestStarted fired", i,
0737: junitTestStartedCount);
0738: }
0739:
0740: public void assertJUnitTestEndedCount(int i) {
0741: assertEquals("number of times junitTestEnded fired", i,
0742: junitTestEndedCount);
0743: }
0744:
0745: public void assertJUnitEndCount(int i) {
0746: assertEquals("number of times junitEnded fired", i,
0747: junitEndCount);
0748: }
0749:
0750: public void assertInteractionStartCount(int i) {
0751: assertEquals("number of times interactionStarted fired", i,
0752: interactionStartCount);
0753: }
0754:
0755: public void assertInteractionEndCount(int i) {
0756: assertEquals("number of times interactionEnded fired", i,
0757: interactionEndCount);
0758: }
0759:
0760: public void assertInteractionErrorCount(int i) {
0761: assertEquals("number of times interactionError fired", i,
0762: interactionErrorCount);
0763: }
0764:
0765: public void assertInterpreterChangedCount(int i) {
0766: assertEquals("number of times interpreterChanged fired", i,
0767: interpreterChangedCount);
0768: }
0769:
0770: // /** Not used */
0771: // public void assertInteractionCaretPositionChangedCount(int i) {
0772: // assertEquals("number of times interactionCaretPositionChanged fired", i, interactionCaretPositionChangedCount);
0773: // }
0774:
0775: public void assertCompileStartCount(int i) {
0776: assertEquals("number of times compileStarted fired", i,
0777: compileStartCount);
0778: }
0779:
0780: public void assertCompileEndCount(int i) {
0781: assertEquals("number of times compileEnded fired", i,
0782: compileEndCount);
0783: }
0784:
0785: public void assertRunStartCount(int i) {
0786: assertEquals("number of times runStarted fired", i,
0787: runStartCount);
0788: }
0789:
0790: public void assertInterpreterResettingCount(int i) {
0791: assertEquals("number of times interpreterResetting fired",
0792: i, interpreterResettingCount);
0793: }
0794:
0795: public void assertInterpreterReadyCount(int i) {
0796: assertEquals("number of times interpreterReady fired", i,
0797: interpreterReadyCount);
0798: }
0799:
0800: public void assertInterpreterResetFailedCount(int i) {
0801: assertEquals(
0802: "number of times interpreterResetFailed fired", i,
0803: interpreterResetFailedCount);
0804: }
0805:
0806: public void assertInterpreterExitedCount(int i) {
0807: assertEquals("number of times interpreterExited fired", i,
0808: interpreterExitedCount);
0809: }
0810:
0811: public void assertInteractionsErrorCount(int i) {
0812: assertEquals("number of times interactionsError fired", i,
0813: interactionErrorCount);
0814: }
0815:
0816: public void assertConsoleResetCount(int i) {
0817: assertEquals("number of times consoleReset fired", i,
0818: consoleResetCount);
0819: }
0820:
0821: public void assertSaveBeforeCompileCount(int i) {
0822: assertEquals("number of times saveBeforeCompile fired", i,
0823: saveBeforeCompileCount);
0824: }
0825:
0826: // /** Not used.*/
0827: // public void assertSaveBeforeRunCount(int i) {
0828: // assertEquals("number of times saveBeforeRun fired", i, saveBeforeRunCount);
0829: // }
0830: //
0831: public void assertCompileBeforeJUnitCount(int i) {
0832: assertEquals("number of times compileBeforeJUnit fired", i,
0833: compileBeforeJUnitCount);
0834: }
0835:
0836: public void assertSaveBeforeJavadocCount(int i) {
0837: assertEquals("number of times saveBeforeJavadoc fired", i,
0838: saveBeforeJavadocCount);
0839: }
0840:
0841: // /** Not used. */
0842: // public void assertSaveBeforeDebugCount(int i) {
0843: // assertEquals("number of times saveBeforeDebug fired",
0844: // i,
0845: // saveBeforeDebugCount);
0846: // }
0847:
0848: public void assertNonTestCaseCount(int i) {
0849: assertEquals("number of times nonTestCase fired", i,
0850: nonTestCaseCount);
0851: }
0852:
0853: public void assertFileRevertedCount(int i) {
0854: assertEquals("number of times fileReverted fired", i,
0855: fileRevertedCount);
0856: }
0857:
0858: public void assertUndoableEditCount(int i) {
0859: assertEquals("number of times undoableEditHappened fired",
0860: i, undoableEditCount);
0861: }
0862:
0863: public void assertShouldRevertFileCount(int i) {
0864: assertEquals("number of times shouldRevertFile fired", i,
0865: shouldRevertFileCount);
0866: }
0867:
0868: public void assertInteractionIncompleteCount(int i) {
0869: assertEquals("number of times interactionIncomplete fired",
0870: i, interactionIncompleteCount);
0871: }
0872:
0873: public <P, R> void executeAsyncTask(AsyncTask<P, R> task,
0874: P param, boolean showProgress, boolean lockUI) {
0875: listenerFail("executeAswyncTask fired unexpectedly");
0876: }
0877:
0878: public void handleAlreadyOpenDocument(
0879: OpenDefinitionsDocument doc) {
0880: listenerFail("handleAlreadyOpenDocument fired unexpectedly");
0881: }
0882:
0883: public void newFileCreated(OpenDefinitionsDocument doc) {
0884: listenerFail("newFileCreated fired unexpectedly");
0885: }
0886:
0887: public void filesNotFound(File... f) {
0888: listenerFail("fileNotFound fired unexpectedly");
0889: }
0890:
0891: // Recent revision defers opening files until the document for a file is requested forcing the following comment out
0892: public void fileOpened(OpenDefinitionsDocument doc) { /* listenerFail("fileOpened fired unexpectedly"); */
0893: }
0894:
0895: public void fileClosed(OpenDefinitionsDocument doc) {
0896: listenerFail("fileClosed fired unexpectedly");
0897: }
0898:
0899: public void fileSaved(OpenDefinitionsDocument doc) {
0900: listenerFail("fileSaved fired unexpectedly");
0901: }
0902:
0903: public void fileReverted(OpenDefinitionsDocument doc) {
0904: listenerFail("fileReverted fired unexpectedly");
0905: }
0906:
0907: public void undoableEditHappened() {
0908: listenerFail("undoableEditHappened fired unexpectedly");
0909: }
0910:
0911: public void saveBeforeCompile() {
0912: listenerFail("saveBeforeCompile fired unexpectedly");
0913: }
0914:
0915: public void junitStarted() {
0916: listenerFail("junitStarted fired unexpectedly");
0917: }
0918:
0919: public void junitClassesStarted() {
0920: listenerFail("junitAllStarted fired unexpectedly");
0921: }
0922:
0923: public void junitSuiteStarted(int numTests) {
0924: listenerFail("junitSuiteStarted fired unexpectedly");
0925: }
0926:
0927: public void junitTestStarted(String name) {
0928: listenerFail("junitTestStarted fired unexpectedly");
0929: }
0930:
0931: public void junitTestEnded(String name, boolean wasSuccessful,
0932: boolean causedError) {
0933: listenerFail("junitTestEnded fired unexpectedly");
0934: }
0935:
0936: public void junitEnded() {
0937: listenerFail("junitEnded fired unexpectedly");
0938: }
0939:
0940: public void javadocStarted() {
0941: listenerFail("javadocStarted fired unexpectedly");
0942: }
0943:
0944: public void javadocEnded(boolean success, File destDir,
0945: boolean allDocs) {
0946: listenerFail("javadocEnded fired unexpectedly");
0947: }
0948:
0949: public void interactionStarted() {
0950: listenerFail("interactionStarted fired unexpectedly");
0951: }
0952:
0953: public void interactionEnded() {
0954: listenerFail("interactionEnded fired unexpectedly");
0955: }
0956:
0957: public void interactionErrorOccurred(int offset, int length) {
0958: listenerFail("interpreterErrorOccurred fired unexpectedly");
0959: }
0960:
0961: public void interpreterChanged(boolean inProgress) {
0962: listenerFail("interpreterChanged fired unexpectedly");
0963: }
0964:
0965: // /**Not used */
0966: // public void interactionCaretPositionChanged(int pos) {
0967: // listenerFail("interactionCaretPosition fired unexpectedly");
0968: // }
0969:
0970: public void compileStarted() {
0971: listenerFail("compileStarted fired unexpectedly");
0972: }
0973:
0974: public void compileEnded(File workDir,
0975: List<? extends File> excludedFiles) {
0976: listenerFail("compileEnded fired unexpectedly");
0977: }
0978:
0979: public void runStarted(OpenDefinitionsDocument doc) {
0980: listenerFail("runStarted fired unexpectedly");
0981: }
0982:
0983: public void interpreterResetting() {
0984: listenerFail("interpreterResetting fired unexpectedly");
0985: }
0986:
0987: public void interpreterReady(File wd) {
0988: listenerFail("interpreterReady fired unexpectedly");
0989: }
0990:
0991: public void interpreterExited(int status) {
0992: listenerFail("interpreterExited(" + status
0993: + ") fired unexpectedly");
0994: }
0995:
0996: public void interpreterResetFailed(Throwable t) {
0997: listenerFail("interpreterResetFailed fired unexpectedly");
0998: }
0999:
1000: public void slaveJVMUsed() { /* not directly tested; ignore it */
1001: }
1002:
1003: public void consoleReset() {
1004: listenerFail("consoleReset fired unexpectedly");
1005: }
1006:
1007: public void saveUntitled() {
1008: listenerFail("saveUntitled fired unexpectedly");
1009: }
1010:
1011: public void compileBeforeJUnit(CompilerListener cl) {
1012: compileBeforeJUnitCount++;
1013: }
1014:
1015: public void saveBeforeJavadoc() {
1016: listenerFail("saveBeforeJavadoc fired unexpectedly");
1017: }
1018:
1019: public void nonTestCase(boolean isTestAll) {
1020: listenerFail("nonTestCase fired unexpectedly");
1021: }
1022:
1023: public boolean canAbandonFile(OpenDefinitionsDocument doc) {
1024: listenerFail("canAbandonFile fired unexpectedly");
1025: throw new UnexpectedException();
1026: }
1027:
1028: public boolean quitFile(OpenDefinitionsDocument doc) {
1029: listenerFail("quitFile fired unexpectedly");
1030: throw new UnexpectedException();
1031: }
1032:
1033: public void classFileError(ClassFileError e) {
1034: listenerFail("classFileError fired unexpectedly");
1035: }
1036:
1037: public boolean shouldRevertFile(OpenDefinitionsDocument doc) {
1038: listenerFail("shouldRevertfile fired unexpectedly");
1039: throw new UnexpectedException();
1040: }
1041:
1042: public void interactionIncomplete() {
1043: listenerFail("interactionIncomplete fired unexpectedly");
1044: }
1045:
1046: public void filePathContainsPound() {
1047: listenerFail("filePathContainsPound fired unexpectedly");
1048: }
1049:
1050: public void documentNotFound(OpenDefinitionsDocument d, File f) {
1051: listenerFail("documentNotFound fired unexpectedly");
1052: }
1053:
1054: public void activeDocumentChanged(OpenDefinitionsDocument active) { /* this event is not directly tested */
1055: }
1056:
1057: public void activeDocumentRefreshed(
1058: OpenDefinitionsDocument active) { /* this event is not directly tested */
1059: }
1060:
1061: public void focusOnDefinitionsPane() { /* this event is not dircectly tested */
1062: }
1063:
1064: public void focusOnLastFocusOwner() { /* this event is not dircectly tested */
1065: }
1066: }
1067:
1068: public static class InteractionListener extends TestListener {
1069: private volatile boolean _interactionDone = false; // records when the interaction is done
1070: private final Object _interactionLock = new Object(); // lock for _interactionDone
1071:
1072: private volatile boolean _resetDone = false; // records when the interaction is done
1073: private final Object _resetLock = new Object(); // lock for _interactionDone
1074:
1075: private volatile int _lastExitStatus = -1;
1076:
1077: /** Relying on the default constructor. */
1078:
1079: public synchronized void interactionStarted() {
1080: interactionStartCount++;
1081: }
1082:
1083: public void interactionEnded() {
1084: // assertInteractionStartCount(1);
1085:
1086: synchronized (this ) {
1087: interactionEndCount++;
1088: }
1089: synchronized (_interactionLock) {
1090: _interactionDone = true;
1091: _interactionLock.notify();
1092: }
1093: }
1094:
1095: public void interpreterExited(int status) {
1096: // Utilities.showDebug("GlobalModelOtherTest: interpreterExited");
1097: // assertInteractionStartCount(1);
1098: // assertInterpreterResettingCount(0);
1099: synchronized (this ) {
1100: interpreterExitedCount++;
1101: _lastExitStatus = status;
1102: }
1103: synchronized (_interactionLock) {
1104: _interactionDone = true;
1105: _interactionLock.notify();
1106: }
1107: }
1108:
1109: public void interpreterResetting() {
1110: assertInterpreterResettingCount(0);
1111: assertInterpreterReadyCount(0);
1112: synchronized (this ) {
1113: interpreterResettingCount++;
1114: }
1115: }
1116:
1117: public void interpreterReady(File wd) {
1118: // Utilities.showDebug("GlobalModelOtherTest: interpreterReady");
1119: synchronized (this ) {
1120: interpreterReadyCount++;
1121: }
1122: synchronized (_resetLock) {
1123: // assertInteractionStartCount(1);
1124: // assertInterpreterExitedCount(1);
1125: // assertInterpreterResettingCount(1);
1126: // Utilities.showDebug("GlobalModelOtherTest: notifying resetDone");
1127: _resetDone = true;
1128: _resetLock.notify();
1129: }
1130: }
1131:
1132: public void consoleReset() {
1133: assertConsoleResetCount(0);
1134: // assertCompileStartCount(1);
1135: // assertCompileEndCount(1);
1136: // don't care whether interactions or console are reset first
1137: synchronized (this ) {
1138: consoleResetCount++;
1139: }
1140: }
1141:
1142: public void resetConsoleResetCount() {
1143: consoleResetCount = 0;
1144: }
1145:
1146: public void logInteractionStart() {
1147: _interactionDone = false;
1148: _resetDone = false;
1149: }
1150:
1151: public void waitInteractionDone() throws InterruptedException {
1152: synchronized (_interactionLock) {
1153: while (!_interactionDone)
1154: _interactionLock.wait();
1155: }
1156: }
1157:
1158: public void waitResetDone() throws InterruptedException {
1159: synchronized (_resetLock) {
1160: while (!_resetDone)
1161: _resetLock.wait();
1162: }
1163: }
1164:
1165: public int getLastExitStatus() {
1166: return _lastExitStatus;
1167: }
1168: };
1169:
1170: /** A model listener for situations expecting a compilation to fail. The _expectReset flag determines if interactions
1171: * are reset after a compilation. The interactionsReset() method notifies when reset has occurred.
1172: */
1173: public static class CompileShouldSucceedListener extends
1174: InteractionListener {
1175: private volatile boolean _expectReset;
1176:
1177: private volatile boolean _compileDone = false; // records when compilaton is done
1178: private final Object _compileLock = new Object(); // lock for _compileDone
1179:
1180: public void logCompileStart() {
1181: logInteractionStart();
1182: _compileDone = false;
1183: }
1184:
1185: public void compile(OpenDefinitionsDocument doc)
1186: throws IOException, InterruptedException {
1187: logCompileStart();
1188: doc.startCompile();
1189: waitCompileDone();
1190: }
1191:
1192: public void waitCompileDone() throws InterruptedException {
1193: synchronized (_compileLock) {
1194: while (!_compileDone) {
1195: // System.err.println("Waiting for JUnit to complete");
1196: _compileLock.wait();
1197: }
1198: }
1199: }
1200:
1201: private void _notifyCompileDone() {
1202: synchronized (_compileLock) {
1203: _compileDone = true;
1204: _compileLock.notify();
1205: }
1206: }
1207:
1208: /** Standard constructor.
1209: * @param expectReset Whether to listen for interactions being
1210: */
1211: public CompileShouldSucceedListener(boolean expectReset) {
1212: _expectReset = expectReset;
1213: }
1214:
1215: public CompileShouldSucceedListener() {
1216: this (false);
1217: }
1218:
1219: // public boolean notDone() { return ! _interactionsReset; }
1220:
1221: @Override
1222: public void newFileCreated(OpenDefinitionsDocument doc) { /* ingore this operation */
1223: }
1224:
1225: @Override
1226: public void compileStarted() {
1227: // Utilities.showDebug("compileStarted called in CSSListener");
1228: assertCompileStartCount(0);
1229: assertCompileEndCount(0);
1230: assertInterpreterResettingCount(0);
1231: assertInterpreterReadyCount(0);
1232: assertConsoleResetCount(0);
1233: synchronized (this ) {
1234: compileStartCount++;
1235: }
1236: }
1237:
1238: @Override
1239: public void compileEnded(File workDir,
1240: List<? extends File> excludedFiles) {
1241: // Utilities.showDebug("compileEnded called in CSSListener");
1242: assertCompileEndCount(0);
1243: assertCompileStartCount(1);
1244: assertInterpreterResettingCount(0);
1245: assertInterpreterReadyCount(0);
1246: assertConsoleResetCount(0);
1247: synchronized (this ) {
1248: compileEndCount++;
1249: }
1250: _notifyCompileDone();
1251: }
1252:
1253: public void checkCompileOccurred() {
1254: assertCompileEndCount(1);
1255: assertCompileStartCount(1);
1256: // if (_expectReset) {
1257: // assertInterpreterResettingCount(1);
1258: // assertInterpreterReadyCount(1);
1259: // }
1260: // else {
1261: // assertInterpreterResettingCount(0);
1262: // assertInterpreterReadyCount(0);
1263: // }
1264:
1265: // Note: console is no longer reset after a compile
1266: //assertConsoleResetCount(1);
1267: }
1268: }
1269:
1270: /** A model listener for situations expecting a compilation to fail. */
1271: public static class CompileShouldFailListener extends TestListener {
1272:
1273: private volatile boolean _compileDone = false; // records when compilaton is done
1274: private final Object _compileLock = new Object(); // lock for _compileDone
1275:
1276: public void logCompileStart() {
1277: _compileDone = false;
1278: }
1279:
1280: public void waitCompileDone() throws InterruptedException {
1281: synchronized (_compileLock) {
1282: while (!_compileDone) {
1283: // System.err.println("Waiting for JUnit to complete");
1284: _compileLock.wait();
1285: }
1286: }
1287: }
1288:
1289: public void compile(OpenDefinitionsDocument doc)
1290: throws IOException, InterruptedException {
1291: logCompileStart();
1292: doc.startCompile();
1293: waitCompileDone();
1294: }
1295:
1296: private void _notifyCompileDone() {
1297: synchronized (_compileLock) {
1298: _compileDone = true;
1299: _compileLock.notify();
1300: }
1301: }
1302:
1303: @Override
1304: public void compileStarted() {
1305: assertCompileStartCount(0);
1306: assertCompileEndCount(0);
1307: synchronized (this ) {
1308: compileStartCount++;
1309: }
1310: }
1311:
1312: @Override
1313: public void compileEnded(File workDir,
1314: List<? extends File> excludedFiles) {
1315: // Utilities.showDebug("compileEnded called in CSSListener");
1316: assertCompileEndCount(0);
1317: assertCompileStartCount(1);
1318: assertInterpreterResettingCount(0);
1319: assertInterpreterReadyCount(0);
1320: assertConsoleResetCount(0);
1321: synchronized (this ) {
1322: compileEndCount++;
1323: }
1324: _notifyCompileDone();
1325: }
1326:
1327: public void checkCompileOccurred() {
1328: assertCompileEndCount(1);
1329: assertCompileStartCount(1);
1330: }
1331:
1332: }
1333:
1334: public static class JUnitTestListener extends
1335: CompileShouldSucceedListener {
1336:
1337: protected volatile boolean _junitDone = false;
1338: protected final Object _junitLock = new Object();
1339:
1340: // handle System.out's separately but default to outer class's printMessage value
1341: protected volatile boolean printMessages = GlobalModelJUnitTest.printMessages;
1342:
1343: public void logJUnitStart() {
1344: logCompileStart();
1345: _junitDone = false;
1346: }
1347:
1348: /** Runs JUnit on doc to completion. */
1349: public void runJUnit(OpenDefinitionsDocument doc)
1350: throws IOException, ClassNotFoundException,
1351: InterruptedException {
1352: logJUnitStart();
1353: // System.err.println("Starting JUnit on " + doc);
1354: doc.startJUnit();
1355: // System.err.println("JUnit Started on " + doc);
1356: waitJUnitDone();
1357: }
1358:
1359: public void runJUnit(JUnitModel jm) throws IOException,
1360: ClassNotFoundException, InterruptedException {
1361: logJUnitStart();
1362: // System.err.println("Starting JUnit");
1363: jm.junitAll();
1364: waitJUnitDone();
1365: }
1366:
1367: public void waitJUnitDone() throws InterruptedException {
1368: synchronized (_junitLock) {
1369: while (!_junitDone) {
1370: _junitLock.wait();
1371: }
1372: }
1373: }
1374:
1375: private void _notifyJUnitDone() {
1376: synchronized (_junitLock) {
1377: _junitDone = true;
1378: _junitLock.notify();
1379: }
1380: }
1381:
1382: /** Construct JUnitTestListener without resetting interactions */
1383: public JUnitTestListener() {
1384: this (false, false);
1385: }
1386:
1387: public JUnitTestListener(boolean shouldResetAfterCompile) {
1388: this (shouldResetAfterCompile, false);
1389: }
1390:
1391: public JUnitTestListener(boolean shouldResetAfterCompile,
1392: boolean printListenerMessages) {
1393: super (shouldResetAfterCompile);
1394: this .printMessages = printListenerMessages;
1395: }
1396:
1397: public void resetCompileCounts() {
1398: compileStartCount = 0;
1399: compileEndCount = 0;
1400: }
1401:
1402: @Override
1403: public void junitStarted() {
1404: if (printMessages)
1405: System.out.println("listener.junitStarted");
1406: synchronized (this ) {
1407: junitStartCount++;
1408: }
1409: }
1410:
1411: @Override
1412: public void junitSuiteStarted(int numTests) {
1413: if (printMessages)
1414: System.out
1415: .println("listener.junitSuiteStarted, numTests = "
1416: + numTests);
1417: assertJUnitStartCount(1);
1418: synchronized (this ) {
1419: junitSuiteStartedCount++;
1420: }
1421: }
1422:
1423: @Override
1424: public void junitTestStarted(String name) {
1425: if (printMessages)
1426: System.out.println(" listener.junitTestStarted, "
1427: + name);
1428: synchronized (this ) {
1429: junitTestStartedCount++;
1430: }
1431: }
1432:
1433: @Override
1434: public void junitTestEnded(String name, boolean wasSuccessful,
1435: boolean causedError) {
1436: if (printMessages)
1437: System.out.println(" listener.junitTestEnded, name = "
1438: + name + " succ = " + wasSuccessful + " err = "
1439: + causedError);
1440: synchronized (this ) {
1441: junitTestEndedCount++;
1442: }
1443: assertEquals(
1444: "junitTestEndedCount should be same as junitTestStartedCount",
1445: junitTestEndedCount, junitTestStartedCount);
1446: }
1447:
1448: @Override
1449: public void nonTestCase(boolean isTestAll) {
1450: if (printMessages)
1451: System.out.println("listener.nonTestCase, isTestAll="
1452: + isTestAll);
1453: synchronized (this ) {
1454: nonTestCaseCount++;
1455: }
1456: _notifyJUnitDone();
1457: }
1458:
1459: @Override
1460: public void classFileError(ClassFileError e) {
1461: if (printMessages)
1462: System.out.println("listener.classFileError, e=" + e);
1463: synchronized (this ) {
1464: classFileErrorCount++;
1465: }
1466: _notifyJUnitDone();
1467: }
1468:
1469: @Override
1470: public void junitEnded() {
1471: //assertJUnitSuiteStartedCount(1);
1472: if (printMessages)
1473: System.out.println("junitEnded event!");
1474: synchronized (this ) {
1475: junitEndCount++;
1476: }
1477: _notifyJUnitDone();
1478: }
1479: }
1480:
1481: /** Listener class for failing JUnit invocation. */
1482: public static class JUnitNonTestListener extends JUnitTestListener {
1483: private volatile boolean _shouldBeTestAll;
1484:
1485: public JUnitNonTestListener() {
1486: this (false);
1487: }
1488:
1489: public JUnitNonTestListener(boolean shouldBeTestAll) {
1490: _shouldBeTestAll = shouldBeTestAll;
1491: }
1492:
1493: public void nonTestCase(boolean isTestAll) {
1494: synchronized (this ) {
1495: nonTestCaseCount++;
1496: }
1497: assertEquals(
1498: "Non test case heard the wrong value for test current/test all",
1499: _shouldBeTestAll, isTestAll);
1500: // Utilities.show("synchronizing on _junitLock");
1501: synchronized (_junitLock) {
1502: // System.err.println("JUnit aborted as nonTestCase");
1503: _junitDone = true;
1504: _junitLock.notify();
1505: }
1506: }
1507: }
1508:
1509: /* A variant of DefaultGlobalModel used only for testing purposes. This variant
1510: * does not change the working directory when resetting interactions. This test class and its
1511: * descendants were written before the distinction between getWorkingDirectory and getMasterDirectory.
1512: * This method override restores the old semantics for getWorkingDirectory. The new definition breaks
1513: * some unit tests because the slave JVM keeps its working directory open until it shuts down.
1514: */
1515: public class TestGlobalModel extends DefaultGlobalModel {
1516: public File getWorkingDirectory() {
1517: return getMasterWorkingDirectory();
1518: }
1519: }
1520: }
|