diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-04-22 16:49:24 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-04-24 09:03:25 +0200 |
commit | e029fccb7e104fed53b1d9fb0f3dd3ac1b5f5e93 (patch) | |
tree | 88315190b3028470cfd5217437241915b12495e9 /sonar-batch | |
parent | 1bd188e2987cc110ada4c19c90e3e0e7f6d6083b (diff) | |
download | sonarqube-e029fccb7e104fed53b1d9fb0f3dd3ac1b5f5e93.tar.gz sonarqube-e029fccb7e104fed53b1d9fb0f3dd3ac1b5f5e93.zip |
SONAR-4221 Delay loading of project properties
to allow several consecutive executions of ScanContainer
Diffstat (limited to 'sonar-batch')
3 files changed, 53 insertions, 46 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java index b1f4ef3d693..72d038aa58d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java @@ -25,7 +25,7 @@ import org.apache.commons.lang.StringUtils; import org.json.simple.JSONValue; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; @@ -41,40 +41,41 @@ public class BatchSettings extends Settings { // module key -> <key,val> private Map<String, Map<String, String>> moduleProperties = Maps.newHashMap(); - public BatchSettings(BootstrapSettings bootstrapSettings, PropertyDefinitions propertyDefinitions, - ServerClient client, Configuration deprecatedConfiguration, BootstrapProperties globalProperties) { - this(bootstrapSettings, propertyDefinitions, client, deprecatedConfiguration, globalProperties, null); - } + private final BootstrapSettings bootstrapSettings; + private final ServerClient client; public BatchSettings(BootstrapSettings bootstrapSettings, PropertyDefinitions propertyDefinitions, - ServerClient client, Configuration deprecatedConfiguration, BootstrapProperties bootstrapProperties, - @Nullable ProjectReactor projectReactor) { + ServerClient client, Configuration deprecatedConfiguration) { super(propertyDefinitions); + this.bootstrapSettings = bootstrapSettings; + this.client = client; this.deprecatedConfiguration = deprecatedConfiguration; - init(bootstrapSettings, client, bootstrapProperties, projectReactor); + init(null); } - private void init(BootstrapSettings bootstrapSettings, ServerClient client, BootstrapProperties bootstrapProperties, @Nullable ProjectReactor reactor) { - LoggerFactory.getLogger(BatchSettings.class).info("Load batch settings"); + public void init(@Nullable ProjectDefinition rootProject) { + clear(); - if (reactor != null) { + if (rootProject != null) { + LoggerFactory.getLogger(BatchSettings.class).info("Load project settings"); String branch = bootstrapSettings.property(CoreProperties.PROJECT_BRANCH_PROPERTY); - String projectKey = reactor.getRoot().getKey(); + String projectKey = rootProject.getKey(); if (StringUtils.isNotBlank(branch)) { projectKey = String.format("%s:%s", projectKey, branch); } downloadSettings(client, projectKey); } else { + LoggerFactory.getLogger(BatchSettings.class).info("Load batch settings"); downloadSettings(client, null); } - // order is important -> bottom-up. The last one overrides all the others. - addProperties(bootstrapProperties.properties()); - if (reactor != null) { - addProperties(reactor.getRoot().getProperties()); + addProperties(bootstrapSettings.properties()); + // Reload reactor properties in case reactor has changed since bootstrap + if (rootProject != null) { + addProperties(rootProject.getProperties()); } - addEnvironmentVariables(); - addSystemProperties(); + properties.putAll(System.getenv()); + addProperties(System.getProperties()); } private void downloadSettings(ServerClient client, @Nullable String projectKey) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index bfcb3d57cb7..4da42dc8392 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -29,6 +29,7 @@ import org.sonar.batch.DefaultFileLinesContextFactory; import org.sonar.batch.DefaultResourceCreationLock; import org.sonar.batch.ProjectConfigurator; import org.sonar.batch.ProjectTree; +import org.sonar.batch.bootstrap.BatchSettings; import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ExtensionMatcher; import org.sonar.batch.bootstrap.ExtensionUtils; @@ -121,6 +122,8 @@ public class ProjectScanContainer extends ComponentContainer { @Override protected void doAfterStart() { ProjectTree tree = getComponentByType(ProjectTree.class); + BatchSettings settings = getComponentByType(BatchSettings.class); + settings.init(tree.getProjectDefinition(tree.getRootProject())); scanRecursively(tree.getRootProject()); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java index 2c87d50ec36..7ac5b3ba19b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java @@ -21,10 +21,8 @@ package org.sonar.batch.bootstrap; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; -import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.PropertyDefinitions; import java.util.Collections; @@ -36,64 +34,68 @@ import static org.mockito.Mockito.when; public class BatchSettingsTest { - private static final String JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}," + + private static final String JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}]"; + + private static final String REACTOR_JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}," + "{\"k\":\"sonar.java.coveragePlugin\",\"v\":\"jacoco\",\"p\":\"struts\"}," + "{\"k\":\"sonar.java.coveragePlugin\",\"v\":\"cobertura\",\"p\":\"struts-core\"}]"; ServerClient client = mock(ServerClient.class); ProjectDefinition project = ProjectDefinition.create().setKey("struts"); - ProjectReactor reactor = new ProjectReactor(project); Configuration deprecatedConf = new BaseConfiguration(); - BootstrapSettings bootstrapSettings = new BootstrapSettings(new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); - + BootstrapSettings bootstrapSettings = new BootstrapSettings(new BootstrapProperties(Collections.<String, String> emptyMap())); @Test public void should_load_system_props() { - when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); System.setProperty("BatchSettingsTest.testSystemProp", "system"); + // Reconstruct bootstrap settings to get system property + bootstrapSettings = new BootstrapSettings(new BootstrapProperties(Collections.<String, String> emptyMap())); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, - new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getString("BatchSettingsTest.testSystemProp")).isEqualTo("system"); } @Test public void should_load_project_props() { - when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); project.setProperty("project.prop", "project"); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, - new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); + batchSettings.init(project); assertThat(batchSettings.getString("project.prop")).isEqualTo("project"); } @Test public void should_load_global_settings() { - when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, - new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getBoolean("sonar.cpd.cross")).isTrue(); } @Test public void should_load_project_root_settings() { - when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, - new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); + batchSettings.init(project); assertThat(batchSettings.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco"); } @Test public void should_keep_module_settings_for_later() { - when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, - new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); + when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); + + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); + batchSettings.init(project); Map<String, String> moduleSettings = batchSettings.getModuleProperties("struts-core"); @@ -103,21 +105,22 @@ public class BatchSettingsTest { @Test public void system_props_should_override_build_props() { - when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); System.setProperty("BatchSettingsTest.testSystemProp", "system"); project.setProperty("BatchSettingsTest.testSystemProp", "build"); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, - new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getString("BatchSettingsTest.testSystemProp")).isEqualTo("system"); } @Test public void should_forward_to_deprecated_commons_configuration() { - when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, - new BootstrapProperties(Collections.<String, String>emptyMap()), reactor); + when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); + when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); + + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); + batchSettings.init(project); assertThat(deprecatedConf.getString("sonar.cpd.cross")).isEqualTo("true"); assertThat(deprecatedConf.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco"); @@ -134,7 +137,7 @@ public class BatchSettingsTest { @Test public void project_should_be_optional() { when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf, new BootstrapProperties(Collections.<String, String>emptyMap())); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getProperties()).isNotEmpty(); } } |