From 33e7b484acb9b0cda2e638dd5356b5e33c0ec1b4 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 5 Mar 2015 11:08:17 +0100 Subject: Fix quality flaws --- .../batch/protocol/output/BatchOutputWriter.java | 79 ---------------------- .../batch/protocol/output/BatchReportReader.java | 9 +-- .../batch/protocol/output/BatchReportWriter.java | 79 ++++++++++++++++++++++ 3 files changed, 82 insertions(+), 85 deletions(-) delete mode 100644 sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchOutputWriter.java create mode 100644 sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java (limited to 'sonar-batch-protocol/src/main/java') 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 index ce1ce341d6f..00000000000 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchOutputWriter.java +++ /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 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 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/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 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 index 00000000000..55ced1fa354 --- /dev/null +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java @@ -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 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 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); + } + +} -- cgit v1.2.3