diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-07-31 14:44:08 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-08-03 13:38:15 +0200 |
commit | d5f10d28f6a996af9c34c4757e7dcdf446b43af9 (patch) | |
tree | 8f7157bddf5c70faba36bb43e9d9a0cc2106bf4c /sonar-batch/src/test/java | |
parent | ad2a3d41e24fd99b452d6d6d9481829c0a5928d8 (diff) | |
download | sonarqube-d5f10d28f6a996af9c34c4757e7dcdf446b43af9.tar.gz sonarqube-d5f10d28f6a996af9c34c4757e7dcdf446b43af9.zip |
SONAR-6770 Improve logging of cache usage
Diffstat (limited to 'sonar-batch/src/test/java')
7 files changed, 58 insertions, 46 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java index 78995ef7a74..e42962aca94 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java @@ -45,15 +45,16 @@ public class BatchPluginInstallerTest { public ExpectedException thrown = ExpectedException.none(); FileCache fileCache = mock(FileCache.class); + ServerClient serverClient = mock(ServerClient.class); BatchPluginPredicate pluginPredicate = mock(BatchPluginPredicate.class); @Test public void listRemotePlugins() { WSLoader wsLoader = mock(WSLoader.class); - when(wsLoader.load("/deploy/plugins/index.txt")).thenReturn("checkstyle\nsqale".getBytes()); - when(wsLoader.loadString("/deploy/plugins/index.txt")).thenReturn("checkstyle\nsqale"); - BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, fileCache, pluginPredicate); + when(wsLoader.load("/deploy/plugins/index.txt")).thenReturn(new WSLoaderResult<byte[]>("checkstyle\nsqale".getBytes(), true)); + when(wsLoader.loadString("/deploy/plugins/index.txt")).thenReturn(new WSLoaderResult<String>("checkstyle\nsqale", true)); + BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, serverClient, fileCache, pluginPredicate); List<RemotePlugin> remotePlugins = installer.listRemotePlugins(); assertThat(remotePlugins).extracting("key").containsOnly("checkstyle", "sqale"); @@ -65,7 +66,7 @@ public class BatchPluginInstallerTest { when(fileCache.get(eq("checkstyle-plugin.jar"), eq("fakemd5_1"), any(FileCache.Downloader.class))).thenReturn(pluginJar); WSLoader wsLoader = mock(WSLoader.class); - BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, fileCache, pluginPredicate); + BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, serverClient, fileCache, pluginPredicate); RemotePlugin remote = new RemotePlugin("checkstyle").setFile("checkstyle-plugin.jar", "fakemd5_1"); File file = installer.download(remote); @@ -80,6 +81,6 @@ public class BatchPluginInstallerTest { WSLoader wsLoader = mock(WSLoader.class); doThrow(new IllegalStateException()).when(wsLoader).load("/deploy/plugins/index.txt"); - new BatchPluginInstaller(wsLoader, fileCache, pluginPredicate).installRemotes(); + new BatchPluginInstaller(wsLoader, serverClient, fileCache, pluginPredicate).installRemotes(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTest.java index c4571c9e8e6..31c70e85d4b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WSLoaderTest.java @@ -74,10 +74,9 @@ public class WSLoaderTest { when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenThrow(new IllegalStateException()); WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.SERVER_FIRST); - loader.setCacheEnabled(true); - assertThat(loader.loadString(ID)).isEqualTo(cacheValue); - assertThat(loader.loadString(ID)).isEqualTo(cacheValue); + assertResult(loader.loadString(ID), cacheValue, true); + assertResult(loader.loadString(ID), cacheValue, true); // only try once the server verify(client, times(1)).load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt()); @@ -89,9 +88,8 @@ public class WSLoaderTest { when(cache.get(ID, null)).thenReturn(null); WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.CACHE_FIRST); - loader.setCacheEnabled(true); - loader.load(ID); + assertResult(loader.load(ID), serverValue.getBytes(), false); InOrder inOrder = Mockito.inOrder(client, cache); inOrder.verify(cache).get(ID, null); @@ -103,8 +101,8 @@ public class WSLoaderTest { when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenThrow(new IllegalStateException()); WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.SERVER_FIRST); - loader.setCacheEnabled(true); - assertThat(loader.loadString(ID)).isEqualTo(cacheValue); + + assertResult(loader.loadString(ID), cacheValue, true); InOrder inOrder = Mockito.inOrder(client, cache); inOrder.verify(client).load(eq(ID), anyString(), anyBoolean(), anyInt(), anyInt()); @@ -123,9 +121,9 @@ public class WSLoaderTest { public void test_throw_cache_exception_fallback() throws IOException { when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenThrow(new IllegalStateException()); when(cache.get(ID, null)).thenThrow(new NullPointerException()); + WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.SERVER_FIRST); - loader.setCacheEnabled(true); loader.load(ID); } @@ -133,9 +131,9 @@ public class WSLoaderTest { @Test(expected = IllegalStateException.class) public void test_throw_cache_exception() throws IOException { when(cache.get(ID, null)).thenThrow(new IllegalStateException()); + WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.CACHE_FIRST); - loader.setCacheEnabled(true); loader.load(ID); } @@ -144,15 +142,15 @@ public class WSLoaderTest { public void test_throw_http_exceptions() { HttpDownloader.HttpException httpException = mock(HttpDownloader.HttpException.class); IllegalStateException wrapperException = new IllegalStateException(httpException); - + when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenThrow(wrapperException); - + WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.SERVER_FIRST); - + try { loader.load(ID); - } catch(IllegalStateException e) { + } catch (IllegalStateException e) { // cache should not be used verifyNoMoreInteractions(cache); throw e; @@ -160,19 +158,6 @@ public class WSLoaderTest { } @Test - public void test_server_not_accessible() throws IOException { - when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenThrow(new IllegalStateException()); - WSLoader loader = new WSLoader(true, cache, client); - loader.setStrategy(LoadStrategy.SERVER_FIRST); - loader.load(ID); - loader.load(ID); - - // only try once from server - verify(client, times(1)).load(eq(ID), anyString(), anyBoolean(), anyInt(), anyInt()); - verify(cache, times(2)).get(ID, null); - } - - @Test public void test_change_strategy() throws IOException { WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.CACHE_FIRST); @@ -190,7 +175,7 @@ public class WSLoaderTest { public void test_server_strategy() throws IOException { WSLoader loader = new WSLoader(true, cache, client); loader.setStrategy(LoadStrategy.SERVER_FIRST); - loader.load(ID); + assertResult(loader.load(ID), serverValue.getBytes(), false); // should not fetch from cache verify(cache).put(ID, serverValue.getBytes()); @@ -209,6 +194,12 @@ public class WSLoaderTest { @Test public void test_string() { WSLoader loader = new WSLoader(cache, client); - assertThat(loader.loadString(ID)).isEqualTo(serverValue); + assertResult(loader.loadString(ID), serverValue, false); + } + + private <T> void assertResult(WSLoaderResult<T> result, T expected, boolean fromCache) { + assertThat(result).isNotNull(); + assertThat(result.get()).isEqualTo(expected); + assertThat(result.isFromCache()).isEqualTo(fromCache); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java index 991e61dc463..d1880ef7d3c 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java @@ -19,8 +19,9 @@ */ package org.sonar.batch.issue.tracking; -import org.sonar.batch.bootstrap.WSLoader; +import org.sonar.batch.bootstrap.WSLoaderResult; +import org.sonar.batch.bootstrap.WSLoader; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -48,7 +49,7 @@ public class DefaultServerLineHashesLoaderTest { @Test public void should_download_source_from_ws_if_preview_mode() { WSLoader wsLoader = mock(WSLoader.class); - when(wsLoader.loadString(anyString())).thenReturn("ae12\n\n43fb"); + when(wsLoader.loadString(anyString())).thenReturn(new WSLoaderResult("ae12\n\n43fb", true)); ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(wsLoader); @@ -60,7 +61,7 @@ public class DefaultServerLineHashesLoaderTest { @Test public void should_download_source_with_space_from_ws_if_preview_mode() { WSLoader server = mock(WSLoader.class); - when(server.loadString(anyString())).thenReturn("ae12\n\n43fb"); + when(server.loadString(anyString())).thenReturn(new WSLoaderResult("ae12\n\n43fb", true)); ServerLineHashesLoader lastSnapshots = new DefaultServerLineHashesLoader(server); diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java index 63a69e0c600..7a17f5f5e56 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java @@ -19,8 +19,12 @@ */ package org.sonar.batch.repository; +import org.sonar.batch.bootstrap.WSLoaderResult; + import com.google.common.collect.Maps; + import java.util.Date; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -34,7 +38,6 @@ import org.sonar.batch.bootstrap.WSLoader; import org.sonar.batch.protocol.input.ProjectRepositories; import org.sonar.batch.protocol.input.QProfile; import org.sonar.batch.rule.ModuleQProfiles; - import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -58,7 +61,7 @@ public class DefaultProjectRepositoriesLoaderTest { globalMode = mock(GlobalMode.class); loader = new DefaultProjectRepositoriesLoader(wsLoader, globalMode); loader = spy(loader); - when(wsLoader.loadString(anyString())).thenReturn("{}"); + when(wsLoader.loadString(anyString())).thenReturn(new WSLoaderResult("{}", true)); taskProperties = new AnalysisProperties(Maps.<String, String>newHashMap(), ""); } @@ -98,15 +101,15 @@ public class DefaultProjectRepositoriesLoaderTest { thrown.expectMessage("No quality profiles has been found this project, you probably don't have any language plugin suitable for this analysis."); reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo")); - when(wsLoader.loadString(anyString())).thenReturn(new ProjectRepositories().toJson()); + when(wsLoader.loadString(anyString())).thenReturn(new WSLoaderResult(new ProjectRepositories().toJson(), true)); loader.load(reactor, taskProperties); } - private void addQualityProfile(){ + private void addQualityProfile() { ProjectRepositories projectRepositories = new ProjectRepositories(); projectRepositories.addQProfile(new QProfile("key", "name", "language", new Date())); - when(wsLoader.loadString(anyString())).thenReturn(projectRepositories.toJson()); + when(wsLoader.loadString(anyString())).thenReturn(new WSLoaderResult(projectRepositories.toJson(), true)); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java index d1686d63372..3b370f6d08a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java @@ -19,8 +19,9 @@ */ package org.sonar.batch.repository; -import com.google.common.io.ByteSource; +import org.sonar.batch.bootstrap.WSLoaderResult; +import com.google.common.io.ByteSource; import org.sonar.batch.bootstrap.WSLoader; import com.google.common.base.Function; import org.junit.Before; @@ -50,7 +51,7 @@ public class DefaultServerIssuesLoaderTest { @Test public void loadFromWs() throws Exception { ByteSource bs = mock(ByteSource.class); - when(wsLoader.loadSource("/batch/issues?key=foo")).thenReturn(bs); + when(wsLoader.loadSource("/batch/issues?key=foo")).thenReturn(new WSLoaderResult(bs, true)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java index 6910f2dc462..0057f208038 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java @@ -19,8 +19,9 @@ */ package org.sonar.batch.repository.user; -import com.google.common.io.ByteSource; +import org.sonar.batch.bootstrap.WSLoaderResult; +import com.google.common.io.ByteSource; import org.sonar.batch.bootstrap.WSLoader; import org.junit.Test; import org.sonar.batch.protocol.input.BatchInput; @@ -48,7 +49,7 @@ public class UserRepositoryTest { builder.setLogin("sbrandhof").setName("Simon").build().writeDelimitedTo(out); ByteSource source = mock(ByteSource.class); - when(wsLoader.loadSource("/batch/users?logins=fmallet,sbrandhof")).thenReturn(source); + when(wsLoader.loadSource("/batch/users?logins=fmallet,sbrandhof")).thenReturn(new WSLoaderResult(source, true)); when(source.openStream()).thenReturn(new ByteArrayInputStream(out.toByteArray())); assertThat(userRepo.loadFromWs(Arrays.asList("fmallet", "sbrandhof"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon")); diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java index 4f2f85e8567..0f2cc51d76d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java @@ -21,6 +21,9 @@ package org.sonar.batch.rule; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; + +import org.sonar.batch.bootstrap.WSLoaderResult; + import org.sonarqube.ws.Rules.ListResponse.Rule; import com.google.common.io.ByteSource; import com.google.common.io.Resources; @@ -38,9 +41,20 @@ public class DefaultRulesLoaderTest { public void testParseServerResponse() throws IOException { WSLoader wsLoader = mock(WSLoader.class); ByteSource source = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf")); - when(wsLoader.loadSource(anyString())).thenReturn(source); + when(wsLoader.loadSource(anyString())).thenReturn(new WSLoaderResult(source, true)); DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader); List<Rule> ruleList = loader.load(); assertThat(ruleList).hasSize(318); } + + @Test + public void testLoadedFromCache() { + WSLoader wsLoader = mock(WSLoader.class); + ByteSource source = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf")); + when(wsLoader.loadSource(anyString())).thenReturn(new WSLoaderResult(source, true)); + DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader); + loader.load(); + + assertThat(loader.loadedFromCache()).isTrue(); + } } |