|
34 | 34 |
|
35 | 35 | import static org.hamcrest.CoreMatchers.containsString; |
36 | 36 | import static org.hamcrest.MatcherAssert.assertThat; |
37 | | -import static org.testng.Assert.assertEquals; |
38 | | -import static org.testng.Assert.assertNotNull; |
| 37 | +import static org.testng.Assert.*; |
39 | 38 |
|
40 | 39 | public class ModelManagerTests { |
41 | 40 |
|
@@ -265,6 +264,116 @@ private Map<WFile, String> keepErrorsInMap(ModelManagerImpl manager) { |
265 | 264 | return results; |
266 | 265 | } |
267 | 266 |
|
| 267 | + private CacheFixture setupCacheFixture(String projectName) throws IOException { |
| 268 | + GlobalCaches.clearAll(); |
| 269 | + |
| 270 | + File projectFolder = new File("./temp/" + projectName + "/"); |
| 271 | + File wurstFolder = new File(projectFolder, "wurst"); |
| 272 | + newCleanFolder(wurstFolder); |
| 273 | + |
| 274 | + String packageA = string( |
| 275 | + "package A", |
| 276 | + "import B", |
| 277 | + "public function a() returns int", |
| 278 | + " return b()" |
| 279 | + ); |
| 280 | + |
| 281 | + String packageB = string( |
| 282 | + "package B", |
| 283 | + "import C", |
| 284 | + "public function b() returns int", |
| 285 | + " return c()" |
| 286 | + ); |
| 287 | + |
| 288 | + String packageC = string( |
| 289 | + "package C", |
| 290 | + "public function c() returns int", |
| 291 | + " return 1" |
| 292 | + ); |
| 293 | + |
| 294 | + String packageD = string( |
| 295 | + "package D", |
| 296 | + "public function d() returns int", |
| 297 | + " return 2" |
| 298 | + ); |
| 299 | + |
| 300 | + WFile fileA = WFile.create(new File(wurstFolder, "A.wurst")); |
| 301 | + WFile fileB = WFile.create(new File(wurstFolder, "B.wurst")); |
| 302 | + WFile fileC = WFile.create(new File(wurstFolder, "C.wurst")); |
| 303 | + WFile fileD = WFile.create(new File(wurstFolder, "D.wurst")); |
| 304 | + WFile fileWurst = WFile.create(new File(wurstFolder, "Wurst.wurst")); |
| 305 | + |
| 306 | + writeFile(fileA, packageA); |
| 307 | + writeFile(fileB, packageB); |
| 308 | + writeFile(fileC, packageC); |
| 309 | + writeFile(fileD, packageD); |
| 310 | + writeFile(fileWurst, "package Wurst\n"); |
| 311 | + |
| 312 | + ModelManagerImpl manager = new ModelManagerImpl(projectFolder, new BufferManager()); |
| 313 | + Map<WFile, String> results = keepErrorsInMap(manager); |
| 314 | + manager.buildProject(); |
| 315 | + |
| 316 | + CompilationUnit cuA = manager.getCompilationUnit(fileA); |
| 317 | + CompilationUnit cuB = manager.getCompilationUnit(fileB); |
| 318 | + CompilationUnit cuC = manager.getCompilationUnit(fileC); |
| 319 | + CompilationUnit cuD = manager.getCompilationUnit(fileD); |
| 320 | + |
| 321 | + assertNotNull(cuA); |
| 322 | + assertNotNull(cuB); |
| 323 | + assertNotNull(cuC); |
| 324 | + assertNotNull(cuD); |
| 325 | + |
| 326 | + WPackage pkgA = cuA.getPackages().get(0); |
| 327 | + WPackage pkgB = cuB.getPackages().get(0); |
| 328 | + WPackage pkgC = cuC.getPackages().get(0); |
| 329 | + WPackage pkgD = cuD.getPackages().get(0); |
| 330 | + |
| 331 | + GlobalCaches.CacheKey keyA = new GlobalCaches.CacheKey(pkgA, "markerA", GlobalCaches.LookupType.FUNC); |
| 332 | + GlobalCaches.CacheKey keyB = new GlobalCaches.CacheKey(pkgB, "markerB", GlobalCaches.LookupType.FUNC); |
| 333 | + GlobalCaches.CacheKey keyC = new GlobalCaches.CacheKey(pkgC, "markerC", GlobalCaches.LookupType.FUNC); |
| 334 | + GlobalCaches.CacheKey keyD = new GlobalCaches.CacheKey(pkgD, "markerD", GlobalCaches.LookupType.FUNC); |
| 335 | + |
| 336 | + GlobalCaches.lookupCache.put(keyA, Boolean.TRUE); |
| 337 | + GlobalCaches.lookupCache.put(keyB, Boolean.TRUE); |
| 338 | + GlobalCaches.lookupCache.put(keyC, Boolean.TRUE); |
| 339 | + GlobalCaches.lookupCache.put(keyD, Boolean.TRUE); |
| 340 | + |
| 341 | + assertTrue(GlobalCaches.lookupCache.containsKey(keyA)); |
| 342 | + assertTrue(GlobalCaches.lookupCache.containsKey(keyB)); |
| 343 | + assertTrue(GlobalCaches.lookupCache.containsKey(keyC)); |
| 344 | + assertTrue(GlobalCaches.lookupCache.containsKey(keyD)); |
| 345 | + |
| 346 | + return new CacheFixture(manager, results, fileA, fileB, fileC, fileD, keyA, keyB, keyC, keyD); |
| 347 | + } |
| 348 | + |
| 349 | + private static final class CacheFixture { |
| 350 | + final ModelManagerImpl manager; |
| 351 | + final Map<WFile, String> results; |
| 352 | + final WFile fileA; |
| 353 | + final WFile fileB; |
| 354 | + final WFile fileC; |
| 355 | + final WFile fileD; |
| 356 | + final GlobalCaches.CacheKey keyA; |
| 357 | + final GlobalCaches.CacheKey keyB; |
| 358 | + final GlobalCaches.CacheKey keyC; |
| 359 | + final GlobalCaches.CacheKey keyD; |
| 360 | + |
| 361 | + CacheFixture(ModelManagerImpl manager, Map<WFile, String> results, WFile fileA, WFile fileB, |
| 362 | + WFile fileC, WFile fileD, GlobalCaches.CacheKey keyA, GlobalCaches.CacheKey keyB, |
| 363 | + GlobalCaches.CacheKey keyC, GlobalCaches.CacheKey keyD) { |
| 364 | + this.manager = manager; |
| 365 | + this.results = results; |
| 366 | + this.fileA = fileA; |
| 367 | + this.fileB = fileB; |
| 368 | + this.fileC = fileC; |
| 369 | + this.fileD = fileD; |
| 370 | + this.keyA = keyA; |
| 371 | + this.keyB = keyB; |
| 372 | + this.keyC = keyC; |
| 373 | + this.keyD = keyD; |
| 374 | + } |
| 375 | + } |
| 376 | + |
268 | 377 | private void newCleanFolder(File f) throws IOException { |
269 | 378 | FileUtils.deleteRecursively(f); |
270 | 379 | Files.createDirectories(f.toPath()); |
@@ -355,6 +464,49 @@ public void visit(ClassDef c) { |
355 | 464 |
|
356 | 465 | } |
357 | 466 |
|
| 467 | + @Test |
| 468 | + public void selectiveCacheInvalidationSkipsUnaffectedUnits() throws IOException { |
| 469 | + CacheFixture fixture = setupCacheFixture("cacheInvalidationProject1"); |
| 470 | + fixture.results.clear(); |
| 471 | + |
| 472 | + String packageBUpdated = string( |
| 473 | + "package B", |
| 474 | + "import C", |
| 475 | + "public function b() returns int", |
| 476 | + " return c() + 1" |
| 477 | + ); |
| 478 | + |
| 479 | + ModelManager.Changes changes = fixture.manager.syncCompilationUnitContent(fixture.fileB, packageBUpdated); |
| 480 | + fixture.manager.reconcile(changes); |
| 481 | + |
| 482 | + assertEquals(fixture.results.keySet(), ImmutableSet.of(fixture.fileA, fixture.fileB)); |
| 483 | + assertFalse(GlobalCaches.lookupCache.containsKey(fixture.keyA)); |
| 484 | + assertFalse(GlobalCaches.lookupCache.containsKey(fixture.keyB)); |
| 485 | + assertTrue(GlobalCaches.lookupCache.containsKey(fixture.keyC)); |
| 486 | + assertTrue(GlobalCaches.lookupCache.containsKey(fixture.keyD)); |
| 487 | + } |
| 488 | + |
| 489 | + @Test |
| 490 | + public void selectiveCacheInvalidationCoversTransitiveDependents() throws IOException { |
| 491 | + CacheFixture fixture = setupCacheFixture("cacheInvalidationProject2"); |
| 492 | + fixture.results.clear(); |
| 493 | + |
| 494 | + String packageCUpdated = string( |
| 495 | + "package C", |
| 496 | + "public function c() returns int", |
| 497 | + " return 2" |
| 498 | + ); |
| 499 | + |
| 500 | + ModelManager.Changes changes = fixture.manager.syncCompilationUnitContent(fixture.fileC, packageCUpdated); |
| 501 | + fixture.manager.reconcile(changes); |
| 502 | + |
| 503 | + assertEquals(fixture.results.keySet(), ImmutableSet.of(fixture.fileA, fixture.fileB, fixture.fileC)); |
| 504 | + assertFalse(GlobalCaches.lookupCache.containsKey(fixture.keyA)); |
| 505 | + assertFalse(GlobalCaches.lookupCache.containsKey(fixture.keyB)); |
| 506 | + assertFalse(GlobalCaches.lookupCache.containsKey(fixture.keyC)); |
| 507 | + assertTrue(GlobalCaches.lookupCache.containsKey(fixture.keyD)); |
| 508 | + } |
| 509 | + |
358 | 510 | @Test |
359 | 511 | public void changeModuleAbstractMethod() throws IOException { |
360 | 512 | File projectFolder = new File("./temp/testProject2/"); |
|
0 commit comments