From 54f61474b336e943669021dc657e170d4d451995 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 5 Sep 2013 12:40:10 +0200 Subject: [PATCH] SONAR-4632 Fix loading of settings when using Sonar Runner 2.3 --- .../scan/DefaultProjectBootstrapper.java | 19 ++--- .../batch/scan/ProjectScanContainer.java | 85 ++++++++++--------- .../scan/DefaultProjectBootstrapperTest.java | 56 ++++++------ 3 files changed, 83 insertions(+), 77 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java b/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java index b141ff79ffe..910c5696cb8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java @@ -34,14 +34,13 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectBootstrapper; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.config.Settings; +import org.sonar.batch.bootstrap.BootstrapSettings; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -80,7 +79,7 @@ class DefaultProjectBootstrapper implements ProjectBootstrapper { PROPERTY_OLD_TESTS, PROPERTY_TESTS, PROPERTY_OLD_BINARIES, PROPERTY_BINARIES, PROPERTY_OLD_LIBRARIES, PROPERTY_LIBRARIES - ); + ); /** * @since 1.4 @@ -111,17 +110,17 @@ class DefaultProjectBootstrapper implements ProjectBootstrapper { */ private static final List NON_HERITED_PROPERTIES_FOR_CHILD = Lists.newArrayList(PROPERTY_PROJECT_BASEDIR, PROPERTY_MODULES, CoreProperties.PROJECT_DESCRIPTION_PROPERTY); - private Settings settings; + private BootstrapSettings settings; private File rootProjectWorkDir; - DefaultProjectBootstrapper(Settings settings) { + DefaultProjectBootstrapper(BootstrapSettings settings) { this.settings = settings; } @Override public ProjectReactor bootstrap() { Properties bootstrapProperties = new Properties(); - bootstrapProperties.putAll(settings.getProperties()); + bootstrapProperties.putAll(settings.properties()); ProjectDefinition rootProject = defineProject(bootstrapProperties, null); rootProjectWorkDir = rootProject.getWorkDir(); defineChildren(rootProject); @@ -145,14 +144,14 @@ class DefaultProjectBootstrapper implements ProjectBootstrapper { } ProjectDefinition definition = ProjectDefinition.create().setProperties(properties) - .setBaseDir(baseDir) - .setWorkDir(workDir); + .setBaseDir(baseDir) + .setWorkDir(workDir); return definition; } @VisibleForTesting protected File initRootProjectWorkDir(File baseDir) { - String workDir = settings.getString(PROPERTY_WORK_DIRECTORY); + String workDir = settings.property(PROPERTY_WORK_DIRECTORY); if (StringUtils.isBlank(workDir)) { return new File(baseDir, DEF_VALUE_WORK_DIRECTORY); } @@ -389,7 +388,7 @@ class DefaultProjectBootstrapper implements ProjectBootstrapper { if (sourceFolder.isDirectory()) { LOG.warn("/!\\ A multi-module project can't have source folders, so '{}' won't be used for the analysis. " + "If you want to analyse files of this folder, you should create another sub-module and move them inside it.", - sourceFolder.toString()); + sourceFolder.toString()); } } 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 234fe0f1813..9bc0c43c940 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 @@ -33,6 +33,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.BootstrapSettings; import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ExtensionMatcher; import org.sonar.batch.bootstrap.ExtensionUtils; @@ -98,7 +99,7 @@ public class ProjectScanContainer extends ComponentContainer { ProjectBootstrapper bootstrapper = getComponentByType(ProjectBootstrapper.class); if (bootstrapper == null) { // Use default SonarRunner project bootstrapper - Settings settings = getComponentByType(Settings.class); + BootstrapSettings settings = getComponentByType(BootstrapSettings.class); bootstrapper = new DefaultProjectBootstrapper(settings); } reactor = bootstrapper.bootstrap(); @@ -111,51 +112,51 @@ public class ProjectScanContainer extends ComponentContainer { private void addBatchComponents() { add( - DefaultResourceCreationLock.class, - DefaultPersistenceManager.class, - DependencyPersister.class, - EventPersister.class, - LinkPersister.class, - MeasurePersister.class, - MemoryOptimizer.class, - DefaultResourcePersister.class, - SourcePersister.class, - DefaultNotificationManager.class, - MetricProvider.class, - ProjectConfigurator.class, - DefaultIndex.class, - DefaultFileLinesContextFactory.class, - ProjectLock.class, - LastSnapshots.class, - Caches.class, - SnapshotCache.class, - ResourceCache.class, - ComponentDataCache.class, - ComponentDataPersister.class, + DefaultResourceCreationLock.class, + DefaultPersistenceManager.class, + DependencyPersister.class, + EventPersister.class, + LinkPersister.class, + MeasurePersister.class, + MemoryOptimizer.class, + DefaultResourcePersister.class, + SourcePersister.class, + DefaultNotificationManager.class, + MetricProvider.class, + ProjectConfigurator.class, + DefaultIndex.class, + DefaultFileLinesContextFactory.class, + ProjectLock.class, + LastSnapshots.class, + Caches.class, + SnapshotCache.class, + ResourceCache.class, + ComponentDataCache.class, + ComponentDataPersister.class, - // issues - IssueUpdater.class, - FunctionExecutor.class, - IssueWorkflow.class, - DeprecatedViolations.class, - IssueCache.class, - ScanIssueStorage.class, - IssuePersister.class, - IssueNotifications.class, + // issues + IssueUpdater.class, + FunctionExecutor.class, + IssueWorkflow.class, + DeprecatedViolations.class, + IssueCache.class, + ScanIssueStorage.class, + IssuePersister.class, + IssueNotifications.class, - // tests - TestPlanPerspectiveLoader.class, - TestablePerspectiveLoader.class, - TestPlanBuilder.class, - TestableBuilder.class, - ScanGraph.create(), - GraphPersister.class, + // tests + TestPlanPerspectiveLoader.class, + TestablePerspectiveLoader.class, + TestPlanBuilder.class, + TestableBuilder.class, + ScanGraph.create(), + GraphPersister.class, - // lang - HighlightableBuilder.class, - SymbolizableBuilder.class, + // lang + HighlightableBuilder.class, + SymbolizableBuilder.class, - ProjectSettingsReady.class); + ProjectSettingsReady.class); } private void fixMavenExecutor() { diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/DefaultProjectBootstrapperTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/DefaultProjectBootstrapperTest.java index 45681f502db..f42129fdc22 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/DefaultProjectBootstrapperTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/DefaultProjectBootstrapperTest.java @@ -19,13 +19,15 @@ */ package org.sonar.batch.scan; +import com.google.common.collect.Maps; import org.apache.commons.io.IOUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.config.Settings; +import org.sonar.batch.bootstrap.BootstrapProperties; +import org.sonar.batch.bootstrap.BootstrapSettings; import org.sonar.test.TestUtils; import java.io.File; @@ -53,7 +55,7 @@ public class DefaultProjectBootstrapperTest { assertThat(projectDefinition.getDescription()).isEqualTo("Description of Foo Project"); assertThat(projectDefinition.getSourceDirs()).contains("sources"); assertThat(projectDefinition.getLibraries()).contains(TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath(), - TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath()); + TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath()); } @Test @@ -62,8 +64,8 @@ public class DefaultProjectBootstrapperTest { assertThat(projectDefinition.getSourceDirs()).contains("sources"); assertThat(projectDefinition.getLibraries()).contains( - TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath(), - TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath()); + TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath(), + TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath()); } @Test @@ -106,9 +108,9 @@ public class DefaultProjectBootstrapperTest { assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(rootProject.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root")); + .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root")); assertThat(rootProject.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar")); + .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar")); // CHECK MODULES List modules = rootProject.getSubProjects(); @@ -130,9 +132,9 @@ public class DefaultProjectBootstrapperTest { assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(module1.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1")); + .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1")); assertThat(module1.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_module1")); + .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_module1")); // Module 2 ProjectDefinition module2 = modules.get(1); @@ -149,9 +151,9 @@ public class DefaultProjectBootstrapperTest { assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull(); // Check baseDir and workDir assertThat(module2.getBaseDir().getCanonicalFile()) - .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2")); + .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2")); assertThat(module2.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_com.foo.project.module2")); + .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_com.foo.project.module2")); } @Test @@ -276,7 +278,7 @@ public class DefaultProjectBootstrapperTest { assertThat(module1.getSourceDirs()).contains("src/main/java"); // and module properties must have been cleaned assertThat(module1.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_java-module")); + .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_java-module")); // Module 2 ProjectDefinition module2 = modules.get(1); @@ -284,7 +286,7 @@ public class DefaultProjectBootstrapperTest { assertThat(module2.getSourceDirs()).contains("src/main/groovy"); // and module properties must have been cleaned assertThat(module2.getWorkDir().getCanonicalFile()) - .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_groovy-module")); + .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_groovy-module")); } @Test @@ -489,7 +491,7 @@ public class DefaultProjectBootstrapperTest { @Test public void shouldGetRelativeFile() { assertThat(DefaultProjectBootstrapper.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/"))) - .isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties")); + .isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties")); } @Test @@ -497,7 +499,7 @@ public class DefaultProjectBootstrapperTest { File file = TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties"); assertThat(DefaultProjectBootstrapper.getFileFromPath(file.getAbsolutePath(), TestUtils.getResource(this.getClass(), "/"))) - .isEqualTo(file); + .isEqualTo(file); } @Test @@ -528,7 +530,7 @@ public class DefaultProjectBootstrapperTest { @Test public void shouldInitRootWorkDir() { - DefaultProjectBootstrapper builder = new DefaultProjectBootstrapper(new Settings()); + DefaultProjectBootstrapper builder = new DefaultProjectBootstrapper(new BootstrapSettings(new BootstrapProperties(Maps. newHashMap()))); File baseDir = new File("target/tmp/baseDir"); File workDir = builder.initRootProjectWorkDir(baseDir); @@ -538,9 +540,9 @@ public class DefaultProjectBootstrapperTest { @Test public void shouldInitWorkDirWithCustomRelativeFolder() { - Settings settings = new Settings(); - settings.setProperty("sonar.working.directory", ".foo"); - DefaultProjectBootstrapper builder = new DefaultProjectBootstrapper(settings); + Map props = Maps. newHashMap(); + props.put("sonar.working.directory", ".foo"); + DefaultProjectBootstrapper builder = new DefaultProjectBootstrapper(new BootstrapSettings(new BootstrapProperties(props))); File baseDir = new File("target/tmp/baseDir"); File workDir = builder.initRootProjectWorkDir(baseDir); @@ -550,9 +552,9 @@ public class DefaultProjectBootstrapperTest { @Test public void shouldInitRootWorkDirWithCustomAbsoluteFolder() { - Settings settings = new Settings(); - settings.setProperty("sonar.working.directory", new File("src").getAbsolutePath()); - DefaultProjectBootstrapper builder = new DefaultProjectBootstrapper(settings); + Map props = Maps. newHashMap(); + props.put("sonar.working.directory", new File("src").getAbsolutePath()); + DefaultProjectBootstrapper builder = new DefaultProjectBootstrapper(new BootstrapSettings(new BootstrapProperties(props))); File baseDir = new File("target/tmp/baseDir"); File workDir = builder.initRootProjectWorkDir(baseDir); @@ -619,10 +621,14 @@ public class DefaultProjectBootstrapperTest { } private ProjectDefinition loadProjectDefinition(String projectFolder) throws IOException { - Settings settings = new Settings(); - settings.addProperties(DefaultProjectBootstrapper.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"))); - settings.setProperty("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath()); - ProjectReactor projectReactor = new DefaultProjectBootstrapper(settings).bootstrap(); + Map props = Maps. newHashMap(); + Properties runnerProps = DefaultProjectBootstrapper.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties")); + for (final String name : runnerProps.stringPropertyNames()) { + props.put(name, runnerProps.getProperty(name)); + } + props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath()); + BootstrapProperties bootstrapProps = new BootstrapProperties(props); + ProjectReactor projectReactor = new DefaultProjectBootstrapper(new BootstrapSettings(bootstrapProps)).bootstrap(); return projectReactor.getRoot(); } -- 2.39.5