]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 5 Mar 2015 10:08:17 +0000 (11:08 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 5 Mar 2015 10:10:00 +0000 (11:10 +0100)
19 files changed:
server/sonar-server/src/main/java/org/sonar/server/activity/Activity.java
server/sonar-server/src/main/java/org/sonar/server/activity/index/ActivityDoc.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/ParseReportStep.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivityQuery.java
server/sonar-server/src/test/java/org/sonar/server/computation/ComputationServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/ParseReportStepTest.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchOutputWriter.java [deleted file]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java [new file with mode: 0644]
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputWriterTest.java [deleted file]
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java
sonar-batch/src/main/java/org/sonar/batch/report/IssuesPublisher.java
sonar-batch/src/main/java/org/sonar/batch/report/PublishReportJob.java
sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java
sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java
sonar-batch/src/test/java/org/sonar/batch/report/IssuesPublisherTest.java
sonar-core/src/main/java/org/sonar/core/activity/db/ActivityDto.java

index f0a50a40c5e669bb5ddd819201d5fca8f3414bbd..ff9a5a2d1452c0ffd54db44c1810ae4611e8eabb 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.activity;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
 import java.util.LinkedHashMap;
@@ -51,6 +52,7 @@ public class Activity {
     this.action = action;
   }
 
+  @CheckForNull
   public String getMessage() {
     return message;
   }
index 46e122bae512c0e976fd6b0fd13d20131593c7e8..c0472856ada5ebf3c1194736a6784a984c2dc172 100644 (file)
@@ -67,7 +67,7 @@ public class ActivityDoc extends BaseDoc {
   }
 
   public String getType() {
-    return ((String) getField(ActivityIndexDefinition.FIELD_TYPE));
+    return (String) getField(ActivityIndexDefinition.FIELD_TYPE);
   }
 
   public void setType(String s) {
@@ -83,9 +83,8 @@ public class ActivityDoc extends BaseDoc {
     setField(ActivityIndexDefinition.FIELD_ACTION, s);
   }
 
-  @CheckForNull
   public Map<String, String> getDetails() {
-    return this.getNullableField(ActivityIndexDefinition.FIELD_DETAILS);
+    return this.getField(ActivityIndexDefinition.FIELD_DETAILS);
   }
 
   public void setDetails(Map<String, String> details) {
@@ -101,6 +100,7 @@ public class ActivityDoc extends BaseDoc {
     setField(ActivityIndexDefinition.FIELD_MESSAGE, s);
   }
 
+  @Override
   public String toString() {
     return ReflectionToStringBuilder.toString(this);
   }
index 08c92210404d2c1d7b8f3d2a26ad7360684cc22b..c9a8c173b231f14b551aa5b2e15e174c17e85769 100644 (file)
@@ -52,12 +52,10 @@ public class ParseReportStep implements ComputationStep {
   private void recursivelyProcessComponent(ComputationContext context, int componentRef) {
     BatchReportReader reportReader = context.getReportReader();
     BatchReport.Component component = reportReader.readComponent(componentRef);
-    if (component != null) {
-      List<BatchReport.Issue> issues = reportReader.readComponentIssues(componentRef);
-      issueComputation.processComponentIssues(context, component.getUuid(), issues);
-      for (Integer childRef : component.getChildRefsList()) {
-        recursivelyProcessComponent(context, childRef);
-      }
+    List<BatchReport.Issue> issues = reportReader.readComponentIssues(componentRef);
+    issueComputation.processComponentIssues(context, component.getUuid(), issues);
+    for (Integer childRef : component.getChildRefsList()) {
+      recursivelyProcessComponent(context, childRef);
     }
   }
 
index 5cce6da31fecc06e9ac6d9c62d4fe2a1d9ad21b7..fd41cdd86b4b97af9ef2a2cd5de901ee482ce5a9 100644 (file)
@@ -42,7 +42,9 @@ public class QProfileActivityQuery extends ActivityQuery {
   }
 
   public QProfileActivityQuery setQprofileKey(@Nullable String qprofileKey) {
-    addDataOrFilter("profileKey", qprofileKey);
+    if (qprofileKey != null) {
+      addDataOrFilter("profileKey", qprofileKey);
+    }
     return this;
   }
 }
index 26682ddb642cd8b0d8cc5a83653ce191aee2bf4b..adeecf0d06fc73e26692e1ebc608a11edac4c0da 100644 (file)
@@ -50,7 +50,7 @@ import org.sonar.api.utils.ZipUtils;
 import org.sonar.api.utils.internal.JUnitTempFolder;
 import org.sonar.api.utils.log.LogTester;
 import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.core.computation.db.AnalysisReportDto;
 import org.sonar.core.persistence.DbTester;
@@ -178,7 +178,7 @@ public class ComputationServiceTest {
 
   private File generateZip() throws IOException {
     File dir = tempFolder.newDir();
-    BatchOutputWriter writer = new BatchOutputWriter(dir);
+    BatchReportWriter writer = new BatchReportWriter(dir);
     writer.writeMetadata(BatchReport.Metadata.newBuilder()
       .setRootComponentRef(1)
       .setProjectKey("PROJECT_KEY")
index 1b0d625148a3bf161801546d8d2ccc1551486497..12dee15de9658bbd391a2dae32c39ca0790cef09 100644 (file)
@@ -24,7 +24,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.batch.protocol.Constants;
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.batch.protocol.output.BatchReportReader;
 import org.sonar.core.component.ComponentDto;
@@ -78,7 +78,7 @@ public class ParseReportStepTest extends BaseStepTest {
   private File generateReport() throws IOException {
     File dir = temp.newFolder();
     // project and 2 files
-    BatchOutputWriter writer = new BatchOutputWriter(dir);
+    BatchReportWriter writer = new BatchReportWriter(dir);
     writer.writeMetadata(BatchReport.Metadata.newBuilder()
       .setRootComponentRef(1)
       .setProjectKey("PROJECT_KEY")
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchOutputWriter.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchOutputWriter.java
deleted file mode 100644 (file)
index ce1ce34..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.batch.protocol.output;
-
-import org.sonar.batch.protocol.ProtobufUtil;
-
-import java.io.File;
-
-public class BatchOutputWriter {
-
-  private final FileStructure fileStructure;
-
-  public BatchOutputWriter(File dir) {
-    if (!dir.exists() && !dir.mkdirs()) {
-      throw new IllegalStateException("Unable to create directory: " + dir);
-    }
-    this.fileStructure = new FileStructure(dir);
-  }
-
-  FileStructure getFileStructure() {
-    return fileStructure;
-  }
-
-  public boolean hasComponentData(FileStructure.Domain domain, int componentRef) {
-    File file = fileStructure.fileFor(domain, componentRef);
-    return file.exists() && file.isFile();
-  }
-
-  /**
-   * Metadata is mandatory
-   */
-  public void writeMetadata(BatchReport.Metadata metadata) {
-    ProtobufUtil.writeToFile(metadata, fileStructure.metadataFile());
-  }
-
-  public void writeComponent(BatchReport.Component component) {
-    File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, component.getRef());
-    ProtobufUtil.writeToFile(component, file);
-  }
-
-  public void writeComponentIssues(int componentRef, Iterable<BatchReport.Issue> issues) {
-    BatchReport.Issues.Builder issuesBuilder = BatchReport.Issues.newBuilder();
-    issuesBuilder.setComponentRef(componentRef);
-    issuesBuilder.addAllList(issues);
-    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
-    ProtobufUtil.writeToFile(issuesBuilder.build(), file);
-  }
-
-  /**
-   * Issues on components which have been deleted are stored in another location.
-   * Temporary hack, waiting for computation stack
-   */
-  public void writeDeletedComponentIssues(int componentRef, String componentUuid, Iterable<BatchReport.Issue> issues) {
-    BatchReport.Issues.Builder issuesBuilder = BatchReport.Issues.newBuilder();
-    issuesBuilder.setComponentRef(componentRef);
-    issuesBuilder.setComponentUuid(componentUuid);
-    issuesBuilder.addAllList(issues);
-    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES_ON_DELETED, componentRef);
-    ProtobufUtil.writeToFile(issuesBuilder.build(), file);
-  }
-
-}
index 72a319a4e0986d088d2109f9911c333b441d3fca..a2cee3bd75fcf22f5d6f2be7f8756234ff4ad732 100644 (file)
@@ -22,8 +22,6 @@ package org.sonar.batch.protocol.output;
 import org.sonar.batch.protocol.ProtobufUtil;
 import org.sonar.batch.protocol.output.BatchReport.Issues;
 
-import javax.annotation.CheckForNull;
-
 import java.io.File;
 import java.util.Collections;
 import java.util.List;
@@ -44,13 +42,12 @@ public class BatchReportReader {
     return ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER);
   }
 
-  @CheckForNull
   public BatchReport.Component readComponent(int componentRef) {
     File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, componentRef);
-    if (file.exists() && file.isFile()) {
-      return ProtobufUtil.readFile(file, BatchReport.Component.PARSER);
+    if (!file.exists() || !file.isFile()) {
+      throw new IllegalStateException("Unable to find report for component #" + componentRef + ". File does not exist: " + file);
     }
-    return null;
+    return ProtobufUtil.readFile(file, BatchReport.Component.PARSER);
   }
 
   public List<BatchReport.Issue> readComponentIssues(int componentRef) {
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java
new file mode 100644 (file)
index 0000000..55ced1f
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.batch.protocol.output;
+
+import org.sonar.batch.protocol.ProtobufUtil;
+
+import java.io.File;
+
+public class BatchReportWriter {
+
+  private final FileStructure fileStructure;
+
+  public BatchReportWriter(File dir) {
+    if (!dir.exists() && !dir.mkdirs()) {
+      throw new IllegalStateException("Unable to create directory: " + dir);
+    }
+    this.fileStructure = new FileStructure(dir);
+  }
+
+  FileStructure getFileStructure() {
+    return fileStructure;
+  }
+
+  public boolean hasComponentData(FileStructure.Domain domain, int componentRef) {
+    File file = fileStructure.fileFor(domain, componentRef);
+    return file.exists() && file.isFile();
+  }
+
+  /**
+   * Metadata is mandatory
+   */
+  public void writeMetadata(BatchReport.Metadata metadata) {
+    ProtobufUtil.writeToFile(metadata, fileStructure.metadataFile());
+  }
+
+  public void writeComponent(BatchReport.Component component) {
+    File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, component.getRef());
+    ProtobufUtil.writeToFile(component, file);
+  }
+
+  public void writeComponentIssues(int componentRef, Iterable<BatchReport.Issue> issues) {
+    BatchReport.Issues.Builder issuesBuilder = BatchReport.Issues.newBuilder();
+    issuesBuilder.setComponentRef(componentRef);
+    issuesBuilder.addAllList(issues);
+    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
+    ProtobufUtil.writeToFile(issuesBuilder.build(), file);
+  }
+
+  /**
+   * Issues on components which have been deleted are stored in another location.
+   * Temporary hack, waiting for computation stack
+   */
+  public void writeDeletedComponentIssues(int componentRef, String componentUuid, Iterable<BatchReport.Issue> issues) {
+    BatchReport.Issues.Builder issuesBuilder = BatchReport.Issues.newBuilder();
+    issuesBuilder.setComponentRef(componentRef);
+    issuesBuilder.setComponentUuid(componentUuid);
+    issuesBuilder.addAllList(issues);
+    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES_ON_DELETED, componentRef);
+    ProtobufUtil.writeToFile(issuesBuilder.build(), file);
+  }
+
+}
diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputWriterTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputWriterTest.java
deleted file mode 100644 (file)
index c64d1a0..0000000
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.batch.protocol.output;
-
-import org.apache.commons.io.FileUtils;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.batch.protocol.Constants;
-import org.sonar.batch.protocol.ProtobufUtil;
-
-import java.io.File;
-import java.util.Arrays;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class BatchOutputWriterTest {
-
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
-
-  @Test
-  public void create_dir_if_does_not_exist() throws Exception {
-    File dir = temp.newFolder();
-    FileUtils.deleteQuietly(dir);
-
-    new BatchOutputWriter(dir);
-
-    assertThat(dir).isDirectory().exists();
-  }
-
-  @Test
-  public void write_metadata() throws Exception {
-    File dir = temp.newFolder();
-    BatchOutputWriter writer = new BatchOutputWriter(dir);
-    BatchReport.Metadata.Builder metadata = BatchReport.Metadata.newBuilder()
-      .setAnalysisDate(15000000L)
-      .setProjectKey("PROJECT_A")
-      .setRootComponentRef(1);
-    writer.writeMetadata(metadata.build());
-
-    BatchReport.Metadata read = ProtobufUtil.readFile(writer.getFileStructure().metadataFile(), BatchReport.Metadata.PARSER);
-    assertThat(read.getAnalysisDate()).isEqualTo(15000000L);
-    assertThat(read.getProjectKey()).isEqualTo("PROJECT_A");
-    assertThat(read.getRootComponentRef()).isEqualTo(1);
-  }
-
-  @Test
-  public void write_component() throws Exception {
-    File dir = temp.newFolder();
-    BatchOutputWriter writer = new BatchOutputWriter(dir);
-
-    // no data yet
-    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isFalse();
-
-    // write data
-    BatchReport.Component.Builder component = BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setLanguage("java")
-      .setPath("src/Foo.java")
-      .setUuid("UUID_A")
-      .setType(Constants.ComponentType.FILE)
-      .setIsTest(false)
-      .addChildRefs(5)
-      .addChildRefs(42);
-    writer.writeComponent(component.build());
-
-    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
-    File file = writer.getFileStructure().fileFor(FileStructure.Domain.COMPONENT, 1);
-    assertThat(file).exists().isFile();
-    BatchReport.Component read = ProtobufUtil.readFile(file, BatchReport.Component.PARSER);
-    assertThat(read.getRef()).isEqualTo(1);
-    assertThat(read.getChildRefsList()).containsOnly(5, 42);
-    assertThat(read.hasName()).isFalse();
-    assertThat(read.getIsTest()).isFalse();
-    assertThat(read.getUuid()).isEqualTo("UUID_A");
-  }
-
-  @Test
-  public void write_issues() throws Exception {
-    File dir = temp.newFolder();
-    BatchOutputWriter writer = new BatchOutputWriter(dir);
-
-    // no data yet
-    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES, 1)).isFalse();
-
-    // write data
-    BatchReport.Issue issue = BatchReport.Issue.newBuilder()
-      .setUuid("ISSUE_A")
-      .setLine(50)
-      .setMsg("the message")
-      .build();
-
-    writer.writeComponentIssues(1, Arrays.asList(issue));
-
-    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES, 1)).isTrue();
-    File file = writer.getFileStructure().fileFor(FileStructure.Domain.ISSUES, 1);
-    assertThat(file).exists().isFile();
-    BatchReport.Issues read = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER);
-    assertThat(read.getComponentRef()).isEqualTo(1);
-    assertThat(read.hasComponentUuid()).isFalse();
-    assertThat(read.getListCount()).isEqualTo(1);
-  }
-
-  @Test
-  public void write_issues_of_deleted_component() throws Exception {
-    File dir = temp.newFolder();
-    BatchOutputWriter writer = new BatchOutputWriter(dir);
-
-    // no data yet
-    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isFalse();
-
-    // write data
-    BatchReport.Issue issue = BatchReport.Issue.newBuilder()
-      .setUuid("ISSUE_A")
-      .setLine(50)
-      .setMsg("the message")
-      .build();
-
-    writer.writeDeletedComponentIssues(1, "componentUuid", Arrays.asList(issue));
-
-    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isTrue();
-    File file = writer.getFileStructure().fileFor(FileStructure.Domain.ISSUES_ON_DELETED, 1);
-    assertThat(file).exists().isFile();
-    BatchReport.Issues read = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER);
-    assertThat(read.getComponentRef()).isEqualTo(1);
-    assertThat(read.getComponentUuid()).isEqualTo("componentUuid");
-    assertThat(read.getListCount()).isEqualTo(1);
-  }
-}
index 8dd814247eb65fb12219b67cc7cfa53ea8849b5c..cee15cf1c65b4b5e42675bd2cb27a74caae02fa3 100644 (file)
@@ -48,15 +48,48 @@ public class BatchReportReaderTest {
     assertThat(reader.readComponentIssues(1)).hasSize(1);
     assertThat(reader.readComponentIssues(200)).isEmpty();
     assertThat(reader.readComponent(1).getUuid()).isEqualTo("UUID_A");
-    assertThat(reader.readComponent(200)).isNull();
     Issues deletedComponentIssues = reader.readDeletedComponentIssues(1);
     assertThat(deletedComponentIssues.getComponentUuid()).isEqualTo("compUuid");
     assertThat(deletedComponentIssues.getListList()).hasSize(1);
+  }
+
+  @Test(expected = IllegalStateException.class)
+  public void fail_if_missing_metadata_file() throws Exception {
+    File dir = temp.newFolder();
+
+    BatchReportReader reader = new BatchReportReader(dir);
+    reader.readMetadata();
+  }
+
+  @Test(expected = IllegalStateException.class)
+   public void fail_if_missing_file_on_deleted_component() throws Exception {
+    File dir = temp.newFolder();
+
+    BatchReportReader reader = new BatchReportReader(dir);
+    reader.readDeletedComponentIssues(666);
+  }
+
+  @Test(expected = IllegalStateException.class)
+  public void fail_if_missing_file_on_component() throws Exception {
+    File dir = temp.newFolder();
+
+    BatchReportReader reader = new BatchReportReader(dir);
+    reader.readComponent(666);
+  }
 
