From a3fd8f33a955deb6aac7485cd84104c471cee35f Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Thu, 13 Sep 2012 16:07:23 +0200 Subject: [PATCH] SONARPLUGINS-2202 "sonar.projectName" default value is now module ID --- .../internal/batch/SonarProjectBuilder.java | 15 +++++---- .../batch/SonarProjectBuilderTest.java | 32 +++++++++++++------ .../sonar-project.properties | 3 +- .../module1/sonar-project.properties | 3 +- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/sonar/runner/internal/batch/SonarProjectBuilder.java b/src/main/java/org/sonar/runner/internal/batch/SonarProjectBuilder.java index e4cd483..93a5d81 100644 --- a/src/main/java/org/sonar/runner/internal/batch/SonarProjectBuilder.java +++ b/src/main/java/org/sonar/runner/internal/batch/SonarProjectBuilder.java @@ -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); } } diff --git a/src/test/java/org/sonar/runner/internal/batch/SonarProjectBuilderTest.java b/src/test/java/org/sonar/runner/internal/batch/SonarProjectBuilderTest.java index aa8209f..d7a2164 100644 --- a/src/test/java/org/sonar/runner/internal/batch/SonarProjectBuilderTest.java +++ b/src/test/java/org/sonar/runner/internal/batch/SonarProjectBuilderTest.java @@ -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 diff --git a/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-all-in-root/sonar-project.properties b/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-all-in-root/sonar-project.properties index 4e29926..0f06d31 100644 --- a/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-all-in-root/sonar-project.properties +++ b/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-all-in-root/sonar-project.properties @@ -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 diff --git a/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-in-each-module/module1/sonar-project.properties b/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-in-each-module/module1/sonar-project.properties index 460d349..7bace22 100644 --- a/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-in-each-module/module1/sonar-project.properties +++ b/src/test/resources/org/sonar/runner/internal/batch/SonarProjectBuilderTest/multi-module-definitions-in-each-module/module1/sonar-project.properties @@ -1,2 +1 @@ -sonar.projectKey=com.foo.project.module1 -sonar.projectName=Foo Module 1 +# Mandatory properties for module1 are all inferred from the module ID -- 2.39.5