diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-07-21 15:42:01 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-07-24 13:14:57 +0200 |
commit | ac84ea03ec56004c5ef88c6862ef2d4faf505444 (patch) | |
tree | b7c4f4ae049db2abc0fd3c3f117fe0ac044d8766 /sonar-batch | |
parent | 01e9d5426116c86be015764ae5dfc277d9a09593 (diff) | |
download | sonarqube-ac84ea03ec56004c5ef88c6862ef2d4faf505444.tar.gz sonarqube-ac84ea03ec56004c5ef88c6862ef2d4faf505444.zip |
SONAR-6577 Offline mode in preview mode
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/phases/PhaseExecutor.java | 2 | ||||
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java | 10 | ||||
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/scan/ProjectAnalysisMode.java | 44 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java | 12 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/scan/ProjectAnalysisModeTest.java (renamed from sonar-batch/src/test/java/org/sonar/batch/scan/DefaultAnalysisModeTest.java) | 48 |
5 files changed, 84 insertions, 32 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PhaseExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PhaseExecutor.java index b49448be10c..7daea18961d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PhaseExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/PhaseExecutor.java @@ -56,7 +56,7 @@ public final class PhaseExecutor { SensorContext sensorContext, DefaultIndex index, EventBus eventBus, ReportPublisher reportPublisher, ProjectInitializer pi, FileSystemLogger fsLogger, IssuesReports jsonReport, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier, - IssueExclusionsLoader issueExclusionsLoader, DefaultAnalysisMode analysisMode, LocalIssueTracking localIssueTracking) { + IssueExclusionsLoader issueExclusionsLoader, ProjectAnalysisMode analysisMode, LocalIssueTracking localIssueTracking) { this.postJobsExecutor = postJobsExecutor; this.initializersExecutor = initializersExecutor; this.sensorsExecutor = sensorsExecutor; diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java index da008967662..dc39233376b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.repository; -import org.sonar.batch.scan.ProjectAnalysisMode; +import org.sonar.batch.bootstrap.GlobalMode; import org.sonar.batch.util.BatchUtils; import org.sonar.batch.bootstrap.WSLoader; @@ -37,11 +37,11 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad private static final String BATCH_PROJECT_URL = "/batch/project"; private final WSLoader wsLoader; - private final ProjectAnalysisMode analysisMode; + private final GlobalMode globalMode; - public DefaultProjectRepositoriesLoader(WSLoader wsLoader, ProjectAnalysisMode analysisMode) { + public DefaultProjectRepositoriesLoader(WSLoader wsLoader, GlobalMode globalMode) { this.wsLoader = wsLoader; - this.analysisMode = analysisMode; + this.globalMode = globalMode; } @Override @@ -53,7 +53,7 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server."); url += "&profile=" + BatchUtils.encodeForUrl(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP)); } - url += "&preview=" + analysisMode.isPreview(); + url += "&preview=" + globalMode.isPreview(); return ProjectRepositories.fromJson(wsLoader.loadString(url)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectAnalysisMode.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectAnalysisMode.java index 1f544864cbe..051fdcb2c0f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectAnalysisMode.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectAnalysisMode.java @@ -19,8 +19,9 @@ */ package org.sonar.batch.scan; -import org.sonar.batch.bootstrap.AnalysisProperties; +import org.sonar.batch.bootstrap.BootstrapProperties; +import org.sonar.batch.bootstrap.AnalysisProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; @@ -42,8 +43,8 @@ public class ProjectAnalysisMode implements AnalysisMode { private boolean quick; private boolean mediumTestMode; - public ProjectAnalysisMode(AnalysisProperties props) { - init(props.properties()); + public ProjectAnalysisMode(BootstrapProperties globalProps, AnalysisProperties props) { + init(globalProps.properties(), props.properties()); } @Override @@ -65,18 +66,31 @@ public class ProjectAnalysisMode implements AnalysisMode { return mediumTestMode; } - private void init(Map<String, String> props) { - if (props.containsKey(CoreProperties.DRY_RUN)) { + private void init(Map<String, String> globalProps, Map<String, String> analysisProps) { + // make sure analysis is consistent with global properties + boolean globalPreview = isPreview(globalProps); + boolean analysisPreview = isPreview(analysisProps); + + if (!globalPreview && analysisPreview) { + throw new IllegalStateException("Inconsistent properties: global properties doesn't enable preview mode while analysis properties enables it"); + } + + load(globalProps, analysisProps); + } + + private void load(Map<String, String> globalProps, Map<String, String> analysisProps) { + if (getPropertyWithFallback(analysisProps, globalProps, CoreProperties.DRY_RUN) != null) { LOG.warn(MessageFormat.format("Property {0} is deprecated. Please use {1} instead.", CoreProperties.DRY_RUN, CoreProperties.ANALYSIS_MODE)); - preview = "true".equals(props.get(CoreProperties.DRY_RUN)); + preview = "true".equals(getPropertyWithFallback(analysisProps, globalProps, CoreProperties.DRY_RUN)); incremental = false; } else { - String mode = props.get(CoreProperties.ANALYSIS_MODE); + String mode = getPropertyWithFallback(analysisProps, globalProps, CoreProperties.ANALYSIS_MODE); preview = CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); incremental = CoreProperties.ANALYSIS_MODE_INCREMENTAL.equals(mode); quick = CoreProperties.ANALYSIS_MODE_QUICK.equals(mode); } - mediumTestMode = "true".equals(props.get(BatchMediumTester.MEDIUM_TEST_ENABLED)); + mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, BatchMediumTester.MEDIUM_TEST_ENABLED)); + if (incremental) { LOG.info("Incremental mode"); } else if (preview) { @@ -89,4 +103,18 @@ public class ProjectAnalysisMode implements AnalysisMode { } } + private static String getPropertyWithFallback(Map<String, String> props1, Map<String, String> props2, String key) { + if (props1.containsKey(key)) { + return props1.get(key); + } + + return props2.get(key); + } + + private static boolean isPreview(Map<String, String> props) { + String mode = props.get(CoreProperties.ANALYSIS_MODE); + + return "true".equals(props.get(CoreProperties.DRY_RUN)) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode) || + CoreProperties.ANALYSIS_MODE_INCREMENTAL.equals(mode) || CoreProperties.ANALYSIS_MODE_QUICK.equals(mode); + } } 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 c9cadbe05ee..32e32e068cf 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,7 +19,7 @@ */ package org.sonar.batch.repository; -import org.sonar.batch.scan.ProjectAnalysisMode; +import org.sonar.batch.bootstrap.GlobalMode; import org.sonar.batch.bootstrap.WSLoader; import com.google.common.collect.Maps; @@ -39,15 +39,15 @@ public class DefaultProjectRepositoriesLoaderTest { private DefaultProjectRepositoriesLoader loader; private WSLoader wsLoader; - private ProjectAnalysisMode analysisMode; + private GlobalMode globalMode; private ProjectReactor reactor; private AnalysisProperties taskProperties; @Before public void prepare() { wsLoader = mock(WSLoader.class); - analysisMode = mock(ProjectAnalysisMode.class); - loader = new DefaultProjectRepositoriesLoader(wsLoader, analysisMode); + globalMode = mock(GlobalMode.class); + loader = new DefaultProjectRepositoriesLoader(wsLoader, globalMode); loader = spy(loader); when(wsLoader.loadString(anyString())).thenReturn("{}"); taskProperties = new AnalysisProperties(Maps.<String, String>newHashMap(), ""); @@ -56,11 +56,11 @@ public class DefaultProjectRepositoriesLoaderTest { @Test public void passPreviewParameter() { reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo")); - when(analysisMode.isPreview()).thenReturn(false); + when(globalMode.isPreview()).thenReturn(false); loader.load(reactor, taskProperties); verify(wsLoader).loadString("/batch/project?key=foo&preview=false"); - when(analysisMode.isPreview()).thenReturn(true); + when(globalMode.isPreview()).thenReturn(true); loader.load(reactor, taskProperties); verify(wsLoader).loadString("/batch/project?key=foo&preview=true"); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/DefaultAnalysisModeTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectAnalysisModeTest.java index a2b20592b68..75a705a95d3 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/DefaultAnalysisModeTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectAnalysisModeTest.java @@ -19,22 +19,24 @@ */ package org.sonar.batch.scan; +import javax.annotation.Nullable; + +import org.sonar.batch.bootstrap.BootstrapProperties; import org.sonar.batch.bootstrap.AnalysisProperties; import org.sonar.batch.scan.ProjectAnalysisMode; import org.junit.Test; import org.sonar.api.CoreProperties; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; -public class DefaultAnalysisModeTest { +public class ProjectAnalysisModeTest { @Test public void regular_analysis_by_default() { - ProjectAnalysisMode mode = new ProjectAnalysisMode(new AnalysisProperties(Collections.<String, String>emptyMap())); + ProjectAnalysisMode mode = createMode(null); assertThat(mode.isPreview()).isFalse(); assertThat(mode.isIncremental()).isFalse(); @@ -45,9 +47,14 @@ public class DefaultAnalysisModeTest { assertThat(mode.isIncremental()).isFalse(); } + @Test(expected = IllegalStateException.class) + public void fail_if_inconsistent() { + createMode(null, CoreProperties.ANALYSIS_MODE_PREVIEW); + } + @Test public void support_analysis_mode() { - ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ANALYSIS); + ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_ANALYSIS); assertThat(mode.isPreview()).isFalse(); assertThat(mode.isIncremental()).isFalse(); @@ -55,7 +62,7 @@ public class DefaultAnalysisModeTest { @Test public void support_preview_mode() { - ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW); + ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_PREVIEW); assertThat(mode.isPreview()).isTrue(); assertThat(mode.isIncremental()).isFalse(); @@ -63,7 +70,7 @@ public class DefaultAnalysisModeTest { @Test public void support_quick_mode() { - ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_QUICK); + ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_QUICK); assertThat(mode.isPreview()).isTrue(); assertThat(mode.isIncremental()).isFalse(); @@ -72,7 +79,7 @@ public class DefaultAnalysisModeTest { @Test public void support_incremental_mode() { - ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_INCREMENTAL); + ProjectAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_INCREMENTAL); assertThat(mode.isPreview()).isTrue(); assertThat(mode.isIncremental()).isTrue(); @@ -80,16 +87,33 @@ public class DefaultAnalysisModeTest { @Test public void support_deprecated_dryrun_property() { - ProjectAnalysisMode mode = createMode(CoreProperties.DRY_RUN, "true"); + Map<String, String> bootstrapMap = new HashMap<>(); + Map<String, String> analysisMap = new HashMap<>(); + + analysisMap.put(CoreProperties.DRY_RUN, "true"); + bootstrapMap.put(CoreProperties.DRY_RUN, "true"); + + ProjectAnalysisMode mode = new ProjectAnalysisMode(new BootstrapProperties(bootstrapMap), new AnalysisProperties(analysisMap)); assertThat(mode.isPreview()).isTrue(); assertThat(mode.isIncremental()).isFalse(); } - private ProjectAnalysisMode createMode(String key, String value) { - Map<String, String> map = new HashMap<>(); - map.put(key, value); + private static ProjectAnalysisMode createMode(@Nullable String mode) { + return createMode(mode, mode); + } - return new ProjectAnalysisMode(new AnalysisProperties(map)); + private static ProjectAnalysisMode createMode(@Nullable String bootstrapMode, @Nullable String analysisMode) { + Map<String, String> bootstrapMap = new HashMap<>(); + Map<String, String> analysisMap = new HashMap<>(); + + if (bootstrapMode != null) { + bootstrapMap.put(CoreProperties.ANALYSIS_MODE, bootstrapMode); + } + if (analysisMode != null) { + analysisMap.put(CoreProperties.ANALYSIS_MODE, analysisMode); + } + return new ProjectAnalysisMode(new BootstrapProperties(bootstrapMap), new AnalysisProperties(analysisMap)); } + } |