+  /**
+   * no file if no issues
+   */
+  @Test
+  public void ignore_missing_file_on_component_issues() throws Exception {
+    File dir = temp.newFolder();
+
+    BatchReportReader reader = new BatchReportReader(dir);
+    assertThat(reader.readComponentIssues(666)).isEmpty();
   }
 
   private void initFiles(File dir) {
-    BatchOutputWriter writer = new BatchOutputWriter(dir);
+    BatchReportWriter writer = new BatchReportWriter(dir);
 
     BatchReport.Metadata.Builder metadata = BatchReport.Metadata.newBuilder()
       .setAnalysisDate(15000000L)
diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java
new file mode 100644 (file)
index 0000000..a26ba76
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.batch.protocol.output;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.batch.protocol.Constants;
+import org.sonar.batch.protocol.ProtobufUtil;
+
+import java.io.File;
+import java.util.Arrays;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class BatchReportWriterTest {
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  @Test
+  public void create_dir_if_does_not_exist() throws Exception {
+    File dir = temp.newFolder();
+    FileUtils.deleteQuietly(dir);
+
+    new BatchReportWriter(dir);
+
+    assertThat(dir).isDirectory().exists();
+  }
+
+  @Test
+  public void write_metadata() throws Exception {
+    File dir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(dir);
+    BatchReport.Metadata.Builder metadata = BatchReport.Metadata.newBuilder()
+      .setAnalysisDate(15000000L)
+      .setProjectKey("PROJECT_A")
+      .setRootComponentRef(1);
+    writer.writeMetadata(metadata.build());
+
+    BatchReport.Metadata read = ProtobufUtil.readFile(writer.getFileStructure().metadataFile(), BatchReport.Metadata.PARSER);
+    assertThat(read.getAnalysisDate()).isEqualTo(15000000L);
+    assertThat(read.getProjectKey()).isEqualTo("PROJECT_A");
+    assertThat(read.getRootComponentRef()).isEqualTo(1);
+  }
+
+  @Test
+  public void write_component() throws Exception {
+    File dir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(dir);
+
+    // no data yet
+    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isFalse();
+
+    // write data
+    BatchReport.Component.Builder component = BatchReport.Component.newBuilder()
+      .setRef(1)
+      .setLanguage("java")
+      .setPath("src/Foo.java")
+      .setUuid("UUID_A")
+      .setType(Constants.ComponentType.FILE)
+      .setIsTest(false)
+      .addChildRefs(5)
+      .addChildRefs(42);
+    writer.writeComponent(component.build());
+
+    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
+    File file = writer.getFileStructure().fileFor(FileStructure.Domain.COMPONENT, 1);
+    assertThat(file).exists().isFile();
+    BatchReport.Component read = ProtobufUtil.readFile(file, BatchReport.Component.PARSER);
+    assertThat(read.getRef()).isEqualTo(1);
+    assertThat(read.getChildRefsList()).containsOnly(5, 42);
+    assertThat(read.hasName()).isFalse();
+    assertThat(read.getIsTest()).isFalse();
+    assertThat(read.getUuid()).isEqualTo("UUID_A");
+  }
+
+  @Test
+  public void write_issues() throws Exception {
+    File dir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(dir);
+
+    // no data yet
+    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES, 1)).isFalse();
+
+    // write data
+    BatchReport.Issue issue = BatchReport.Issue.newBuilder()
+      .setUuid("ISSUE_A")
+      .setLine(50)
+      .setMsg("the message")
+      .build();
+
+    writer.writeComponentIssues(1, Arrays.asList(issue));
+
+    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES, 1)).isTrue();
+    File file = writer.getFileStructure().fileFor(FileStructure.Domain.ISSUES, 1);
+    assertThat(file).exists().isFile();
+    BatchReport.Issues read = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER);
+    assertThat(read.getComponentRef()).isEqualTo(1);
+    assertThat(read.hasComponentUuid()).isFalse();
+    assertThat(read.getListCount()).isEqualTo(1);
+  }
+
+  @Test
+  public void write_issues_of_deleted_component() throws Exception {
+    File dir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(dir);
+
+    // no data yet
+    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isFalse();
+
+    // write data
+    BatchReport.Issue issue = BatchReport.Issue.newBuilder()
+      .setUuid("ISSUE_A")
+      .setLine(50)
+      .setMsg("the message")
+      .build();
+
+    writer.writeDeletedComponentIssues(1, "componentUuid", Arrays.asList(issue));
+
+    assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isTrue();
+    File file = writer.getFileStructure().fileFor(FileStructure.Domain.ISSUES_ON_DELETED, 1);
+    assertThat(file).exists().isFile();
+    BatchReport.Issues read = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER);
+    assertThat(read.getComponentRef()).isEqualTo(1);
+    assertThat(read.getComponentUuid()).isEqualTo("componentUuid");
+    assertThat(read.getListCount()).isEqualTo(1);
+  }
+}
index 7850f6e2db45c79306ae4dd5fb59e5b9fb79a850..d075a9a6300e828489421458dfbcbb7047e01012 100644 (file)
@@ -26,7 +26,7 @@ import org.sonar.api.resources.ResourceUtils;
 import org.sonar.batch.index.BatchResource;
 import org.sonar.batch.index.ResourceCache;
 import org.sonar.batch.protocol.Constants;
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 import org.sonar.batch.protocol.output.BatchReport;
 
 import javax.annotation.CheckForNull;
