From b59d941f342c5d81cf04eb0f172f393f03180daf Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 30 Aug 2016 14:42:49 +0200 Subject: [PATCH] SONAR-7258 The project version should be optional --- .../module_a1/sonar-project.properties | 0 .../sonar/it/samples/modules/a1/HelloA1.xoo | 0 .../samples/modules/a1/HelloA1.xoo.measures | 0 .../module_a2/sonar-project.properties | 0 .../sonar/it/samples/modules/a2/HelloA2.xoo | 0 .../samples/modules/a2/HelloA2.xoo.measures | 0 .../module_a/sonar-project.properties | 0 .../sonar/it/samples/modules/b1/HelloB1.xoo | 0 .../samples/modules/b1/HelloB1.xoo.measures | 0 .../module_b2/sonar-project.properties | 0 .../sonar/it/samples/modules/b2/HelloB2.xoo | 0 .../samples/modules/b2/HelloB2.xoo.measures | 0 .../module_b/sonar-project.properties | 0 .../sonar-project.properties | 1 - .../src/test/java/it/analysis/BatchTest.java | 53 +++++++------- .../batch/bootstrap/ProjectDefinition.java | 11 ++- .../batch/bootstrap/ProjectBuilderTest.java | 2 +- .../bootstrap/ProjectDefinitionTest.java | 8 +++ .../scanner/report/ComponentsPublisher.java | 12 +++- .../scanner/scan/ProjectReactorBuilder.java | 6 +- .../report/ComponentsPublisherTest.java | 69 +++++++++++++++++++ 21 files changed, 128 insertions(+), 34 deletions(-) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_a/module_a1/sonar-project.properties (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo.measures (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_a/module_a2/sonar-project.properties (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo.measures (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_a/sonar-project.properties (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo.measures (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_b/module_b2/sonar-project.properties (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo.measures (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/module_b/sonar-project.properties (100%) rename it/it-projects/shared/{xoo-multi-module-sample-without-project-name => xoo-multi-module-sample-without-project-name-version}/sonar-project.properties (88%) diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a1/sonar-project.properties b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a1/sonar-project.properties similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a1/sonar-project.properties rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a1/sonar-project.properties diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo.measures b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo.measures similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo.measures rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo.measures diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a2/sonar-project.properties b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a2/sonar-project.properties similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a2/sonar-project.properties rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a2/sonar-project.properties diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo.measures b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo.measures similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo.measures rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo.measures diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/sonar-project.properties b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/sonar-project.properties similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_a/sonar-project.properties rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_a/sonar-project.properties diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo.measures b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo.measures similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo.measures rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo.measures diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b2/sonar-project.properties b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b2/sonar-project.properties similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b2/sonar-project.properties rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b2/sonar-project.properties diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo.measures b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo.measures similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo.measures rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo.measures diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/sonar-project.properties b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/sonar-project.properties similarity index 100% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/module_b/sonar-project.properties rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/module_b/sonar-project.properties diff --git a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/sonar-project.properties b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/sonar-project.properties similarity index 88% rename from it/it-projects/shared/xoo-multi-module-sample-without-project-name/sonar-project.properties rename to it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/sonar-project.properties index 9c22998b8da..074304998f5 100644 --- a/it/it-projects/shared/xoo-multi-module-sample-without-project-name/sonar-project.properties +++ b/it/it-projects/shared/xoo-multi-module-sample-without-project-name-version/sonar-project.properties @@ -1,6 +1,5 @@ # Root project information sonar.projectKey=com.sonarsource.it.samples:multi-modules-sample -sonar.projectVersion=1.0-SNAPSHOT sonar.language=xoo diff --git a/it/it-tests/src/test/java/it/analysis/BatchTest.java b/it/it-tests/src/test/java/it/analysis/BatchTest.java index 62c5a4b6ab8..194c2b74f07 100644 --- a/it/it-tests/src/test/java/it/analysis/BatchTest.java +++ b/it/it-tests/src/test/java/it/analysis/BatchTest.java @@ -193,45 +193,50 @@ public class BatchTest { } @Test - public void should_create_project_without_name() { + public void should_create_project_without_name_version() { //some of the sub-modules have a name defined, others don't - BuildResult buildResult = scan("shared/xoo-multi-module-sample-without-project-name"); + BuildResult buildResult = scan("shared/xoo-multi-module-sample-without-project-name-version"); assertThat(buildResult.isSuccess()).isTrue(); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample", "com.sonarsource.it.samples:multi-modules-sample"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample", "com.sonarsource.it.samples:multi-modules-sample", "not provided"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_b", "module_b"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1", "module_b1"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b2", "Sub-module B2"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b", "module_b", "not provided"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1", "module_b1", "not provided"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b2", "Sub-module B2", "not provided"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_a", "Module A"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1", "Sub-module A1"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a2", "Sub-module A2"); - + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a", "Module A", "not provided"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1", "Sub-module A1", "not provided"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a2", "Sub-module A2", "not provided"); } @Test - public void should_analyze_project_without_name() { + public void should_analyze_project_without_name_version() { orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "My project name"); - BuildResult buildResult = scan("shared/xoo-multi-module-sample-without-project-name"); + BuildResult buildResult = scan("shared/xoo-multi-module-sample-without-project-name-version", + "sonar.projectName", "My project name", + "sonar.projectVersion", "1.0"); + assertThat(buildResult.isSuccess()).isTrue(); + + buildResult = scan("shared/xoo-multi-module-sample-without-project-name-version"); assertThat(buildResult.isSuccess()).isTrue(); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample", "My project name"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample", "My project name", "1.0"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_b", "module_b"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1", "module_b1"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b2", "Sub-module B2"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b", "module_b", "1.0"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1", "module_b1", "1.0"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b2", "Sub-module B2", "1.0"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_a", "Module A"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1", "Sub-module A1"); - assertProjectName("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a2", "Sub-module A2"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a", "Module A", "1.0"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1", "Sub-module A1", "1.0"); + assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a2", "Sub-module A2", "1.0"); } - private void assertProjectName(String projectKey, String expectedProjectName) { - ShowWsRequest req = new ShowWsRequest(); - req.setKey(projectKey); - ShowWsResponse response = ItUtils.newWsClient(orchestrator).components().show(req); - assertThat(response.getComponent().getName()).isEqualTo(expectedProjectName); + private void assertNameAndVersion(String projectKey, String expectedProjectName, String expectedProjectVersion) { + // new WS Client with api/components doesn't return the project version, so use the old one + Resource resource = orchestrator.getServer().getAdminWsClient().find(new ResourceQuery(projectKey)); + assertThat(resource.getName()).isEqualTo(expectedProjectName); + assertThat(resource.getVersion()).isEqualTo(expectedProjectVersion); + } @Test diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java index 33c23cd3f7e..95953da2422 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java @@ -186,9 +186,18 @@ public class ProjectDefinition { } return null; } + + @CheckForNull + public String getOriginalVersion() { + return properties.get(CoreProperties.PROJECT_VERSION_PROPERTY); + } public String getVersion() { - return properties.get(CoreProperties.PROJECT_VERSION_PROPERTY); + String version = properties.get(CoreProperties.PROJECT_VERSION_PROPERTY); + if (StringUtils.isBlank(version)) { + version = "not provided"; + } + return version; } @CheckForNull diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java index c3f2efde151..6fbf0bdfdc7 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java @@ -68,7 +68,7 @@ public class ProjectBuilderTest { subProject.setBaseDir(baseDir); subProject.setWorkDir(new File(baseDir, "target/.sonar")); subProject.setKey("groupId:parentProjectId"); - subProject.setVersion(root.getVersion()); + subProject.setVersion(root.getOriginalVersion()); subProject.setName("Sub Project"); subProject.setSources("src"); root.addSubProject(subProject); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java index 2db3cef3a86..9cee607615c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectDefinitionTest.java @@ -41,6 +41,14 @@ public class ProjectDefinitionTest { assertThat(def.getVersion()).isEqualTo("2.0-SNAPSHOT"); } + @Test + public void shouldSupportNoVersion() { + ProjectDefinition def = ProjectDefinition.create(); + def.setVersion(null); + assertThat(def.getVersion()).isEqualTo("not provided"); + assertThat(def.getOriginalVersion()).isEqualTo(""); + } + @Test public void shouldSetOptionalFields() { ProjectDefinition def = ProjectDefinition.create(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java index 77f36cc7bd7..1f5238b8a8d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java @@ -109,13 +109,19 @@ public class ComponentsPublisher implements ReportPublisherStep { if (c.isProjectOrModule()) { ProjectDefinition def = reactor.getProjectDefinition(c.key()); String version = getVersion(def); - builder.setVersion(version); + if(version != null) { + builder.setVersion(version); + } } } private static String getVersion(ProjectDefinition def) { - String version = def.getVersion(); - return StringUtils.isNotBlank(version) ? version : getVersion(def.getParent()); + String version = def.getOriginalVersion(); + if(StringUtils.isNotBlank(version)) { + return version; + } + + return def.getParent() != null ? getVersion(def.getParent()) : null; } private void writeLinks(BatchComponent c, ScannerReport.Component.Builder builder) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java index 19bd48ad076..f3da9c37976 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java @@ -89,15 +89,13 @@ public class ProjectReactorBuilder { * Array of all mandatory properties required for a project without child. */ private static final String[] MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT = { - PROPERTY_PROJECT_BASEDIR, CoreProperties.PROJECT_KEY_PROPERTY, - CoreProperties.PROJECT_VERSION_PROPERTY, PROPERTY_SOURCES + PROPERTY_PROJECT_BASEDIR, CoreProperties.PROJECT_KEY_PROPERTY, PROPERTY_SOURCES }; /** * Array of all mandatory properties required for a project with children. */ - private static final String[] MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT = {PROPERTY_PROJECT_BASEDIR, CoreProperties.PROJECT_KEY_PROPERTY, - CoreProperties.PROJECT_VERSION_PROPERTY}; + private static final String[] MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT = {PROPERTY_PROJECT_BASEDIR, CoreProperties.PROJECT_KEY_PROPERTY}; /** * Array of all mandatory properties required for a child project before its properties get merged with its parent ones. diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java index 7929b0ebe4f..0f6eb6dbdcc 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java @@ -20,6 +20,8 @@ package org.sonar.scanner.report; import java.io.File; +import java.io.IOException; + import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -116,6 +118,73 @@ public class ComponentsPublisherTest { assertThat(module1Protobuf.getDescription()).isEqualTo("Module description"); assertThat(module1Protobuf.getVersion()).isEqualTo("1.0"); } + + @Test + public void add_components_without_version_and_name() throws IOException { + ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo"); + Project root = new Project("foo").setDescription("Root description") + .setAnalysisDate(DateUtils.parseDate(("2012-12-12"))); + root.setId(1).setUuid("PROJECT_UUID"); + resourceCache.add(root, null).setInputComponent(new DefaultInputModule("foo")); + + Project module1 = new Project("module1").setDescription("Module description"); + module1.setParent(root); + module1.setId(2).setUuid("MODULE_UUID"); + resourceCache.add(module1, root).setInputComponent(new DefaultInputModule("module1")); + rootDef.addSubProject(ProjectDefinition.create().setKey("module1")); + + Directory dir = Directory.create("src"); + dir.setEffectiveKey("module1:src"); + dir.setId(3).setUuid("DIR_UUID"); + resourceCache.add(dir, module1).setInputComponent(new DefaultInputDir("foo", "src")); + + org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", FakeJava.INSTANCE, false); + file.setEffectiveKey("module1:src/Foo.java"); + file.setId(4).setUuid("FILE_UUID"); + resourceCache.add(file, dir).setInputComponent(new DefaultInputFile("module1", "src/Foo.java").setLines(2)); + + org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", null, false); + fileWithoutLang.setEffectiveKey("module1:src/make"); + fileWithoutLang.setId(5).setUuid("FILE_WITHOUT_LANG_UUID"); + resourceCache.add(fileWithoutLang, dir).setInputComponent(new DefaultInputFile("module1", "src/make").setLines(10)); + + org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", FakeJava.INSTANCE, true); + testFile.setEffectiveKey("module1:test/FooTest.java"); + testFile.setId(6).setUuid("TEST_FILE_UUID"); + resourceCache.add(testFile, dir).setInputComponent(new DefaultInputFile("module1", "test/FooTest.java").setLines(4)); + + ImmutableProjectReactor reactor = new ImmutableProjectReactor(rootDef); + + ComponentsPublisher publisher = new ComponentsPublisher(reactor, resourceCache); + + File outputDir = temp.newFolder(); + ScannerReportWriter writer = new ScannerReportWriter(outputDir); + publisher.publish(writer); + + assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); + assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue(); + assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue(); + assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue(); + assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue(); + assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue(); + + // no such reference + assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isFalse(); + + ScannerReportReader reader = new ScannerReportReader(outputDir); + Component rootProtobuf = reader.readComponent(1); + assertThat(rootProtobuf.getKey()).isEqualTo("foo"); + assertThat(rootProtobuf.getName()).isEqualTo(""); + assertThat(rootProtobuf.getDescription()).isEqualTo("Root description"); + assertThat(rootProtobuf.getVersion()).isEqualTo(""); + assertThat(rootProtobuf.getLinkCount()).isEqualTo(0); + + Component module1Protobuf = reader.readComponent(2); + assertThat(module1Protobuf.getKey()).isEqualTo("module1"); + assertThat(module1Protobuf.getName()).isEqualTo(""); + assertThat(module1Protobuf.getDescription()).isEqualTo("Module description"); + assertThat(module1Protobuf.getVersion()).isEqualTo(""); + } @Test public void add_components_with_links_and_branch() throws Exception { -- 2.39.5