]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-2202 sonar.projectKey not mandatory on a module def.
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Mon, 10 Sep 2012 17:04:52 +0000 (19:04 +0200)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Tue, 11 Sep 2012 07:18:23 +0000 (09:18 +0200)
=> it is automatically generated from the module Id found in
   "sonar.modules"

src/main/java/org/sonar/runner/internal/Main.java
src/main/java/org/sonar/runner/internal/batch/SonarProjectBuilder.java
src/test/java/org/sonar/runner/internal/batch/SonarProjectBuilderTest.java

index 61d70efc09c188950160e0f89c935f335103854f..8b7566b7cd23ab183563c9d96fa2728410e41f6f 100644 (file)
 
 package org.sonar.runner.internal;
 
-import org.sonar.runner.internal.bootstrapper.BootstrapException;
-
 import org.sonar.runner.Runner;
 import org.sonar.runner.RunnerException;
-
+import org.sonar.runner.internal.bootstrapper.BootstrapException;
 import org.sonar.runner.internal.bootstrapper.utils.PrivateIOUtils;
-
 import org.sonar.runner.utils.SonarRunnerVersion;
 
 import java.io.File;
@@ -120,7 +117,7 @@ public final class Main {
     result.putAll(loadProjectProperties(commandLineProps));
     result.putAll(commandLineProps);
 
-    if (result.contains(PROJECT_HOME)) {
+    if (result.containsKey(PROJECT_HOME)) {
       // the real property of the Sonar Runner is "sonar.projectDir"
       String baseDir = result.getProperty(PROJECT_HOME);
       result.remove(PROJECT_HOME);
index e3b5c193846d7c197a222d7ef089526f4eb91ae1..efbb3d2b6fed23dfdc0be01ec62bea1883735c4c 100644 (file)
@@ -169,6 +169,7 @@ public final class SonarProjectBuilder {
   }
 
   private ProjectDefinition loadChildProjectFromProperties(ProjectDefinition parentProject, Properties childProps, String moduleId) {
+    setProjectKeyIfNotDefined(childProps, moduleId);
     checkMandatoryProperties(moduleId, childProps, MANDATORY_PROPERTIES_FOR_CHILD);
     mergeParentProperties(childProps, parentProject.getProperties());
 
@@ -215,6 +216,13 @@ public final class SonarProjectBuilder {
     return defineProject(childProps);
   }
 
+  @VisibleForTesting
+  protected static void setProjectKeyIfNotDefined(Properties childProps, String moduleId) {
+    if (!childProps.containsKey(PROPERTY_PROJECT_KEY)) {
+      childProps.put(PROPERTY_PROJECT_KEY, moduleId);
+    }
+  }
+
   @VisibleForTesting
   protected static void checkUnicityOfChildKey(ProjectDefinition childProject, ProjectDefinition parentProject) {
     for (ProjectDefinition definition : parentProject.getSubProjects()) {
index b0d3d8f707c1b17b68761fa7e01eb3f8d82d9660..2e7d5968c47d91efdaab4c66d547a89b0b57c3fa 100644 (file)
@@ -363,4 +363,18 @@ public class SonarProjectBuilderTest {
     SonarProjectBuilder.checkUnicityOfChildKey(mod2, root);
   }
 
+  @Test
+  public void shouldSetProjectKeyIfNotPresent() {
+    Properties props = new Properties();
+    props.put("sonar.projectVersion", "1.0");
+
+    // should be set
+    SonarProjectBuilder.setProjectKeyIfNotDefined(props, "foo");
+    assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
+
+    // but not this 2nd time
+    SonarProjectBuilder.setProjectKeyIfNotDefined(props, "bar");
+    assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
+  }
+
 }