]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4632 Fix loading of settings when using Sonar Runner 2.3
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 5 Sep 2013 10:40:10 +0000 (12:40 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 5 Sep 2013 10:40:10 +0000 (12:40 +0200)
sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
sonar-batch/src/test/java/org/sonar/batch/scan/DefaultProjectBootstrapperTest.java

index b141ff79ffebadca7e51797f618202f8fbae8755..910c5696cb82ffa14ffbd256cc4c01cd9ecf2e54 100644 (file)
@@ -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<String> 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());
       }
     }
 
index 234fe0f1813e3c0c9615e85d7e76673859845c53..9bc0c43c94047cf9d7290e42caa22ece85e11c14 100644 (file)
@@ -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() {
index 45681f502db7be1bc8ec08890f1bc87dbba0e8c4..f42129fdc22639af97254daa86c4773090a53e19 100644 (file)
  */
 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<ProjectDefinition> 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.<String, String> 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<String, String> props = Maps.<String, String> 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<String, String> props = Maps.<String, String> 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<String, String> props = Maps.<String, String> 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();
   }