From 2205d5242d2d2ebb579f4a20c7c2dc1a17fbe1a8 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 5 Oct 2015 10:20:02 +0200 Subject: [PATCH] SONAR-6386 New modules should inherit parent settings --- .../src/test/java/batch/BatchTest.java | 24 +++++++++++- .../batch/repository/ProjectRepositories.java | 10 +++-- .../org/sonar/batch/scan/ModuleSettings.java | 13 +++++-- .../sonar/batch/scan/ModuleSettingsTest.java | 39 ++++++++++++++----- 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/it/it-tests/src/test/java/batch/BatchTest.java b/it/it-tests/src/test/java/batch/BatchTest.java index b7bc903fd5c..f64b9dd8b49 100644 --- a/it/it-tests/src/test/java/batch/BatchTest.java +++ b/it/it-tests/src/test/java/batch/BatchTest.java @@ -74,7 +74,7 @@ public class BatchTest { Resource branch = sonar.find(new ResourceQuery("com.sonarsource.it.samples:multi-modules-sample:branch/0.x")); assertThat(branch.getName()).isEqualTo("Sonar :: Integration Tests :: Multi-modules Sample branch/0.x"); } - + @Test public void use_sonar_profile_without_provisioning_project() { scan("shared/xoo-multi-modules-sample", @@ -116,7 +116,6 @@ public class BatchTest { @Test public void module_should_load_own_settings_from_database() { orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "Sonar :: Integration Tests :: Multi-modules Sample"); - orchestrator.getServer().associateProjectToQualityProfile("com.sonarsource.it.samples:multi-modules-sample", "xoo", "one-issue-per-line"); Sonar sonar = orchestrator.getServer().getAdminWsClient(); String propKey = "myFakeProperty"; @@ -147,6 +146,27 @@ public class BatchTest { assertThat(result.getLogs()).contains(moduleBKey + ":" + propKey + " = moduleB"); } + // SONAR-4680 + @Test + public void module_should_load_settings_from_parent() { + orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "Sonar :: Integration Tests :: Multi-modules Sample"); + + Sonar sonar = orchestrator.getServer().getAdminWsClient(); + String propKey = "myFakeProperty"; + String rootModuleKey = "com.sonarsource.it.samples:multi-modules-sample"; + String moduleBKey = rootModuleKey + ":module_b"; + + // Set property on provisionned project + sonar.update(new PropertyUpdateQuery(propKey, "project", rootModuleKey)); + sonar.delete(new PropertyDeleteQuery(propKey, moduleBKey)); + + BuildResult result = scan("shared/xoo-multi-modules-sample", "sonar.showSettings", propKey); + + assertThat(result.getLogs()).contains(rootModuleKey + ":" + propKey + " = project"); + // Module should inherit from parent + assertThat(result.getLogs()).contains(moduleBKey + ":" + propKey + " = project"); + } + /** * SONAR-3116 */ diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositories.java b/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositories.java index 4c12eb40886..2ce2759d7d4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositories.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositories.java @@ -21,12 +21,10 @@ package org.sonar.batch.repository; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - import java.util.Date; import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; public class ProjectRepositories { private final Table settingsByModule; @@ -61,6 +59,10 @@ public class ProjectRepositories { return fileDataByModuleAndPath; } + public boolean moduleExists(String moduleKey) { + return settingsByModule.containsRow(moduleKey); + } + public Map settings(String moduleKey) { return settingsByModule.row(moduleKey); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java index 96f085d76b2..d2719574e87 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java @@ -35,13 +35,13 @@ import org.sonar.batch.repository.ProjectRepositories; */ public class ModuleSettings extends Settings { - private final ProjectRepositories projectSettingsRepo; + private final ProjectRepositories projectRepos; private final DefaultAnalysisMode analysisMode; public ModuleSettings(GlobalSettings batchSettings, ProjectDefinition moduleDefinition, ProjectRepositories projectSettingsRepo, DefaultAnalysisMode analysisMode, AnalysisContextReportPublisher contextReportPublisher) { super(batchSettings.getDefinitions()); - this.projectSettingsRepo = projectSettingsRepo; + this.projectRepos = projectSettingsRepo; this.analysisMode = analysisMode; getEncryption().setPathToSecretKey(batchSettings.getString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); @@ -57,7 +57,14 @@ public class ModuleSettings extends Settings { private void addProjectProperties(ProjectDefinition moduleDefinition, GlobalSettings batchSettings) { addProperties(batchSettings.getProperties()); - addProperties(projectSettingsRepo.settings(moduleDefinition.getKeyWithBranch())); + ProjectDefinition def = moduleDefinition; + do { + if (projectRepos.moduleExists(def.getKeyWithBranch())) { + addProperties(projectRepos.settings(def.getKeyWithBranch())); + break; + } + def = def.getParent(); + } while (def != null); } private void addBuildProperties(ProjectDefinition project) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java index ab85aa3e5c7..1bbd3949539 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java @@ -19,16 +19,12 @@ */ package org.sonar.batch.scan; -import org.sonar.batch.repository.FileData; - import com.google.common.collect.HashBasedTable; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableTable; import com.google.common.collect.Table; - import java.util.List; import java.util.Map; - import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -39,7 +35,9 @@ import org.sonar.api.utils.MessageException; import org.sonar.batch.analysis.DefaultAnalysisMode; import org.sonar.batch.bootstrap.GlobalSettings; import org.sonar.batch.report.AnalysisContextReportPublisher; +import org.sonar.batch.repository.FileData; import org.sonar.batch.repository.ProjectRepositories; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -82,17 +80,17 @@ public class ModuleSettingsTest { @Test public void test_loading_of_module_settings() { - GlobalSettings batchSettings = mock(GlobalSettings.class); - when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); - when(batchSettings.getProperties()).thenReturn(ImmutableMap.of( + GlobalSettings globalSettings = mock(GlobalSettings.class); + when(globalSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); + when(globalSettings.getProperties()).thenReturn(ImmutableMap.of( "overridding", "batch", "on-batch", "true")); - ProjectRepositories projSettingsRepo = createSettings("struts-core", ImmutableMap.of("on-module", "true", "overridding", "module")); + ProjectRepositories projRepos = createSettings("struts-core", ImmutableMap.of("on-module", "true", "overridding", "module")); ProjectDefinition module = ProjectDefinition.create().setKey("struts-core"); - ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, projSettingsRepo, mode, mock(AnalysisContextReportPublisher.class)); + ModuleSettings moduleSettings = new ModuleSettings(globalSettings, module, projRepos, mode, mock(AnalysisContextReportPublisher.class)); assertThat(moduleSettings.getString("overridding")).isEqualTo("module"); assertThat(moduleSettings.getString("on-batch")).isEqualTo("true"); @@ -100,6 +98,27 @@ public class ModuleSettingsTest { } + // SONAR-6386 + @Test + public void test_loading_of_parent_module_settings_for_new_module() { + GlobalSettings globalSettings = mock(GlobalSettings.class); + when(globalSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); + when(globalSettings.getProperties()).thenReturn(ImmutableMap.of( + "overridding", "batch", + "on-batch", "true")); + + ProjectRepositories projRepos = createSettings("struts", ImmutableMap.of("on-module", "true", "overridding", "module")); + + ProjectDefinition module = ProjectDefinition.create().setKey("struts-core"); + ProjectDefinition.create().setKey("struts").addSubProject(module); + + ModuleSettings moduleSettings = new ModuleSettings(globalSettings, module, projRepos, mode, mock(AnalysisContextReportPublisher.class)); + + assertThat(moduleSettings.getString("overridding")).isEqualTo("module"); + assertThat(moduleSettings.getString("on-batch")).isEqualTo("true"); + assertThat(moduleSettings.getString("on-module")).isEqualTo("true"); + } + @Test public void should_not_fail_when_accessing_secured_properties() { GlobalSettings batchSettings = mock(GlobalSettings.class); @@ -137,7 +156,7 @@ public class ModuleSettingsTest { thrown.expect(MessageException.class); thrown .expectMessage( - "Access to the secured property 'sonar.foo.secured' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); + "Access to the secured property 'sonar.foo.secured' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); moduleSettings.getString("sonar.foo.secured"); } } -- 2.39.5