diff options
81 files changed, 805 insertions, 458 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/MockAnalysisModeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/MockAnalysisModeTest.java index 5fbe93e7cbf..8a8179f9062 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/MockAnalysisModeTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/MockAnalysisModeTest.java @@ -31,13 +31,10 @@ public class MockAnalysisModeTest { assertThat(mode.isIssues()).isFalse(); assertThat(mode.isPreview()).isFalse(); assertThat(mode.isPublish()).isTrue(); - assertThat(mode.isIncremental()).isFalse(); mode.setPreviewOrIssue(true); - mode.setIncremental(true); assertThat(mode.isIssues()).isTrue(); assertThat(mode.isPreview()).isTrue(); assertThat(mode.isPublish()).isFalse(); - assertThat(mode.isIncremental()).isTrue(); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java index 6e7bf1a508c..534dad293ad 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java @@ -20,85 +20,107 @@ package org.sonar.scanner.analysis; import java.util.Map; -import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; -import org.sonar.api.CoreProperties; +import org.sonar.api.batch.AnalysisMode; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.scanner.bootstrap.AbstractAnalysisMode; -import org.sonar.scanner.bootstrap.GlobalProperties; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; +import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.scan.branch.BranchConfiguration; @Immutable -public class DefaultAnalysisMode extends AbstractAnalysisMode { - +public class DefaultAnalysisMode implements AnalysisMode { private static final Logger LOG = Loggers.get(DefaultAnalysisMode.class); private static final String KEY_SCAN_ALL = "sonar.scanAllFiles"; + private static final String KEY_INCREMENTAL = "sonar.incremental"; + + private final Map<String, String> analysisProps; + private final GlobalAnalysisMode analysisMode; + private final BranchConfiguration branchConfig; + private final ProjectRepositories projectRepos; + private final ValidateIncremental validateIncremental; private boolean scanAllFiles; + private boolean incremental; - public DefaultAnalysisMode(GlobalProperties globalProps, AnalysisProperties props, BranchConfiguration branchConfig) { - init(globalProps.properties(), props.properties(), branchConfig); + public DefaultAnalysisMode(AnalysisProperties props, BranchConfiguration branchConfig, GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos) { + this(props, branchConfig, analysisMode, projectRepos, null); } - public boolean scanAllFiles() { - return scanAllFiles; + public DefaultAnalysisMode(AnalysisProperties props, BranchConfiguration branchConfig, + GlobalAnalysisMode analysisMode, ProjectRepositories projectRepos, @Nullable ValidateIncremental validateIncremental) { + this.branchConfig = branchConfig; + this.analysisMode = analysisMode; + this.projectRepos = projectRepos; + this.validateIncremental = validateIncremental; + this.analysisProps = props.properties(); + load(); } - private void init(Map<String, String> globalProps, Map<String, String> analysisProps, BranchConfiguration branchConfig) { - // make sure analysis is consistent with global properties - boolean globalPreview = isIssues(globalProps); - boolean analysisPreview = isIssues(analysisProps); - - if (!globalPreview && analysisPreview) { - throw new IllegalStateException("Inconsistent properties: global properties doesn't enable issues mode while analysis properties enables it"); - } - - load(globalProps, analysisProps, branchConfig.isShortLivingBranch()); + @Override + public boolean isIncremental() { + return incremental; } - private void load(Map<String, String> globalProps, Map<String, String> analysisProps, boolean isShortLivingBranch) { - String mode = getPropertyWithFallback(analysisProps, globalProps, CoreProperties.ANALYSIS_MODE); - validate(mode); - issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); - mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, MEDIUM_TEST_ENABLED)); - String scanAllStr = getPropertyWithFallback(analysisProps, globalProps, KEY_SCAN_ALL); - scanAllFiles = !isShortLivingBranch && (!issues || "true".equals(scanAllStr)); + public boolean scanAllFiles() { + return scanAllFiles; } - public void printMode() { - if (preview) { - LOG.info("Preview mode"); - } else if (issues) { - LOG.info("Issues mode"); - } else { - LOG.info("Publish mode"); - } - if (mediumTestMode) { - LOG.info("Medium test mode"); - } + public void printFlags() { if (!scanAllFiles) { LOG.info("Scanning only changed files"); } } - @Override - public boolean isIncremental() { - return false; + private void load() { + String scanAllStr = analysisProps.get(KEY_SCAN_ALL); + scanAllFiles = !branchConfig.isShortLivingBranch() && (!analysisMode.isIssues() || "true".equals(scanAllStr)); + incremental = incremental(); } - @CheckForNull - private static String getPropertyWithFallback(Map<String, String> props1, Map<String, String> props2, String key) { - if (props1.containsKey(key)) { - return props1.get(key); + private boolean incremental() { + String inc = analysisProps.get(KEY_INCREMENTAL); + if ("true".equals(inc)) { + if (validateIncremental == null || !validateIncremental.validate()) { + throw MessageException.of("Incremental mode is not available. Please contact your administrator."); + } + + if (!analysisMode.isPublish()) { + throw new IllegalStateException("Incremental analysis is only available in publish mode"); + } + + if (branchConfig.branchName() != null) { + LOG.warn("Incremental analysis mode has been activated but it's not compatible with branches so a full analysis will be done."); + return false; + } + + if (!projectRepos.exists() || projectRepos.lastAnalysisDate() == null) { + LOG.warn("Incremental analysis mode has been activated but the project was never analyzed before so a full analysis is about to be done."); + return false; + } + + LOG.debug("Reference analysis is {}", DateUtils.formatDateTime(projectRepos.lastAnalysisDate())); + return true; } - return props2.get(key); + return false; } - private static boolean isIssues(Map<String, String> props) { - String mode = props.get(CoreProperties.ANALYSIS_MODE); + @Override + public boolean isPreview() { + return analysisMode.isPreview(); + } - return CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode); + @Override + public boolean isIssues() { + return analysisMode.isIssues(); + } + + @Override + public boolean isPublish() { + return analysisMode.isPublish(); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/ValidateIncremental.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/ValidateIncremental.java new file mode 100644 index 00000000000..0db769b34b3 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/ValidateIncremental.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.analysis; + +public interface ValidateIncremental { + boolean validate(); +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchComponents.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchComponents.java index 94536420f0c..48f4d5a14d5 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchComponents.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchComponents.java @@ -22,7 +22,6 @@ package org.sonar.scanner.bootstrap; import com.google.common.collect.Lists; import java.util.Collection; import java.util.List; -import org.sonar.api.batch.AnalysisMode; import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.config.CorePropertyDefinitions; import org.sonar.core.issue.tracking.Tracker; @@ -49,7 +48,7 @@ public class BatchComponents { // only static stuff } - public static Collection<Object> all(AnalysisMode analysisMode) { + public static Collection<Object> all(GlobalAnalysisMode analysisMode) { List<Object> components = Lists.newArrayList( DefaultResourceTypes.get(), diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java index 97366e2179d..a25a44f8597 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java @@ -24,7 +24,6 @@ import javax.annotation.Nullable; import org.sonar.api.ExtensionProvider; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; -import org.sonar.api.batch.AnalysisMode; import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; @@ -33,9 +32,9 @@ public class ExtensionInstaller { private final SonarRuntime sonarRuntime; private final PluginRepository pluginRepository; - private final AnalysisMode analysisMode; + private final GlobalAnalysisMode analysisMode; - public ExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository, AnalysisMode analysisMode) { + public ExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository, GlobalAnalysisMode analysisMode) { this.sonarRuntime = sonarRuntime; this.pluginRepository = pluginRepository; this.analysisMode = analysisMode; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/AbstractAnalysisMode.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java index d9ba1203e42..a6dddc7f92e 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/AbstractAnalysisMode.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java @@ -20,33 +20,30 @@ package org.sonar.scanner.bootstrap; import java.util.Arrays; +import javax.annotation.concurrent.Immutable; import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.AnalysisMode; -public abstract class AbstractAnalysisMode implements AnalysisMode { - - private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES}; +@Immutable +public class GlobalAnalysisMode { public static final String MEDIUM_TEST_ENABLED = "sonar.mediumTest.enabled"; + private static final Logger LOG = LoggerFactory.getLogger(GlobalAnalysisMode.class); + private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES}; protected boolean preview; protected boolean issues; protected boolean mediumTestMode; - protected AbstractAnalysisMode() { - } - - @Override public boolean isPreview() { return preview; } - @Override public boolean isIssues() { return issues; } - @Override public boolean isPublish() { return !preview && !issues; } @@ -70,4 +67,20 @@ public abstract class AbstractAnalysisMode implements AnalysisMode { } + public GlobalAnalysisMode(GlobalProperties props) { + String mode = props.property(CoreProperties.ANALYSIS_MODE); + validate(mode); + issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); + mediumTestMode = "true".equals(props.property(MEDIUM_TEST_ENABLED)); + if (preview) { + LOG.debug("Preview mode"); + } else if (issues) { + LOG.debug("Issues mode"); + } else { + LOG.debug("Publish mode"); + } + if (mediumTestMode) { + LOG.info("Medium test mode"); + } + } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java index 336d0ce73ff..b41cf7d4f0b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java @@ -26,7 +26,6 @@ import javax.annotation.concurrent.Immutable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.config.Encryption; import org.sonar.api.config.PropertyDefinitions; import org.sonar.scanner.config.DefaultConfiguration; @@ -48,7 +47,7 @@ public class GlobalConfiguration extends DefaultConfiguration { private final Map<String, String> serverSideSettings; - public GlobalConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, AnalysisMode mode, + public GlobalConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, GlobalAnalysisMode mode, Map<String, String> settings, Map<String, String> serverSideSettings) { super(propertyDefinitions, encryption, mode, settings); this.serverSideSettings = serverSideSettings; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java index 26467a6e5df..46a591b75b1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java @@ -29,7 +29,7 @@ public class GlobalConfigurationProvider extends ProviderAdapter { private GlobalConfiguration globalSettings; - public GlobalConfiguration provide(SettingsLoader loader, GlobalProperties globalProps, PropertyDefinitions propertyDefinitions, GlobalMode mode) { + public GlobalConfiguration provide(SettingsLoader loader, GlobalProperties globalProps, PropertyDefinitions propertyDefinitions, GlobalAnalysisMode mode) { if (globalSettings == null) { Map<String, String> serverSideSettings = loader.load(null); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java index 259058a7163..94dbe459a7c 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java @@ -65,7 +65,7 @@ public class GlobalContainer extends ComponentContainer { @Override protected void doBeforeStart() { GlobalProperties bootstrapProps = new GlobalProperties(bootstrapProperties); - GlobalMode globalMode = new GlobalMode(bootstrapProps); + GlobalAnalysisMode globalMode = new GlobalAnalysisMode(bootstrapProps); add(bootstrapProps); add(globalMode); addBootstrapComponents(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalMode.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalMode.java deleted file mode 100644 index df217760051..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalMode.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.bootstrap; - -import javax.annotation.concurrent.Immutable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; - -@Immutable -public class GlobalMode extends AbstractAnalysisMode { - private static final Logger LOG = LoggerFactory.getLogger(GlobalMode.class); - - public GlobalMode(GlobalProperties props) { - String mode = props.property(CoreProperties.ANALYSIS_MODE); - validate(mode); - issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); - mediumTestMode = "true".equals(props.property(MEDIUM_TEST_ENABLED)); - if (preview) { - LOG.debug("Preview global mode"); - } else if (issues) { - LOG.debug("Issues global mode"); - } else { - LOG.debug("Publish global mode"); - } - if (mediumTestMode) { - LOG.info("Medium test mode"); - } - } - - @Override - public boolean isIncremental() { - throw new UnsupportedOperationException(); - } - -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/MutableGlobalSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/MutableGlobalSettings.java index e31ce436394..b3355a89228 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/MutableGlobalSettings.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/MutableGlobalSettings.java @@ -22,7 +22,6 @@ package org.sonar.scanner.bootstrap; import java.util.HashMap; import java.util.Map; import java.util.Optional; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; @@ -32,7 +31,7 @@ import org.sonar.api.utils.MessageException; @Deprecated public class MutableGlobalSettings extends Settings { - private final AnalysisMode mode; + private final GlobalAnalysisMode mode; private final Map<String, String> mutableProperties = new HashMap<>(); public MutableGlobalSettings(GlobalConfiguration globalSettings) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginPredicate.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginPredicate.java index dc39f5c0ced..a219abe87aa 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginPredicate.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginPredicate.java @@ -47,9 +47,9 @@ public class ScannerPluginPredicate implements Predicate<String> { private final Set<String> whites = new HashSet<>(); private final Set<String> blacks = new HashSet<>(); - private final GlobalMode mode; + private final GlobalAnalysisMode mode; - public ScannerPluginPredicate(Configuration settings, GlobalMode mode) { + public ScannerPluginPredicate(Configuration settings, GlobalAnalysisMode mode) { this.mode = mode; if (mode.isPreview() || mode.isIssues()) { // These default values are not supported by Settings because the class CorePlugin diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java index 19e0169e182..6ec0eaac6dc 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java @@ -50,9 +50,9 @@ public class ScannerWsClient { private final WsClient target; private final boolean hasCredentials; - private final GlobalMode globalMode; + private final GlobalAnalysisMode globalMode; - public ScannerWsClient(WsClient target, boolean hasCredentials, GlobalMode globalMode) { + public ScannerWsClient(WsClient target, boolean hasCredentials, GlobalAnalysisMode globalMode) { this.target = target; this.hasCredentials = hasCredentials; this.globalMode = globalMode; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java index 3ffd9275d39..51d0b4c50e5 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java @@ -39,7 +39,7 @@ public class ScannerWsClientProvider extends ProviderAdapter { private ScannerWsClient wsClient; - public synchronized ScannerWsClient provide(final GlobalProperties settings, final EnvironmentInformation env, GlobalMode globalMode) { + public synchronized ScannerWsClient provide(final GlobalProperties settings, final EnvironmentInformation env, GlobalAnalysisMode globalMode) { if (wsClient == null) { String url = defaultIfBlank(settings.property("sonar.host.url"), CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE); HttpConnector.Builder connectorBuilder = HttpConnector.newBuilder(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java index 62e5d10fc0d..6433faa01f0 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java @@ -32,7 +32,6 @@ import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.apache.commons.lang.ArrayUtils; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.config.Configuration; import org.sonar.api.config.Encryption; import org.sonar.api.config.PropertyDefinition; @@ -40,6 +39,7 @@ import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import static java.util.Objects.requireNonNull; import static org.apache.commons.lang.StringUtils.trim; @@ -51,10 +51,10 @@ public abstract class DefaultConfiguration implements Configuration { private final PropertyDefinitions definitions; private final Encryption encryption; - private final AnalysisMode mode; + private final GlobalAnalysisMode mode; private final Map<String, String> properties = new HashMap<>(); - public DefaultConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, AnalysisMode mode, Map<String, String> props) { + public DefaultConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, GlobalAnalysisMode mode, Map<String, String> props) { this.definitions = requireNonNull(propertyDefinitions); this.encryption = encryption; this.mode = mode; @@ -64,7 +64,7 @@ public abstract class DefaultConfiguration implements Configuration { }); } - public AnalysisMode getMode() { + public GlobalAnalysisMode getMode() { return mode; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java index af5bddfc8f5..df2d09e1f1a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java @@ -45,7 +45,7 @@ import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReport.Duplicate; import org.sonar.scanner.protocol.output.ScannerReport.Duplication; import org.sonar.scanner.report.ReportPublisher; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import org.sonar.scanner.util.ProgressReport; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java index 8f4a0c420d6..bd1d9f8914a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/DeprecatedSensorContext.java @@ -40,7 +40,7 @@ import org.sonar.api.resources.Resource; import org.sonar.api.resources.ResourceUtils; import org.sonar.core.component.ComponentKeys; import org.sonar.scanner.index.DefaultIndex; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.sensor.DefaultSensorContext; public class DeprecatedSensorContext extends DefaultSensorContext implements SensorContext { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java index c8dd7a3097b..a36dbcef2c0 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java @@ -44,7 +44,7 @@ import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLi import org.sonar.scanner.protocol.output.ScannerReport.Issue; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; /** * Adds components and analysis metadata to output report diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java index 9d9d62328f9..18390ef6a89 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java @@ -21,12 +21,12 @@ package org.sonar.scanner.report; import java.util.Map.Entry; import java.util.Optional; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.config.Configuration; import org.sonar.scanner.ProjectAnalysisInfo; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.bootstrap.ScannerPlugin; import org.sonar.scanner.bootstrap.ScannerPluginRepository; import org.sonar.scanner.cpd.CpdSettings; @@ -35,7 +35,7 @@ import org.sonar.scanner.protocol.output.ScannerReport.Metadata.BranchType; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.rule.ModuleQProfiles; import org.sonar.scanner.rule.QProfile; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.sonar.core.config.ScannerProperties.ORGANIZATION; @@ -46,12 +46,12 @@ public class MetadataPublisher implements ReportPublisherStep { private final ProjectAnalysisInfo projectAnalysisInfo; private final InputModuleHierarchy moduleHierarchy; private final CpdSettings cpdSettings; - private final AnalysisMode mode; + private final DefaultAnalysisMode mode; private final ScannerPluginRepository pluginRepository; private final BranchConfiguration branchConfiguration; public MetadataPublisher(ProjectAnalysisInfo projectAnalysisInfo, InputModuleHierarchy moduleHierarchy, Configuration settings, - ModuleQProfiles qProfiles, CpdSettings cpdSettings, AnalysisMode mode, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration) { + ModuleQProfiles qProfiles, CpdSettings cpdSettings, DefaultAnalysisMode mode, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration) { this.projectAnalysisInfo = projectAnalysisInfo; this.moduleHierarchy = moduleHierarchy; this.settings = settings; @@ -101,8 +101,8 @@ public class MetadataPublisher implements ReportPublisherStep { writer.writeMetadata(builder.build()); } - private static BranchType toProtobufBranchType(BranchConfiguration.BranchType branchType) { - if (branchType == BranchConfiguration.BranchType.LONG) { + private static BranchType toProtobufBranchType(org.sonar.scanner.scan.branch.BranchType branchType) { + if (branchType == org.sonar.scanner.scan.branch.BranchType.LONG) { return BranchType.LONG; } return BranchType.SHORT; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java index 790e6192772..4f24f13b3dd 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java @@ -45,9 +45,10 @@ import org.sonar.api.utils.ZipUtils; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.ScannerWsClient; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonarqube.ws.MediaTypes; import org.sonarqube.ws.WsCe; import org.sonarqube.ws.client.HttpException; @@ -71,21 +72,23 @@ public class ReportPublisher implements Startable { private final ScannerWsClient wsClient; private final AnalysisContextReportPublisher contextPublisher; private final InputModuleHierarchy moduleHierarchy; - private final DefaultAnalysisMode analysisMode; + private final GlobalAnalysisMode analysisMode; private final TempFolder temp; private final ReportPublisherStep[] publishers; private final Server server; private final BranchConfiguration branchConfiguration; + private final DefaultAnalysisMode analysisFlags; private Path reportDir; private ScannerReportWriter writer; - public ReportPublisher(Configuration settings, ScannerWsClient wsClient, Server server, AnalysisContextReportPublisher contextPublisher, - InputModuleHierarchy moduleHierarchy, DefaultAnalysisMode analysisMode, TempFolder temp, ReportPublisherStep[] publishers, BranchConfiguration branchConfiguration) { + public ReportPublisher(Configuration settings, ScannerWsClient wsClient, Server server, AnalysisContextReportPublisher contextPublisher, DefaultAnalysisMode analysisFlags, + InputModuleHierarchy moduleHierarchy, GlobalAnalysisMode analysisMode, TempFolder temp, ReportPublisherStep[] publishers, BranchConfiguration branchConfiguration) { this.settings = settings; this.wsClient = wsClient; this.server = server; this.contextPublisher = contextPublisher; + this.analysisFlags = analysisFlags; this.moduleHierarchy = moduleHierarchy; this.analysisMode = analysisMode; this.temp = temp; @@ -177,7 +180,7 @@ public class ReportPublisher implements Startable { .setParam("projectBranch", moduleHierarchy.root().getBranch()) .setPart("report", filePart); - if (analysisMode.isIncremental()) { + if (analysisFlags.isIncremental()) { post.setParam("characteristic", "incremental=true"); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java index d4675ff1c07..a35ddde0fbd 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java @@ -24,23 +24,20 @@ import org.sonar.api.batch.bootstrap.ProjectKey; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.scanner.analysis.DefaultAnalysisMode; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; +import org.sonar.scanner.scan.branch.BranchConfiguration; public class ProjectRepositoriesProvider extends ProviderAdapter { private static final Logger LOG = Loggers.get(ProjectRepositoriesProvider.class); private static final String LOG_MSG = "Load project repositories"; private ProjectRepositories project = null; - public ProjectRepositories provide(ProjectRepositoriesLoader loader, ProjectKey projectKey, DefaultAnalysisMode mode, BranchConfiguration branchConfig) { - return provideInternal(loader, projectKey, mode.isIssues(), branchConfig); - } - - protected ProjectRepositories provideInternal(ProjectRepositoriesLoader loader, ProjectKey projectKey, boolean isIssueMode, BranchConfiguration branchConfig) { + public ProjectRepositories provide(ProjectRepositoriesLoader loader, ProjectKey projectKey, GlobalAnalysisMode mode, BranchConfiguration branchConfig) { if (project == null) { + boolean isIssuesMode = mode.isIssues(); Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - project = loader.load(projectKey.get(), isIssueMode, branchConfig.branchBase()); - checkProject(isIssueMode); + project = loader.load(projectKey.get(), isIssuesMode, branchConfig.branchBase()); + checkProject(isIssuesMode); profiler.stopInfo(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java index 928d9c8ba1f..a999fa980cf 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java @@ -20,15 +20,19 @@ package org.sonar.scanner.repository.settings; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Throwables; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.annotation.Nullable; import org.apache.commons.lang.StringEscapeUtils; +import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; @@ -38,9 +42,10 @@ import org.sonarqube.ws.Settings.FieldValues.Value; import org.sonarqube.ws.Settings.Setting; import org.sonarqube.ws.Settings.ValuesWsResponse; import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpException; public class DefaultSettingsLoader implements SettingsLoader { - + static final String URL = "api/settings/values.protobuf"; private ScannerWsClient wsClient; private static final Logger LOG = Loggers.get(DefaultSettingsLoader.class); @@ -50,23 +55,58 @@ public class DefaultSettingsLoader implements SettingsLoader { @Override public Map<String, String> load(@Nullable String componentKey) { - String url = "api/settings/values.protobuf"; Profiler profiler = Profiler.create(LOG); - if (componentKey != null) { - url += "?component=" + ScannerUtils.encodeForUrl(componentKey); - profiler.startInfo("Load settings for component '" + componentKey + "'"); - } else { - profiler.startInfo("Load global settings"); + + try { + if (componentKey != null) { + profiler.startInfo("Load settings for component '" + componentKey + "'"); + return loadProjectSettings(componentKey); + } else { + profiler.startInfo("Load global settings"); + return loadSettings(URL); + } + } catch (IOException e) { + throw new IllegalStateException("Failed to load server settings", e); + } finally { + profiler.stopInfo(); } + } + + private Map<String, String> loadSettings(String url) throws IOException { try (InputStream is = wsClient.call(new GetRequest(url)).contentStream()) { ValuesWsResponse values = ValuesWsResponse.parseFrom(is); - profiler.stopInfo(); return toMap(values.getSettingsList()); - } catch (IOException e) { - throw new IllegalStateException("Failed to load server settings", e); } } + private Map<String, String> loadProjectSettings(String componentKey) throws IOException { + String url = URL + "?component=" + ScannerUtils.encodeForUrl(componentKey); + try { + return loadSettings(url); + } catch (RuntimeException e) { + if (shouldThrow(e)) { + throw e; + } + + LOG.debug("Project settings not available - continuing without it"); + } + return Collections.emptyMap(); + } + + private static boolean shouldThrow(Exception e) { + for (Throwable t : Throwables.getCausalChain(e)) { + if (t instanceof HttpException) { + HttpException http = (HttpException) t; + return http.code() != HttpURLConnection.HTTP_NOT_FOUND; + } + if (t instanceof MessageException) { + return true; + } + } + + return false; + } + @VisibleForTesting static Map<String, String> toMap(List<Setting> settingsList) { Map<String, String> result = new LinkedHashMap<>(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettings.java index a2a4c0b90b0..c6a73416a9d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettings.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettings.java @@ -20,14 +20,14 @@ package org.sonar.scanner.scan; import java.util.Map; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.config.Encryption; import org.sonar.api.config.PropertyDefinitions; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.config.DefaultConfiguration; public class ModuleSettings extends DefaultConfiguration { - public ModuleSettings(PropertyDefinitions propertyDefinitions, Encryption encryption, AnalysisMode mode, Map<String, String> props) { + public ModuleSettings(PropertyDefinitions propertyDefinitions, Encryption encryption, GlobalAnalysisMode mode, Map<String, String> props) { super(propertyDefinitions, encryption, mode, props); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettingsProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettingsProvider.java index 25db68aad37..46742e63153 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettingsProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleSettingsProvider.java @@ -25,9 +25,9 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.report.AnalysisContextReportPublisher; import org.sonar.scanner.repository.ProjectRepositories; @@ -37,7 +37,7 @@ public class ModuleSettingsProvider extends ProviderAdapter { private ModuleSettings projectSettings; public ModuleSettings provide(GlobalConfiguration globalSettings, DefaultInputModule module, ProjectRepositories projectRepos, - AnalysisMode analysisMode, AnalysisContextReportPublisher contextReportPublisher) { + GlobalAnalysisMode analysisMode, AnalysisContextReportPublisher contextReportPublisher) { if (projectSettings == null) { Map<String, String> settings = new LinkedHashMap<>(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java index da9caf1a825..f906f59f532 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java @@ -23,10 +23,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; -import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.bootstrap.MutableGlobalSettings; import org.sonar.scanner.repository.ProjectRepositories; @@ -37,11 +37,11 @@ import org.sonar.scanner.repository.ProjectRepositories; public class MutableModuleSettings extends Settings { private final ProjectRepositories projectRepos; - private final DefaultAnalysisMode analysisMode; + private final AnalysisMode analysisMode; private final Map<String, String> properties = new HashMap<>(); public MutableModuleSettings(MutableGlobalSettings batchSettings, ProjectDefinition moduleDefinition, ProjectRepositories projectSettingsRepo, - DefaultAnalysisMode analysisMode) { + AnalysisMode analysisMode) { super(batchSettings.getDefinitions(), batchSettings.getEncryption()); this.projectRepos = projectSettingsRepo; this.analysisMode = analysisMode; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java index 8f0fb5ea142..8c234a9ed33 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java @@ -25,7 +25,7 @@ import java.util.Optional; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; -import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.MutableGlobalSettings; import org.sonar.scanner.repository.ProjectRepositories; @@ -35,10 +35,10 @@ import org.sonar.scanner.repository.ProjectRepositories; @Deprecated public class MutableProjectSettings extends Settings { - private final DefaultAnalysisMode mode; + private final GlobalAnalysisMode mode; private final Map<String, String> properties = new HashMap<>(); - public MutableProjectSettings(ProjectReactor reactor, MutableGlobalSettings mutableGlobalSettings, ProjectRepositories projectRepositories, DefaultAnalysisMode mode) { + public MutableProjectSettings(ProjectReactor reactor, MutableGlobalSettings mutableGlobalSettings, ProjectRepositories projectRepositories, GlobalAnalysisMode mode) { super(mutableGlobalSettings.getDefinitions(), mutableGlobalSettings.getEncryption()); this.mode = mode; addProperties(mutableGlobalSettings.getProperties()); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java index 81f2ba4ffd9..24d1b6a4bca 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java @@ -24,28 +24,32 @@ import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; +import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.utils.MessageException; import org.sonar.core.component.ComponentKeys; import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.scan.branch.BranchParamsValidator; +import org.sonar.scanner.scan.branch.DefaultBranchParamsValidator; /** * This class aims at validating project reactor * @since 3.6 */ public class ProjectReactorValidator { - private final DefaultAnalysisMode mode; - + private final AnalysisMode mode; private final BranchParamsValidator branchParamsValidator; + private final DefaultAnalysisMode analysisFlags; - public ProjectReactorValidator(DefaultAnalysisMode mode, BranchParamsValidator branchParamsValidator) { + public ProjectReactorValidator(AnalysisMode mode, DefaultAnalysisMode analysisFlags, BranchParamsValidator branchParamsValidator) { this.mode = mode; + this.analysisFlags = analysisFlags; this.branchParamsValidator = branchParamsValidator; } - public ProjectReactorValidator(DefaultAnalysisMode mode) { - this(mode, new DefaultBranchParamsValidator()); + public ProjectReactorValidator(AnalysisMode mode, DefaultAnalysisMode analysisFlags) { + this(mode, analysisFlags, new DefaultBranchParamsValidator()); } public void validate(ProjectReactor reactor) { @@ -61,7 +65,7 @@ public class ProjectReactorValidator { String deprecatedBranchName = reactor.getRoot().getBranch(); - branchParamsValidator.validate(validationMessages, deprecatedBranchName, mode.isIncremental()); + branchParamsValidator.validate(validationMessages, deprecatedBranchName, analysisFlags.isIncremental()); validateBranch(validationMessages, deprecatedBranchName); if (!validationMessages.isEmpty()) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 42311f87b75..7deb3166880 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -41,6 +41,7 @@ import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.bootstrap.ExtensionInstaller; import org.sonar.scanner.bootstrap.ExtensionMatcher; import org.sonar.scanner.bootstrap.ExtensionUtils; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.MetricProvider; import org.sonar.scanner.cpd.CpdExecutor; import org.sonar.scanner.cpd.CpdSettings; @@ -75,6 +76,7 @@ import org.sonar.scanner.repository.DefaultQualityProfileLoader; import org.sonar.scanner.repository.DefaultServerIssuesLoader; import org.sonar.scanner.repository.ProjectRepositories; import org.sonar.scanner.repository.ProjectRepositoriesLoader; +import org.sonar.scanner.repository.ProjectRepositoriesProvider; import org.sonar.scanner.repository.QualityProfileLoader; import org.sonar.scanner.repository.QualityProfileProvider; import org.sonar.scanner.repository.ServerIssuesLoader; @@ -85,6 +87,10 @@ import org.sonar.scanner.rule.DefaultActiveRulesLoader; import org.sonar.scanner.rule.DefaultRulesLoader; import org.sonar.scanner.rule.RulesLoader; import org.sonar.scanner.rule.RulesProvider; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfigurationProvider; +import org.sonar.scanner.scan.branch.BranchType; +import org.sonar.scanner.scan.branch.ProjectBranchesProvider; import org.sonar.scanner.scan.filesystem.BatchIdGenerator; import org.sonar.scanner.scan.filesystem.InputComponentStoreProvider; import org.sonar.scanner.scan.measure.DefaultMetricFinder; @@ -137,6 +143,9 @@ public class ProjectScanContainer extends ComponentContainer { Storages.class, new RulesProvider(), new BranchConfigurationProvider(), + new ProjectBranchesProvider(), + DefaultAnalysisMode.class, + new ProjectRepositoriesProvider(), // temp new AnalysisTempFolderProvider(), @@ -223,10 +232,9 @@ public class ProjectScanContainer extends ComponentContainer { @Override protected void doAfterStart() { - DefaultAnalysisMode analysisMode = getComponentByType(DefaultAnalysisMode.class); + GlobalAnalysisMode analysisMode = getComponentByType(GlobalAnalysisMode.class); InputModuleHierarchy tree = getComponentByType(InputModuleHierarchy.class); - analysisMode.printMode(); LOG.info("Project key: {}", tree.root().key()); String organization = props.property("sonar.organization"); if (StringUtils.isNotEmpty(organization)) { @@ -251,7 +259,7 @@ public class ProjectScanContainer extends ComponentContainer { } } - private static String toDisplayName(BranchConfiguration.BranchType branchType) { + private static String toDisplayName(BranchType branchType) { switch (branchType) { case LONG: return "long living"; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettings.java index a25dc51ad43..930a728d209 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettings.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettings.java @@ -20,14 +20,14 @@ package org.sonar.scanner.scan; import java.util.Map; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.config.Encryption; import org.sonar.api.config.PropertyDefinitions; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.config.DefaultConfiguration; public class ProjectSettings extends DefaultConfiguration { - public ProjectSettings(PropertyDefinitions propertyDefinitions, Encryption encryption, AnalysisMode mode, Map<String, String> props) { + public ProjectSettings(PropertyDefinitions propertyDefinitions, Encryption encryption, GlobalAnalysisMode mode, Map<String, String> props) { super(propertyDefinitions, encryption, mode, props); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java index ec167a39290..4823c8ec006 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectSettingsProvider.java @@ -22,25 +22,29 @@ package org.sonar.scanner.scan; import java.util.LinkedHashMap; import java.util.Map; import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.scanner.bootstrap.GlobalConfiguration; -import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; +import org.sonar.scanner.repository.settings.SettingsLoader; public class ProjectSettingsProvider extends ProviderAdapter { private ProjectSettings projectSettings; - public ProjectSettings provide(ProjectReactor reactor, GlobalConfiguration globalSettings, ProjectRepositories projectRepositories, AnalysisMode mode) { + public ProjectSettings provide(ProjectReactor reactor, GlobalConfiguration globalSettings, SettingsLoader settingsLoader, GlobalAnalysisMode mode) { if (projectSettings == null) { Map<String, String> settings = new LinkedHashMap<>(); settings.putAll(globalSettings.getProperties()); - settings.putAll(projectRepositories.settings(reactor.getRoot().getKeyWithBranch())); + settings.putAll(loadProjectSettings(settingsLoader, reactor.getRoot().getKeyWithBranch())); settings.putAll(reactor.getRoot().properties()); projectSettings = new ProjectSettings(globalSettings.getDefinitions(), globalSettings.getEncryption(), mode, settings); } return projectSettings; } + + private Map<String, String> loadProjectSettings(SettingsLoader settingsLoader, String projectKey) { + return settingsLoader.load(projectKey); + } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java index 2a4152c6174..7c29f02c648 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.scanner.scan; +package org.sonar.scanner.scan.branch; import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; @@ -25,10 +25,6 @@ import javax.annotation.concurrent.Immutable; @Immutable public interface BranchConfiguration { - enum BranchType { - SHORT, LONG - } - /** * The type of the branch we're on, determined by: * diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationLoader.java index 5d50c99c64e..ba0e0cd0f8a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationLoader.java @@ -17,14 +17,14 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.scanner.scan; +package org.sonar.scanner.scan.branch; import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.ScannerSide; -import org.sonar.scanner.bootstrap.GlobalConfiguration; +import org.sonar.scanner.scan.ProjectSettings; @ScannerSide @InstantiationStrategy(InstantiationStrategy.PER_BATCH) public interface BranchConfigurationLoader { - BranchConfiguration load(String projectKey, GlobalConfiguration settings); + BranchConfiguration load(ProjectSettings projectSettings, ProjectBranches branches); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java index db35c12ca80..aac1661959d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchConfigurationProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java @@ -17,30 +17,29 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.scanner.scan; +package org.sonar.scanner.scan.branch; import org.picocontainer.annotations.Nullable; import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.bootstrap.ProjectKey; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.scanner.bootstrap.GlobalConfiguration; +import org.sonar.scanner.scan.ProjectSettings; public class BranchConfigurationProvider extends ProviderAdapter { private static final Logger LOG = Loggers.get(BranchConfigurationProvider.class); - private static final String LOG_MSG = "Load project branches"; + private static final String LOG_MSG = "Load branch configuration"; private BranchConfiguration branchConfiguration = null; - public BranchConfiguration provide(@Nullable BranchConfigurationLoader loader, ProjectKey projectKey, GlobalConfiguration globalConfiguration) { + public BranchConfiguration provide(@Nullable BranchConfigurationLoader loader, ProjectSettings projectSettings, ProjectBranches branches) { if (branchConfiguration == null) { if (loader == null) { branchConfiguration = new DefaultBranchConfiguration(); } else { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - branchConfiguration = loader.load(projectKey.get(), globalConfiguration); + branchConfiguration = loader.load(projectSettings, branches); profiler.stopInfo(); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchInfo.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchInfo.java new file mode 100644 index 00000000000..508d8d1ddfe --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchInfo.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan.branch; + +import javax.annotation.concurrent.Immutable; + +/** + * Container class for information about a branch. + */ +@Immutable +public class BranchInfo { + private final String name; + private final BranchType type; + private final boolean isMain; + + public BranchInfo(String name, BranchType type, boolean isMain) { + this.name = name; + this.type = type; + this.isMain = isMain; + } + + public String name() { + return name; + } + + public BranchType type() { + return type; + } + + public boolean isMain() { + return isMain; + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchParamsValidator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchParamsValidator.java index 441ca02a03b..99bc2b6a287 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/BranchParamsValidator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchParamsValidator.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.scanner.scan; +package org.sonar.scanner.scan.branch; import java.util.List; import javax.annotation.Nullable; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java new file mode 100644 index 00000000000..e5a93e40b3c --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan.branch; + +public enum BranchType { + SHORT, LONG +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java index 97a9ddf5cc1..1d172246a9e 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.scanner.scan; +package org.sonar.scanner.scan.branch; import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchParamsValidator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchParamsValidator.java index 4d39cda6dfc..09db0edb0f6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultBranchParamsValidator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchParamsValidator.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.scanner.scan; +package org.sonar.scanner.scan.branch; import java.util.List; import javax.annotation.Nullable; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java new file mode 100644 index 00000000000..697ab5dbaf4 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan.branch; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import javax.annotation.concurrent.Immutable; + +/** + * Container class for information about the branches of a project. + */ +@Immutable +public class ProjectBranches { + + private final Map<String, BranchInfo> branches; + + public ProjectBranches(List<BranchInfo> branchInfos) { + branches = branchInfos.stream().collect(Collectors.toMap(BranchInfo::name, Function.identity())); + } + + public BranchInfo get(String name) { + return branches.get(name); + } + + public boolean isEmpty() { + return branches.isEmpty(); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesLoader.java new file mode 100644 index 00000000000..8b615ff4e1e --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesLoader.java @@ -0,0 +1,34 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan.branch; + +import org.sonar.api.batch.InstantiationStrategy; +import org.sonar.api.batch.ScannerSide; + +@ScannerSide +@InstantiationStrategy(InstantiationStrategy.PER_BATCH) +public interface ProjectBranchesLoader { + + /** + * Load the branches of a project. + */ + ProjectBranches load(String projectKey); + +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesProvider.java new file mode 100644 index 00000000000..197030c54db --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesProvider.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan.branch; + +import java.util.Collections; +import org.picocontainer.annotations.Nullable; +import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.api.batch.bootstrap.ProjectKey; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; +import org.sonar.core.config.ScannerProperties; +import org.sonar.scanner.bootstrap.GlobalConfiguration; + +public class ProjectBranchesProvider extends ProviderAdapter { + + private static final Logger LOG = Loggers.get(ProjectBranchesProvider.class); + private static final String LOG_MSG = "Load project branches"; + + private ProjectBranches branches = null; + + public ProjectBranches provide(@Nullable ProjectBranchesLoader loader, ProjectKey projectKey, GlobalConfiguration settings) { + if (branches == null) { + if (loader == null || !settings.get(ScannerProperties.BRANCH_NAME).isPresent()) { + branches = new ProjectBranches(Collections.emptyList()); + } else { + Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); + branches = loader.load(projectKey.get()); + profiler.stopInfo(); + } + } + return branches; + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/package-info.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/package-info.java new file mode 100644 index 00000000000..e3779c0d7ab --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@ParametersAreNonnullByDefault +package org.sonar.scanner.scan.branch; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java index 3c50e847b28..9e5d573351d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java @@ -32,7 +32,6 @@ import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; import javax.annotation.CheckForNull; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.ScannerSide; import org.sonar.api.batch.fs.InputComponent; import org.sonar.api.batch.fs.InputDir; @@ -44,7 +43,8 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.FileExtensionPredicate; import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.scan.branch.BranchConfiguration; /** * Store of all files and dirs. This cache is shared amongst all project modules. Inclusion and @@ -65,12 +65,12 @@ public class InputComponentStore { private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create(); private final SetMultimap<String, InputFile> filesByExtensionCache = LinkedHashMultimap.create(); private final InputModule root; - private final AnalysisMode mode; + private final DefaultAnalysisMode analysisFlags; private final BranchConfiguration branchConfiguration; - public InputComponentStore(DefaultInputModule root, AnalysisMode mode, BranchConfiguration branchConfiguration) { + public InputComponentStore(DefaultInputModule root, DefaultAnalysisMode analysisFlags, BranchConfiguration branchConfiguration) { this.root = root; - this.mode = mode; + this.analysisFlags = analysisFlags; this.branchConfiguration = branchConfiguration; this.put(root); } @@ -83,7 +83,7 @@ public class InputComponentStore { return inputFileCache.values().stream() .map(f -> (DefaultInputFile) f) .filter(DefaultInputFile::isPublished) - .filter(f -> (!mode.isIncremental() && !branchConfiguration.isShortLivingBranch()) || f.status() != Status.SAME)::iterator; + .filter(f -> (!analysisFlags.isIncremental() && !branchConfiguration.isShortLivingBranch()) || f.status() != Status.SAME)::iterator; } public Iterable<InputFile> allFiles() { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStoreProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStoreProvider.java index f8e55b011de..79ad929d9a9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStoreProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStoreProvider.java @@ -20,16 +20,16 @@ package org.sonar.scanner.scan.filesystem; import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.scan.branch.BranchConfiguration; public class InputComponentStoreProvider extends ProviderAdapter { private InputComponentStore store; - public InputComponentStore provide(InputModuleHierarchy hierarchy, AnalysisMode mode, BranchConfiguration branchConfiguration) { + public InputComponentStore provide(InputModuleHierarchy hierarchy, DefaultAnalysisMode analysisFlags, BranchConfiguration branchConfiguration) { if (store == null) { - store = new InputComponentStore(hierarchy.root(), mode, branchConfiguration); + store = new InputComponentStore(hierarchy.root(), analysisFlags, branchConfiguration); } return store; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java index 50cf4958c5f..39a304cf46c 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java @@ -37,7 +37,7 @@ import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.report.ReportPublisher; import org.sonar.scanner.repository.FileData; import org.sonar.scanner.repository.ProjectRepositories; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.DefaultModuleFileSystem; import org.sonar.scanner.scan.filesystem.ModuleInputComponentStore; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java index db2e7f8e014..0aa49b0ecc1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java @@ -46,7 +46,7 @@ import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; import org.sonar.api.config.Configuration; import org.sonar.api.config.Settings; import org.sonar.api.utils.Version; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.sensor.noop.NoOpNewAnalysisError; import org.sonar.scanner.sensor.noop.NoOpNewCoverage; import org.sonar.scanner.sensor.noop.NoOpNewCpdTokens; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java index e788a597f51..c989f6ef533 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java @@ -23,7 +23,6 @@ import com.google.common.collect.Sets; import java.util.Map; import java.util.Set; import java.util.stream.StreamSupport; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.Phase; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; @@ -40,6 +39,7 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.KeyValueFormat; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.scan.measure.MeasureCache; import static org.sonar.core.util.stream.MoreCollectors.toSet; @@ -50,11 +50,11 @@ public final class ZeroCoverageSensor implements Sensor { private static final Logger LOG = Loggers.get(ZeroCoverageSensor.class); private final MeasureCache measureCache; - private final AnalysisMode mode; + private final DefaultAnalysisMode analysisFlags; - public ZeroCoverageSensor(MeasureCache measureCache, AnalysisMode mode) { + public ZeroCoverageSensor(MeasureCache measureCache, DefaultAnalysisMode analysisFlags) { this.measureCache = measureCache; - this.mode = mode; + this.analysisFlags = analysisFlags; } @Override @@ -65,7 +65,7 @@ public final class ZeroCoverageSensor implements Sensor { @Override public void execute(final SensorContext context) { - if (mode.isIncremental()) { + if (analysisFlags.isIncremental()) { LOG.debug("Incremental mode: not forcing coverage to zero"); return; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/ScanTask.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/ScanTask.java index 56423077a51..eb8b22e125a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/ScanTask.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/ScanTask.java @@ -24,8 +24,6 @@ import org.sonar.api.task.Task; import org.sonar.api.task.TaskDefinition; import org.sonar.core.platform.ComponentContainer; import org.sonar.scanner.analysis.AnalysisProperties; -import org.sonar.scanner.analysis.DefaultAnalysisMode; -import org.sonar.scanner.repository.ProjectRepositoriesProvider; import org.sonar.scanner.scan.ProjectScanContainer; public class ScanTask implements Task { @@ -47,8 +45,6 @@ public class ScanTask implements Task { public void execute() { AnalysisProperties props = new AnalysisProperties(taskProps.properties(), taskProps.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); ProjectScanContainer scanContainer = new ProjectScanContainer(taskContainer, props); - scanContainer.add(DefaultAnalysisMode.class); - scanContainer.add(new ProjectRepositoriesProvider()); scanContainer.execute(); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java index 32d3b81b141..477bd4d2d83 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java @@ -70,21 +70,14 @@ public class TaskContainer extends ComponentContainer { @Override public void doAfterStart() { + // default value is declared in CorePlugin String taskKey = StringUtils.defaultIfEmpty(taskProperties.get(CoreProperties.TASK), CoreProperties.SCAN_TASK); - boolean incremental = "true".equals(taskProperties.get("sonar.incremental")); - if (CoreProperties.SCAN_TASK.equals(taskKey) && incremental) { - taskKey = "incremental"; - } // Release memory taskProperties.clear(); TaskDefinition def = getComponentByType(Tasks.class).definition(taskKey); if (def == null) { - if (incremental) { - throw MessageException.of("Incremental mode is not available. Please contact your administrator."); - } else { - throw MessageException.of("Task '" + taskKey + "' does not exist. Please use '" + ListTask.KEY + "' task to see all available tasks."); - } + throw MessageException.of("Task '" + taskKey + "' does not exist. Please use '" + ListTask.KEY + "' task to see all available tasks."); } Task task = getComponentByType(def.taskClass()); if (task != null) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java index 8841dd881c6..5b68fd567a4 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/DefaultAnalysisModeTest.java @@ -19,122 +19,83 @@ */ package org.sonar.scanner.analysis; -import java.util.HashMap; -import java.util.Map; -import javax.annotation.Nullable; +import java.util.Collections; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonar.api.CoreProperties; -import org.sonar.scanner.bootstrap.GlobalProperties; -import org.sonar.scanner.scan.BranchConfiguration; -import org.sonar.scanner.scan.DefaultBranchConfiguration; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; +import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class DefaultAnalysisModeTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void regular_analysis_by_default() { - DefaultAnalysisMode mode = createMode(null, null); - assertThat(mode.isPreview()).isFalse(); - assertThat(mode.isPublish()).isTrue(); - } - - @Test(expected = IllegalStateException.class) - public void fail_if_inconsistent() { - createMode(null, CoreProperties.ANALYSIS_MODE_ISSUES); + private BranchConfiguration branchConfig; + private ProjectRepositories projectRepos; + private GlobalAnalysisMode globalMode; + private ValidateIncremental validateIncremental; + + @Before + public void setUp() { + branchConfig = mock(BranchConfiguration.class); + projectRepos = mock(ProjectRepositories.class); + globalMode = mock(GlobalAnalysisMode.class); + validateIncremental = mock(ValidateIncremental.class); } - @Test - public void support_publish_mode() { - DefaultAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_PUBLISH); - - assertThat(mode.isPreview()).isFalse(); - assertThat(mode.isPublish()).isTrue(); - } - - @Test - public void incremental_mode_no_longer_valid() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("This mode was removed in SonarQube 5.2"); - - createMode(CoreProperties.ANALYSIS_MODE_INCREMENTAL); - } + @Rule + public ExpectedException thrown = ExpectedException.none(); @Test - public void invalidate_mode() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("[preview, publish, issues]"); + public void dont_scan_all_if_short_lived_branch() { + AnalysisProperties analysisProps = new AnalysisProperties(Collections.singletonMap("sonar.scanAllFiles", "true")); + when(branchConfig.isShortLivingBranch()).thenReturn(true); + DefaultAnalysisMode mode = createmode(analysisProps); - createMode("invalid"); + assertThat(mode.scanAllFiles()).isFalse(); } @Test - public void preview_mode_fallback_issues() { - DefaultAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_PREVIEW); + public void reuse_global_mode() { + when(globalMode.isIssues()).thenReturn(true); + when(globalMode.isPublish()).thenReturn(true); + when(globalMode.isPreview()).thenReturn(true); + DefaultAnalysisMode mode = createmode(new AnalysisProperties(Collections.emptyMap())); assertThat(mode.isIssues()).isTrue(); - assertThat(mode.isPreview()).isFalse(); + assertThat(mode.isPublish()).isTrue(); + assertThat(mode.isPreview()).isTrue(); } @Test - public void scan_all() { - Map<String, String> props = new HashMap<>(); - props.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES); - GlobalProperties globalProps = new GlobalProperties(props); - BranchConfiguration branchConfig = new DefaultBranchConfiguration(); - - AnalysisProperties analysisProps = new AnalysisProperties(new HashMap<String, String>()); - DefaultAnalysisMode mode = new DefaultAnalysisMode(globalProps, analysisProps, branchConfig); - assertThat(mode.scanAllFiles()).isFalse(); - - props.put("sonar.scanAllFiles", "true"); - analysisProps = new AnalysisProperties(props); - - mode = new DefaultAnalysisMode(globalProps, analysisProps, branchConfig); - assertThat(mode.scanAllFiles()).isTrue(); - - props.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PUBLISH); - analysisProps = new AnalysisProperties(props); + public void scan_all_if_publish() { + when(globalMode.isIssues()).thenReturn(false); + DefaultAnalysisMode mode = createmode(new AnalysisProperties(Collections.emptyMap())); - mode = new DefaultAnalysisMode(globalProps, analysisProps, branchConfig); assertThat(mode.scanAllFiles()).isTrue(); } @Test - public void default_publish_mode() { - DefaultAnalysisMode mode = createMode(null); - assertThat(mode.isPublish()).isTrue(); + public void scan_all_if_property_set() { + AnalysisProperties analysisProps = new AnalysisProperties(Collections.singletonMap("sonar.scanAllFiles", "true")); + DefaultAnalysisMode mode = createmode(analysisProps); + assertThat(mode.scanAllFiles()).isTrue(); } @Test - public void support_issues_mode() { - DefaultAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_ISSUES); + public void dont_scan_all_if_issues_mode() { + when(globalMode.isIssues()).thenReturn(true); + DefaultAnalysisMode mode = createmode(new AnalysisProperties(Collections.emptyMap())); - assertThat(mode.isIssues()).isTrue(); assertThat(mode.scanAllFiles()).isFalse(); } - private static DefaultAnalysisMode createMode(@Nullable String mode) { - return createMode(mode, mode); - } - - private static DefaultAnalysisMode createMode(@Nullable String bootstrapMode, @Nullable String analysisMode) { - Map<String, String> bootstrapMap = new HashMap<>(); - Map<String, String> analysisMap = new HashMap<>(); - BranchConfiguration branchConfig = new DefaultBranchConfiguration(); - - if (bootstrapMode != null) { - bootstrapMap.put(CoreProperties.ANALYSIS_MODE, bootstrapMode); - } - if (analysisMode != null) { - analysisMap.put(CoreProperties.ANALYSIS_MODE, analysisMode); - } - return new DefaultAnalysisMode(new GlobalProperties(bootstrapMap), new AnalysisProperties(analysisMap), branchConfig); + private DefaultAnalysisMode createmode(AnalysisProperties analysisProps) { + return new DefaultAnalysisMode(analysisProps, branchConfig, globalMode, projectRepos, validateIncremental); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ExtensionInstallerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ExtensionInstallerTest.java index 927e9629959..618edbd4295 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ExtensionInstallerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ExtensionInstallerTest.java @@ -28,7 +28,6 @@ import org.sonar.api.BatchExtension; import org.sonar.api.ExtensionProvider; import org.sonar.api.SonarPlugin; import org.sonar.api.SonarRuntime; -import org.sonar.api.batch.AnalysisMode; import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.PluginInfo; @@ -38,7 +37,7 @@ import static org.mockito.Mockito.when; public class ExtensionInstallerTest { - GlobalMode mode; + GlobalAnalysisMode mode; ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class); private static SonarPlugin newPluginInstance(final Object... extensions) { @@ -51,7 +50,7 @@ public class ExtensionInstallerTest { @Before public void setUp() { - mode = mock(GlobalMode.class); + mode = mock(GlobalAnalysisMode.class); } @Test @@ -60,7 +59,7 @@ public class ExtensionInstallerTest { when(pluginRepository.getPluginInstance("foo")).thenReturn(newPluginInstance(Foo.class, Bar.class)); ComponentContainer container = new ComponentContainer(); - ExtensionInstaller installer = new ExtensionInstaller(mock(SonarRuntime.class), pluginRepository, mock(AnalysisMode.class)); + ExtensionInstaller installer = new ExtensionInstaller(mock(SonarRuntime.class), pluginRepository, mock(GlobalAnalysisMode.class)); installer.install(container, new FooMatcher()); assertThat(container.getComponentByType(Foo.class)).isNotNull(); @@ -72,7 +71,7 @@ public class ExtensionInstallerTest { when(pluginRepository.getPluginInfos()).thenReturn(Arrays.asList(new PluginInfo("foo"))); when(pluginRepository.getPluginInstance("foo")).thenReturn(newPluginInstance(new FooProvider(), new BarProvider())); ComponentContainer container = new ComponentContainer(); - ExtensionInstaller installer = new ExtensionInstaller(mock(SonarRuntime.class), pluginRepository, mock(AnalysisMode.class)); + ExtensionInstaller installer = new ExtensionInstaller(mock(SonarRuntime.class), pluginRepository, mock(GlobalAnalysisMode.class)); installer.install(container, new FooMatcher()); @@ -85,7 +84,7 @@ public class ExtensionInstallerTest { when(pluginRepository.getPluginInfos()).thenReturn(Arrays.asList(new PluginInfo("foo"))); when(pluginRepository.getPluginInstance("foo")).thenReturn(newPluginInstance(new FooBarProvider())); ComponentContainer container = new ComponentContainer(); - ExtensionInstaller installer = new ExtensionInstaller(mock(SonarRuntime.class), pluginRepository, mock(AnalysisMode.class)); + ExtensionInstaller installer = new ExtensionInstaller(mock(SonarRuntime.class), pluginRepository, mock(GlobalAnalysisMode.class)); installer.install(container, new TrueMatcher()); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalModeTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalAnalysisModeTest.java index fed2ba5aa92..0581e431f88 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalModeTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalAnalysisModeTest.java @@ -28,7 +28,7 @@ import org.sonar.api.CoreProperties; import static org.assertj.core.api.Assertions.assertThat; -public class GlobalModeTest { +public class GlobalAnalysisModeTest { @Rule public ExpectedException thrown = ExpectedException.none(); @@ -41,8 +41,24 @@ public class GlobalModeTest { } @Test + public void incremental_mode_no_longer_valid() { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("This mode was removed in SonarQube 5.2"); + + createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_INCREMENTAL); + } + + @Test + public void invalidate_mode() { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("[preview, publish, issues]"); + + createMode(CoreProperties.ANALYSIS_MODE, "invalid"); + } + + @Test public void testOtherProperty() { - GlobalMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PUBLISH); + GlobalAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PUBLISH); assertThat(mode.isPreview()).isFalse(); assertThat(mode.isIssues()).isFalse(); assertThat(mode.isPublish()).isTrue(); @@ -50,7 +66,7 @@ public class GlobalModeTest { @Test public void testIssuesMode() { - GlobalMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES); + GlobalAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES); assertThat(mode.isPreview()).isFalse(); assertThat(mode.isIssues()).isTrue(); assertThat(mode.isPublish()).isFalse(); @@ -58,7 +74,7 @@ public class GlobalModeTest { @Test public void preview_mode_fallback_issues() { - GlobalMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW); + GlobalAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW); assertThat(mode.isIssues()).isTrue(); assertThat(mode.isPreview()).isFalse(); @@ -66,7 +82,7 @@ public class GlobalModeTest { @Test public void testDefault() { - GlobalMode mode = createMode(null, null); + GlobalAnalysisMode mode = createMode(null, null); assertThat(mode.isPreview()).isFalse(); assertThat(mode.isIssues()).isFalse(); assertThat(mode.isPublish()).isTrue(); @@ -77,12 +93,12 @@ public class GlobalModeTest { createMode(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ANALYSIS); } - private GlobalMode createMode(String key, String value) { + private GlobalAnalysisMode createMode(String key, String value) { Map<String, String> map = new HashMap<>(); if (key != null) { map.put(key, value); } GlobalProperties props = new GlobalProperties(map); - return new GlobalMode(props); + return new GlobalAnalysisMode(props); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalConfigurationProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalConfigurationProviderTest.java index 0ee71d91ecf..6dba27c160a 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalConfigurationProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalConfigurationProviderTest.java @@ -45,13 +45,13 @@ public class GlobalConfigurationProviderTest { SettingsLoader settingsLoader; GlobalProperties bootstrapProps; - private GlobalMode mode; + private GlobalAnalysisMode mode; @Before public void prepare() { settingsLoader = mock(SettingsLoader.class); bootstrapProps = new GlobalProperties(Collections.<String, String>emptyMap()); - mode = mock(GlobalMode.class); + mode = mock(GlobalAnalysisMode.class); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginPredicateTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginPredicateTest.java index 6ae96de59e8..cdc5938ef35 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginPredicateTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginPredicateTest.java @@ -30,7 +30,7 @@ import static org.mockito.Mockito.when; public class ScannerPluginPredicateTest { private MapSettings settings = new MapSettings(); - private GlobalMode mode = mock(GlobalMode.class); + private GlobalAnalysisMode mode = mock(GlobalAnalysisMode.class); @Test public void accept_if_no_inclusions_nor_exclusions() { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java index 07c68101a65..7a9a490476b 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java @@ -37,7 +37,7 @@ public class ScannerWsClientProviderTest { public void provide_client_with_default_settings() { GlobalProperties settings = new GlobalProperties(new HashMap<>()); - ScannerWsClient client = underTest.provide(settings, env, new GlobalMode(new GlobalProperties(Collections.emptyMap()))); + ScannerWsClient client = underTest.provide(settings, env, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))); assertThat(client).isNotNull(); assertThat(client.baseUrl()).isEqualTo("http://localhost:9000/"); @@ -57,7 +57,7 @@ public class ScannerWsClientProviderTest { props.put("sonar.ws.timeout", "42"); GlobalProperties settings = new GlobalProperties(props); - ScannerWsClient client = underTest.provide(settings, env, new GlobalMode(new GlobalProperties(Collections.emptyMap()))); + ScannerWsClient client = underTest.provide(settings, env, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))); assertThat(client).isNotNull(); HttpConnector httpConnector = (HttpConnector) client.wsConnector(); @@ -68,8 +68,8 @@ public class ScannerWsClientProviderTest { @Test public void build_singleton() { GlobalProperties settings = new GlobalProperties(new HashMap<>()); - ScannerWsClient first = underTest.provide(settings, env, new GlobalMode(new GlobalProperties(Collections.emptyMap()))); - ScannerWsClient second = underTest.provide(settings, env, new GlobalMode(new GlobalProperties(Collections.emptyMap()))); + ScannerWsClient first = underTest.provide(settings, env, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))); + ScannerWsClient second = underTest.provide(settings, env, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))); assertThat(first).isSameAs(second); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientTest.java index 3b58773dd66..fa7c8b08916 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientTest.java @@ -55,7 +55,7 @@ public class ScannerWsClientTest { when(wsClient.wsConnector().call(request)).thenReturn(response); logTester.setLevel(LoggerLevel.DEBUG); - ScannerWsClient underTest = new ScannerWsClient(wsClient, false, new GlobalMode(new GlobalProperties(Collections.emptyMap()))); + ScannerWsClient underTest = new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))); WsResponse result = underTest.call(request); @@ -78,7 +78,7 @@ public class ScannerWsClientTest { WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); - new ScannerWsClient(wsClient, false, new GlobalMode(new GlobalProperties(Collections.emptyMap()))).call(request); + new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))).call(request); } @Test @@ -90,7 +90,7 @@ public class ScannerWsClientTest { WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); - new ScannerWsClient(wsClient, /* credentials are configured */true, new GlobalMode(new GlobalProperties(Collections.emptyMap()))).call(request); + new ScannerWsClient(wsClient, /* credentials are configured */true, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))).call(request); } @Test @@ -104,7 +104,7 @@ public class ScannerWsClientTest { .setContent("{\"errors\":[{\"msg\":\"missing scan permission\"}, {\"msg\":\"missing another permission\"}]}"); when(wsClient.wsConnector().call(request)).thenReturn(response); - new ScannerWsClient(wsClient, true, new GlobalMode(new GlobalProperties(Collections.emptyMap()))).call(request); + new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))).call(request); } @Test @@ -118,7 +118,7 @@ public class ScannerWsClientTest { .setContent("{\"errors\":[{\"msg\":\"Boo! bad request! bad!\"}]}"); when(wsClient.wsConnector().call(request)).thenReturn(response); - new ScannerWsClient(wsClient, true, new GlobalMode(new GlobalProperties(Collections.emptyMap()))).call(request); + new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new GlobalProperties(Collections.emptyMap()))).call(request); } private MockWsResponse newResponse() { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java index 0432e2511a0..adf55780f1d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java @@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableMap; import java.util.Arrays; import org.junit.Rule; import org.junit.Test; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.config.Configuration; import org.sonar.api.config.Encryption; import org.sonar.api.config.PropertyDefinition; @@ -31,6 +30,7 @@ import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.PropertyFieldDefinition; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -46,7 +46,7 @@ public class DefaultConfigurationTest { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("single").multiValues(false).build(), PropertyDefinition.builder("multiA").multiValues(true).build())), new Encryption(null), - mock(AnalysisMode.class), + mock(GlobalAnalysisMode.class), ImmutableMap.of("single", "foo", "multiA", "a,b", "notDeclared", "c,d")) { }; @@ -74,7 +74,7 @@ public class DefaultConfigurationTest { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("props").fields(PropertyFieldDefinition.build("foo1").name("Foo1").build(), PropertyFieldDefinition.build("foo2").name("Foo2").build()).build())), new Encryption(null), - mock(AnalysisMode.class), + mock(GlobalAnalysisMode.class), ImmutableMap.of("props", "1,2", "props.1.foo1", "a", "props.1.foo2", "b")) { }; @@ -94,7 +94,7 @@ public class DefaultConfigurationTest { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("single").multiValues(false).defaultValue("default").build(), PropertyDefinition.builder("multiA").multiValues(true).defaultValue("foo,bar").build())), new Encryption(null), - mock(AnalysisMode.class), + mock(GlobalAnalysisMode.class), ImmutableMap.of()) { }; @@ -125,7 +125,7 @@ public class DefaultConfigurationTest { private String[] getStringArray(String value) { return new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("multi").multiValues(true).build())), new Encryption(null), - mock(AnalysisMode.class), + mock(GlobalAnalysisMode.class), ImmutableMap.of("multi", value)) { }.getStringArray("multi"); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java index 0d0ead878ab..2ef29f1d751 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java @@ -30,7 +30,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; @@ -42,13 +41,14 @@ import org.sonar.duplications.block.Block; import org.sonar.duplications.block.ByteArray; import org.sonar.duplications.index.CloneGroup; import org.sonar.duplications.index.ClonePart; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; import org.sonar.scanner.protocol.output.ScannerReport.Duplicate; import org.sonar.scanner.protocol.output.ScannerReport.Duplication; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.report.ReportPublisher; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import static org.assertj.core.api.Assertions.assertThat; @@ -90,7 +90,7 @@ public class CpdExecutorTest { index = new SonarCpdBlockIndex(publisher, settings); DefaultInputModule inputModule = TestInputFileBuilder.newDefaultInputModule("foo", baseDir); - componentStore = new InputComponentStore(inputModule, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + componentStore = new InputComponentStore(inputModule, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); executor = new CpdExecutor(settings, index, publisher, componentStore, branchConfig); reader = new ScannerReportReader(outputDir); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java index df04b69b62e..ffd845e3479 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java @@ -52,8 +52,7 @@ import org.sonar.api.utils.DateUtils; import org.sonar.batch.bootstrapper.Batch; import org.sonar.batch.bootstrapper.EnvironmentInformation; import org.sonar.batch.bootstrapper.LogOutput; -import org.sonar.scanner.bootstrap.GlobalConfiguration; -import org.sonar.scanner.bootstrap.GlobalMode; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.issue.tracking.ServerLineHashesLoader; import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue; import org.sonar.scanner.report.ReportPublisher; @@ -68,8 +67,11 @@ import org.sonar.scanner.repository.settings.SettingsLoader; import org.sonar.scanner.rule.ActiveRulesLoader; import org.sonar.scanner.rule.LoadedActiveRule; import org.sonar.scanner.rule.RulesLoader; -import org.sonar.scanner.scan.BranchConfiguration; -import org.sonar.scanner.scan.BranchConfigurationLoader; +import org.sonar.scanner.scan.ProjectSettings; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfigurationLoader; +import org.sonar.scanner.scan.branch.BranchType; +import org.sonar.scanner.scan.branch.ProjectBranches; import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; import org.sonarqube.ws.Rules.ListResponse.Rule; @@ -239,7 +241,7 @@ public class ScannerMediumTester extends ExternalResource { throw new IllegalStateException(e); } registerCoreMetrics(); - globalProperties.put(GlobalMode.MEDIUM_TEST_ENABLED, "true"); + globalProperties.put(GlobalAnalysisMode.MEDIUM_TEST_ENABLED, "true"); globalProperties.put(ReportPublisher.KEEP_REPORT_PROP_KEY, "true"); globalProperties.put("sonar.userHome", userHome.toString()); } @@ -420,7 +422,7 @@ public class ScannerMediumTester extends ExternalResource { } } - public ScannerMediumTester setBranchType(BranchConfiguration.BranchType branchType) { + public ScannerMediumTester setBranchType(BranchType branchType) { branchConfiguration.branchType = branchType; return this; } @@ -437,7 +439,7 @@ public class ScannerMediumTester extends ExternalResource { private class FakeBranchConfigurationLoader implements BranchConfigurationLoader { @Override - public BranchConfiguration load(String projectKey, GlobalConfiguration settings) { + public BranchConfiguration load(ProjectSettings settings, ProjectBranches branches) { return branchConfiguration; } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java index 58d9133dddc..2461e5ea786 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java @@ -34,7 +34,7 @@ import org.sonar.scanner.mediumtest.ScannerMediumTester; import org.sonar.scanner.mediumtest.TaskResult; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.repository.FileData; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchType; import org.sonar.xoo.XooPlugin; import org.sonar.xoo.rule.XooRulesDefinition; @@ -80,7 +80,7 @@ public class BranchMediumTest { assertThat(result.getReportReader().readFileSource(fileId)).isNotNull(); // file is skipped for short branches (no report, no coverage, no duplications) - TaskResult result2 = getResult(tester.setBranchType(BranchConfiguration.BranchType.SHORT)); + TaskResult result2 = getResult(tester.setBranchType(BranchType.SHORT)); assertThat(result2.getReportComponent(result2.inputFile(FILE_PATH).key())).isNull(); assertThat(result2.getReportReader().readChangesets(fileId)).isNull(); assertThat(result2.getReportReader().hasCoverage(fileId)).isFalse(); @@ -95,7 +95,7 @@ public class BranchMediumTest { TaskResult result = getResult(tester .setBranchName(branchName) .setBranchTarget(branchTarget) - .setBranchType(BranchConfiguration.BranchType.SHORT)); + .setBranchType(BranchType.SHORT)); ScannerReport.Metadata metadata = result.getReportReader().readMetadata(); assertThat(metadata.getBranchName()).isEqualTo(branchName); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java index 8d679016b61..d7cb80f787e 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/tasks/TasksMediumTest.java @@ -21,6 +21,7 @@ package org.sonar.scanner.mediumtest.tasks; import static org.assertj.core.api.Assertions.assertThat; +import java.io.File; import java.util.Arrays; import java.util.List; @@ -28,6 +29,7 @@ import org.assertj.core.api.Condition; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; import org.sonar.api.SonarPlugin; import org.sonar.api.task.Task; import org.sonar.api.task.TaskDefinition; @@ -46,6 +48,9 @@ public class TasksMediumTest { public LogTester logTester = new LogTester(); @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule public ScannerMediumTester tester = new ScannerMediumTester() .registerPlugin("faketask", new FakeTaskPlugin()); @@ -87,12 +92,16 @@ public class TasksMediumTest { @Test public void incrementalNotFound() throws Exception { + File baseDir = temp.newFolder(); thrown.expect(MessageException.class); thrown.expectMessage( "Incremental mode is not available. Please contact your administrator."); tester.newTask() .properties(ImmutableMap.<String, String>builder() + .put("sonar.projectKey", "key") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.sources", ".") .put("sonar.incremental", "true").build()) .execute(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java index f39cafcadb7..1bbd510f6e7 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java @@ -31,9 +31,10 @@ import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.postjob.issue.PostJobIssue; import org.sonar.api.batch.rule.Severity; import org.sonar.api.config.internal.MapSettings; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.issue.IssueCache; import org.sonar.scanner.issue.tracking.TrackedIssue; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import static org.assertj.core.api.Assertions.assertThat; @@ -55,7 +56,7 @@ public class DefaultPostJobContextTest { public void setUp() throws IOException { issueCache = mock(IssueCache.class); DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule("foo", temp.newFolder()); - componentStore = new InputComponentStore(rootModule, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + componentStore = new InputComponentStore(rootModule, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); settings = new MapSettings(); analysisMode = mock(AnalysisMode.class); context = new DefaultPostJobContext(settings.asConfig(), settings, issueCache, componentStore, analysisMode); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java index a03afd14359..5ebfeb7e852 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java @@ -45,8 +45,8 @@ import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus; import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.scan.BranchConfiguration; -import org.sonar.scanner.scan.BranchConfiguration.BranchType; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchType; import org.sonar.scanner.scan.DefaultComponentTree; import static org.assertj.core.api.Assertions.assertThat; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java index f2a0e614bae..b517367c309 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java @@ -25,17 +25,17 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; import org.sonar.core.util.CloseableIterator; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.protocol.output.ScannerReport.LineCoverage; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import org.sonar.scanner.scan.measure.MeasureCache; @@ -59,7 +59,7 @@ public class CoveragePublisherTest { String moduleKey = "foo"; inputFile = new TestInputFileBuilder(moduleKey, "src/Foo.php").setLines(5).build(); DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(moduleKey, temp.newFolder()); - InputComponentStore componentCache = new InputComponentStore(rootModule, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + InputComponentStore componentCache = new InputComponentStore(rootModule, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); componentCache.put(inputFile); measureCache = mock(MeasureCache.class); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java index 31a60b633c8..b42567c73cc 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java @@ -28,18 +28,18 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; import org.sonar.core.util.CloseableIterator; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.deprecated.test.TestPlanBuilder; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import org.sonar.scanner.scan.measure.MeasureCache; @@ -70,7 +70,7 @@ public class MeasuresPublisherTest { String moduleKey = "foo"; inputModule = TestInputFileBuilder.newDefaultInputModule(moduleKey, temp.newFolder()); inputFile = new TestInputFileBuilder(moduleKey, "src/Foo.php").setPublish(true).build(); - InputComponentStore componentCache = new InputComponentStore(inputModule, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + InputComponentStore componentCache = new InputComponentStore(inputModule, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); componentCache.put(inputFile); measureCache = mock(MeasureCache.class); when(measureCache.byComponentKey(anyString())).thenReturn(Collections.<DefaultMeasure<?>>emptyList()); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index 3bc70f9c875..f00269ac4b1 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -28,7 +28,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; @@ -36,6 +35,7 @@ import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.config.internal.MapSettings; import org.sonar.core.config.ScannerProperties; import org.sonar.scanner.ProjectAnalysisInfo; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.bootstrap.ScannerPlugin; import org.sonar.scanner.bootstrap.ScannerPluginRepository; import org.sonar.scanner.cpd.CpdSettings; @@ -44,7 +44,8 @@ import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.rule.ModuleQProfiles; import org.sonar.scanner.rule.QProfile; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchType; import static java.util.Arrays.asList; import static java.util.Collections.emptyMap; @@ -65,7 +66,7 @@ public class MetadataPublisherTest { private ProjectAnalysisInfo projectAnalysisInfo; private CpdSettings cpdSettings; private InputModuleHierarchy inputModuleHierarchy; - private AnalysisMode analysisMode; + private DefaultAnalysisMode analysisMode; private ScannerPluginRepository pluginRepository; private BranchConfiguration branches; @@ -85,7 +86,7 @@ public class MetadataPublisherTest { rootModule = new DefaultInputModule(def.setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()), TestInputFileBuilder.nextBatchId()); inputModuleHierarchy = mock(InputModuleHierarchy.class); when(inputModuleHierarchy.root()).thenReturn(rootModule); - analysisMode = mock(AnalysisMode.class); + analysisMode = mock(DefaultAnalysisMode.class); branches = mock(BranchConfiguration.class); underTest = new MetadataPublisher(projectAnalysisInfo, inputModuleHierarchy, settings.asConfig(), qProfiles, cpdSettings, analysisMode, pluginRepository, branches); @@ -168,7 +169,7 @@ public class MetadataPublisherTest { public void write_long_lived_branch_info() throws Exception { String branchName = "long-lived"; when(branches.branchName()).thenReturn(branchName); - when(branches.branchType()).thenReturn(BranchConfiguration.BranchType.LONG); + when(branches.branchType()).thenReturn(BranchType.LONG); File outputDir = temp.newFolder(); underTest.publish(new ScannerReportWriter(outputDir)); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java index e7da36601b0..e3bccbd64ff 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java @@ -45,8 +45,9 @@ import org.sonar.api.utils.log.LoggerLevel; import org.sonar.core.config.CorePropertyDefinitions; import org.sonar.core.config.ScannerProperties; import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.ScannerWsClient; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonarqube.ws.WsCe; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.WsRequest; @@ -59,7 +60,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.sonar.scanner.scan.BranchConfiguration.BranchType.SHORT; +import static org.sonar.scanner.scan.branch.BranchType.SHORT; public class ReportPublisherTest { @@ -72,7 +73,8 @@ public class ReportPublisherTest { @Rule public ExpectedException exception = ExpectedException.none(); - DefaultAnalysisMode mode = mock(DefaultAnalysisMode.class); + DefaultAnalysisMode analysisFlags = mock(DefaultAnalysisMode.class); + GlobalAnalysisMode mode = mock(GlobalAnalysisMode.class); MapSettings settings = new MapSettings(new PropertyDefinitions(CorePropertyDefinitions.all())); ScannerWsClient wsClient; Server server = mock(Server.class); @@ -92,8 +94,8 @@ public class ReportPublisherTest { @Test public void log_and_dump_information_about_report_uploading() throws IOException { - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), - new ReportPublisherStep[0], branchConfiguration); + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, + mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); settings.setProperty(ScannerProperties.ORGANIZATION, "MyOrg"); underTest.logSuccess("TASK-123"); @@ -116,8 +118,8 @@ public class ReportPublisherTest { @Test public void parse_upload_error_message() throws IOException { - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), - new ReportPublisherStep[0], branchConfiguration); + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, + mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}"); WsResponse response = mock(WsResponse.class); when(response.failIfNotSuccessful()).thenThrow(ex); @@ -131,7 +133,7 @@ public class ReportPublisherTest { @Test public void log_public_url_if_defined() throws IOException { when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube"); - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); underTest.logSuccess("TASK-123"); @@ -153,7 +155,7 @@ public class ReportPublisherTest { @Test public void fail_if_public_url_malformed() throws IOException { when(server.getPublicRootUrl()).thenReturn("invalid"); - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); exception.expect(MessageException.class); @@ -163,7 +165,7 @@ public class ReportPublisherTest { @Test public void log_but_not_dump_information_when_report_is_not_uploaded() { - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); underTest.logSuccess(/* report not uploaded, no server task */null); @@ -181,7 +183,7 @@ public class ReportPublisherTest { settings.setProperty("sonar.scanner.keepReport", true); Path reportDir = temp.getRoot().toPath().resolve("scanner-report"); Files.createDirectory(reportDir); - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); underTest.start(); @@ -193,7 +195,8 @@ public class ReportPublisherTest { public void should_delete_report_by_default() throws IOException { Path reportDir = temp.getRoot().toPath().resolve("scanner-report"); Files.createDirectory(reportDir); - ReportPublisher job = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], + ReportPublisher job = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), + new ReportPublisherStep[0], branchConfiguration); job.start(); @@ -203,7 +206,7 @@ public class ReportPublisherTest { @Test public void test_ws_parameters() throws Exception { - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); settings.setProperty(ScannerProperties.ORGANIZATION, "MyOrg"); @@ -232,10 +235,10 @@ public class ReportPublisherTest { @Test public void test_send_incremental_characteristic() throws Exception { - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); - when(mode.isIncremental()).thenReturn(true); + when(analysisFlags.isIncremental()).thenReturn(true); String orgName = "MyOrg"; settings.setProperty(ScannerProperties.ORGANIZATION, orgName); @@ -265,7 +268,7 @@ public class ReportPublisherTest { @Test public void test_send_branches_characteristics() throws Exception { - ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), + ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, analysisFlags, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); String orgName = "MyOrg"; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java index b859b3962e8..a343584a911 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java @@ -27,13 +27,13 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.fs.InputFile.Status; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import static org.assertj.core.api.Assertions.assertThat; @@ -48,7 +48,7 @@ public class SourcePublisherTest { private File sourceFile; private ScannerReportWriter writer; private DefaultInputFile inputFile; - private AnalysisMode analysisMode; + private DefaultAnalysisMode analysisFlags; @Before public void prepare() throws IOException { @@ -62,8 +62,8 @@ public class SourcePublisherTest { .build(); DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(moduleKey, baseDir); - analysisMode = mock(AnalysisMode.class); - InputComponentStore componentStore = new InputComponentStore(rootModule, analysisMode, mock(BranchConfiguration.class)); + analysisFlags = mock(DefaultAnalysisMode.class); + InputComponentStore componentStore = new InputComponentStore(rootModule, analysisFlags, mock(BranchConfiguration.class)); componentStore.put(inputFile); publisher = new SourcePublisher(componentStore); @@ -124,7 +124,7 @@ public class SourcePublisherTest { @Test public void publishChangedSourceInIncrementalMode() throws Exception { - when(analysisMode.isIncremental()).thenReturn(true); + when(analysisFlags.isIncremental()).thenReturn(true); FileUtils.write(sourceFile, "1\n2\n3\n4\n5", StandardCharsets.ISO_8859_1); inputFile.setStatus(Status.CHANGED); @@ -136,7 +136,7 @@ public class SourcePublisherTest { @Test public void dontPublishUnchangedSourceInIncrementalMode() throws Exception { - when(analysisMode.isIncremental()).thenReturn(true); + when(analysisFlags.isIncremental()).thenReturn(true); FileUtils.write(sourceFile, "foo", StandardCharsets.ISO_8859_1); inputFile.setStatus(Status.SAME); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java index e7c5b810a10..1935ab503d7 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/ProjectRepositoriesProviderTest.java @@ -27,8 +27,8 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.sonar.api.batch.bootstrap.ProjectKey; -import org.sonar.scanner.analysis.DefaultAnalysisMode; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; @@ -47,7 +47,7 @@ public class ProjectRepositoriesProviderTest { @Mock private ProjectKey projectKey; @Mock - private DefaultAnalysisMode mode; + private GlobalAnalysisMode mode; @Mock private BranchConfiguration branchConfiguration; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultSettingsLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultSettingsLoaderTest.java index f2ecbe4d5fb..2171a2b8809 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultSettingsLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultSettingsLoaderTest.java @@ -19,7 +19,11 @@ */ package org.sonar.scanner.repository.settings; +import java.io.IOException; +import org.junit.Before; import org.junit.Test; +import org.sonar.scanner.WsTestUtil; +import org.sonar.scanner.bootstrap.ScannerWsClient; import org.sonarqube.ws.Settings.FieldValues; import org.sonarqube.ws.Settings.FieldValues.Value; import org.sonarqube.ws.Settings.FieldValues.Value.Builder; @@ -29,8 +33,17 @@ import org.sonarqube.ws.Settings.Values; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; +import static org.mockito.Mockito.mock; public class DefaultSettingsLoaderTest { + private ScannerWsClient wsClient; + private DefaultSettingsLoader loader; + + @Before + public void prepare() throws IOException { + wsClient = mock(ScannerWsClient.class); + loader = new DefaultSettingsLoader(wsClient); + } @Test public void should_load_global_multivalue_settings() { @@ -69,4 +82,10 @@ public class DefaultSettingsLoaderTest { entry("sonar.issue.exclusions.multicriteria.2.rulepattern", "*:S456")); } + @Test + public void continue_on_error() { + WsTestUtil.mockException(wsClient, DefaultSettingsLoader.URL + "?component=project", new IllegalStateException()); + assertThat(loader.load("project")).isEmpty(); + } + } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java index e9d075d327e..fbaca69eeca 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java @@ -21,10 +21,11 @@ package org.sonar.scanner.scan; import java.util.Arrays; import org.junit.Test; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import static org.assertj.core.api.Assertions.assertThat; @@ -38,7 +39,7 @@ public class ModuleIndexerTest { private InputComponentStore componentStore; public void createIndexer(DefaultInputModule rootModule) { - componentStore = new InputComponentStore(rootModule, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + componentStore = new InputComponentStore(rootModule, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); tree = new DefaultComponentTree(); moduleHierarchy = mock(DefaultInputModuleHierarchy.class); indexer = new ModuleIndexer(tree, componentStore, moduleHierarchy); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableModuleSettingsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableModuleSettingsTest.java index c0e4d487632..317849838b3 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableModuleSettingsTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableModuleSettingsTest.java @@ -29,13 +29,13 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.utils.MessageException; -import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.bootstrap.GlobalConfigurationProvider; -import org.sonar.scanner.bootstrap.GlobalMode; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.GlobalProperties; import org.sonar.scanner.bootstrap.MutableGlobalSettings; import org.sonar.scanner.repository.FileData; @@ -51,11 +51,11 @@ public class MutableModuleSettingsTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private DefaultAnalysisMode mode; + private AnalysisMode mode; @Before public void before() { - mode = mock(DefaultAnalysisMode.class); + mode = mock(AnalysisMode.class); } private ProjectRepositories createSettings(String module, Map<String, String> settingsMap) { @@ -157,6 +157,6 @@ public class MutableModuleSettingsTest { private GlobalConfiguration newGlobalSettings(Map<String, String> props) { GlobalProperties globalProps = new GlobalProperties(props); return new GlobalConfigurationProvider().provide(mock(SettingsLoader.class), globalProps, new PropertyDefinitions(), - new GlobalMode(globalProps)); + new GlobalAnalysisMode(globalProps)); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableProjectSettingsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableProjectSettingsTest.java index 0087e27fede..707e4b33a23 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableProjectSettingsTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/MutableProjectSettingsTest.java @@ -33,10 +33,9 @@ import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.LogTester; -import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.bootstrap.GlobalConfigurationProvider; -import org.sonar.scanner.bootstrap.GlobalMode; +import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.GlobalProperties; import org.sonar.scanner.bootstrap.MutableGlobalSettings; import org.sonar.scanner.repository.FileData; @@ -60,16 +59,14 @@ public class MutableProjectSettingsTest { private Table<String, String, FileData> emptyFileData; private Table<String, String, String> emptySettings; - private GlobalMode globalMode; - private DefaultAnalysisMode mode; + private GlobalAnalysisMode globalMode; @Before public void prepare() { emptyFileData = ImmutableTable.of(); emptySettings = ImmutableTable.of(); project = ProjectDefinition.create().setKey("struts"); - globalMode = mock(GlobalMode.class); - mode = mock(DefaultAnalysisMode.class); + globalMode = mock(GlobalAnalysisMode.class); bootstrapProps = new GlobalConfigurationProvider().provide(mock(SettingsLoader.class), new GlobalProperties(Collections.<String, String>emptyMap()), new PropertyDefinitions(), globalMode); } @@ -79,7 +76,7 @@ public class MutableProjectSettingsTest { project.setProperty("project.prop", "project"); projectRef = new ProjectRepositories(emptySettings, emptyFileData, null); - MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, mode); + MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, globalMode); assertThat(batchSettings.getString("project.prop")).isEqualTo("project"); } @@ -91,7 +88,7 @@ public class MutableProjectSettingsTest { settings.put("struts", "sonar.java.coveragePlugin", "jacoco"); projectRef = new ProjectRepositories(settings, emptyFileData, null); - MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, mode); + MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, globalMode); assertThat(batchSettings.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco"); } @@ -105,7 +102,7 @@ public class MutableProjectSettingsTest { projectRef = new ProjectRepositories(settings, emptyFileData, null); - MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, mode); + MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, globalMode); assertThat(batchSettings.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco"); } @@ -117,7 +114,7 @@ public class MutableProjectSettingsTest { settings.put("struts", "sonar.foo.license.secured", "bar2"); projectRef = new ProjectRepositories(settings, emptyFileData, null); - MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, mode); + MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, globalMode); assertThat(batchSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2"); assertThat(batchSettings.getString("sonar.foo.secured")).isEqualTo("bar"); @@ -129,10 +126,10 @@ public class MutableProjectSettingsTest { settings.put("struts", "sonar.foo.secured", "bar"); settings.put("struts", "sonar.foo.license.secured", "bar2"); - when(mode.isIssues()).thenReturn(true); + when(globalMode.isIssues()).thenReturn(true); projectRef = new ProjectRepositories(settings, emptyFileData, null); - MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, mode); + MutableProjectSettings batchSettings = new MutableProjectSettings(new ProjectReactor(project), new MutableGlobalSettings(bootstrapProps), projectRef, globalMode); assertThat(batchSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2"); thrown.expect(MessageException.class); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java index 896b3a9fd06..5c88bd4406a 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java @@ -24,6 +24,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.CoreProperties; +import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.utils.MessageException; @@ -37,13 +38,15 @@ public class ProjectReactorValidatorTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private DefaultAnalysisMode mode; + private AnalysisMode mode; + private DefaultAnalysisMode analysisFlags; private ProjectReactorValidator validator; @Before public void prepare() { - mode = mock(DefaultAnalysisMode.class); - validator = new ProjectReactorValidator(mode); + mode = mock(AnalysisMode.class); + analysisFlags = mock(DefaultAnalysisMode.class); + validator = new ProjectReactorValidator(mode, analysisFlags); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/BranchConfigurationProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java index 41c3f9a17be..7201374fa78 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/BranchConfigurationProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java @@ -17,47 +17,48 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.scanner.scan; +package org.sonar.scanner.scan.branch; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.scanner.scan.ProjectSettings; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.junit.Before; -import org.junit.Test; -import org.sonar.scanner.bootstrap.GlobalConfiguration; -import org.sonar.scanner.scan.BranchConfiguration.BranchType; - public class BranchConfigurationProviderTest { private BranchConfigurationProvider provider = new BranchConfigurationProvider(); - private GlobalConfiguration globalConfiguration; + private ProjectSettings projectSettings; private BranchConfigurationLoader loader; private BranchConfiguration config; + private ProjectBranches branches; @Before public void setUp() { - globalConfiguration = mock(GlobalConfiguration.class); + projectSettings = mock(ProjectSettings.class); loader = mock(BranchConfigurationLoader.class); config = mock(BranchConfiguration.class); + branches = mock(ProjectBranches.class); } @Test public void should_cache_config() { - BranchConfiguration configuration = provider.provide(null, () -> "project", globalConfiguration); - assertThat(provider.provide(null, () -> "project", globalConfiguration)).isSameAs(configuration); + BranchConfiguration configuration = provider.provide(null, projectSettings, branches); + assertThat(provider.provide(null, projectSettings, branches)).isSameAs(configuration); } @Test public void should_use_loader() { - when(loader.load("key", globalConfiguration)).thenReturn(config); - BranchConfiguration branchConfig = provider.provide(loader, () -> "key", globalConfiguration); + when(loader.load(projectSettings, branches)).thenReturn(config); + BranchConfiguration branchConfig = provider.provide(loader, projectSettings, branches); assertThat(branchConfig).isSameAs(config); } @Test public void should_return_default_if_no_loader() { - BranchConfiguration configuration = provider.provide(null, () -> "project", globalConfiguration); + BranchConfiguration configuration = provider.provide(null, projectSettings, branches); assertThat(configuration.branchTarget()).isNull(); assertThat(configuration.branchType()).isEqualTo(BranchType.LONG); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesProviderTest.java new file mode 100644 index 00000000000..3a227987a39 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesProviderTest.java @@ -0,0 +1,66 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan.branch; + +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.config.Configuration; +import org.sonar.scanner.bootstrap.GlobalConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ProjectBranchesProviderTest { + private ProjectBranchesProvider provider = new ProjectBranchesProvider(); + private ProjectBranchesLoader mockLoader; + private ProjectBranches mockBranches; + private GlobalConfiguration mockSettings; + + @Before + public void setUp() { + mockLoader = mock(ProjectBranchesLoader.class); + mockBranches = mock(ProjectBranches.class); + mockSettings = mock(GlobalConfiguration.class); + } + + @Test + public void should_cache_branches() { + ProjectBranches branches = provider.provide(null, () -> "project", mockSettings); + assertThat(provider.provide(null, () -> "project", mockSettings)).isSameAs(branches); + } + + @Test + public void should_use_loader() { + when(mockLoader.load("key")).thenReturn(mockBranches); + when(mockSettings.get(anyString())).thenReturn(Optional.of("somebranch")); + ProjectBranches branches = provider.provide(mockLoader, () -> "key", mockSettings); + + assertThat(branches).isSameAs(mockBranches); + } + + @Test + public void should_return_default_if_no_loader() { + ProjectBranches branches = provider.provide(null, () -> "project", mockSettings); + assertThat(branches.isEmpty()).isTrue(); + } +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java index aacfb609f52..9d96bd65dfb 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java @@ -27,7 +27,6 @@ import java.util.List; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Status; @@ -36,7 +35,8 @@ import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -60,7 +60,7 @@ public class InputComponentStoreTest { DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(rootDef); DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(moduleDef); - InputComponentStore cache = new InputComponentStore(rootModule, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + InputComponentStore cache = new InputComponentStore(rootModule, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); cache.put(subModule); DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java") @@ -104,7 +104,7 @@ public class InputComponentStoreTest { static class InputComponentStoreTester extends InputComponentStore { InputComponentStoreTester() throws IOException { - super(TestInputFileBuilder.newDefaultInputModule("root", temp.newFolder()), mock(AnalysisMode.class), mock(BranchConfiguration.class)); + super(TestInputFileBuilder.newDefaultInputModule("root", temp.newFolder()), mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); } InputFile addFile(String moduleKey, String relpath, String language) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java index 9856377a92c..a50f8de9572 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java @@ -24,13 +24,13 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.SensorStrategy; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.analysis.DefaultAnalysisMode; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; @@ -49,7 +49,7 @@ public class ModuleInputComponentStoreTest { @Before public void setUp() throws IOException { DefaultInputModule root = TestInputFileBuilder.newDefaultInputModule(moduleKey, temp.newFolder()); - componentStore = new InputComponentStore(root, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + componentStore = new InputComponentStore(root, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java index 4b005062ddb..1819252ae3c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java @@ -31,7 +31,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; @@ -46,10 +45,11 @@ import org.sonar.api.config.internal.MapSettings; import org.sonar.api.issue.Issue; import org.sonar.api.platform.Server; import org.sonar.api.rule.RuleKey; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.issue.IssueCache; import org.sonar.scanner.issue.tracking.TrackedIssue; -import org.sonar.scanner.scan.BranchConfiguration; import org.sonar.scanner.scan.DefaultComponentTree; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import static net.javacrumbs.jsonunit.assertj.JsonAssert.assertThatJson; @@ -84,7 +84,7 @@ public class JSONReportTest { DefaultComponentTree inputComponentTree = new DefaultComponentTree(); ProjectDefinition def = ProjectDefinition.create().setBaseDir(projectBaseDir).setWorkDir(temp.newFolder()).setKey("struts"); DefaultInputModule rootModule = new DefaultInputModule(def, 1); - InputComponentStore inputComponentStore = new InputComponentStore(rootModule, mock(AnalysisMode.class), mock(BranchConfiguration.class)); + InputComponentStore inputComponentStore = new InputComponentStore(rootModule, mock(DefaultAnalysisMode.class), mock(BranchConfiguration.class)); DefaultInputModule moduleA = new DefaultInputModule(ProjectDefinition.create().setKey("struts-core").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); inputComponentTree.index(moduleA, rootModule); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java index aa62800ee91..afe8a413d98 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java @@ -37,7 +37,7 @@ import org.sonar.api.config.internal.MapSettings; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.Version; -import org.sonar.scanner.scan.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java index 09a347f71dc..ab3295ac409 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java @@ -21,9 +21,9 @@ package org.sonar.scanner.source; import org.junit.Rule; import org.junit.Test; -import org.sonar.api.batch.AnalysisMode; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.scanner.analysis.DefaultAnalysisMode; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -36,9 +36,9 @@ public class ZeroCoverageSensorTest { @Test public void dontForceCoverageInIncrementalMode() { - AnalysisMode analysisMode = mock(AnalysisMode.class); - when(analysisMode.isIncremental()).thenReturn(true); - ZeroCoverageSensor zeroCoverageSensor = new ZeroCoverageSensor(null, analysisMode); + DefaultAnalysisMode analysisFlags = mock(DefaultAnalysisMode.class); + when(analysisFlags.isIncremental()).thenReturn(true); + ZeroCoverageSensor zeroCoverageSensor = new ZeroCoverageSensor(null, analysisFlags); zeroCoverageSensor.execute(null); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Incremental mode: not forcing coverage to zero"); } |