1 33 34 package edu.rice.cs.drjava.model; 35 36 import edu.rice.cs.drjava.DrJava; 37 import edu.rice.cs.drjava.model.compiler.CompilerListener; 38 import edu.rice.cs.drjava.model.repl.InteractionsDocument; 39 import edu.rice.cs.drjava.model.junit.JUnitModel; 40 import edu.rice.cs.util.FileOpenSelector; 41 import edu.rice.cs.plt.io.IOUtil; 42 import edu.rice.cs.util.Log; 43 import edu.rice.cs.util.OperationCanceledException; 44 import edu.rice.cs.util.StringOps; 45 import edu.rice.cs.util.UnexpectedException; 46 import edu.rice.cs.util.classloader.ClassFileError; 47 import edu.rice.cs.util.swing.Utilities; 48 import edu.rice.cs.util.swing.AsyncTask; 49 import edu.rice.cs.util.text.EditDocumentException; 50 import edu.rice.cs.util.text.EditDocumentInterface; 51 52 import javax.swing.text.BadLocationException ; 53 import java.io.File ; 54 import java.io.IOException ; 55 import java.util.regex.*; 56 import java.util.List ; 57 58 66 public abstract class GlobalModelTestCase extends MultiThreadedTestCase { 67 68 protected static final Log _log = new Log("GlobalModel.txt", false); 69 70 protected volatile DefaultGlobalModel _model; 71 protected volatile File _tempDir; 72 protected volatile OpenDefinitionsDocument _doc; 74 protected static final String FOO_TEXT = "class DrJavaTestFoo {}"; 75 protected static final String BAR_TEXT = "class DrJavaTestBar {}"; 76 protected static final String BAZ_TEXT = "class DrJavaTestBaz extends DrJavaTestFoo { public static int x = 3; }"; 77 protected static final String FOO_MISSING_CLOSE_TEXT = "class DrJavaTestFoo {"; 78 protected static final String FOO_PACKAGE_AFTER_IMPORT = "import java.util.*;\npackage a;\n" + FOO_TEXT; 79 protected static final String FOO_PACKAGE_INSIDE_CLASS = "class DrJavaTestFoo { package a; }"; 80 protected static final String FOO_PACKAGE_AS_FIELD = "class DrJavaTestFoo { int package; }"; 81 protected static final String FOO_PACKAGE_AS_FIELD_2 = "class DrJavaTestFoo { int package = 5; }"; 82 protected static final String FOO_PACKAGE_AS_PART_OF_FIELD = "class DrJavaTestFoo { int cur_package = 5; }"; 83 84 public GlobalModelTestCase() { _log.log("Constructing a " + this); } 85 86 96 public void setUp() throws Exception { 97 _log.log("Setting up " + this); 98 super.setUp(); 99 _model = new TestGlobalModel(); 100 _log.log("Global model created for " + this); 101 DrJava.getConfig().resetToDefaults(); 102 String user = System.getProperty("user.name"); 103 _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, ""); 104 _log.log("Ensuring that interpreter is connected in " + this); 106 _model._jvm.ensureInterpreterConnected(); 107 _log.log("Ensured that intepreter is connected in " + this); 108 _model.setResetAfterCompile(false); 109 _log.log("Completed (GlobalModelTestCase) set up of " + this); 110 111 } 114 115 116 public void tearDown() throws Exception { 117 _log.log("Tearing down " + this); 118 boolean ret = IOUtil.deleteRecursively(_tempDir); 119 assertTrue("delete temp directory " + _tempDir, ret); 120 121 _model.dispose(); 122 _tempDir = null; 123 _model = null; 124 125 super.tearDown(); 126 _log.log("Completed tear down of " + this); 127 } 128 129 130 protected void changeDocumentText(String s, OpenDefinitionsDocument doc) throws BadLocationException { 131 doc.clear(); 132 assertLength(0, doc); 133 doc.append(s, null); 134 assertModified(true, doc); 135 assertContents(s, doc); 136 } 137 138 139 protected File tempFile() throws IOException { 140 File f = File.createTempFile("DrJava-test", ".java", _tempDir).getCanonicalFile(); 141 return f; 143 } 144 145 148 protected File tempFile(int i) throws IOException { 149 return File.createTempFile("DrJava-test" + i, ".java", _tempDir).getCanonicalFile(); 150 } 151 152 153 protected File tempDirectory() throws IOException { 154 return IOUtil.createAndMarkTempDirectory("DrJava-test", "", _tempDir); 155 } 156 157 protected File createFile(String name) { return new File (_tempDir, name); } 158 159 160 protected File classForJava(File sourceFile, String className) { 161 assertTrue(sourceFile.getName().endsWith(".java")); 162 String cname = className + ".class"; 163 return new File (sourceFile.getParent(), cname); 164 } 165 166 169 protected File writeToNewTempFile(String text) throws IOException { 170 File temp = tempFile(); 171 IOUtil.writeStringToFile(temp, text); 172 return temp; 173 } 174 175 179 protected OpenDefinitionsDocument setupDocument(String text) throws BadLocationException { 180 TestListener listener = new TestListener() { 181 public synchronized void newFileCreated(OpenDefinitionsDocument doc) { newCount++; } 182 }; 183 184 _model.addListener(listener); 185 186 int numOpen = _model.getOpenDefinitionsDocuments().size(); 188 Utilities.invokeAndWait(new Runnable () { public void run () { _doc = _model.newFile(); } }); 189 190 assertNumOpenDocs(numOpen + 1); 191 192 listener.assertNewCount(1); 193 assertLength(0, _doc); 194 assertModified(false, _doc); 195 196 changeDocumentText(text, _doc); 197 assertModified(true, _doc); 198 _model.removeListener(listener); 199 200 return _doc; 201 } 202 203 209 protected synchronized OpenDefinitionsDocument doCompile(String text, File file) throws IOException , 210 BadLocationException , InterruptedException { 211 212 OpenDefinitionsDocument doc = setupDocument(text); 213 doCompile(doc, file); 214 return doc; 215 } 216 217 223 protected void doCompile(OpenDefinitionsDocument doc, File file) throws IOException , InterruptedException { 224 doc.saveFile(new FileSelector(file)); 225 226 try { interpret("0"); } 228 catch (EditDocumentException e) { throw new UnexpectedException(e); } 229 Utilities.clearEventQueue(); 230 231 _model.setResetAfterCompile(true); 232 CompileShouldSucceedListener listener = new CompileShouldSucceedListener(true); 233 _model.addListener(listener); 234 235 listener.logCompileStart(); 236 237 doc.startCompile(); 238 Utilities.clearEventQueue(); 239 240 if (_model.getCompilerModel().getNumErrors() > 0) fail("compile failed: " + getCompilerErrorString()); 241 242 listener.waitCompileDone(); 243 244 listener.checkCompileOccurred(); 245 assertCompileErrorsPresent(false); 246 247 listener.waitResetDone(); 248 Utilities.clearEventQueue(); 249 _model.removeListener(listener); 250 } 251 252 253 protected String getCompilerErrorString() { 254 final StringBuilder buf = new StringBuilder (); 255 buf.append(" compiler error(s):\n"); 256 buf.append(_model.getCompilerModel().getCompilerErrorModel().toString()); 257 return buf.toString(); 258 } 259 260 268 protected String interpret(String input) throws EditDocumentException { 269 270 InteractionsDocument interactionsDoc = _model.getInteractionsDocument(); 271 Utilities.clearEventQueue(); 272 273 interactionsDoc.setInProgress(false); interactionsDoc.append(input, InteractionsDocument.DEFAULT_STYLE); 275 276 Utilities.clearEventQueue(); 277 278 final int newLineLen = System.getProperty("line.separator").length(); 280 final int resultsStartLocation = interactionsDoc.getLength() + newLineLen; 281 282 InteractionListener listener = new InteractionListener(); 283 284 _model.addListener(listener); 285 listener.logInteractionStart(); 286 try { 287 _model.interpretCurrentInteraction(); 288 listener.waitInteractionDone(); 289 } 290 catch (InterruptedException ie) { throw new UnexpectedException(ie); } 291 Utilities.clearEventQueue(); 292 _model.removeListener(listener); 293 294 listener.assertInteractionStartCount(1); 295 listener.assertInteractionEndCount(1); 296 297 interactionsDoc.acquireReadLock(); 299 try { 300 final int resultsEndLocation = interactionsDoc.getLength() - newLineLen - interactionsDoc.getPrompt().length(); 301 302 final int resultsLen = resultsEndLocation - resultsStartLocation; 303 if (resultsLen <= 0) return ""; 306 return interactionsDoc.getDocText(resultsStartLocation, resultsLen); 307 } 308 finally { interactionsDoc.releaseReadLock(); } 309 } 310 311 312 protected void interpretIgnoreResult(String input) throws EditDocumentException { 313 InteractionsDocument interactionsDoc = _model.getInteractionsDocument(); 314 interactionsDoc.append(input, InteractionsDocument.DEFAULT_STYLE); 315 _model.interpretCurrentInteraction(); 316 } 317 318 319 protected void assertInteractionsContains(String text) throws EditDocumentException { 320 _assertInteractionContainsHelper(text, true); 321 } 322 323 324 protected void assertInteractionsDoesNotContain(String text) throws EditDocumentException { 325 _assertInteractionContainsHelper(text, false); 326 } 327 328 private void _assertInteractionContainsHelper(String text, boolean shouldContain) throws EditDocumentException { 329 330 String interactText = getInteractionsText(); 331 int contains = interactText.lastIndexOf(text); 332 assertTrue("Interactions document should " + 333 (shouldContain ? "" : "not ") 334 + "contain:\n" 335 + text 336 + "\nActual contents of Interactions document:\n" 337 + interactText, 338 (contains != -1) == shouldContain); 339 } 340 341 342 protected void assertInteractionsMatches(String regex) throws EditDocumentException { 343 _assertInteractionMatchesHelper(regex, true); 344 } 345 346 347 protected void assertInteractionsDoesNotMatch(String regex) 348 throws EditDocumentException { 349 _assertInteractionMatchesHelper(regex, false); 350 } 351 352 private void _assertInteractionMatchesHelper(String regex, boolean shouldMatch) throws EditDocumentException { 353 354 String interactText = getInteractionsText(); 355 boolean matches = Pattern.compile(regex, Pattern.MULTILINE|Pattern.DOTALL).matcher(interactText).matches(); 356 assertTrue("Interactions document should " + 357 (shouldMatch ? "" : "not ") 358 + "match:\n" 359 + regex 360 + "\nActual contents of Interactions document:\n" 361 + interactText, 362 matches == shouldMatch); 363 } 364 365 366 protected String getInteractionsText() throws EditDocumentException { 367 InteractionsDocument doc = _model.getInteractionsDocument(); 368 return doc.getText(); 369 } 370 371 protected void assertNumOpenDocs(int num) { 372 assertEquals("number of open documents", num, _model.getOpenDefinitionsDocuments().size()); 373 } 374 375 protected void assertModified(boolean b, OpenDefinitionsDocument doc) { 376 assertEquals("document isModifiedSinceSave", b, doc.isModifiedSinceSave()); 377 } 378 379 380 protected void assertLength(int len, OpenDefinitionsDocument doc) { 381 assertEquals("document length", len, doc.getLength()); 382 } 383 384 protected void assertContents(String s, OpenDefinitionsDocument doc) throws BadLocationException { 385 assertEquals("document contents", s, doc.getText()); 386 } 387 388 protected void assertCompileErrorsPresent(boolean b) { assertCompileErrorsPresent("", b); } 389 390 protected void assertCompileErrorsPresent(String name, boolean b) { 391 int numErrors = _model.getCompilerModel().getNumErrors(); 393 394 if (name.length() > 0) name += ": "; 395 396 401 assertEquals(name + "compile errors > 0? numErrors =" + numErrors, b, numErrors > 0); 402 } 403 404 public static class OverwriteException extends RuntimeException { } 407 public static class OpenWarningException extends RuntimeException { } 408 public static class FileMovedWarningException extends RuntimeException { } 409 410 public static class WarningFileSelector implements FileOpenSelector, FileSaveSelector { 411 private volatile File _file; 412 public WarningFileSelector(File f) { _file = f; } 413 public File getFile() throws OperationCanceledException { return _file; } 414 public File [] getFiles() throws OperationCanceledException { return new File [] {_file}; } 415 public boolean warnFileOpen(File f) { throw new OpenWarningException(); } 416 public boolean verifyOverwrite() { throw new OverwriteException(); } 417 public boolean shouldSaveAfterFileMoved(OpenDefinitionsDocument doc, File oldFile) { 418 throw new FileMovedWarningException(); 419 } 420 } 421 422 427 428 public static class FileSelector implements FileOpenSelector, FileSaveSelector { 429 private volatile File _file1, _file2; 430 public FileSelector(File f) { _file1 = f; } 431 public FileSelector(File f1, File f2) { 432 _file1 = f1; 433 _file2 = f2; 434 } 435 436 public File getFile() throws OperationCanceledException { return _file1; } 437 438 public File [] getFiles() throws OperationCanceledException { 439 if (_file2 != null) return new File [] {_file1, _file2}; 440 else return new File [] {_file1}; 441 } 442 public boolean warnFileOpen(File f) { return true; } 443 public boolean verifyOverwrite() { return true; } 444 public boolean shouldSaveAfterFileMoved(OpenDefinitionsDocument doc, File oldFile) { return true; } 445 } 446 447 public static class CancelingSelector implements FileOpenSelector, FileSaveSelector { 448 public File getFile() throws OperationCanceledException { throw new OperationCanceledException(); } 449 public File [] getFiles() throws OperationCanceledException { throw new OperationCanceledException(); } 450 public boolean warnFileOpen(File f) { return true; } 451 public boolean verifyOverwrite() {return true; } 452 public boolean shouldSaveAfterFileMoved(OpenDefinitionsDocument doc, File oldFile) { return true; } 453 } 454 455 458 public static class TestListener implements GlobalModelListener { 459 460 protected volatile Exception _startupTrace; 461 462 protected volatile int fileNotFoundCount; 463 protected volatile int newCount; 464 protected volatile int openCount; 465 protected volatile int closeCount; 466 protected volatile int saveCount; 467 protected volatile int canAbandonCount; 468 protected volatile int quitFileCount; 469 protected volatile int classFileErrorCount; 470 protected volatile int compileStartCount; 471 protected volatile int compileEndCount; 472 protected volatile int runStartCount; 473 protected volatile int junitStartCount; 474 protected volatile int junitSuiteStartedCount; 475 protected volatile int junitTestStartedCount; 476 protected volatile int junitTestEndedCount; 477 protected volatile int junitEndCount; 478 protected volatile int interactionStartCount; 479 protected volatile int interactionEndCount; 480 protected volatile int interactionErrorCount; 481 protected volatile int interpreterResettingCount; 482 protected volatile int interpreterReadyCount; 483 protected volatile int interpreterExitedCount; 484 protected volatile int interpreterResetFailedCount; 485 protected volatile int interpreterChangedCount; 486 protected volatile int consoleResetCount; 488 protected volatile int saveBeforeCompileCount; 489 protected volatile int compileBeforeJUnitCount; 491 protected volatile int saveBeforeJavadocCount; 492 protected volatile int nonTestCaseCount; 494 protected volatile int lastExitStatus; 495 protected volatile int fileRevertedCount; 496 protected volatile int shouldRevertFileCount; 497 protected volatile int undoableEditCount; 498 protected volatile int interactionIncompleteCount; 499 protected volatile int filePathContainsPoundCount; 500 501 public TestListener() { 502 _startupTrace = new Exception (); 503 resetCounts(); 504 } 505 506 public synchronized void resetCounts() { 507 fileNotFoundCount = 0; 508 newCount = 0; 509 openCount = 0; 510 closeCount = 0; 511 saveCount = 0; 512 canAbandonCount = 0; 513 quitFileCount = 0; 514 classFileErrorCount = 0; 515 compileStartCount = 0; 516 compileEndCount = 0; 517 runStartCount = 0; 518 junitStartCount = 0; 519 junitSuiteStartedCount = 0; 520 junitTestStartedCount = 0; 521 junitTestEndedCount = 0; 522 junitEndCount = 0; 523 interactionStartCount = 0; 524 interactionEndCount = 0; 525 interactionErrorCount = 0; 526 interpreterChangedCount = 0; 527 consoleResetCount = 0; 529 interpreterResettingCount = 0; 530 interpreterReadyCount = 0; 531 interpreterExitedCount = 0; 532 interpreterResetFailedCount = 0; 533 saveBeforeCompileCount = 0; 534 compileBeforeJUnitCount = 0; 536 saveBeforeJavadocCount = 0; 537 nonTestCaseCount = 0; 539 lastExitStatus = 0; 540 fileRevertedCount = 0; 541 shouldRevertFileCount = 0; 542 undoableEditCount = 0; 543 interactionIncompleteCount = 0; 544 filePathContainsPoundCount = 0; 545 } 546 547 public void projectModified() { } 548 public void projectOpened(File pfile, FileOpenSelector files) { } 549 public void projectClosed() { } 550 public void projectBuildDirChanged() { } 551 public void projectWorkDirChanged() { } 552 public void projectRunnableChanged() { } 553 554 public void currentDirectoryChanged(File dir) { } 555 556 557 public void listenerFail(String message) { 558 String header = "\nTestListener creation stack trace:\n" + StringOps.getStackTrace(_startupTrace); 559 MultiThreadedTestCase.listenerFail(message + header); 560 } 561 562 public void assertFileNotFoundCount(int i) { 563 assertEquals("number of times fileNotFound fired", i, fileNotFoundCount); 564 } 565 566 public void assertAbandonCount(int i) { 567 assertEquals("number of times canAbandon fired", i, canAbandonCount); 568 } 569 570 public void assertQuitFileCount(int i) { 571 assertEquals("number of times quitFile fired", i, quitFileCount); 572 } 573 public void assertClassFileErrorCount(int i) { 574 assertEquals("number of times classFileError fired", i, classFileErrorCount); 575 } 576 public void assertNewCount(int i) { 577 assertEquals("number of times newFile fired", i, newCount); 578 } 579 580 public void assertOpenCount(int i) { 581 assertEquals("number of times openFile fired", i, openCount); 582 } 583 584 public void assertCloseCount(int i) { 585 assertEquals("number of times closeFile fired", i, closeCount); 586 } 587 588 public void assertSaveCount(int i) { 589 assertEquals("number of times saveFile fired", i, saveCount); 590 } 591 592 public void assertJUnitStartCount(int i) { 593 assertEquals("number of times junitStarted fired", i, junitStartCount); 594 } 595 596 public void assertJUnitSuiteStartedCount(int i) { 597 assertEquals("number of times junitSuiteStarted fired", i, junitSuiteStartedCount); 598 } 599 600 public void assertJUnitTestStartedCount(int i) { 601 assertEquals("number of times junitTestStarted fired", i, junitTestStartedCount); 602 } 603 604 public void assertJUnitTestEndedCount(int i) { 605 assertEquals("number of times junitTestEnded fired", i, junitTestEndedCount); 606 } 607 608 public void assertJUnitEndCount(int i) { 609 assertEquals("number of times junitEnded fired", i, junitEndCount); 610 } 611 612 public void assertInteractionStartCount(int i) { 613 assertEquals("number of times interactionStarted fired", i, interactionStartCount); 614 } 615 616 public void assertInteractionEndCount(int i) { 617 assertEquals("number of times interactionEnded fired", i, interactionEndCount); 618 } 619 620 public void assertInteractionErrorCount(int i) { 621 assertEquals("number of times interactionError fired", i, interactionErrorCount ); 622 } 623 624 public void assertInterpreterChangedCount(int i) { 625 assertEquals("number of times interpreterChanged fired", i, interpreterChangedCount); 626 } 627 628 633 public void assertCompileStartCount(int i) { 634 assertEquals("number of times compileStarted fired", i, compileStartCount); 635 } 636 637 public void assertCompileEndCount(int i) { 638 assertEquals("number of times compileEnded fired", i, compileEndCount); 639 } 640 641 public void assertRunStartCount(int i) { 642 assertEquals("number of times runStarted fired", i, runStartCount); 643 } 644 645 public void assertInterpreterResettingCount(int i) { 646 assertEquals("number of times interpreterResetting fired", i, interpreterResettingCount); 647 } 648 649 public void assertInterpreterReadyCount(int i) { 650 assertEquals("number of times interpreterReady fired", i, interpreterReadyCount); 651 } 652 653 public void assertInterpreterResetFailedCount(int i) { 654 assertEquals("number of times interpreterResetFailed fired", i, interpreterResetFailedCount); 655 } 656 657 public void assertInterpreterExitedCount(int i) { 658 assertEquals("number of times interpreterExited fired", i, interpreterExitedCount); 659 } 660 661 public void assertInteractionsErrorCount(int i) { 662 assertEquals("number of times interactionsError fired", i, interactionErrorCount); 663 } 664 665 public void assertConsoleResetCount(int i) { 666 assertEquals("number of times consoleReset fired", i, consoleResetCount); 667 } 668 669 public void assertSaveBeforeCompileCount(int i) { 670 assertEquals("number of times saveBeforeCompile fired", i, saveBeforeCompileCount); 671 } 672 673 public void assertCompileBeforeJUnitCount(int i) { 679 assertEquals("number of times compileBeforeJUnit fired", i, compileBeforeJUnitCount); 680 } 681 682 public void assertSaveBeforeJavadocCount(int i) { 683 assertEquals("number of times saveBeforeJavadoc fired", i, saveBeforeJavadocCount); 684 } 685 686 693 public void assertNonTestCaseCount(int i) { 694 assertEquals("number of times nonTestCase fired", i, nonTestCaseCount); 695 } 696 697 public void assertFileRevertedCount(int i) { 698 assertEquals("number of times fileReverted fired", i, fileRevertedCount); 699 } 700 701 public void assertUndoableEditCount(int i) { 702 assertEquals("number of times undoableEditHappened fired", i, undoableEditCount); 703 } 704 705 public void assertShouldRevertFileCount(int i) { 706 assertEquals("number of times shouldRevertFile fired", i, shouldRevertFileCount); 707 } 708 709 public void assertInteractionIncompleteCount(int i) { 710 assertEquals("number of times interactionIncomplete fired", i, interactionIncompleteCount); 711 } 712 713 public <P,R> void executeAsyncTask(AsyncTask<P,R> task, P param, boolean showProgress, boolean lockUI) { 714 listenerFail("executeAswyncTask fired unexpectedly"); 715 } 716 717 public void handleAlreadyOpenDocument(OpenDefinitionsDocument doc) { 718 listenerFail("handleAlreadyOpenDocument fired unexpectedly"); 719 } 720 721 public void newFileCreated(OpenDefinitionsDocument doc) { listenerFail("newFileCreated fired unexpectedly"); } 722 public void fileNotFound(File f) { listenerFail("fileNotFound fired unexpectedly"); } 723 public void fileOpened(OpenDefinitionsDocument doc) { } 725 public void fileClosed(OpenDefinitionsDocument doc) { listenerFail("fileClosed fired unexpectedly"); } 726 public void fileSaved(OpenDefinitionsDocument doc) { listenerFail("fileSaved fired unexpectedly"); } 727 public void fileReverted(OpenDefinitionsDocument doc) { listenerFail("fileReverted fired unexpectedly"); } 728 public void undoableEditHappened() { listenerFail("undoableEditHappened fired unexpectedly"); } 729 public void saveBeforeCompile() { listenerFail("saveBeforeCompile fired unexpectedly"); } 730 731 public void junitStarted() { listenerFail("junitStarted fired unexpectedly"); } 732 public void junitClassesStarted() { listenerFail("junitAllStarted fired unexpectedly"); } 733 public void junitSuiteStarted(int numTests) { listenerFail("junitSuiteStarted fired unexpectedly"); } 734 public void junitTestStarted(String name) { listenerFail("junitTestStarted fired unexpectedly"); } 735 public void junitTestEnded(String name, boolean wasSuccessful, boolean causedError) { 736 listenerFail("junitTestEnded fired unexpectedly"); 737 } 738 public void junitEnded() { listenerFail("junitEnded fired unexpectedly"); } 739 740 public void javadocStarted() { listenerFail("javadocStarted fired unexpectedly"); } 741 public void javadocEnded(boolean success, File destDir, boolean allDocs) { 742 listenerFail("javadocEnded fired unexpectedly"); 743 } 744 745 public void interactionStarted() { listenerFail("interactionStarted fired unexpectedly"); } 746 public void interactionEnded() { listenerFail("interactionEnded fired unexpectedly"); } 747 public void interactionErrorOccurred(int offset, int length) { 748 listenerFail("interpreterErrorOccurred fired unexpectedly"); 749 } 750 751 public void interpreterChanged(boolean inProgress) { listenerFail("interpreterChanged fired unexpectedly"); } 752 753 758 public void compileStarted() { listenerFail("compileStarted fired unexpectedly"); } 759 public void compileEnded(File workDir, List <? extends File > excludedFiles) { listenerFail("compileEnded fired unexpectedly"); } 760 761 public void runStarted(OpenDefinitionsDocument doc) { listenerFail("runStarted fired unexpectedly"); } 762 763 public void interpreterResetting() { listenerFail("interpreterResetting fired unexpectedly"); } 764 765 public void interpreterReady(File wd) { listenerFail("interpreterReady fired unexpectedly"); } 766 public void interpreterExited(int status) { 767 listenerFail("interpreterExited(" + status + ") fired unexpectedly"); 768 } 769 public void interpreterResetFailed(Throwable t) { listenerFail("interpreterResetFailed fired unexpectedly"); } 770 public void slaveJVMUsed() { } 771 public void consoleReset() { listenerFail("consoleReset fired unexpectedly"); } 772 public void saveUntitled() { listenerFail("saveUntitled fired unexpectedly"); } 773 774 public void compileBeforeJUnit(CompilerListener cl) { compileBeforeJUnitCount++; } 775 776 public void saveBeforeJavadoc() { listenerFail("saveBeforeJavadoc fired unexpectedly"); } 777 public void nonTestCase(boolean isTestAll) { listenerFail("nonTestCase fired unexpectedly"); } 778 779 public boolean canAbandonFile(OpenDefinitionsDocument doc) { 780 listenerFail("canAbandonFile fired unexpectedly"); 781 throw new UnexpectedException(); 782 } 783 784 public boolean quitFile(OpenDefinitionsDocument doc) { 785 listenerFail("quitFile fired unexpectedly"); 786 throw new UnexpectedException(); 787 } 788 789 public void classFileError(ClassFileError e) { 790 listenerFail("classFileError fired unexpectedly"); 791 } 792 793 public boolean shouldRevertFile(OpenDefinitionsDocument doc) { 794 listenerFail("shouldRevertfile fired unexpectedly"); 795 throw new UnexpectedException(); 796 } 797 798 public void interactionIncomplete() { listenerFail("interactionIncomplete fired unexpectedly"); } 799 public void filePathContainsPound() { listenerFail("filePathContainsPound fired unexpectedly"); } 800 801 public void documentNotFound(OpenDefinitionsDocument d, File f) { 802 listenerFail("documentNotFound fired unexpectedly"); 803 } 804 805 public void activeDocumentChanged(OpenDefinitionsDocument active) { 806 807 } 808 809 public void focusOnDefinitionsPane() { } 810 } 811 812 public static class InteractionListener extends TestListener { 813 private volatile boolean _interactionDone = false; private final Object _interactionLock = new Object (); 816 private volatile boolean _resetDone = false; private final Object _resetLock = new Object (); 819 private volatile int _lastExitStatus = -1; 820 821 822 823 public synchronized void interactionStarted() { interactionStartCount++; } 824 825 public void interactionEnded() { 826 828 synchronized(this) { interactionEndCount++; } 829 synchronized(_interactionLock) { 830 _interactionDone = true; 831 _interactionLock.notify(); 832 } 833 } 834 835 public void interpreterExited(int status) { 836 synchronized(this) { 840 interpreterExitedCount++; 841 _lastExitStatus = status; 842 } 843 synchronized(_interactionLock) { 844 _interactionDone = true; 845 _interactionLock.notify(); 846 } 847 } 848 849 public void interpreterResetting() { 850 assertInterpreterResettingCount(0); 851 assertInterpreterReadyCount(0); 852 synchronized(this) { interpreterResettingCount++; } 853 } 854 855 public void interpreterReady(File wd) { 856 synchronized(this) { interpreterReadyCount++; } 858 synchronized(_resetLock) { 859 _resetDone = true; 864 _resetLock.notify(); 865 } 866 } 867 868 public void consoleReset() { 869 assertConsoleResetCount(0); 870 synchronized(this) { consoleResetCount++; } 874 } 875 876 public void resetConsoleResetCount() { consoleResetCount = 0; } 877 878 public void logInteractionStart() { 879 _interactionDone = false; 880 _resetDone = false; 881 } 882 883 public void waitInteractionDone() throws InterruptedException { 884 synchronized(_interactionLock) { while (! _interactionDone) _interactionLock.wait(); } 885 } 886 887 public void waitResetDone() throws InterruptedException { 888 synchronized(_resetLock) { while (! _resetDone) _resetLock.wait(); } 889 } 890 891 public int getLastExitStatus() { return _lastExitStatus; } 892 }; 893 894 895 898 public static class CompileShouldSucceedListener extends InteractionListener { 899 private volatile boolean _expectReset; 900 901 private volatile boolean _compileDone = false; private final Object _compileLock = new Object (); 904 public void logCompileStart() { 905 logInteractionStart(); 906 _compileDone = false; 907 } 908 909 public void compile(OpenDefinitionsDocument doc) throws IOException , InterruptedException { 910 logCompileStart(); 911 doc.startCompile(); 912 waitCompileDone(); 913 } 914 915 public void waitCompileDone() throws InterruptedException { 916 synchronized(_compileLock) { 917 while (! _compileDone) { 918 _compileLock.wait(); 920 } 921 } 922 } 923 924 private void _notifyCompileDone() { 925 synchronized(_compileLock) { 926 _compileDone = true; 927 _compileLock.notify(); 928 } 929 } 930 931 934 public CompileShouldSucceedListener(boolean expectReset) { _expectReset = expectReset; } 935 936 public CompileShouldSucceedListener() { this(false); } 937 938 940 @Override public void newFileCreated(OpenDefinitionsDocument doc) { } 941 942 @Override public void compileStarted() { 943 assertCompileStartCount(0); 945 assertCompileEndCount(0); 946 assertInterpreterResettingCount(0); 947 assertInterpreterReadyCount(0); 948 assertConsoleResetCount(0); 949 synchronized(this) { compileStartCount++; } 950 } 951 952 @Override public void compileEnded(File workDir, List <? extends File > excludedFiles) { 953 assertCompileEndCount(0); 955 assertCompileStartCount(1); 956 assertInterpreterResettingCount(0); 957 assertInterpreterReadyCount(0); 958 assertConsoleResetCount(0); 959 synchronized(this) { compileEndCount++; } 960 _notifyCompileDone(); 961 } 962 963 public void checkCompileOccurred() { 964 assertCompileEndCount(1); 965 assertCompileStartCount(1); 966 975 } 978 } 979 980 981 public static class CompileShouldFailListener extends TestListener { 982 983 private volatile boolean _compileDone = false; private final Object _compileLock = new Object (); 986 public void logCompileStart() { _compileDone = false; } 987 988 public void waitCompileDone() throws InterruptedException { 989 synchronized(_compileLock) { 990 while (! _compileDone) { 991 _compileLock.wait(); 993 } 994 } 995 } 996 997 public void compile(OpenDefinitionsDocument doc) throws IOException , InterruptedException { 998 logCompileStart(); 999 doc.startCompile(); 1000 waitCompileDone(); 1001 } 1002 1003 private void _notifyCompileDone() { 1004 synchronized(_compileLock) { 1005 _compileDone = true; 1006 _compileLock.notify(); 1007 } 1008 } 1009 1010 @Override public void compileStarted() { 1011 assertCompileStartCount(0); 1012 assertCompileEndCount(0); 1013 synchronized(this) { compileStartCount++; } 1014 } 1015 1016 @Override public void compileEnded(File workDir, List <? extends File > excludedFiles) { 1017 assertCompileEndCount(0); 1019 assertCompileStartCount(1); 1020 assertInterpreterResettingCount(0); 1021 assertInterpreterReadyCount(0); 1022 assertConsoleResetCount(0); 1023 synchronized(this) { compileEndCount++; } 1024 _notifyCompileDone(); 1025 } 1026 1027 public void checkCompileOccurred() { 1028 assertCompileEndCount(1); 1029 assertCompileStartCount(1); 1030 } 1031 1032 } 1033 1034 public static class JUnitTestListener extends CompileShouldSucceedListener { 1035 1036 protected volatile boolean _junitDone = false; 1037 protected final Object _junitLock = new Object (); 1038 1039 protected volatile boolean printMessages = GlobalModelJUnitTest.printMessages; 1041 1042 public void logJUnitStart() { 1043 logCompileStart(); 1044 _junitDone = false; 1045 } 1046 1047 1048 public void runJUnit(OpenDefinitionsDocument doc) throws IOException , ClassNotFoundException , 1049 InterruptedException { 1050 logJUnitStart(); 1051 doc.startJUnit(); 1053 waitJUnitDone(); 1055 } 1056 1057 public void runJUnit(JUnitModel jm) throws IOException , ClassNotFoundException , InterruptedException { 1058 logJUnitStart(); 1059 jm.junitAll(); 1061 waitJUnitDone(); 1062 } 1063 1064 public void waitJUnitDone() throws InterruptedException { 1065 synchronized(_junitLock) { while (! _junitDone) { _junitLock.wait(); } } 1066 } 1067 1068 private void _notifyJUnitDone() { 1069 synchronized(_junitLock) { 1070 _junitDone = true; 1071 _junitLock.notify(); 1072 } 1073 } 1074 1075 1076 public JUnitTestListener() { this(false, false); } 1077 public JUnitTestListener(boolean shouldResetAfterCompile) { this(shouldResetAfterCompile, false); } 1078 public JUnitTestListener(boolean shouldResetAfterCompile, boolean printListenerMessages) { 1079 super(shouldResetAfterCompile); 1080 this.printMessages = printListenerMessages; 1081 } 1082 public void resetCompileCounts() { 1083 compileStartCount = 0; 1084 compileEndCount = 0; 1085 } 1086 @Override public void junitStarted() { 1087 if (printMessages) System.out.println("listener.junitStarted"); 1088 synchronized(this) { junitStartCount++; } 1089 } 1090 @Override public void junitSuiteStarted(int numTests) { 1091 if (printMessages) System.out.println("listener.junitSuiteStarted, numTests = "+numTests); 1092 assertJUnitStartCount(1); 1093 synchronized(this) { junitSuiteStartedCount++; } 1094 } 1095 @Override public void junitTestStarted(String name) { 1096 if (printMessages) System.out.println(" listener.junitTestStarted, " + name); 1097 synchronized(this) { junitTestStartedCount++; } 1098 } 1099 @Override public void junitTestEnded(String name, boolean wasSuccessful, boolean causedError) { 1100 if (printMessages) System.out.println(" listener.junitTestEnded, name = " + name + " succ = " + wasSuccessful + 1101 " err = " + causedError); 1102 synchronized(this) { junitTestEndedCount++; } 1103 assertEquals("junitTestEndedCount should be same as junitTestStartedCount", junitTestEndedCount, 1104 junitTestStartedCount); 1105 } 1106 @Override public void nonTestCase(boolean isTestAll) { 1107 if (printMessages) System.out.println("listener.nonTestCase, isTestAll=" + isTestAll); 1108 synchronized(this) { nonTestCaseCount++; } 1109 _notifyJUnitDone(); 1110 } 1111 @Override public void classFileError(ClassFileError e) { 1112 if (printMessages) System.out.println("listener.classFileError, e="+e); 1113 synchronized(this) { classFileErrorCount++; } 1114 _notifyJUnitDone(); 1115 } 1116 @Override public void junitEnded() { 1117 if (printMessages) System.out.println("junitEnded event!"); 1119 synchronized(this) { junitEndCount++; } 1120 _notifyJUnitDone(); 1121 } 1122 } 1123 1124 1125 public static class JUnitNonTestListener extends JUnitTestListener { 1126 private volatile boolean _shouldBeTestAll; 1127 public JUnitNonTestListener() { this(false); } 1128 public JUnitNonTestListener(boolean shouldBeTestAll) { _shouldBeTestAll = shouldBeTestAll; } 1129 public void nonTestCase(boolean isTestAll) { 1130 synchronized(this) { nonTestCaseCount++; } 1131 assertEquals("Non test case heard the wrong value for test current/test all", _shouldBeTestAll, isTestAll); 1132 synchronized(_junitLock) { 1134 _junitDone = true; 1136 _junitLock.notify(); 1137 } 1138 } 1139 } 1140 1141 1147 public class TestGlobalModel extends DefaultGlobalModel { 1148 public File getWorkingDirectory() { return getMasterWorkingDirectory(); } 1149 } 1150} 1151 | Popular Tags |