@@ -45,12 +45,12 @@ public class ComponentsPublisher implements ReportPublisher {
   }
 
   @Override
-  public void publish(BatchOutputWriter writer) {
+  public void publish(BatchReportWriter writer) {
     BatchResource rootProject = resourceCache.get(reactor.getRoot().getKeyWithBranch());
     recursiveWriteComponent(rootProject, writer);
   }
 
-  private void recursiveWriteComponent(BatchResource batchResource, BatchOutputWriter writer) {
+  private void recursiveWriteComponent(BatchResource batchResource, BatchReportWriter writer) {
     Resource r = batchResource.resource();
     BatchReport.Component.Builder builder = BatchReport.Component.newBuilder();
 
index fd7bd13b814310b1e4e6700a6ed13f76bdf1f4e0..0cefc7d25db695344398fa73f31f799cf6befede 100644 (file)
@@ -30,7 +30,7 @@ import org.sonar.batch.index.BatchResource;
 import org.sonar.batch.index.ResourceCache;
 import org.sonar.batch.issue.IssueCache;
 import org.sonar.batch.protocol.Constants;
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 import org.sonar.batch.protocol.output.BatchReport;
 
 import javax.annotation.Nullable;
@@ -52,7 +52,7 @@ public class IssuesPublisher implements ReportPublisher {
   }
 
   @Override
-  public void publish(BatchOutputWriter writer) {
+  public void publish(BatchReportWriter writer) {
     Collection<Object> deletedComponentKeys = issueCache.componentKeys();
     for (BatchResource resource : resourceCache.all()) {
       String componentKey = resource.resource().getEffectiveKey();
@@ -71,7 +71,7 @@ public class IssuesPublisher implements ReportPublisher {
     exportMetadata(writer, count);
   }
 
-  private void exportMetadata(BatchOutputWriter writer, int count) {
+  private void exportMetadata(BatchReportWriter writer, int count) {
     BatchResource rootProject = resourceCache.get(reactor.getRoot().getKeyWithBranch());
     BatchReport.Metadata.Builder builder = BatchReport.Metadata.newBuilder()
       .setAnalysisDate(((Project) rootProject.resource()).getAnalysisDate().getTime())
@@ -85,7 +85,7 @@ public class IssuesPublisher implements ReportPublisher {
     writer.writeMetadata(builder.build());
   }
 
-  private int exportIssuesOfDeletedComponents(Collection<Object> deletedComponentKeys, BatchOutputWriter writer) {
+  private int exportIssuesOfDeletedComponents(Collection<Object> deletedComponentKeys, BatchReportWriter writer) {
     int deletedComponentCount = 0;
     for (Object componentKey : deletedComponentKeys) {
       deletedComponentCount++;
index 76713e6a747af1ca2b542e9b6668f16f0789c2d6..3a70259bede30e1c7fc6d417c6535eaee5e369e2 100644 (file)
@@ -33,7 +33,7 @@ import org.sonar.api.utils.TempFolder;
 import org.sonar.api.utils.ZipUtils;
 import org.sonar.batch.bootstrap.DefaultAnalysisMode;
 import org.sonar.batch.bootstrap.ServerClient;
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 
 import java.io.File;
 import java.io.IOException;
@@ -84,7 +84,7 @@ public class PublishReportJob implements BatchComponent {
     try {
       long startTime = System.currentTimeMillis();
       File reportDir = temp.newDir("batch-report");
-      BatchOutputWriter writer = new BatchOutputWriter(reportDir);
+      BatchReportWriter writer = new BatchReportWriter(reportDir);
       for (ReportPublisher publisher : publishers) {
         publisher.publish(writer);
       }
index 6fe18f50889ac49580c4dd7bd1a0395ae060623a..07aa03fccbeae44349add5c307ee61b4062fcd47 100644 (file)
  */
 package org.sonar.batch.report;
 
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 
 /**
  * Adds a sub-part of data to output report
  */
 public interface ReportPublisher {
 
-  void publish(BatchOutputWriter writer);
+  void publish(BatchReportWriter writer);
 
 }
index 30f56d3a46eae8321ceb884266caa8f6de346e61..5bcc75aa6db999718ab25436a2c3dca0ac51b061 100644 (file)
@@ -30,7 +30,7 @@ import org.sonar.api.resources.Java;
 import org.sonar.api.resources.Project;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.batch.index.ResourceCache;
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 import org.sonar.batch.protocol.output.FileStructure;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -77,7 +77,7 @@ public class ComponentsPublisherTest {
     testFile.setId(6).setUuid("TEST_FILE_UUID");
     resourceCache.add(testFile, dir).setSnapshot(new Snapshot().setId(16));
 
-    BatchOutputWriter writer = new BatchOutputWriter(temp.newFolder());
+    BatchReportWriter writer = new BatchReportWriter(temp.newFolder());
     publisher.publish(writer);
 
     assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
index 2674e1ddc7880b083fc36d3d88381be1d5f7f391..9a020765298682347cf9f3ccbf4ebb77810eb790 100644 (file)
@@ -33,7 +33,7 @@ import org.sonar.api.rule.RuleKey;
 import org.sonar.api.utils.Duration;
 import org.sonar.batch.index.ResourceCache;
 import org.sonar.batch.issue.IssueCache;
-import org.sonar.batch.protocol.output.BatchOutputWriter;
+import org.sonar.batch.protocol.output.BatchReportWriter;
 import org.sonar.batch.protocol.output.BatchReport.Metadata;
 import org.sonar.batch.protocol.output.BatchReportReader;
 
@@ -98,7 +98,7 @@ public class IssuesPublisherTest {
     when(issueCache.byComponent("foo:src/Foo.php")).thenReturn(Arrays.asList(issue1, issue2));
 
     File outputDir = temp.newFolder();
-    BatchOutputWriter writer = new BatchOutputWriter(outputDir);
+    BatchReportWriter writer = new BatchReportWriter(outputDir);
 
     publisher.publish(writer);
 
@@ -149,7 +149,7 @@ public class IssuesPublisherTest {
     when(issueCache.componentKeys()).thenReturn(Arrays.<Object>asList("foo:deleted.php"));
 
     File outputDir = temp.newFolder();
-    BatchOutputWriter writer = new BatchOutputWriter(outputDir);
+    BatchReportWriter writer = new BatchReportWriter(outputDir);
 
     publisher.publish(writer);
 
index 298b88faef99509e1586936af525f40cfa21992d..81454b0542153f4a6c5385bc4804fb6ea43308d8 100644 (file)
@@ -22,6 +22,8 @@ package org.sonar.core.activity.db;
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 
+import javax.annotation.Nullable;
+
 import java.util.Date;
 
 public class ActivityDto {
@@ -65,7 +67,7 @@ public class ActivityDto {
     return author;
   }
 
-  public ActivityDto setAuthor(String author) {
+  public ActivityDto setAuthor(@Nullable String author) {
     this.author = author;
     return this;
   }
@@ -83,7 +85,7 @@ public class ActivityDto {
     return message;
   }
 
-  public ActivityDto setMessage(String message) {
+  public ActivityDto setMessage(@Nullable String message) {
     this.message = message;
     return this;
   }