diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-03-05 11:08:17 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-03-05 11:10:00 +0100 |
commit | 33e7b484acb9b0cda2e638dd5356b5e33c0ec1b4 (patch) | |
tree | b0e198c98a182363d66a09c9a73ef4a22ea1d882 /sonar-batch-protocol | |
parent | 29f7b15feea86c67c8e75fb89c8fa0dd4b807ba8 (diff) | |
download | sonarqube-33e7b484acb9b0cda2e638dd5356b5e33c0ec1b4.tar.gz sonarqube-33e7b484acb9b0cda2e638dd5356b5e33c0ec1b4.zip |
Fix quality flaws
Diffstat (limited to 'sonar-batch-protocol')
-rw-r--r-- | sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java | 9 | ||||
-rw-r--r-- | sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java (renamed from sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchOutputWriter.java) | 4 | ||||
-rw-r--r-- | sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java | 37 | ||||
-rw-r--r-- | sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java (renamed from sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputWriterTest.java) | 12 |
4 files changed, 46 insertions, 16 deletions
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java index 72a319a4e09..a2cee3bd75f 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java @@ -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/BatchOutputWriter.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java index ce1ce341d6f..55ced1fa354 100644 --- 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/BatchReportWriter.java @@ -23,11 +23,11 @@ import org.sonar.batch.protocol.ProtobufUtil; import java.io.File; -public class BatchOutputWriter { +public class BatchReportWriter { private final FileStructure fileStructure; - public BatchOutputWriter(File dir) { + public BatchReportWriter(File dir) { if (!dir.exists() && !dir.mkdirs()) { throw new IllegalStateException("Unable to create directory: " + dir); } diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java index 8dd814247eb..cee15cf1c65 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java @@ -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/BatchOutputWriterTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java index c64d1a0cf07..a26ba76d58d 100644 --- 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/BatchReportWriterTest.java @@ -31,7 +31,7 @@ import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; -public class BatchOutputWriterTest { +public class BatchReportWriterTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -41,7 +41,7 @@ public class BatchOutputWriterTest { File dir = temp.newFolder(); FileUtils.deleteQuietly(dir); - new BatchOutputWriter(dir); + new BatchReportWriter(dir); assertThat(dir).isDirectory().exists(); } @@ -49,7 +49,7 @@ public class BatchOutputWriterTest { @Test public void write_metadata() throws Exception { File dir = temp.newFolder(); - BatchOutputWriter writer = new BatchOutputWriter(dir); + BatchReportWriter writer = new BatchReportWriter(dir); BatchReport.Metadata.Builder metadata = BatchReport.Metadata.newBuilder() .setAnalysisDate(15000000L) .setProjectKey("PROJECT_A") @@ -65,7 +65,7 @@ public class BatchOutputWriterTest { @Test public void write_component() throws Exception { File dir = temp.newFolder(); - BatchOutputWriter writer = new BatchOutputWriter(dir); + BatchReportWriter writer = new BatchReportWriter(dir); // no data yet assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isFalse(); @@ -96,7 +96,7 @@ public class BatchOutputWriterTest { @Test public void write_issues() throws Exception { File dir = temp.newFolder(); - BatchOutputWriter writer = new BatchOutputWriter(dir); + BatchReportWriter writer = new BatchReportWriter(dir); // no data yet assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES, 1)).isFalse(); @@ -122,7 +122,7 @@ public class BatchOutputWriterTest { @Test public void write_issues_of_deleted_component() throws Exception { File dir = temp.newFolder(); - BatchOutputWriter writer = new BatchOutputWriter(dir); + BatchReportWriter writer = new BatchReportWriter(dir); // no data yet assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isFalse(); |