]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 5 Jan 2015 09:35:47 +0000 (10:35 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 5 Jan 2015 09:42:52 +0000 (10:42 +0100)
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/resource/ReportComponent.java
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java
sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java

index 578cf92d73deee752157f207bcc03e197f616d11..e9ff00c57ace9d629224008e0736baa215b124b5 100644 (file)
@@ -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;
   }
index c111c485e497f100c4ae55a34cde1d77c3352a56..e05a5a59519438deb002fd0a12d5af165831012a 100644 (file)
@@ -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()));
index 12058267e874c8d68b8fef2e22b4d44cfed22445..8e617830527a5221c6476eb50d21d981f08441b4 100644 (file)
@@ -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();
index 05efe6cf0ae952f19e71f4b83eda9acfdf32e69b..f1e8458f75706817553305dda9e831717c0bdf3b 100644 (file)
@@ -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");