diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-08-30 14:42:49 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-08-31 11:30:25 +0200 |
commit | b59d941f342c5d81cf04eb0f172f393f03180daf (patch) | |
tree | 0624065e2262e7287c611a08eb3e9674b1249d87 /sonar-scanner-engine | |
parent | 7fb4681ba4ac8a7739896b150e3f86655e6ce849 (diff) | |
download | sonarqube-b59d941f342c5d81cf04eb0f172f393f03180daf.tar.gz sonarqube-b59d941f342c5d81cf04eb0f172f393f03180daf.zip |
SONAR-7258 The project version should be optional
Diffstat (limited to 'sonar-scanner-engine')
3 files changed, 80 insertions, 7 deletions
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 { |