1 33 34 package edu.rice.cs.drjava.model.cache; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 import edu.rice.cs.drjava.model.*; 38 import edu.rice.cs.drjava.model.definitions.DefinitionsDocument; 39 import edu.rice.cs.plt.io.IOUtil; 40 import edu.rice.cs.util.FileOpenSelector; 41 import edu.rice.cs.util.OperationCanceledException; 42 43 import javax.swing.text.BadLocationException ; 44 import java.io.File ; 45 import java.io.IOException ; 46 import java.util.Hashtable ; 47 import java.util.List ; 48 49 50 public class DocumentCacheTest extends DrJavaTestCase { 51 52 private DefaultGlobalModel _model; 53 private DocumentCache _cache; 54 private Hashtable <OpenDefinitionsDocument, DCacheAdapter> _adapterTable; 55 56 private int _doc_made; 57 private int _doc_saved; 58 59 protected File _tempDir; 60 61 public void setUp() throws Exception { 62 super.setUp(); 63 createModel(); 64 65 String user = System.getProperty("user.name"); 66 _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, ""); 67 68 _cache = _model.getDocumentCache(); 69 _cache.setCacheSize(4); 70 _adapterTable = new Hashtable <OpenDefinitionsDocument, DCacheAdapter> (); 71 _cache.addRegistrationListener(new DocumentCache.RegistrationListener() { 72 public void registered(OpenDefinitionsDocument odd, DCacheAdapter a) { 73 _adapterTable.put(odd, a); 74 } 75 }); 76 _doc_made = 0; 77 _doc_saved = 0; 78 } 79 80 public void tearDown() throws Exception { 81 boolean ret = IOUtil.deleteRecursively(_tempDir); 82 assertTrue("delete temp directory " + _tempDir, ret); 83 _model.dispose(); 84 85 _tempDir = null; 86 _model = null; 87 super.tearDown(); 88 } 89 90 91 protected void createModel() { 92 _model = new TestGlobalModel(); 94 95 _model.waitForInterpreter(); 97 } 98 99 100 protected File tempFile() throws IOException { 101 return File.createTempFile("DrJava-test", ".java", _tempDir).getCanonicalFile(); 102 } 103 104 108 protected File tempFile(int i) throws IOException { 109 return File.createTempFile("DrJava-test" + i, ".java", _tempDir).getCanonicalFile(); 110 } 111 112 protected OpenDefinitionsDocument openFile(final File f) throws IOException { 113 try{ 114 OpenDefinitionsDocument doc = _model.openFile(new FileOpenSelector() { 115 public File [] getFiles() { return new File [] {f}; } 116 }); 117 return doc; 118 } 119 catch(AlreadyOpenException e) { throw new IOException (e.getMessage()); } 120 catch(OperationCanceledException e) { throw new IOException (e.getMessage());} 121 } 122 123 124 public void testCacheSize() { 125 _cache.setCacheSize(6); 126 assertEquals("Wrong cache size", 6, _cache.getCacheSize()); 127 _cache.setCacheSize(34); 128 assertEquals("Wrong cache size", 34, _cache.getCacheSize()); 129 130 133 try { 134 _cache.setCacheSize(0); 135 fail("IllegalArgumentException expected."); 136 } 137 catch (IllegalArgumentException iae) { 138 } 140 141 try { 142 _cache.setCacheSize(-34); 143 fail("IllegalArgumentException expected."); 144 } 145 catch (IllegalArgumentException iae) { 146 } 148 } 149 150 public void testNewDocumentsInAndOutOfTheCache() throws BadLocationException , IOException { 151 assertEquals("Wrong Cache Size", 4, _cache.getCacheSize()); 152 153 156 OpenDefinitionsDocument doc1 = _model.newFile(); 157 assertEquals("There should be 0 documents in the cache", 0, _cache.getNumInCache()); 159 OpenDefinitionsDocument doc2 = _model.newFile(); 160 assertEquals("There should be 0 documents in the cache", 0, _cache.getNumInCache()); 162 OpenDefinitionsDocument doc3 = _model.newFile(); 163 assertEquals("There should be 0 documents in the cache", 0, _cache.getNumInCache()); 165 OpenDefinitionsDocument doc4 = _model.newFile(); 166 assertEquals("There should be 0 documents in the cache", 0, _cache.getNumInCache()); 168 OpenDefinitionsDocument doc5 = _model.newFile(); 169 assertEquals("There should be 0 documents in the cache", 0, _cache.getNumInCache()); 171 OpenDefinitionsDocument doc6 = _model.newFile(); 172 assertEquals("There should be 0 documents in the cache", 0, _cache.getNumInCache()); 174 177 assertFalse("Document 1 shouldn't be modified", doc1.isModifiedSinceSave()); 178 assertFalse("Document 2 shouldn't be modified", doc2.isModifiedSinceSave()); 179 assertFalse("Document 3 shouldn't be modified", doc3.isModifiedSinceSave()); 180 assertFalse("Document 4 shouldn't be modified", doc4.isModifiedSinceSave()); 181 assertFalse("Document 5 shouldn't be modified", doc5.isModifiedSinceSave()); 182 assertFalse("Document 6 shouldn't be modified", doc6.isModifiedSinceSave()); 183 assertEquals("There should be 0 documents in the cache", 0, _cache.getNumInCache()); 184 185 assertFalse("Document 1 shouldn't be ready", _adapterTable.get(doc1).isReady()); 186 assertFalse("Document 2 shouldn't be ready", _adapterTable.get(doc2).isReady()); 187 assertFalse("Document 3 shouldn't be ready", _adapterTable.get(doc3).isReady()); 188 assertFalse("Document 4 shouldn't be ready", _adapterTable.get(doc4).isReady()); 189 assertFalse("Document 5 shouldn't be ready", _adapterTable.get(doc5).isReady()); 190 assertFalse("Document 6 shouldn't be ready", _adapterTable.get(doc6).isReady()); 191 192 193 doc1.getLength(); 195 doc2.getLength(); 196 doc3.getLength(); 197 doc4.getLength(); 198 doc5.getLength(); 199 doc6.getLength(); 200 201 assertTrue("Document 1 should be ready", _adapterTable.get(doc1).isReady()); 202 assertTrue("Document 2 should be ready", _adapterTable.get(doc2).isReady()); 203 assertTrue("Document 3 should be ready", _adapterTable.get(doc3).isReady()); 204 assertTrue("Document 4 should be ready", _adapterTable.get(doc4).isReady()); 205 assertTrue("Document 5 should be ready", _adapterTable.get(doc5).isReady()); 206 assertTrue("Document 6 should be ready", _adapterTable.get(doc6).isReady()); 207 208 210 assertEquals("Confirm that cache is empty", 0, _cache.getNumInCache()); 211 212 } 213 214 public void testOldDocumentsInAndOutOfTheCache() throws BadLocationException , IOException { 215 216 File file1 = tempFile(1); 217 File file2 = tempFile(2); 218 File file3 = tempFile(3); 219 File file4 = tempFile(4); 220 File file5 = tempFile(5); 221 File file6 = tempFile(6); 222 223 OpenDefinitionsDocument doc1 = openFile(file1); 225 doc1.getLength(); assertEquals("There should be 1 document in the cache", 1, _cache.getNumInCache()); 227 OpenDefinitionsDocument doc2 = openFile(file2); 228 doc2.getLength(); assertEquals("There should be 2 documents in the cache", 2, _cache.getNumInCache()); 230 OpenDefinitionsDocument doc3 = openFile(file3); 231 doc3.getLength(); assertEquals("There should be 3 documents in the cache", 3, _cache.getNumInCache()); 233 OpenDefinitionsDocument doc4 = openFile(file4); 234 doc4.getLength(); assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 236 OpenDefinitionsDocument doc5 = openFile(file5); 237 doc5.getLength(); assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 239 OpenDefinitionsDocument doc6 = openFile(file6); 240 doc6.getLength(); assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 242 243 assertEquals("Wrong Cache Size", 4, _cache.getCacheSize()); 244 245 249 assertFalse("Document 1 shouldn't be modified", doc1.isModifiedSinceSave()); 250 assertFalse("Document 2 shouldn't be modified", doc2.isModifiedSinceSave()); 251 assertFalse("Document 3 shouldn't be modified", doc3.isModifiedSinceSave()); 252 assertFalse("Document 4 shouldn't be modified", doc4.isModifiedSinceSave()); 253 assertFalse("Document 5 shouldn't be modified", doc5.isModifiedSinceSave()); 254 assertFalse("Document 6 shouldn't be modified", doc6.isModifiedSinceSave()); 255 256 assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 257 258 assertFalse("Document 1 shouldn't be ready", _adapterTable.get(doc1).isReady()); 259 assertFalse("Document 2 shouldn't be ready", _adapterTable.get(doc2).isReady()); 260 assertTrue("Document 3 should be ready", _adapterTable.get(doc3).isReady()); 261 assertTrue("Document 4 should be ready", _adapterTable.get(doc4).isReady()); 262 assertTrue("Document 5 should be ready", _adapterTable.get(doc5).isReady()); 263 assertTrue("Document 6 should be ready", _adapterTable.get(doc6).isReady()); 264 265 266 268 doc1.getLength(); 269 doc2.getLength(); 270 doc3.getLength(); 271 doc4.getLength(); 272 273 275 assertTrue("Document 1 should be ready", _adapterTable.get(doc1).isReady()); 276 assertTrue("Document 2 should be ready", _adapterTable.get(doc2).isReady()); 277 assertTrue("Document 3 should be ready", _adapterTable.get(doc3).isReady()); 278 assertTrue("Document 4 should be ready", _adapterTable.get(doc4).isReady()); 279 280 doc5.getLength(); 281 assertFalse("Document 1 is not longer ready", _adapterTable.get(doc1).isReady()); 283 assertTrue("Document 5 should be ready", _adapterTable.get(doc5).isReady()); 284 285 doc6.getLength(); 286 assertFalse("Document 2 is not longer ready", _adapterTable.get(doc2).isReady()); 288 assertTrue("Document 6 should be ready", _adapterTable.get(doc6).isReady()); 289 assertTrue("Document 3 should be ready", _adapterTable.get(doc3).isReady()); 290 assertTrue("Document 4 should be ready", _adapterTable.get(doc4).isReady()); 291 assertTrue("Document 5 should be ready", _adapterTable.get(doc5).isReady()); 292 293 doc1.getLength(); assertTrue("The document 1 should should now be in the cache", _adapterTable.get(doc1).isReady()); 295 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 296 assertFalse("The document 3 should have been kicked out of the cache", _adapterTable.get(doc3).isReady()); 297 298 doc2.getLength(); assertTrue("The document 2 should should now be in the cache", _adapterTable.get(doc2).isReady()); 300 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 301 assertFalse("The document 4 should have been kicked out of the cache", _adapterTable.get(doc4).isReady()); 302 303 doc3.getLength(); assertTrue("The document 3 should should now be in the cache", _adapterTable.get(doc3).isReady()); 305 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 306 assertFalse("The document 5 should have been kicked out of the cache", _adapterTable.get(doc5).isReady()); 307 308 doc4.getLength(); assertTrue("The document 4 should should now be in the cache", _adapterTable.get(doc4).isReady()); 310 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 311 assertFalse("The document 6 should have been kicked out of the cache", _adapterTable.get(doc6).isReady()); 312 313 doc5.getLength(); assertTrue("The document 5 should should now be in the cache", _adapterTable.get(doc5).isReady()); 315 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 316 assertFalse("The document 1 should have been kicked out of the cache", _adapterTable.get(doc1).isReady()); 317 318 doc6.getLength(); assertTrue("The document 6 should should now be in the cache", _adapterTable.get(doc6).isReady()); 320 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 321 assertFalse("The document 2 should have been kicked out of the cache", _adapterTable.get(doc2).isReady()); 322 323 doc4.getLength(); assertTrue("The document 3 should should still be in the cache", _adapterTable.get(doc3).isReady()); 326 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 327 doc5.getLength(); assertTrue("The document 3 should should still be in the cache", _adapterTable.get(doc3).isReady()); 329 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 330 doc3.getLength(); assertTrue("The document 6 should should still be in the cache", _adapterTable.get(doc6).isReady()); 332 assertEquals("There should still be 4 documents in the cache", 4, _cache.getNumInCache()); 333 doc4.getLength(); assertTrue("The document 6 should should still be in the cache", _adapterTable.get(doc6).isReady()); 335 336 assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 337 assertFalse("The document 1 should still be out of the cache", _adapterTable.get(doc1).isReady()); 338 assertFalse("The document 2 should still be out of the cache", _adapterTable.get(doc2).isReady()); 339 340 _cache.setCacheSize(5); assertEquals("The cache size should now be 5", 5, _cache.getCacheSize()); 343 assertEquals("There should still only be 4 files in the cache", 4, _cache.getNumInCache()); 344 345 doc2.getLength(); assertTrue("The document 2 should now be in the cache", _adapterTable.get(doc2).isReady()); 347 assertFalse("The document 1 should still be out of the cache", _adapterTable.get(doc1).isReady()); 348 assertEquals("There should be 5 documents in the cache", 5, _cache.getNumInCache()); 349 350 _cache.setCacheSize(3); 352 assertEquals("The cache size should now be 3", 3, _cache.getCacheSize()); 353 assertEquals("There should be 3 documents in the cache", 3, _cache.getNumInCache()); 354 assertTrue("The document 2 should be in the cache", _adapterTable.get(doc2).isReady()); 355 assertTrue("The document 6 should be in the cache", _adapterTable.get(doc6).isReady()); 356 assertTrue("The document 5 should be in the cache", _adapterTable.get(doc5).isReady()); 357 assertFalse("The document 3 should now be out of the cache", _adapterTable.get(doc3).isReady()); 358 assertFalse("The document 4 should now be out of the cache", _adapterTable.get(doc4).isReady()); 359 assertFalse("The document 1 should still be out of the cache", _adapterTable.get(doc1).isReady()); 360 } 361 362 public void testGetDDocFromCache() throws BadLocationException , IOException , OperationCanceledException { 363 File file1 = tempFile(1); 364 File file2 = tempFile(2); 365 File file3 = tempFile(3); 366 File file4 = tempFile(4); 367 File file5 = tempFile(5); 368 File file6 = tempFile(6); 369 370 OpenDefinitionsDocument doc1 = openFile(file1); 372 doc1.getLength(); assertTrue("The document should not start out in the cache", _adapterTable.get(doc1).isReady()); 374 assertEquals("There should be 1 documents in the cache", 1, _cache.getNumInCache()); 375 376 OpenDefinitionsDocument doc2 = openFile(file2); 377 doc2.getLength(); assertTrue("The document should not start out in the cache", _adapterTable.get(doc2).isReady()); 379 assertEquals("There should be 2 documents in the cache", 2, _cache.getNumInCache()); 380 381 OpenDefinitionsDocument doc3 = openFile(file3); 382 doc3.getLength(); assertTrue("The document should not start out in the cache", _adapterTable.get(doc3).isReady()); 384 assertEquals("There should be 3 documents in the cache", 3, _cache.getNumInCache()); 385 386 OpenDefinitionsDocument doc4 = openFile(file4); 387 doc4.getLength(); assertTrue("The document should not start out in the cache", _adapterTable.get(doc4).isReady()); 389 assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 390 391 OpenDefinitionsDocument doc5 = openFile(file5); 392 doc5.getLength(); assertTrue("The document should not start out in the cache", _adapterTable.get(doc5).isReady()); 394 assertFalse("The document should not start out in the cache", _adapterTable.get(doc1).isReady()); 395 assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 396 397 OpenDefinitionsDocument doc6 = openFile(file6); 398 doc6.getLength(); assertTrue("The document should not start out in the cache", _adapterTable.get(doc6).isReady()); 400 assertFalse("The document should not start out in the cache", _adapterTable.get(doc2).isReady()); 401 assertEquals("There should be 4 documents in the cache", 4, _cache.getNumInCache()); 402 } 403 404 405 private DefinitionsDocument _saved; 407 public void testReconstructor() throws IOException { 408 DDReconstructor d = new DDReconstructor() { 409 public DefinitionsDocument make() { 410 _doc_made++; 411 return _saved; 412 } 413 public void saveDocInfo(DefinitionsDocument doc) { _doc_saved++; } 414 public void addDocumentListener(javax.swing.event.DocumentListener dl) { } 415 }; 416 417 OpenDefinitionsDocument doc1 = _model.newFile(); 418 assertFalse("The document should not be in the cache", _adapterTable.get(doc1).isReady()); 419 _saved = _adapterTable.get(doc1).getDocument(); 420 assertTrue("The document should be in the cache", _adapterTable.get(doc1).isReady()); 421 422 } 430 431 public void testNoDDocInCache() { 434 OpenDefinitionsDocument doc1 = _model.newFile(); 435 _model.closeFile(doc1); 436 assertFalse("The document should now be closed", _adapterTable.get(doc1).isReady()); 437 } 438 439 440 public void testNumListeners() { 441 OpenDefinitionsDocument doc1 = _model.newFile(); 442 OpenDefinitionsDocument doc2 = _model.newFile(); 443 OpenDefinitionsDocument doc3 = _model.newFile(); 444 OpenDefinitionsDocument doc4 = _model.newFile(); 445 OpenDefinitionsDocument doc5 = _model.newFile(); 446 447 int numDocListeners = doc1.getDocumentListeners().length; 448 int numUndoListeners = doc1.getUndoableEditListeners().length; 449 450 doc1.getLength(); 451 doc2.getLength(); 452 doc3.getLength(); 453 doc4.getLength(); 454 455 doc5.getLength(); 457 458 doc1.getLength(); 460 461 assertEquals("the number of document listeners is the same after reconstruction", numDocListeners, 462 doc1.getDocumentListeners().length); 463 assertEquals("the number of undoableEditListeners is the same after reconstruction", numUndoListeners, 464 doc1.getUndoableEditListeners().length); 465 466 } 467 468 481 public void testMemoryLeak() throws InterruptedException , IOException { 482 _memLeakCounter=0; 483 FinalizationListener<DefinitionsDocument> fl = new FinalizationListener<DefinitionsDocument>() { 484 public void finalized(FinalizationEvent<DefinitionsDocument> fe) { 485 _memLeakCounter++; 486 } 487 }; 488 489 491 492 OpenDefinitionsDocument doc1 = openFile(tempFile(1)); 493 OpenDefinitionsDocument doc2 = openFile(tempFile(2)); 494 OpenDefinitionsDocument doc3 = openFile(tempFile(3)); 495 OpenDefinitionsDocument doc4 = openFile(tempFile(4)); 496 OpenDefinitionsDocument doc5 = openFile(tempFile(5)); 497 498 doc1.addFinalizationListener(fl); 499 doc2.addFinalizationListener(fl); 500 doc3.addFinalizationListener(fl); 501 doc4.addFinalizationListener(fl); 502 doc5.addFinalizationListener(fl); 504 assertEquals("There should be 4 in the QUEUE", 4, _cache.getNumInCache()); 505 System.gc(); 506 Thread.sleep(100); 507 508 509 assertFalse("doc1 should be the one that's not ready", _adapterTable.get(doc1).isReady()); 510 assertEquals("One doc should have been collected", 1, _memLeakCounter); 511 512 doc1.getLength(); 514 List <FinalizationListener<DefinitionsDocument>> list = doc1.getFinalizationListeners(); 516 assertEquals("There should only be one finalization listener", 1, list.size()); 517 assertEquals("The finalization listener should be fl", fl, list.get(0)); 518 519 doc2.getLength(); doc3.getLength(); doc4.getLength(); doc5.getLength(); 524 System.gc(); 525 Thread.sleep(100); 526 assertEquals("several docs should have been collected", 6, _memLeakCounter); 527 528 } 529 private int _memLeakCounter; 530 531 532 private class TestGlobalModel extends DefaultGlobalModel { 533 public void aboutToSaveFromSaveAll(OpenDefinitionsDocument doc) { } 534 public void saveAllFiles(FileSaveSelector fs) throws IOException { saveAllFilesHelper(fs); } 535 536 public OpenDefinitionsDocument newFile() { return newFile(getMasterWorkingDirectory()); } 537 public OpenDefinitionsDocument openFile(FileOpenSelector fs) 538 throws IOException , OperationCanceledException, AlreadyOpenException { 539 return openFileHelper(fs); 540 } 541 public boolean closeFile(OpenDefinitionsDocument doc) { return closeFileHelper(doc); } 542 public OpenDefinitionsDocument[] openFiles(FileOpenSelector com) 543 throws IOException , OperationCanceledException, AlreadyOpenException { 544 return openFilesHelper(com); 545 } 546 public boolean closeAllFiles() { 547 closeAllFilesOnQuit(); 548 return true; 549 } 550 } 551 } 552 | Popular Tags |