aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-home/src/test
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-09-18 11:43:24 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-09-30 16:27:12 +0200
commit0847774db59344316629a7171c3943dbfaa3f52d (patch)
treec0ccebef9f3a29c97cb07b3fb1d1b2c667770e9e /sonar-home/src/test
parent1dae583a5cca52b34f6a11ed6cf515998ca06d4b (diff)
downloadsonarqube-0847774db59344316629a7171c3943dbfaa3f52d.tar.gz
sonarqube-0847774db59344316629a7171c3943dbfaa3f52d.zip
SONAR-6777 Project cache sync
Diffstat (limited to 'sonar-home/src/test')
-rw-r--r--sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java94
-rw-r--r--sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheBuilderTest.java37
-rw-r--r--sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java107
3 files changed, 152 insertions, 86 deletions
diff --git a/sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java b/sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java
new file mode 100644
index 00000000000..fc1d994bd6f
--- /dev/null
+++ b/sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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 static org.mockito.Mockito.mock;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.rules.ExpectedException;
+
+import java.nio.channels.OverlappingFileLockException;
+import java.nio.file.Paths;
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+
+public class DirectoryLockTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+ private DirectoryLock lock;
+
+ @Before
+ public void setUp() {
+ lock = new DirectoryLock(temp.getRoot().toPath(), mock(Logger.class));
+ }
+
+ @Test
+ public void lock() {
+ assertThat(temp.getRoot().list()).isEmpty();
+ lock.lock();
+ assertThat(temp.getRoot().toPath().resolve(".sonar_lock")).exists();
+ lock.unlock();
+ assertThat(temp.getRoot().list()).isEmpty();
+ }
+
+ @Test
+ public void tryLock() {
+ assertThat(temp.getRoot().list()).isEmpty();
+ lock.tryLock();
+ assertThat(temp.getRoot().toPath().resolve(".sonar_lock")).exists();
+ lock.unlock();
+ assertThat(temp.getRoot().list()).isEmpty();
+ }
+
+ @Test(expected = OverlappingFileLockException.class)
+ public void error_2locks() {
+ assertThat(temp.getRoot().list()).isEmpty();
+ lock.lock();
+ lock.lock();
+ }
+
+ @Test
+ public void unlockWithoutLock() {
+ lock.unlock();
+ }
+
+ @Test
+ public void errorCreatingLock() {
+ lock = new DirectoryLock(Paths.get("non", "existing", "path"), mock(Logger.class));
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to create lock");
+ lock.lock();
+ }
+
+ @Test
+ public void errorTryLock() {
+ lock = new DirectoryLock(Paths.get("non", "existing", "path"), mock(Logger.class));
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to create lock");
+ lock.tryLock();
+ }
+}
diff --git a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheBuilderTest.java b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheBuilderTest.java
index 78efe696695..7a57e98d68c 100644
--- a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheBuilderTest.java
+++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheBuilderTest.java
@@ -20,10 +20,11 @@
package org.sonar.home.cache;
import java.nio.file.Files;
+import java.nio.file.Paths;
+
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
@@ -35,17 +36,18 @@ public class PersistentCacheBuilderTest {
@Test
public void user_home_property_can_be_null() {
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(null).build();
- assertTrue(Files.isDirectory(cache.getBaseDirectory()));
- assertThat(cache.getBaseDirectory().getFileName().toString()).isEqualTo("ws_cache");
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(null).setAreaForGlobal("url", "0").build();
+ assertTrue(Files.isDirectory(cache.getDirectory()));
+ assertThat(cache.getDirectory()).endsWith(Paths.get("url-0", "global"));
}
@Test
public void set_user_home() {
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(temp.getRoot().toPath()).build();
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(temp.getRoot().toPath()).setAreaForGlobal("url", "0").build();
- assertThat(cache.getBaseDirectory().getParent().toString()).isEqualTo(temp.getRoot().toPath().toString());
- assertTrue(Files.isDirectory(cache.getBaseDirectory()));
+ assertThat(cache.getDirectory()).isDirectory();
+ assertThat(cache.getDirectory()).startsWith(temp.getRoot().toPath());
+ assertTrue(Files.isDirectory(cache.getDirectory()));
}
@Test
@@ -54,11 +56,22 @@ public class PersistentCacheBuilderTest {
System.setProperty("user.home", temp.getRoot().getAbsolutePath());
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).build();
- assertTrue(Files.isDirectory(cache.getBaseDirectory()));
- assertThat(cache.getBaseDirectory().getFileName().toString()).isEqualTo("ws_cache");
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForGlobal("url", "0").build();
+ assertTrue(Files.isDirectory(cache.getDirectory()));
+ assertThat(cache.getDirectory()).startsWith(temp.getRoot().toPath());
+ }
+
+ @Test
+ public void directories() {
+ System.setProperty("user.home", temp.getRoot().getAbsolutePath());
+
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForProject("url", "0", "proj").build();
+ assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url-0", "projects", "proj"));
+
+ cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForLocalProject("url", "0").build();
+ assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url-0", "local"));
- String expectedSonarHome = temp.getRoot().toPath().resolve(".sonar").toString();
- assertThat(cache.getBaseDirectory().getParent().toString()).isEqualTo(expectedSonarHome);
+ cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForGlobal("url", "0").build();
+ assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url-0", "global"));
}
}
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 9760b096676..77ee30908e1 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,7 +20,13 @@
package org.sonar.home.cache;
import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.verify;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
@@ -28,20 +34,21 @@ 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;
-import static org.mockito.Mockito.when;
public class PersistentCacheTest {
private final static String URI = "key1";
private final static String VALUE = "cache content";
private PersistentCache cache = null;
+ private DirectoryLock lock = null;
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
@Before
public void setUp() {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
+ lock = mock(DirectoryLock.class);
+ when(lock.getFileLockName()).thenReturn("lock");
+ cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), lock);
}
@Test
@@ -50,59 +57,43 @@ public class PersistentCacheTest {
}
@Test
- public void testNullLoader() throws Exception {
- assertThat(cache.get(URI, null)).isNull();
- assertCacheHit(false);
- }
-
- @Test
- public void testNullLoaderString() throws Exception {
- assertThat(cache.getString(URI, null)).isNull();
- assertCacheHit(false);
- }
-
- @Test
- public void testNullValue() throws Exception {
- // mocks have their methods returning null by default
- PersistentCacheLoader<byte[]> c = mock(PersistentCacheLoader.class);
- assertThat(cache.get(URI, c)).isNull();
- verify(c).get();
- assertCacheHit(false);
- }
-
- @Test
public void testClean() throws Exception {
+ Path lockFile = cache.getDirectory().resolve("lock");
// puts entry
- assertCacheHit(false);
+ 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), null);
+ cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), lock);
cache.clean();
assertCacheHit(false);
+ // lock file should not get deleted
+ assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
}
@Test
public void testClear() throws Exception {
- assertCacheHit(false);
+ Path lockFile = cache.getDirectory().resolve("lock");
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
+ Files.write(lockFile, "test".getBytes(StandardCharsets.UTF_8));
+ assertCacheHit(true);
cache.clear();
assertCacheHit(false);
+ // lock file should not get deleted
+ assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
}
@Test
public void testCacheHit() throws Exception {
- assertCacheHit(false);
- assertCacheHit(true);
- }
-
- @Test
- public void testPut() throws Exception {
- cache.put(URI, VALUE.getBytes());
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
assertCacheHit(true);
}
@Test
public void testReconfigure() throws Exception {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
+ 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);
File root = tmp.getRoot();
@@ -113,26 +104,16 @@ public class PersistentCacheTest {
assertThat(root).exists();
assertCacheHit(false);
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
assertCacheHit(true);
}
@Test
public void testExpiration() throws Exception {
- // negative time to make sure it is expired on the second call
- cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null);
- assertCacheHit(false);
- assertCacheHit(false);
- }
-
- @Test
- public void testDifferentServerVersions() throws Exception {
+ // negative time to make sure it is expired
+ cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), lock);
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
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 {
@@ -140,31 +121,9 @@ public class PersistentCacheTest {
}
private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception {
- CacheFillerString c = new CacheFillerString();
- assertThat(pCache.getString(URI, c)).isEqualTo(VALUE);
- assertThat(c.wasCalled).isEqualTo(!hit);
- }
-
- private class CacheFillerString implements PersistentCacheLoader<String> {
- public boolean wasCalled = false;
-
- @Override
- public String get() {
- wasCalled = true;
- return VALUE;
- }
- }
-
- /**
- * WSCache should be transparent regarding exceptions: if an exception is thrown by the value loader, it should pass through
- * the cache to the original caller using the cache.
- * @throws Exception
- */
- @Test(expected = ArithmeticException.class)
- public void testExceptions() throws Exception {
- PersistentCacheLoader<byte[]> c = mock(PersistentCacheLoader.class);
- when(c.get()).thenThrow(ArithmeticException.class);
- cache.get(URI, c);
+ String expected = hit ? VALUE : null;
+ assertThat(pCache.getString(URI)).isEqualTo(expected);
+ verify(lock, atLeast(1)).unlock();
}
}