--- /dev/null
+sonar.projectName=Sub-module A1
--- /dev/null
+package com.sonar.it.samples.modules.a1;
+
+public class HelloA1 {
+ private int i;
+ private HelloA1() {
+
+ }
+
+ public void hello() {
+ System.out.println("hello" + " xoo");
+ }
+
+ protected String getHello() {
+ return "hello";
+ }
+}
\ No newline at end of file
--- /dev/null
+ncloc:12
+classes:1
--- /dev/null
+sonar.projectName=Sub-module A2
--- /dev/null
+package com.sonar.it.samples.modules.a2;
+
+public class HelloA2 {
+
+ public HelloA2(int i) {
+ int j = i++;
+ }
+
+ public void hello() {
+ System.out.println("hello" + " xoo");
+ }
+
+ private String myMethod() {
+ if (foo == bar) {
+ return "hello";
+ } else {
+ throw new IllegalStateException();
+ }
+ }
+}
--- /dev/null
+ncloc:15
+classes:1
--- /dev/null
+sonar.projectKey=module_a
+sonar.projectName=Module A
+
+sonar.modules=module_a1,module_a2
--- /dev/null
+package com.sonar.it.samples.modules.b1;
+
+public class HelloB1 {
+ private int i;
+ private HelloB1() {
+
+ }
+
+ public void hello() {
+ System.out.println("hello" + " world");
+ }
+}
\ No newline at end of file
--- /dev/null
+ncloc:12
+classes:1
\ No newline at end of file
--- /dev/null
+sonar.projectName=Sub-module B2
--- /dev/null
+package com.sonar.it.samples.modules.b2;
+
+public class HelloB2 {
+ private int i;
+ private HelloB2() {
+
+ }
+
+ public void hello() {
+ System.out.println("hello" + " world");
+ }
+}
\ No newline at end of file
--- /dev/null
+ncloc:12
+classes:1
\ No newline at end of file
--- /dev/null
+sonar.projectKey=module_b
+
+sonar.modules=module_b1,module_b2
--- /dev/null
+# Root project information
+sonar.projectKey=com.sonarsource.it.samples:multi-modules-sample
+
+sonar.language=xoo
+
+# Some properties that will be inherited by the modules
+sonar.sources=src/main/xoo
+
+# List of the module identifiers
+sonar.modules=module_a,module_b
+++ /dev/null
-sonar.projectName=Sub-module A1
+++ /dev/null
-package com.sonar.it.samples.modules.a1;
-
-public class HelloA1 {
- private int i;
- private HelloA1() {
-
- }
-
- public void hello() {
- System.out.println("hello" + " xoo");
- }
-
- protected String getHello() {
- return "hello";
- }
-}
\ No newline at end of file
+++ /dev/null
-ncloc:12
-classes:1
+++ /dev/null
-sonar.projectName=Sub-module A2
+++ /dev/null
-package com.sonar.it.samples.modules.a2;
-
-public class HelloA2 {
-
- public HelloA2(int i) {
- int j = i++;
- }
-
- public void hello() {
- System.out.println("hello" + " xoo");
- }
-
- private String myMethod() {
- if (foo == bar) {
- return "hello";
- } else {
- throw new IllegalStateException();
- }
- }
-}
+++ /dev/null
-ncloc:15
-classes:1
+++ /dev/null
-sonar.projectKey=module_a
-sonar.projectName=Module A
-
-sonar.modules=module_a1,module_a2
+++ /dev/null
-package com.sonar.it.samples.modules.b1;
-
-public class HelloB1 {
- private int i;
- private HelloB1() {
-
- }
-
- public void hello() {
- System.out.println("hello" + " world");
- }
-}
\ No newline at end of file
+++ /dev/null
-ncloc:12
-classes:1
\ No newline at end of file
+++ /dev/null
-sonar.projectName=Sub-module B2
+++ /dev/null
-package com.sonar.it.samples.modules.b2;
-
-public class HelloB2 {
- private int i;
- private HelloB2() {
-
- }
-
- public void hello() {
- System.out.println("hello" + " world");
- }
-}
\ No newline at end of file
+++ /dev/null
-ncloc:12
-classes:1
\ No newline at end of file
+++ /dev/null
-sonar.projectKey=module_b
-
-sonar.modules=module_b1,module_b2
+++ /dev/null
-# Root project information
-sonar.projectKey=com.sonarsource.it.samples:multi-modules-sample
-sonar.projectVersion=1.0-SNAPSHOT
-
-sonar.language=xoo
-
-# Some properties that will be inherited by the modules
-sonar.sources=src/main/xoo
-
-# List of the module identifiers
-sonar.modules=module_a,module_b
}
@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
}
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
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);
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();
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) {
* 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.
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;
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 {