diff options
Diffstat (limited to 'sonar-home')
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java | 40 | ||||
-rw-r--r-- | sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java | 27 |
2 files changed, 43 insertions, 24 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java index 640d5c29401..b8fdca5d1d5 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java @@ -142,7 +142,7 @@ public class PersistentCache { logger.info("cache: clearing"); try { lock(); - deleteCacheEntries(createClearFilter()); + deleteCacheEntries(new DirectoryClearFilter()); } catch (IOException e) { logger.error("Error clearing cache", e); } finally { @@ -157,7 +157,7 @@ public class PersistentCache { logger.info("cache: cleaning"); try { lock(); - deleteCacheEntries(createCleanFilter()); + deleteCacheEntries(new DirectoryCleanFilter(defaultDurationToExpireMs)); } catch (IOException e) { logger.error("Error cleaning cache", e); } finally { @@ -229,26 +229,28 @@ public class PersistentCache { } } - private DirectoryStream.Filter<Path> createClearFilter() throws IOException { - return new DirectoryStream.Filter<Path>() { - @Override - public boolean accept(Path entry) throws IOException { - return !LOCK_FNAME.equals(entry.getFileName().toString()); - } - }; + private static class DirectoryClearFilter implements DirectoryStream.Filter<Path> { + @Override + public boolean accept(Path entry) throws IOException { + return !LOCK_FNAME.equals(entry.getFileName().toString()); + } } - private DirectoryStream.Filter<Path> createCleanFilter() throws IOException { - return new DirectoryStream.Filter<Path>() { - @Override - public boolean accept(Path entry) throws IOException { - if (LOCK_FNAME.equals(entry.getFileName().toString())) { - return false; - } + private static class DirectoryCleanFilter implements DirectoryStream.Filter<Path> { + private long defaultDurationToExpireMs; - return isCacheEntryExpired(entry, PersistentCache.this.defaultDurationToExpireMs); + DirectoryCleanFilter(long defaultDurationToExpireMs) { + this.defaultDurationToExpireMs = defaultDurationToExpireMs; + } + + @Override + public boolean accept(Path entry) throws IOException { + if (LOCK_FNAME.equals(entry.getFileName().toString())) { + return false; } - }; + + return isCacheEntryExpired(entry, defaultDurationToExpireMs); + } } private void putCache(String key, byte[] value) throws IOException { @@ -280,7 +282,7 @@ public class PersistentCache { return true; } - private boolean isCacheEntryExpired(Path cacheEntryPath, long durationToExpireMs) throws IOException { + private static boolean isCacheEntryExpired(Path cacheEntryPath, long durationToExpireMs) throws IOException { BasicFileAttributes attr = Files.readAttributes(cacheEntryPath, BasicFileAttributes.class); long modTime = attr.lastModifiedTime().toMillis(); diff --git a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java index c327ff7c5eb..72f594bf7d1 100644 --- a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java +++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java @@ -20,12 +20,13 @@ package org.sonar.home.cache; import java.io.File; +import java.io.IOException; + import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; - import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -66,6 +67,16 @@ public class PersistentCacheTest { @Test public void testClean() throws Exception { + // puts entry + assertCacheHit(false); + // negative time to make sure it is expired + cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null); + cache.clean(); + assertCacheHit(false); + } + + @Test + public void testClear() throws Exception { assertCacheHit(false); cache.clear(); assertCacheHit(false); @@ -78,6 +89,12 @@ public class PersistentCacheTest { } @Test + public void testPut() throws Exception { + cache.put(URI, VALUE.getBytes()); + assertCacheHit(true); + } + + @Test public void testReconfigure() throws Exception { cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null); assertCacheHit(false); @@ -101,22 +118,22 @@ public class PersistentCacheTest { assertCacheHit(false); assertCacheHit(false); } - + @Test public void testDifferentServerVersions() throws Exception { assertCacheHit(false); assertCacheHit(true); - + PersistentCache cache2 = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), "5.2"); assertCacheHit(cache2, false); assertCacheHit(cache2, true); - + } private void assertCacheHit(boolean hit) throws Exception { assertCacheHit(cache, hit); } - + private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception { CacheFillerString c = new CacheFillerString(); assertThat(pCache.getString(URI, c)).isEqualTo(VALUE); |