]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-2202 "sonar.projectName" default value is now module ID
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 13 Sep 2012 14:07:23 +0000 (16:07 +0200)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 13 Sep 2012 16:06:47 +0000 (18:06 +0200)
src/main/java/org/sonar/runner/internal/batch/SonarProjectBuilder.java
src/test/java/org/sonar/runner/internal/batch/SonarProjectBuilderTest.java
src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-all-in-root/sonar-project.properties
src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-in-each-module/module1/sonar-project.properties

index e4cd483b088c05c0a1c8edb358314f8615160495..93a5d81eef55e2352204edf32d75edc320e11fd7 100644 (file)
@@ -124,7 +124,7 @@ public final class SonarProjectBuilder {
   }
 
   private ProjectDefinition defineProject(Properties properties) {
-    checkMandatoryProperties("root project", properties, MANDATORY_PROPERTIES_FOR_PROJECT);
+    checkMandatoryProperties(properties, MANDATORY_PROPERTIES_FOR_PROJECT);
     File baseDir = new File(properties.getProperty(PROPERTY_PROJECT_BASEDIR));
     ProjectDefinition definition = ProjectDefinition.create((Properties) properties.clone())
         .setBaseDir(baseDir)
@@ -163,7 +163,7 @@ public final class SonarProjectBuilder {
   }
 
   private ProjectDefinition loadChildProject(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
-    setProjectKeyIfNotDefined(moduleProps, moduleId);
+    setProjectKeyAndNameIfNotDefined(moduleProps, moduleId);
 
     if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
       File baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), parentProject.getBaseDir());
@@ -178,7 +178,7 @@ public final class SonarProjectBuilder {
     }
 
     // and finish
-    checkMandatoryProperties(moduleId, moduleProps, MANDATORY_PROPERTIES_FOR_CHILD);
+    checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD);
     mergeParentProperties(moduleProps, parentProject.getProperties());
 
     prefixProjectKeyWithParentKey(moduleProps, parentProject.getKey());
@@ -235,10 +235,13 @@ public final class SonarProjectBuilder {
   }
 
   @VisibleForTesting
-  protected static void setProjectKeyIfNotDefined(Properties childProps, String moduleId) {
+  protected static void setProjectKeyAndNameIfNotDefined(Properties childProps, String moduleId) {
     if (!childProps.containsKey(PROPERTY_PROJECT_KEY)) {
       childProps.put(PROPERTY_PROJECT_KEY, moduleId);
     }
+    if (!childProps.containsKey(PROPERTY_PROJECT_NAME)) {
+      childProps.put(PROPERTY_PROJECT_NAME, moduleId);
+    }
   }
 
   @VisibleForTesting
@@ -264,7 +267,7 @@ public final class SonarProjectBuilder {
   }
 
   @VisibleForTesting
-  protected static void checkMandatoryProperties(String moduleId, Properties props, String[] mandatoryProps) {
+  protected static void checkMandatoryProperties(Properties props, String[] mandatoryProps) {
     replaceDeprecatedProperties(props);
     StringBuilder missing = new StringBuilder();
     for (String mandatoryProperty : mandatoryProps) {
@@ -277,7 +280,7 @@ public final class SonarProjectBuilder {
     }
     if (missing.length() != 0) {
       String projectKey = props.getProperty(PROPERTY_PROJECT_KEY);
-      throw new RunnerException("You must define the following mandatory properties for '" + (projectKey == null ? moduleId : projectKey) + "': " + missing);
+      throw new RunnerException("You must define the following mandatory properties for '" + (projectKey == null ? "Unknown" : projectKey) + "': " + missing);
     }
   }
 
index aa8209fb769b377983817888bf6aaeef8980f892..d7a216402f98d4040ca5b2d32ea317243f34be03 100644 (file)
@@ -99,8 +99,8 @@ public class SonarProjectBuilderTest {
     // Module 1
     ProjectDefinition module1 = modules.get(0);
     assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1"));
-    assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
-    assertThat(module1.getName()).isEqualTo("Foo Module 1");
+    assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
+    assertThat(module1.getName()).isEqualTo("module1");
     assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
     // Description should not be inherited from parent if not set
     assertThat(module1.getDescription()).isNull();
@@ -150,8 +150,8 @@ public class SonarProjectBuilderTest {
     // Module 1
     ProjectDefinition module1 = modules.get(0);
     assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module1"));
-    assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
-    assertThat(module1.getName()).isEqualTo("Foo Module 1");
+    assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
+    assertThat(module1.getName()).isEqualTo("module1");
     assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
     // Description should not be inherited from parent if not set
     assertThat(module1.getDescription()).isNull();
@@ -255,9 +255,21 @@ public class SonarProjectBuilderTest {
     props.setProperty("foo4", "bla");
 
     thrown.expect(RunnerException.class);
-    thrown.expectMessage("You must define the following mandatory properties for 'foo': foo2, foo3");
+    thrown.expectMessage("You must define the following mandatory properties for 'Unknown': foo2, foo3");
 
-    SonarProjectBuilder.checkMandatoryProperties("foo", props, new String[] {"foo1", "foo2", "foo3"});
+    SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
+  }
+
+  @Test
+  public void shouldFailIfMandatoryPropertiesAreNotPresentButWithProjectKey() {
+    Properties props = new Properties();
+    props.setProperty("foo1", "bla");
+    props.setProperty("sonar.projectKey", "my-project");
+
+    thrown.expect(RunnerException.class);
+    thrown.expectMessage("You must define the following mandatory properties for 'my-project': foo2, foo3");
+
+    SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
   }
 
   @Test
@@ -266,7 +278,7 @@ public class SonarProjectBuilderTest {
     props.setProperty("foo1", "bla");
     props.setProperty("foo4", "bla");
 
-    SonarProjectBuilder.checkMandatoryProperties("foo", props, new String[] {"foo1"});
+    SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1"});
 
     // No exception should be thrown
   }
@@ -411,12 +423,14 @@ public class SonarProjectBuilderTest {
     props.put("sonar.projectVersion", "1.0");
 
     // should be set
-    SonarProjectBuilder.setProjectKeyIfNotDefined(props, "foo");
+    SonarProjectBuilder.setProjectKeyAndNameIfNotDefined(props, "foo");
     assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
+    assertThat(props.getProperty("sonar.projectName")).isEqualTo("foo");
 
     // but not this 2nd time
-    SonarProjectBuilder.setProjectKeyIfNotDefined(props, "bar");
+    SonarProjectBuilder.setProjectKeyAndNameIfNotDefined(props, "bar");
     assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
+    assertThat(props.getProperty("sonar.projectName")).isEqualTo("foo");
   }
 
   @Test
index 4e299262e5d98b89cc8036326b329720afff7b0c..0f06d31466e7ea796cecb5e6c7dea27e545fe0d7 100644 (file)
@@ -10,8 +10,7 @@ sonar.binaries=target/classes
 sonar.modules=module1,\
               module2
 
-module1.sonar.projectKey=com.foo.project.module1
-module1.sonar.projectName=Foo Module 1
+# Mandatory properties for module1 are all inferred from the module ID
 
 module2.sonar.projectKey=com.foo.project.module2
 module2.sonar.projectName=Foo Module 2