From 475074d8d4d7a3e6e8f811cae891207ac7b1c1c5 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 2 Oct 2015 15:42:07 +0200 Subject: [PATCH] Add rules in project sync phase --- .../cache/NonAssociatedCacheSynchronizer.java | 30 +++++++++------- .../batch/cache/ProjectCacheSynchronizer.java | 28 +++++++++------ .../org/sonar/batch/rule/RulesLoader.java | 7 ++-- .../NonAssociatedCacheSynchronizerTest.java | 12 ++++--- .../cache/ProjectCacheSynchronizerTest.java | 34 ++++++++++--------- 5 files changed, 64 insertions(+), 47 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizer.java b/sonar-batch/src/main/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizer.java index 592ad0ed305..b9ec4f0b503 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizer.java @@ -19,27 +19,29 @@ */ package org.sonar.batch.cache; -import org.sonarqube.ws.QualityProfiles.WsSearchResponse.QualityProfile; -import org.sonar.batch.rule.ActiveRulesLoader; -import org.sonar.batch.repository.QualityProfileLoader; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; +import org.sonar.batch.repository.QualityProfileLoader; +import org.sonar.batch.rule.ActiveRulesLoader; +import org.sonar.batch.rule.RulesLoader; +import org.sonarqube.ws.QualityProfiles.WsSearchResponse.QualityProfile; public class NonAssociatedCacheSynchronizer { private static final Logger LOG = LoggerFactory.getLogger(NonAssociatedCacheSynchronizer.class); - private ProjectCacheStatus cacheStatus; - private QualityProfileLoader qualityProfileLoader; - private ActiveRulesLoader activeRulesLoader; + private final ProjectCacheStatus cacheStatus; + private final QualityProfileLoader qualityProfileLoader; + private final ActiveRulesLoader activeRulesLoader; + private final RulesLoader rulesLoader; - public NonAssociatedCacheSynchronizer(QualityProfileLoader qualityProfileLoader, ActiveRulesLoader activeRulesLoader, ProjectCacheStatus cacheStatus) { + public NonAssociatedCacheSynchronizer(RulesLoader rulesLoader, QualityProfileLoader qualityProfileLoader, ActiveRulesLoader activeRulesLoader, ProjectCacheStatus cacheStatus) { + this.rulesLoader = rulesLoader; this.qualityProfileLoader = qualityProfileLoader; this.activeRulesLoader = activeRulesLoader; this.cacheStatus = cacheStatus; @@ -76,6 +78,10 @@ public class NonAssociatedCacheSynchronizer { private void loadData() { Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class)); + profiler.startInfo("Load rules"); + rulesLoader.load(null); + profiler.stopInfo(); + profiler.startInfo("Load default quality profiles"); Collection qProfiles = qualityProfileLoader.loadDefault(null, null); profiler.stopInfo(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheSynchronizer.java b/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheSynchronizer.java index c6366cfce6d..0b834e8b4d5 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheSynchronizer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheSynchronizer.java @@ -19,9 +19,14 @@ */ package org.sonar.batch.cache; -import org.sonar.batch.repository.ProjectRepositoriesLoader; -import org.sonarqube.ws.QualityProfiles.WsSearchResponse.QualityProfile; import com.google.common.base.Function; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,18 +34,13 @@ import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; import org.sonar.batch.protocol.input.BatchInput.ServerIssue; import org.sonar.batch.repository.ProjectRepositories; +import org.sonar.batch.repository.ProjectRepositoriesLoader; import org.sonar.batch.repository.QualityProfileLoader; import org.sonar.batch.repository.ServerIssuesLoader; import org.sonar.batch.repository.user.UserRepositoryLoader; import org.sonar.batch.rule.ActiveRulesLoader; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import org.sonar.batch.rule.RulesLoader; +import org.sonarqube.ws.QualityProfiles.WsSearchResponse.QualityProfile; public class ProjectCacheSynchronizer { private static final Logger LOG = LoggerFactory.getLogger(ProjectCacheSynchronizer.class); @@ -51,10 +51,12 @@ public class ProjectCacheSynchronizer { private final QualityProfileLoader qualityProfileLoader; private final ProjectRepositoriesLoader projectRepositoriesLoader; private final ActiveRulesLoader activeRulesLoader; + private final RulesLoader rulesLoader; - public ProjectCacheSynchronizer(QualityProfileLoader qualityProfileLoader, ProjectRepositoriesLoader projectSettingsLoader, + public ProjectCacheSynchronizer(RulesLoader rulesLoader, QualityProfileLoader qualityProfileLoader, ProjectRepositoriesLoader projectSettingsLoader, ActiveRulesLoader activeRulesLoader, ServerIssuesLoader issuesLoader, UserRepositoryLoader userRepository, ProjectCacheStatus cacheStatus) { + this.rulesLoader = rulesLoader; this.qualityProfileLoader = qualityProfileLoader; this.projectRepositoriesLoader = projectSettingsLoader; this.activeRulesLoader = activeRulesLoader; @@ -116,6 +118,10 @@ public class ProjectCacheSynchronizer { private void loadData(String projectKey) { Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class)); + profiler.startInfo("Load rules"); + rulesLoader.load(null); + profiler.stopInfo(); + profiler.startInfo("Load project settings"); ProjectRepositories projectRepo = projectRepositoriesLoader.load(projectKey, true, null); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesLoader.java index e470ce72b8e..88f440ae65c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesLoader.java @@ -19,12 +19,11 @@ */ package org.sonar.batch.rule; -import org.apache.commons.lang.mutable.MutableBoolean; - import java.util.List; - +import javax.annotation.Nullable; +import org.apache.commons.lang.mutable.MutableBoolean; import org.sonarqube.ws.Rules.ListResponse.Rule; public interface RulesLoader { - List load(MutableBoolean fromCache); + List load(@Nullable MutableBoolean fromCache); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizerTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizerTest.java index f16a22a0444..6bbde374460 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizerTest.java @@ -20,6 +20,7 @@ package org.sonar.batch.cache; import com.google.common.collect.ImmutableList; +import java.util.Date; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -27,17 +28,19 @@ import org.mockito.MockitoAnnotations; import org.sonar.batch.repository.QualityProfileLoader; import org.sonar.batch.rule.ActiveRulesLoader; import org.sonar.batch.rule.LoadedActiveRule; +import org.sonar.batch.rule.RulesLoader; import org.sonarqube.ws.QualityProfiles.WsSearchResponse.QualityProfile; -import java.util.Date; - import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; public class NonAssociatedCacheSynchronizerTest { private NonAssociatedCacheSynchronizer synchronizer; + @Mock + private RulesLoader rulesLoader; @Mock private QualityProfileLoader qualityProfileLoader; @Mock @@ -55,14 +58,14 @@ public class NonAssociatedCacheSynchronizerTest { when(qualityProfileLoader.loadDefault(null, null)).thenReturn(ImmutableList.of(pf)); when(activeRulesLoader.load("profile", null)).thenReturn(ImmutableList.of(ar)); - synchronizer = new NonAssociatedCacheSynchronizer(qualityProfileLoader, activeRulesLoader, cacheStatus); + synchronizer = new NonAssociatedCacheSynchronizer(rulesLoader, qualityProfileLoader, activeRulesLoader, cacheStatus); } @Test public void dont_sync_if_exists() { when(cacheStatus.getSyncStatus()).thenReturn(new Date()); synchronizer.execute(false); - verifyNoMoreInteractions(qualityProfileLoader, activeRulesLoader); + verifyZeroInteractions(rulesLoader, qualityProfileLoader, activeRulesLoader); } @Test @@ -81,6 +84,7 @@ public class NonAssociatedCacheSynchronizerTest { private void checkSync() { verify(cacheStatus).getSyncStatus(); verify(cacheStatus).save(); + verify(rulesLoader).load(null); verify(qualityProfileLoader).loadDefault(null, null); verify(activeRulesLoader).load("profile", null); diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectCacheSynchronizerTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectCacheSynchronizerTest.java index 3d2e5510deb..fabfe251eee 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectCacheSynchronizerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectCacheSynchronizerTest.java @@ -19,10 +19,11 @@ */ package org.sonar.batch.cache; -import org.sonar.batch.rule.LoadedActiveRule; - import com.google.common.base.Function; import com.google.common.collect.ImmutableList; +import java.io.IOException; +import java.util.Date; +import java.util.HashMap; import org.apache.commons.lang.mutable.MutableBoolean; import org.junit.Before; import org.junit.Rule; @@ -43,14 +44,12 @@ import org.sonar.batch.repository.ServerIssuesLoader; import org.sonar.batch.repository.user.UserRepositoryLoader; import org.sonar.batch.rule.ActiveRulesLoader; import org.sonar.batch.rule.DefaultActiveRulesLoader; +import org.sonar.batch.rule.LoadedActiveRule; +import org.sonar.batch.rule.RulesLoader; import org.sonarqube.ws.QualityProfiles.WsSearchResponse.QualityProfile; -import java.io.IOException; -import java.util.Date; -import java.util.HashMap; - -import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; @@ -72,6 +71,8 @@ public class ProjectCacheSynchronizerTest { private DefaultAnalysisMode analysisMode; @Mock private AnalysisProperties properties; + @Mock + private RulesLoader rulesLoader; private ServerIssuesLoader issuesLoader; private UserRepositoryLoader userRepositoryLoader; @@ -105,7 +106,7 @@ public class ProjectCacheSynchronizerTest { when(repo.exists()).thenReturn(projectExists); when(projectRepositoriesLoader.load(anyString(), anyBoolean(), any(MutableBoolean.class))).thenReturn(repo); - return new ProjectCacheSynchronizer(qualityProfileLoader, projectRepositoriesLoader, activeRulesLoader, issuesLoader, userRepositoryLoader, cacheStatus); + return new ProjectCacheSynchronizer(rulesLoader, qualityProfileLoader, projectRepositoriesLoader, activeRulesLoader, issuesLoader, userRepositoryLoader, cacheStatus); } @Test @@ -114,6 +115,7 @@ public class ProjectCacheSynchronizerTest { synchronizer.load(PROJECT_KEY, false); verify(issuesLoader).load(eq(PROJECT_KEY), any(Function.class)); + verify(rulesLoader).load(null); verify(qualityProfileLoader).load(PROJECT_KEY, null, null); verify(activeRulesLoader).load("profile", null); verify(projectRepositoriesLoader).load(eq(PROJECT_KEY), eq(true), any(MutableBoolean.class)); @@ -148,7 +150,7 @@ public class ProjectCacheSynchronizerTest { @Test public void testLastAnalysisToday() { ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date()); - + when(cacheStatus.getSyncStatus()).thenReturn(new Date()); synchronizer.load(PROJECT_KEY, false); @@ -159,7 +161,7 @@ public class ProjectCacheSynchronizerTest { @Test public void testLastAnalysisYesterday() { ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date()); - + Date d = new Date(new Date().getTime() - 60 * 60 * 24 * 1000); when(cacheStatus.getSyncStatus()).thenReturn(d); synchronizer.load(PROJECT_KEY, false); @@ -167,25 +169,25 @@ public class ProjectCacheSynchronizerTest { verify(cacheStatus).save(); verify(cacheStatus).getSyncStatus(); } - + @Test public void testDontFailOnError() { ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date()); - + Date d = new Date(new Date().getTime() - 60 * 60 * 24 * 1000); when(cacheStatus.getSyncStatus()).thenReturn(d); - + when(projectRepositoriesLoader.load(anyString(), anyBoolean(), any(MutableBoolean.class))).thenThrow(IllegalStateException.class); synchronizer.load(PROJECT_KEY, false); - + verify(cacheStatus).getSyncStatus(); verifyNoMoreInteractions(cacheStatus); } - + @Test public void testForce() { ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date()); - + when(cacheStatus.getSyncStatus()).thenReturn(new Date()); synchronizer.load(PROJECT_KEY, true); -- 2.39.5