diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-09-30 15:16:56 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-10-01 14:34:34 +0200 |
commit | 943e74fd412f701247e1bea5aa3760418a10ec0d (patch) | |
tree | 7ae154b773b7cc92903825f6359b9400bac8c4be /sonar-home/src/test | |
parent | 3badd476071ad30333b9a1a4080531e84953b6e4 (diff) | |
download | sonarqube-943e74fd412f701247e1bea5aa3760418a10ec0d.tar.gz sonarqube-943e74fd412f701247e1bea5aa3760418a10ec0d.zip |
Decouple cache invalidation criteria
Diffstat (limited to 'sonar-home/src/test')
-rw-r--r-- | sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java | 17 | ||||
-rw-r--r-- | sonar-home/src/test/java/org/sonar/home/cache/TTLCacheInvalidationTest.java | 55 |
2 files changed, 65 insertions, 7 deletions
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 5063fbdb22f..5bb52298bb6 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 @@ -28,6 +28,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import static org.mockito.Matchers.any; + import static org.mockito.Mockito.when; import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.verify; @@ -44,15 +46,18 @@ public class PersistentCacheTest { private final static String VALUE = "cache content"; private PersistentCache cache = null; private DirectoryLock lock = null; + private PersistentCacheInvalidation invalidation = null; @Rule public TemporaryFolder tmp = new TemporaryFolder(); @Before - public void setUp() { + public void setUp() throws IOException { + invalidation = mock(PersistentCacheInvalidation.class); + when(invalidation.test(any(Path.class))).thenReturn(false); lock = mock(DirectoryLock.class); when(lock.getFileLockName()).thenReturn("lock"); - cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), lock); + cache = new PersistentCache(tmp.getRoot().toPath(), invalidation, mock(Logger.class), lock); } @Test @@ -67,9 +72,9 @@ public class PersistentCacheTest { cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8)); Files.write(lockFile, "test".getBytes(StandardCharsets.UTF_8)); assertCacheHit(true); - // negative time to make sure it is expired - cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), lock); + when(invalidation.test(any(Path.class))).thenReturn(true); cache.clean(); + when(invalidation.test(any(Path.class))).thenReturn(false); assertCacheHit(false); // lock file should not get deleted assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test"); @@ -104,7 +109,6 @@ public class PersistentCacheTest { @Test public void testReconfigure() throws Exception { - cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), lock); assertCacheHit(false); cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8)); assertCacheHit(true); @@ -123,8 +127,7 @@ public class PersistentCacheTest { @Test public void testExpiration() throws Exception { - // negative time to make sure it is expired - cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), lock); + when(invalidation.test(any(Path.class))).thenReturn(true); cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8)); assertCacheHit(false); } diff --git a/sonar-home/src/test/java/org/sonar/home/cache/TTLCacheInvalidationTest.java b/sonar-home/src/test/java/org/sonar/home/cache/TTLCacheInvalidationTest.java new file mode 100644 index 00000000000..92c7e5134a6 --- /dev/null +++ b/sonar-home/src/test/java/org/sonar/home/cache/TTLCacheInvalidationTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.home.cache; + +import org.junit.Before; + +import java.io.IOException; +import java.nio.file.Path; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +public class TTLCacheInvalidationTest { + private Path testFile; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Before + public void setUp() throws IOException { + testFile = temp.newFile().toPath(); + } + + @Test + public void testExpired() throws IOException { + TTLCacheInvalidation invalidation = new TTLCacheInvalidation(-100); + assertThat(invalidation.test(testFile)).isEqualTo(true); + } + + @Test + public void testValid() throws IOException { + TTLCacheInvalidation invalidation = new TTLCacheInvalidation(100_000); + assertThat(invalidation.test(testFile)).isEqualTo(false); + } +} |