*/
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;
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<QualityProfile> qProfiles = qualityProfileLoader.loadDefault(null, null);
profiler.stopInfo();
*/
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;
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);
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;
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);
*/
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<Rule> load(MutableBoolean fromCache);
+ List<Rule> load(@Nullable MutableBoolean fromCache);
}
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;
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
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
private void checkSync() {
verify(cacheStatus).getSyncStatus();
verify(cacheStatus).save();
+ verify(rulesLoader).load(null);
verify(qualityProfileLoader).loadDefault(null, null);
verify(activeRulesLoader).load("profile", null);
*/
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;
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;
private DefaultAnalysisMode analysisMode;
@Mock
private AnalysisProperties properties;
+ @Mock
+ private RulesLoader rulesLoader;
private ServerIssuesLoader issuesLoader;
private UserRepositoryLoader userRepositoryLoader;
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
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));
@Test
public void testLastAnalysisToday() {
ProjectCacheSynchronizer synchronizer = createMockedLoaders(true, new Date());
-
+
when(cacheStatus.getSyncStatus()).thenReturn(new Date());
synchronizer.load(PROJECT_KEY, false);
@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);
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);