diff options
10 files changed, 52 insertions, 16 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 5242184a30f..2df946dc0ae 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 @@ -79,7 +79,7 @@ public class NonAssociatedCacheSynchronizer { Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class)); profiler.startInfo("Load default quality profiles"); - Collection<QualityProfile> qProfiles = qualityProfileLoader.loadDefault(null); + Collection<QualityProfile> qProfiles = qualityProfileLoader.loadDefault(null, null); profiler.stopInfo(); profiler.startInfo("Load default active rules"); 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 4ba15ab16a6..fbba13fbbc8 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 @@ -105,7 +105,7 @@ public class ProjectCacheSynchronizer { if (projectRepo.exists()) { qProfiles = qualityProfileLoader.load(projectKey, null, null); } else { - qProfiles = qualityProfileLoader.loadDefault(null); + qProfiles = qualityProfileLoader.loadDefault(null, null); } profiler.stopInfo(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java index 3eace767103..c8cc3f391cf 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java @@ -45,8 +45,11 @@ public class DefaultQualityProfileLoader implements QualityProfileLoader { } @Override - public List<QualityProfile> loadDefault(@Nullable MutableBoolean fromCache) { + public List<QualityProfile> loadDefault(@Nullable String profileName, @Nullable MutableBoolean fromCache) { String url = WS_URL + "?defaults=true"; + if(profileName != null) { + url += "&profileName=" + BatchUtils.encodeForUrl(profileName); + } return loadResource(url, fromCache); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileLoader.java index 1ab61a9e438..266d24a3d7a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileLoader.java @@ -29,5 +29,5 @@ import java.util.List; public interface QualityProfileLoader { List<QualityProfile> load(String projectKey, @Nullable String profileName, @Nullable MutableBoolean fromCache); - List<QualityProfile> loadDefault(@Nullable MutableBoolean fromCache); + List<QualityProfile> loadDefault(@Nullable String profileName, @Nullable MutableBoolean fromCache); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileProvider.java b/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileProvider.java index d35ab28fb6b..b53635e7bd5 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileProvider.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.repository; +import javax.annotation.CheckForNull; + import org.sonar.api.utils.log.Profiler; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; @@ -45,7 +47,7 @@ public class QualityProfileProvider extends ProviderAdapter { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); if (mode.isNotAssociated() || !projectRepositories.exists()) { - profileList = loader.loadDefault(fromCache); + profileList = loader.loadDefault(getSonarProfile(props, mode), fromCache); } else { profileList = loader.load(projectKey.get(), getSonarProfile(props, mode), fromCache); } @@ -56,6 +58,7 @@ public class QualityProfileProvider extends ProviderAdapter { return profiles; } + @CheckForNull private static String getSonarProfile(AnalysisProperties props, DefaultAnalysisMode mode) { String profile = null; if (!mode.isIssues()) { 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 7934c0fed45..f16a22a0444 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 @@ -52,7 +52,7 @@ public class NonAssociatedCacheSynchronizerTest { QualityProfile pf = QualityProfile.newBuilder().setKey("profile").setName("profile").setLanguage("lang").build(); LoadedActiveRule ar = new LoadedActiveRule(); - when(qualityProfileLoader.loadDefault(null)).thenReturn(ImmutableList.of(pf)); + when(qualityProfileLoader.loadDefault(null, null)).thenReturn(ImmutableList.of(pf)); when(activeRulesLoader.load("profile", null)).thenReturn(ImmutableList.of(ar)); synchronizer = new NonAssociatedCacheSynchronizer(qualityProfileLoader, activeRulesLoader, cacheStatus); @@ -81,7 +81,7 @@ public class NonAssociatedCacheSynchronizerTest { private void checkSync() { verify(cacheStatus).getSyncStatus(); verify(cacheStatus).save(); - verify(qualityProfileLoader).loadDefault(null); + verify(qualityProfileLoader).loadDefault(null, null); verify(activeRulesLoader).load("profile", null); verifyNoMoreInteractions(qualityProfileLoader, activeRulesLoader); 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 548e6124313..6098850be55 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 @@ -102,7 +102,7 @@ public class ProjectCacheSynchronizerTest { ProjectRepositories repo = mock(ProjectRepositories.class); when(qualityProfileLoader.load(PROJECT_KEY, null, null)).thenReturn(ImmutableList.of(pf)); - when(qualityProfileLoader.loadDefault(null)).thenReturn(ImmutableList.of(pf)); + when(qualityProfileLoader.loadDefault(null, null)).thenReturn(ImmutableList.of(pf)); when(activeRulesLoader.load("profile", null)).thenReturn(ImmutableList.of(ar)); when(repo.lastAnalysisDate()).thenReturn(lastAnalysisDate); when(repo.exists()).thenReturn(projectExists); @@ -142,7 +142,7 @@ public class ProjectCacheSynchronizerTest { synchronizer.load(PROJECT_KEY, false); verify(projectRepositoriesLoader).load(eq(PROJECT_KEY), eq(true), any(MutableBoolean.class)); - verify(qualityProfileLoader).loadDefault(null); + verify(qualityProfileLoader).loadDefault(null, null); verify(activeRulesLoader).load("profile", null); verifyNoMoreInteractions(issuesLoader, userRepositoryLoader, qualityProfileLoader, activeRulesLoader, projectRepositoriesLoader); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java index ab8ee270983..c330d304a8e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -459,7 +459,7 @@ public class BatchMediumTester { } @Override - public List<QualityProfile> loadDefault(MutableBoolean fromCache) { + public List<QualityProfile> loadDefault(String profileName, MutableBoolean fromCache) { return qualityProfiles; } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java index 6a52a3f1c50..f0215aa18e2 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java @@ -89,7 +89,7 @@ public class DefaultQualityProfileLoaderTest { InputStream is = getTestResource("quality_profile_search_default"); when(ws.loadStream(anyString())).thenReturn(new WSLoaderResult<InputStream>(is, false)); - List<QualityProfile> loaded = qpLoader.loadDefault(null); + List<QualityProfile> loaded = qpLoader.loadDefault(null, null); verify(ws).loadStream("/api/qualityprofiles/search.protobuf?defaults=true"); verifyNoMoreInteractions(ws); assertThat(loaded).hasSize(1); diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/QualityProfileProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/QualityProfileProviderTest.java index 361d147ac0d..799022ecbe3 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/QualityProfileProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/QualityProfileProviderTest.java @@ -20,6 +20,9 @@ package org.sonar.batch.repository; import static org.mockito.Mockito.when; + +import org.mockito.Matchers; + import org.apache.commons.lang.mutable.MutableBoolean; import org.sonar.api.batch.bootstrap.ProjectKey; import org.sonar.batch.analysis.DefaultAnalysisMode; @@ -28,8 +31,8 @@ import org.sonarqube.ws.QualityProfiles.WsSearchResponse.QualityProfile; import java.util.ArrayList; import java.util.List; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.isNull; - import static org.mockito.Matchers.eq; import static org.mockito.Matchers.any; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -84,11 +87,11 @@ public class QualityProfileProviderTest { @Test public void testNonAssociated() { when(mode.isNotAssociated()).thenReturn(true); - when(loader.loadDefault(any(MutableBoolean.class))).thenReturn(response); + when(loader.loadDefault(anyString(), any(MutableBoolean.class))).thenReturn(response); ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode); assertResponse(qps); - verify(loader).loadDefault(any(MutableBoolean.class)); + verify(loader).loadDefault(anyString(), any(MutableBoolean.class)); verifyNoMoreInteractions(loader); } @@ -96,11 +99,11 @@ public class QualityProfileProviderTest { public void testProjectDoesntExist() { when(mode.isNotAssociated()).thenReturn(false); when(projectRepo.exists()).thenReturn(false); - when(loader.loadDefault(any(MutableBoolean.class))).thenReturn(response); + when(loader.loadDefault(anyString(), any(MutableBoolean.class))).thenReturn(response); ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode); assertResponse(qps); - verify(loader).loadDefault(any(MutableBoolean.class)); + verify(loader).loadDefault(anyString(), any(MutableBoolean.class)); verifyNoMoreInteractions(loader); } @@ -117,6 +120,33 @@ public class QualityProfileProviderTest { verifyNoMoreInteractions(loader); } + @Test + public void testIgnoreSonarProfileIssuesMode() { + when(mode.isNotAssociated()).thenReturn(false); + when(mode.isIssues()).thenReturn(true); + when(loader.load(eq("project"), (String) eq(null), any(MutableBoolean.class))).thenReturn(response); + when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("custom"); + + ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode); + assertResponse(qps); + + verify(loader).load(eq("project"), (String) eq(null), any(MutableBoolean.class)); + verifyNoMoreInteractions(loader); + } + + @Test + public void testProfilePropDefault() { + when(mode.isNotAssociated()).thenReturn(true); + when(loader.loadDefault(eq("custom"), any(MutableBoolean.class))).thenReturn(response); + when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("custom"); + + ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props, mode); + assertResponse(qps); + + verify(loader).loadDefault(eq("custom"), any(MutableBoolean.class)); + verifyNoMoreInteractions(loader); + } + private void assertResponse(ModuleQProfiles qps) { assertThat(qps.findAll()).hasSize(1); assertThat(qps.findAll()).extracting("key").containsExactly("profile"); |