aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-home/src/test/java/org
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-05-29 11:29:09 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-04 10:53:02 +0200
commit8f1df5c561f9bd1a94aaf95dcda0b474bab472d2 (patch)
tree053ca3cbe8f041eca854c0314bbeb0229034790e /sonar-home/src/test/java/org
parent35f206a6cefc849aa105769c2545d30257188de4 (diff)
downloadsonarqube-8f1df5c561f9bd1a94aaf95dcda0b474bab472d2.tar.gz
sonarqube-8f1df5c561f9bd1a94aaf95dcda0b474bab472d2.zip
SONAR-6577 Offline mode in preview mode
Diffstat (limited to 'sonar-home/src/test/java/org')
-rw-r--r--sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheBuilderTest.java62
-rw-r--r--sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java132
2 files changed, 194 insertions, 0 deletions
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
new file mode 100644
index 00000000000..027e2d68924
--- /dev/null
+++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheBuilderTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.assertj.core.api.Assertions.assertThat;
+
+import java.nio.file.Files;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+
+public class PersistentCacheBuilderTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void user_home_property_can_be_null() {
+ PersistentCache cache = new PersistentCacheBuilder().setSonarHome(null).build();
+ assertTrue(Files.isDirectory(cache.getBaseDirectory()));
+ assertThat(cache.getBaseDirectory().getFileName().toString()).isEqualTo("ws_cache");
+ }
+
+ @Test
+ public void set_user_home() {
+ PersistentCache cache = new PersistentCacheBuilder().setSonarHome(temp.getRoot().toPath()).build();
+
+ assertThat(cache.getBaseDirectory().getParent().toString()).isEqualTo(temp.getRoot().toPath().toString());
+ assertTrue(Files.isDirectory(cache.getBaseDirectory()));
+ }
+
+ @Test
+ public void read_system_env() {
+ System.setProperty("user.home", temp.getRoot().getAbsolutePath());
+
+ PersistentCache cache = new PersistentCacheBuilder().build();
+ assertTrue(Files.isDirectory(cache.getBaseDirectory()));
+ assertThat(cache.getBaseDirectory().getFileName().toString()).isEqualTo("ws_cache");
+
+ String expectedSonarHome = temp.getRoot().toPath().resolve(".sonar").toString();
+ assertThat(cache.getBaseDirectory().getParent().toString()).isEqualTo(expectedSonarHome);
+ }
+}
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
new file mode 100644
index 00000000000..8127e41885d
--- /dev/null
+++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.sonar.home.log.Slf4jLog;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+
+import java.util.concurrent.Callable;
+
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.mock;
+import static org.assertj.core.api.Assertions.assertThat;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PersistentCacheTest {
+ private final static String URI = "key1";
+ private final static String VALUE = "cache content";
+ private final static long CACHE_EXPIRE = 1000;
+ private PersistentCache cache = null;
+
+ @Rule
+ public TemporaryFolder tmp = new TemporaryFolder();
+
+ private Slf4jLog log = new Slf4jLog(FileCacheTest.class);
+
+ @Before
+ public void setUp() {
+ cache = new PersistentCache(tmp.getRoot().toPath(), CACHE_EXPIRE, log, false);
+ }
+
+ @Test
+ public void testCacheMiss() throws Exception {
+ assertCacheHit(false);
+ }
+
+ @Test
+ public void testNullLoader() throws Exception {
+ assertThat(cache.get(URI, null)).isNull();
+ assertCacheHit(false);
+ }
+
+ @Test
+ public void testNullValue() throws Exception {
+ // mocks have their methods returning null by default
+ Callable<byte[]> c = mock(Callable.class);
+ assertThat(cache.get(URI, c)).isNull();
+ verify(c).call();
+ assertCacheHit(false);
+ }
+
+ @Test
+ public void testClean() throws Exception {
+ assertCacheHit(false);
+ cache.clear();
+ assertCacheHit(false);
+ }
+
+ @Test
+ public void testCacheHit() throws Exception {
+ assertCacheHit(false);
+ assertCacheHit(true);
+ }
+
+ @Test
+ public void testForceUpdate() throws Exception {
+ cache = new PersistentCache(tmp.getRoot().toPath(), CACHE_EXPIRE, log, true);
+
+ assertCacheHit(false);
+ assertCacheHit(false);
+ assertCacheHit(false);
+
+ // with forceUpdate, it should still have cached the last call
+ cache = new PersistentCache(tmp.getRoot().toPath(), CACHE_EXPIRE, log, false);
+ assertCacheHit(true);
+ }
+
+ @Test
+ public void testExpiration() throws Exception {
+ assertCacheHit(false);
+ Thread.sleep(CACHE_EXPIRE);
+ assertCacheHit(false);
+ }
+
+ private void assertCacheHit(boolean hit) throws Exception {
+ CacheFillerString c = new CacheFillerString();
+ assertThat(cache.getString(URI, c)).isEqualTo(VALUE);
+ assertThat(c.wasCalled).isEqualTo(!hit);
+ }
+
+ private class CacheFillerString implements Callable<String> {
+ public boolean wasCalled = false;
+
+ @Override
+ public String call() throws Exception {
+ 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 {
+ Callable<byte[]> c = mock(Callable.class);
+ when(c.call()).thenThrow(ArithmeticException.class);
+ cache.get(URI, c);
+ }
+
+}