1 33 34 package edu.rice.cs.drjava.model; 35 36 import java.io.*; 37 import javax.swing.text.BadLocationException ; 38 import javax.swing.event.*; 39 import java.util.Vector ; 40 41 import edu.rice.cs.drjava.DrJava; 42 import edu.rice.cs.drjava.config.*; 43 import edu.rice.cs.drjava.model.repl.*; 44 import edu.rice.cs.util.Log; 45 import edu.rice.cs.util.UnexpectedException; 46 import edu.rice.cs.util.text.EditDocumentException; 47 import edu.rice.cs.util.swing.Utilities; 48 49 52 public final class GlobalModelOtherTest extends GlobalModelTestCase implements OptionConstants { 53 54 Log _log = new Log("GlobalModelOtherTest.txt", false); 56 57 private static final String FOO_CLASS = 58 "package bar;\n" + 59 "public class Foo {\n" + 60 " public static void main(String[] args) {\n" + 61 " System.out.println(\"Foo\");\n" + 62 " }\n" + 63 "}\n"; 64 65 66 private File makeCanonical(File f) { 67 try { return f.getCanonicalFile(); } 68 catch (IOException e) { fail("Can't get a canonical path for file " + f); return null; } 69 } 70 71 72 public void testUndoEventsOccur() throws BadLocationException { 73 final OpenDefinitionsDocument doc = _model.newFile(); 74 75 doc.addUndoableEditListener(new UndoableEditListener() { 77 public void undoableEditHappened(UndoableEditEvent e) { doc.getUndoManager().addEdit(e.getEdit()); } 78 }); 79 80 TestListener listener = new TestListener() { public void undoableEditHappened() { undoableEditCount++; } }; 81 82 _model.addListener(listener); 83 changeDocumentText("test", doc); 84 85 Utilities.clearEventQueue(); 86 _model.removeListener(listener); 87 listener.assertUndoableEditCount(1); 88 90 } 92 93 94 public void testExitInteractions() throws EditDocumentException, InterruptedException { 95 InteractionListener listener = new InteractionListener() { 96 97 }; 99 100 _model.addListener(listener); 101 102 listener.logInteractionStart(); 103 interpretIgnoreResult("System.exit(23);"); 104 listener.waitInteractionDone(); 105 listener.waitResetDone(); 106 Utilities.clearEventQueue(); 107 108 _model.removeListener(listener); 109 110 listener.assertInteractionStartCount(1); 112 listener.assertInterpreterResettingCount(1); 113 listener.assertInterpreterReadyCount(1); 114 listener.assertInterpreterExitedCount(1); 115 assertEquals("exit status", 23, listener.getLastExitStatus()); 116 117 _log.log("testExitInteractions() completed"); 118 } 119 120 129 public void testInteractionAbort() throws BadLocationException , EditDocumentException, InterruptedException , 130 IOException { 131 132 doCompile(setupDocument(FOO_TEXT), tempFile()); 133 134 Utilities.clearEventQueue(); 135 136 final String beforeAbort = interpret("DrJavaTestFoo.class.getName()"); 137 138 _log.log("Completed initial interpret call"); 139 140 assertEquals("\"DrJavaTestFoo\"", beforeAbort); 141 142 Utilities.clearEventQueue(); 143 144 InteractionListener listener = new InteractionListener(); 145 146 _model.addListener(listener); 147 listener.logInteractionStart(); 148 _log.log("Starting infinite loop"); 149 interpretIgnoreResult("while (true) {}"); 150 Utilities.clearEventQueue(); 151 152 listener.assertInteractionStartCount(1); 153 154 _log.log("Resetting interactions"); 155 _model.resetInteractions(FileOption.NULL_FILE); 156 listener.waitResetDone(); 157 158 Utilities.clearEventQueue(); 159 160 listener.assertInterpreterResettingCount(1); 161 listener.assertInterpreterReadyCount(1); 162 listener.assertInterpreterExitedCount(0); 163 listener.assertConsoleResetCount(0); 164 165 assertEquals("5", interpret("5")); 167 _model.removeListener(listener); 168 169 final String afterAbort = interpret("DrJavaTestFoo.class.getName()"); 171 Utilities.clearEventQueue(); 172 assertEquals("\"DrJavaTestFoo\"", afterAbort); 173 _log.log("testInteractionAbort() completed"); 174 } 175 176 177 public void testResetConsole() throws EditDocumentException, InterruptedException { 178 InteractionListener listener = new InteractionListener(); 180 181 _model.addListener(listener); 182 183 listener.logInteractionStart(); 184 185 _model.resetConsole(); 186 Utilities.clearEventQueue(); 187 188 assertEquals("Length of console text", 0, _model.getConsoleDocument().getLength()); 189 190 listener.assertConsoleResetCount(1); 191 192 listener.logInteractionStart(); listener.resetConsoleResetCount(); 195 interpretIgnoreResult("System.out.print(\"a\");"); 196 listener.waitInteractionDone(); 197 198 Utilities.clearEventQueue(); 199 200 assertEquals("Length of console text", 1, _model.getConsoleDocument().getLength()); 201 202 _model.resetConsole(); 203 204 Utilities.clearEventQueue(); 205 assertEquals("Length of console text", 0, _model.getConsoleDocument().getLength()); 206 207 listener.assertConsoleResetCount(1); 208 _log.log("testResetConsole() completed"); 209 } 210 211 214 public void testInteractionsCanSeeCompiledClasses() throws BadLocationException , EditDocumentException, 215 IOException, InterruptedException { 216 OpenDefinitionsDocument doc1 = setupDocument(FOO_TEXT); 218 File dir1 = makeCanonical(new File(_tempDir, "dir1")); 219 dir1.mkdir(); 220 File file1 = makeCanonical(new File(dir1, "TestFile1.java")); 221 doCompile(doc1, file1); 222 223 assertEquals("interactions result", "\"DrJavaTestFoo\"", interpret("new DrJavaTestFoo().getClass().getName()")); 224 225 Vector <File> cp = new Vector <File>(); 227 cp.add(dir1); 228 DrJava.getConfig().setSetting(EXTRA_CLASSPATH, cp); 229 230 Utilities.clearEventQueue(); 231 _model.closeFile(doc1); 232 233 OpenDefinitionsDocument doc2 = setupDocument(BAZ_TEXT); 235 File dir2 = makeCanonical(new File(_tempDir, "dir2")); 236 dir2.mkdir(); 237 File file2 = makeCanonical(new File(dir2, "TestFile1.java")); 238 doCompile(doc2, file2); 239 240 assertEquals("interactions result", "\"DrJavaTestBaz\"", interpret("new DrJavaTestBaz().getClass().getName()")); 242 243 assertEquals("result of static field", "3", interpret("DrJavaTestBaz.x")); 245 246 assertEquals("interactions result", "\"DrJavaTestFoo\"", interpret("new DrJavaTestFoo().getClass().getName()")); 248 249 _log.log("testInteractionsCanSeeCompletedClasses() completed"); 250 } 251 252 256 public void testInteractionsVariableWithLowercaseClassName() throws BadLocationException , EditDocumentException, 257 IOException, InterruptedException { 258 OpenDefinitionsDocument doc1 = setupDocument("public class DrJavaTestClass {}"); 260 File file1 = makeCanonical(new File(_tempDir, "DrJavaTestClass.java")); 261 doCompile(doc1, file1); 262 263 assertEquals("interactions result", "", interpret("drJavaTestClass = new DrJavaTestClass();")); 265 _log.log("testInteractionsVariableWithLowercaseClassName() completed"); 266 } 267 268 269 public void testInteractionsCanSeeChangedClass() throws BadLocationException , EditDocumentException, 270 IOException, InterruptedException { 271 final String text_before = "class DrJavaTestFoo { public int m() { return "; 272 final String text_after = "; } }"; 273 final int num_iterations = 3; 274 File file; 275 OpenDefinitionsDocument doc; 276 277 for (int i = 0; i < num_iterations; i++) { 278 doc = setupDocument(text_before + i + text_after); 279 file = tempFile(i); 280 doCompile(doc, file); 281 282 assertEquals("interactions result, i=" + i, String.valueOf(i), interpret("new DrJavaTestFoo().m()")); 283 } 284 _log.log("testInteractionsCanSeeChangedClass() completed"); 285 } 286 287 288 public void testInteractionsDefineAnonymousInnerClass() throws BadLocationException , EditDocumentException, 289 IOException, InterruptedException { 290 final String interface_text = "public interface I { int getValue(); }"; 291 final File file = createFile("I.java"); 292 293 OpenDefinitionsDocument doc; 294 295 doc = setupDocument(interface_text); 296 doCompile(doc, file); 297 298 for (int i = 0; i < 3; i++) { 299 String s = "new I() { public int getValue() { return " + i + "; } }.getValue()"; 300 301 assertEquals("interactions result, i=" + i, String.valueOf(i), interpret(s)); 302 } 303 _log.log("testInteractionsDefineAnonymousInnerClass() completed"); 304 } 305 306 public void testGetSourceRootDefaultPackage() throws BadLocationException , IOException { 307 308 File[] roots = _model.getSourceRootSet(); 310 assertEquals("number of source roots", 0, roots.length); 311 312 File baseTempDir = tempDirectory(); 314 315 File subdir = makeCanonical(new File(baseTempDir, "a")); 317 subdir = makeCanonical(new File(subdir, "b")); 318 subdir = makeCanonical(new File(subdir, "c")); 319 subdir.mkdirs(); 320 321 File fooFile = makeCanonical(new File(subdir, "DrJavaTestFoo.java")); 323 OpenDefinitionsDocument doc = setupDocument(FOO_TEXT); 324 doc.saveFileAs(new FileSelector(fooFile)); 325 326 _model.addListener(new TestListener()); 328 329 roots = _model.getSourceRootSet(); 331 assertEquals("number of source roots", 1, roots.length); 332 assertEquals("source root", subdir, roots[0]); 334 335 _log.log("testGetSourceRootDefaultPackage() completed"); 336 } 337 338 public void testGetSourceRootPackageThreeDeepValid() throws BadLocationException , IOException { 339 File baseTempDir = tempDirectory(); 341 342 File subdir = makeCanonical(new File(baseTempDir, "a")); 344 subdir = makeCanonical(new File(subdir, "b").getCanonicalFile()); 345 subdir = makeCanonical(new File(subdir, "c").getCanonicalFile()); 346 subdir.mkdirs(); 347 348 File fooFile = makeCanonical(new File(subdir, "DrJavaTestFoo.java")); 350 OpenDefinitionsDocument doc = setupDocument("package a.b.c;\n" + FOO_TEXT); 351 doc.saveFileAs(new FileSelector(fooFile)); 352 354 _model.addListener(new TestListener()); 356 357 File[] roots = _model.getSourceRootSet(); 359 assertEquals("number of source roots", 1, roots.length); 360 assertEquals("source root", baseTempDir.getCanonicalFile(), roots[0].getCanonicalFile()); 361 362 _log.log("testGetSourceRootPackageThreeDeepValid() completed"); 363 } 364 365 366 public void testGetSourceRootPackageThreeDeepValidRelative() throws BadLocationException , IOException { 367 File baseTempDir = tempDirectory(); 369 File subdir = makeCanonical(new File(baseTempDir, "a")); 370 subdir = makeCanonical(new File(subdir, "b")); 371 subdir = makeCanonical(new File(subdir, "c")); 372 subdir.mkdirs(); 373 374 File relDir = makeCanonical(new File(baseTempDir, "./a/b/../b/c")); 377 File fooFile = makeCanonical(new File(relDir, "DrJavaTestFoo.java")); 378 OpenDefinitionsDocument doc = 379 setupDocument("package a.b.c;\n" + FOO_TEXT); 380 doc.saveFileAs(new FileSelector(fooFile)); 381 382 _model.addListener(new TestListener()); 384 385 File[] roots = _model.getSourceRootSet(); 387 assertEquals("number of source roots", 1, roots.length); 388 assertEquals("source root", baseTempDir.getCanonicalFile(), roots[0].getCanonicalFile()); 389 390 _log.log("testGetSourceRootPackageThreeDeepValidRelative() completed"); 391 } 392 393 public void testGetSourceRootPackageThreeDeepInvalid() throws BadLocationException , IOException { 394 File baseTempDir = tempDirectory(); 396 397 File subdir = makeCanonical(new File(baseTempDir, "a")); 399 subdir = makeCanonical(new File(subdir, "b")); 400 subdir = makeCanonical(new File(subdir, "d")); 401 subdir.mkdirs(); 402 403 File fooFile = makeCanonical(new File(subdir, "DrJavaTestFoo.java")); 405 OpenDefinitionsDocument doc = 406 setupDocument("package a.b.c;\n" + FOO_TEXT); 407 doc.saveFileAs(new FileSelector(fooFile)); 408 409 _model.addListener(new TestListener()); 411 412 File[] roots = _model.getSourceRootSet(); 414 assertEquals("number of source roots", 0, roots.length); 415 416 _log.log("testGetSourceRootPackageThreeDeepInvalid() completed"); 417 } 418 419 public void testGetSourceRootPackageOneDeepValid() throws BadLocationException , IOException { 420 File baseTempDir = tempDirectory(); 422 423 File subdir = makeCanonical(new File(baseTempDir, "a")); 425 subdir.mkdir(); 426 427 File fooFile = makeCanonical(new File(subdir, "DrJavaTestFoo.java")); 429 OpenDefinitionsDocument doc = setupDocument("package a;\n" + FOO_TEXT); 430 doc.saveFileAs(new FileSelector(fooFile)); 431 432 _model.addListener(new TestListener()); 434 435 File[] roots = _model.getSourceRootSet(); 437 assertEquals("number of source roots", 1, roots.length); 438 assertEquals("source root", baseTempDir.getCanonicalFile(), roots[0].getCanonicalFile()); 439 440 _log.log("testGetSourceRootPackageOneDeepValid() completed"); 441 } 442 443 444 public void testGetMultipleSourceRootsDefaultPackage() throws BadLocationException , IOException { 445 File baseTempDir = tempDirectory(); 447 448 File subdir1 = makeCanonical(new File(baseTempDir, "a")); 450 subdir1.mkdir(); 451 File subdir2 = makeCanonical(new File(baseTempDir, "b")); 452 subdir2.mkdir(); 453 454 File file1 = makeCanonical(new File(subdir1, "DrJavaTestFoo.java")); 456 OpenDefinitionsDocument doc1 = setupDocument(FOO_TEXT); 457 doc1.saveFileAs(new FileSelector(file1)); 458 459 File file2 = makeCanonical(new File(subdir1, "Bar.java")); 461 OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT); 462 doc2.saveFileAs(new FileSelector(file2)); 463 464 File file3 = makeCanonical(new File(subdir2, "Bar.java")); 466 OpenDefinitionsDocument doc3 = setupDocument(BAR_TEXT); 467 doc3.saveFileAs(new FileSelector(file3)); 468 469 Utilities.clearEventQueue(); 470 471 _model.addListener(new TestListener()); 473 474 File[] roots = _model.getSourceRootSet(); 476 assertEquals("number of source roots", 2, roots.length); 477 File root1 = roots[0]; 478 File root2 = roots[1]; 479 480 if (!( (root1.equals(subdir1) && root2.equals(subdir2)) || (root1.equals(subdir2) && root2.equals(subdir1)) )) { 483 fail("source roots did not match"); 484 } 485 486 _log.log("testGetMultipleSourceRootsDefaultPackage() completed"); 487 } 488 489 490 public void testInteractionsLiveUpdateClassPath() throws BadLocationException , EditDocumentException, 491 IOException, InterruptedException { 492 493 OpenDefinitionsDocument doc = setupDocument(FOO_TEXT); 494 Utilities.clearEventQueue(); 495 496 File f = tempFile(); 497 498 doCompile(doc, f); 499 500 String tempPath = f.getParent(); 502 File tempDir = makeCanonical(new File(tempPath)); 503 tempDir.renameTo(makeCanonical(new File(tempPath + "a"))); 504 505 String result = interpret("new DrJavaTestFoo().getClass().getName()"); 506 507 assertFalse("interactions should have an error, not the correct answer", "\"DrJavaTestFoo\"".equals(result)); 510 512 Vector <File> cp = new Vector <File>(); 514 cp.add(makeCanonical(new File(tempPath + "a"))); 515 DrJava.getConfig().setSetting(EXTRA_CLASSPATH, cp); 516 517 Utilities.clearEventQueue(); 518 _model.resetInteractionsClassPath(); 519 520 result = interpret("new DrJavaTestFoo().getClass().getName()"); 521 522 assertEquals("interactions result", "\"DrJavaTestFoo\"", result); 524 525 tempDir = makeCanonical(new File(tempPath + "a")); 527 boolean renamed = tempDir.renameTo(makeCanonical(new File(tempPath))); 528 529 _log.log("testInteractionsLiveUpdateClasspath() completed"); 530 } 531 532 533 public void testSwitchInterpreters() { 534 TestListener listener = new TestListener() { 535 public void interpreterChanged(boolean inProgress) { 536 assertTrue("should not be in progress", !inProgress); 537 interpreterChangedCount++; 538 } 539 }; 540 _model.addListener(listener); 541 542 DefaultInteractionsModel dim = _model.getInteractionsModel(); 543 544 dim.addJavaInterpreter("testInterpreter"); 546 547 dim.setActiveInterpreter("testInterpreter", "myPrompt>"); 548 549 Utilities.clearEventQueue(); 550 listener.assertInterpreterChangedCount(1); 551 _model.removeListener(listener); 552 553 _log.log("testSwitchInterpreters() completed"); 554 } 555 556 public void testRunMainMethod() throws Exception { 557 File dir = makeCanonical(new File(_tempDir, "bar")); 558 dir.mkdir(); 559 File file = makeCanonical(new File(dir, "Foo.java")); 560 final OpenDefinitionsDocument doc = doCompile(FOO_CLASS, file); 561 Utilities.invokeAndWait(new Runnable () { 562 public void run() { 563 try { doc.runMain(); } 564 catch(Exception e) { throw new UnexpectedException(e); } 565 } 566 }); 567 568 assertInteractionsContains(InteractionsModel.BANNER_PREFIX); 570 doc.insertString(doc.getLength(), " ", null); 571 572 Utilities.invokeAndWait(new Runnable () { 573 public void run() { 574 try { doc.runMain(); } 575 catch(Exception e) { throw new UnexpectedException(e); } 576 } 577 }); 578 579 assertInteractionsContains(DefaultGlobalModel.DOCUMENT_OUT_OF_SYNC_MSG); 581 Utilities.clearEventQueue(); 582 584 _log.log("testRunMainMethod() completed"); 585 } 586 } 587 | Popular Tags |