From e87e06f2789cc3c42c30de31f62084552f6295d3 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 5 Jan 2015 10:35:47 +0100 Subject: [PATCH] Fix some quality flaws --- .../output/resource/ReportComponent.java | 15 ++++++++++++--- .../org/sonar/batch/index/DefaultIndex.java | 2 +- .../batch/report/ComponentsPublisher.java | 2 ++ .../sonar/batch/index/DefaultIndexTest.java | 18 +++++++++++++++++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/resource/ReportComponent.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/resource/ReportComponent.java index 578cf92d73d..e9ff00c57ac 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/resource/ReportComponent.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/resource/ReportComponent.java @@ -20,6 +20,7 @@ package org.sonar.batch.protocol.output.resource; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collection; @@ -83,11 +84,15 @@ public class ReportComponent { return path; } - public ReportComponent setName(String name) { + public ReportComponent setName(@Nullable String name) { this.name = name; return this; } + /** + * @return null for files and directories since it is the same as the path + */ + @CheckForNull public String name() { return name; } @@ -101,7 +106,7 @@ public class ReportComponent { return type; } - public ReportComponent setTest(Boolean isTest) { + public ReportComponent setTest(@Nullable Boolean isTest) { this.isTest = isTest; return this; } @@ -114,11 +119,15 @@ public class ReportComponent { return isTest; } - public ReportComponent setLanguageKey(String languageKey) { + public ReportComponent setLanguageKey(@Nullable String languageKey) { this.languageKey = languageKey; return this; } + /** + * @return null when not a file + */ + @CheckForNull public String languageKey() { return languageKey; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index c111c485e49..e05a5a59519 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -497,7 +497,7 @@ public class DefaultIndex extends SonarIndex { Resource resource = getResource(reference); if (resource instanceof File) { File file = (File) resource; - Project module = (Project) file.getParent().getParent(); + Project module = currentProject; ProjectDefinition def = projectTree.getProjectDefinition(module); try { return FileUtils.readFileToString(new java.io.File(def.getBaseDir(), file.getPath())); diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java index 12058267e87..8e617830527 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java @@ -72,6 +72,7 @@ public class ComponentsPublisher implements ReportPublisher { return result; } + @CheckForNull private Boolean isTest(Resource r) { return ResourceUtils.isFile(r) ? ResourceUtils.isUnitTestClass(r) : null; } @@ -82,6 +83,7 @@ public class ComponentsPublisher implements ReportPublisher { return ResourceUtils.isFile(r) && language != null ? language.getKey() : null; } + @CheckForNull private String getName(Resource r) { // Don't return name for directories and files since it can be guessed from the path return (ResourceUtils.isFile(r) || ResourceUtils.isDirectory(r)) ? null : r.getName(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java index 05efe6cf0ae..f1e8458f757 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java @@ -19,6 +19,7 @@ */ package org.sonar.batch.index; +import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -69,6 +70,8 @@ public class DefaultIndexTest { Project moduleB; Project moduleB1; + private java.io.File baseDir; + @Before public void createIndex() throws IOException { deprecatedViolations = mock(DeprecatedViolations.class); @@ -82,7 +85,7 @@ public class DefaultIndexTest { mock(ResourceKeyMigration.class), mock(MeasureCache.class)); - java.io.File baseDir = temp.newFolder(); + baseDir = temp.newFolder(); project = new Project("project"); when(projectTree.getProjectDefinition(project)).thenReturn(ProjectDefinition.create().setBaseDir(baseDir)); moduleA = new Project("moduleA").setParent(project); @@ -134,6 +137,19 @@ public class DefaultIndexTest { assertThat(index.getParent(fileRef)).isInstanceOf(Directory.class); } + @Test + public void shouldGetSource() throws Exception { + Directory directory = Directory.create("src/org/foo", "org/foo"); + File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", Java.INSTANCE, false); + FileUtils.write(new java.io.File(baseDir, "src/org/foo/Bar.java"), "Foo bar"); + + assertThat(index.index(directory)).isTrue(); + assertThat(index.index(file, directory)).isTrue(); + + File fileRef = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false); + assertThat(index.getSource(fileRef)).isEqualTo("Foo bar"); + } + @Test public void shouldIndexLibraryOutsideProjectTree() { Library lib = new Library("junit", "4.8"); -- 2.39.5