]> source.dussan.org Git - sonarqube.git/commitdiff
Fix issue when publishing components on Views
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Dec 2014 08:38:43 +0000 (09:38 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Dec 2014 08:38:43 +0000 (09:38 +0100)
sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java
sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java
sonar-batch/src/test/resources/org/sonar/batch/report/ComponentsPublisherTest/testComponentPublisher_containing_file_without_language.json [new file with mode: 0644]

index 85f5081d29f84fbdc9ae3674d150c28afbc7ada2..12058267e874c8d68b8fef2e22b4d44cfed22445 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.batch.report;
 
 import org.apache.commons.io.FileUtils;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.ResourceUtils;
@@ -29,6 +30,8 @@ import org.sonar.batch.index.ResourceCache;
 import org.sonar.batch.protocol.output.resource.ReportComponent;
 import org.sonar.batch.protocol.output.resource.ReportComponents;
 
+import javax.annotation.CheckForNull;
+
 import java.io.File;
 import java.io.IOException;
 
@@ -73,8 +76,10 @@ public class ComponentsPublisher implements ReportPublisher {
     return ResourceUtils.isFile(r) ? ResourceUtils.isUnitTestClass(r) : null;
   }
 
+  @CheckForNull
   private String getLanguageKey(Resource r) {
-    return ResourceUtils.isFile(r) ? r.getLanguage().getKey() : null;
+    Language language = r.getLanguage();
+    return ResourceUtils.isFile(r) && language != null ? language.getKey() : null;
   }
 
   private String getName(Resource r) {
index d5a700b9d8f4eebb2f488d51de13c63d498bfc06..28932c22148ffa9799528cc93b1d4e0b5d766af9 100644 (file)
@@ -28,9 +28,7 @@ import org.skyscreamer.jsonassert.JSONAssert;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.Project;
+import org.sonar.api.resources.*;
 import org.sonar.batch.index.ResourceCache;
 
 import java.io.File;
@@ -74,11 +72,82 @@ public class ComponentsPublisherTest {
     File exportDir = temp.newFolder();
     publisher.export(exportDir);
 
-    System.out.println(FileUtils.readFileToString(new File(exportDir, "components.json")));
-
     JSONAssert
       .assertEquals(
         IOUtils.toString(this.getClass().getResourceAsStream("ComponentsPublisherTest/expected.json"), "UTF-8"),
         FileUtils.readFileToString(new File(exportDir, "components.json")), true);
   }
+
+  @Test
+  public void testComponentPublisher_containing_file_without_language() throws Exception {
+    ProjectReactor reactor = new ProjectReactor(ProjectDefinition.create().setKey("ALL_PROJECT"));
+    ResourceCache resourceCache = new ResourceCache();
+    ComponentsPublisher publisher = new ComponentsPublisher(reactor, resourceCache);
+
+    View view = new View("ALL_PROJECT");
+    view.setId(1);
+    view.setAnalysisDate(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/2012"));
+    resourceCache.add(view, null, new Snapshot().setId(11));
+
+    org.sonar.api.resources.File mainFile = org.sonar.api.resources.File.create("ALL_PROJECTsample", "ALL_PROJECTsample", null, false);
+    mainFile.setEffectiveKey("ALL_PROJECTsample");
+    mainFile.setId(2);
+    resourceCache.add(mainFile, view, new Snapshot().setId(12));
+
+    File exportDir = temp.newFolder();
+    publisher.export(exportDir);
+
+    JSONAssert
+      .assertEquals(
+        IOUtils.toString(this.getClass().getResourceAsStream("ComponentsPublisherTest/testComponentPublisher_containing_file_without_language.json"), "UTF-8"),
+        FileUtils.readFileToString(new File(exportDir, "components.json")), true);
+  }
+
+  private static class View extends Project {
+
+    private View(String key) {
+      super(key);
+    }
+
+    @Override
+    public String getName() {
+      return "All Projects";
+    }
+
+    @Override
+    public String getLongName() {
+      return null;
+    }
+
+    @Override
+    public String getDescription() {
+      return null;
+    }
+
+    @Override
+    public Language getLanguage() {
+      return null;
+    }
+
+    @Override
+    public String getScope() {
+      return Scopes.PROJECT;
+    }
+
+    @Override
+    public String getQualifier() {
+      return Qualifiers.VIEW;
+    }
+
+    @Override
+    public Project getParent() {
+      return null;
+    }
+
+    @Override
+    public boolean matchFilePattern(String antPattern) {
+      return false;
+    }
+  }
+
 }
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/report/ComponentsPublisherTest/testComponentPublisher_containing_file_without_language.json b/sonar-batch/src/test/resources/org/sonar/batch/report/ComponentsPublisherTest/testComponentPublisher_containing_file_without_language.json
new file mode 100644 (file)
index 0000000..4c82576
--- /dev/null
@@ -0,0 +1,21 @@
+{
+  "analysisDate": "2012-12-12T00:00:00+0100",
+  "root": {
+    "batchId": 1,
+    "id": 1,
+    "snapshotId": 11,
+    "name": "All Projects",
+    "type": "VIEW",
+    "children": [
+      {
+        "batchId": 2,
+        "id": 2,
+        "snapshotId": 12,
+        "path": "ALL_PROJECTsample",
+        "type": "FIL",
+        "isTest": false,
+        "children": []
+      }
+    ]
+  }
+}