diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-22 14:17:06 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-22 14:17:16 +0200 |
commit | c98aca6054048d0293fdb713910b4fa98beaa524 (patch) | |
tree | 2335acba0b9ecab3115dc6a49e67db4431dcd53b /sonar-batch-protocol | |
parent | 5539cfffd327697a49278361901335e2d4d6e075 (diff) | |
download | sonarqube-c98aca6054048d0293fdb713910b4fa98beaa524.tar.gz sonarqube-c98aca6054048d0293fdb713910b4fa98beaa524.zip |
Improve utility org.sonar.core.util.Protobuf
Diffstat (limited to 'sonar-batch-protocol')
6 files changed, 34 insertions, 236 deletions
diff --git a/sonar-batch-protocol/compile_protobuf.sh b/sonar-batch-protocol/compile_protobuf.sh deleted file mode 100755 index f2d14c4eebc..00000000000 --- a/sonar-batch-protocol/compile_protobuf.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -OUTPUT_DIR="src/main/gen-java" - -mkdir -p ${OUTPUT_DIR} -protoc --proto_path=src/main/protobuf --java_out=${OUTPUT_DIR} src/main/protobuf/*.proto diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java deleted file mode 100644 index 43d8af6bfbf..00000000000 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java +++ /dev/null @@ -1,121 +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; - -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Message; -import com.google.protobuf.Parser; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import org.apache.commons.io.IOUtils; -import org.sonar.core.util.CloseableIterator; - -import static java.lang.String.format; - -public class ProtobufUtil { - private ProtobufUtil() { - // only static stuff - } - - /** - * Returns the message contained in the given file. Throws an unchecked exception - * if the file does not exist or is empty. - */ - public static <MSG extends Message> MSG readFile(File file, Parser<MSG> parser) { - InputStream input = null; - try { - input = new BufferedInputStream(new FileInputStream(file)); - return parser.parseFrom(input); - } catch (IOException e) { - throw new IllegalStateException(format("Unable to read file %s", file), e); - } finally { - IOUtils.closeQuietly(input); - } - } - - /** - * Writes a single message to a file, by replacing existing content. The message is - * NOT appended. - */ - public static void writeToFile(Message message, File toFile) { - OutputStream out = null; - try { - out = new BufferedOutputStream(new FileOutputStream(toFile, false)); - message.writeTo(out); - } catch (IOException e) { - throw new IllegalStateException(format("Unable to write protobuf message to file %s", toFile), e); - } finally { - IOUtils.closeQuietly(out); - } - } - - public static void writeStreamToFile(Iterable<? extends Message> messages, File toFile, boolean append) { - OutputStream out = null; - try { - out = new BufferedOutputStream(new FileOutputStream(toFile, append)); - for (Message message : messages) { - message.writeDelimitedTo(out); - } - } catch (IOException e) { - throw new IllegalStateException(format("Unable to write protobuf messages to file %s", toFile), e); - } finally { - IOUtils.closeQuietly(out); - } - } - - public static <MSG extends Message> CloseableIterator<MSG> readStreamFromFile(File file, Parser<MSG> parser) { - try { - return new ProtobufIterator<>(parser, new BufferedInputStream(new FileInputStream(file))); - } catch (FileNotFoundException e) { - throw new IllegalStateException(format("Unable to read protobuf file %s", file), e); - } - } - - private static class ProtobufIterator<MSG extends Message> extends CloseableIterator<MSG> { - private final Parser<MSG> parser; - private final InputStream input; - - private ProtobufIterator(Parser<MSG> parser, InputStream input) { - this.parser = parser; - this.input = input; - } - - @Override - protected MSG doNext() { - try { - return parser.parseDelimitedFrom(input); - } catch (InvalidProtocolBufferException e) { - throw new IllegalStateException(e); - } - } - - @Override - protected void doClose() throws Exception { - IOUtils.closeQuietly(input); - } - } -} 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 70da7179b9f..9cc90da20c5 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 @@ -21,8 +21,8 @@ package org.sonar.batch.protocol.output; import java.io.File; import javax.annotation.CheckForNull; -import org.sonar.batch.protocol.ProtobufUtil; import org.sonar.core.util.CloseableIterator; +import org.sonar.core.util.Protobuf; public class BatchReportReader { @@ -37,7 +37,7 @@ public class BatchReportReader { if (!fileExists(file)) { throw new IllegalStateException("Metadata file is missing in analysis report: " + file); } - return ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER); + return Protobuf.read(file, BatchReport.Metadata.PARSER); } public CloseableIterator<BatchReport.ActiveRule> readActiveRules() { @@ -45,13 +45,13 @@ public class BatchReportReader { if (!fileExists(file)) { return CloseableIterator.emptyCloseableIterator(); } - return ProtobufUtil.readStreamFromFile(file, BatchReport.ActiveRule.PARSER); + return Protobuf.readStream(file, BatchReport.ActiveRule.PARSER); } public CloseableIterator<BatchReport.Measure> readComponentMeasures(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef); if (fileExists(file)) { - return ProtobufUtil.readStreamFromFile(file, BatchReport.Measure.PARSER); + return Protobuf.readStream(file, BatchReport.Measure.PARSER); } return CloseableIterator.emptyCloseableIterator(); } @@ -60,7 +60,7 @@ public class BatchReportReader { public BatchReport.Changesets readChangesets(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, componentRef); if (fileExists(file)) { - return ProtobufUtil.readFile(file, BatchReport.Changesets.PARSER); + return Protobuf.read(file, BatchReport.Changesets.PARSER); } return null; } @@ -70,13 +70,13 @@ public class BatchReportReader { if (!fileExists(file)) { throw new IllegalStateException("Unable to find report for component #" + componentRef + ". File does not exist: " + file); } - return ProtobufUtil.readFile(file, BatchReport.Component.PARSER); + return Protobuf.read(file, BatchReport.Component.PARSER); } public CloseableIterator<BatchReport.Issue> readComponentIssues(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef); if (fileExists(file)) { - return ProtobufUtil.readStreamFromFile(file, BatchReport.Issue.PARSER); + return Protobuf.readStream(file, BatchReport.Issue.PARSER); } return CloseableIterator.emptyCloseableIterator(); } @@ -84,7 +84,7 @@ public class BatchReportReader { public CloseableIterator<BatchReport.Duplication> readComponentDuplications(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATIONS, componentRef); if (fileExists(file)) { - return ProtobufUtil.readStreamFromFile(file, BatchReport.Duplication.PARSER); + return Protobuf.readStream(file, BatchReport.Duplication.PARSER); } return CloseableIterator.emptyCloseableIterator(); } @@ -92,7 +92,7 @@ public class BatchReportReader { public CloseableIterator<BatchReport.Symbol> readComponentSymbols(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef); if (fileExists(file)) { - return ProtobufUtil.readStreamFromFile(file, BatchReport.Symbol.PARSER); + return Protobuf.readStream(file, BatchReport.Symbol.PARSER); } return CloseableIterator.emptyCloseableIterator(); } 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 index b7ac04ed116..bf0cdfcd736 100644 --- 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 @@ -20,7 +20,7 @@ package org.sonar.batch.protocol.output; import java.io.File; -import org.sonar.batch.protocol.ProtobufUtil; +import org.sonar.core.util.Protobuf; public class BatchReportWriter { @@ -46,72 +46,72 @@ public class BatchReportWriter { * Metadata is mandatory */ public File writeMetadata(BatchReport.Metadata metadata) { - ProtobufUtil.writeToFile(metadata, fileStructure.metadataFile()); + Protobuf.write(metadata, fileStructure.metadataFile()); return fileStructure.metadataFile(); } public File writeActiveRules(Iterable<BatchReport.ActiveRule> activeRules) { - ProtobufUtil.writeStreamToFile(activeRules, fileStructure.activeRules(), false); + Protobuf.writeStream(activeRules, fileStructure.activeRules(), false); return fileStructure.metadataFile(); } public File writeComponent(BatchReport.Component component) { File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, component.getRef()); - ProtobufUtil.writeToFile(component, file); + Protobuf.write(component, file); return file; } public File writeComponentIssues(int componentRef, Iterable<BatchReport.Issue> issues) { File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef); - ProtobufUtil.writeStreamToFile(issues, file, false); + Protobuf.writeStream(issues, file, false); return file; } public File writeComponentMeasures(int componentRef, Iterable<BatchReport.Measure> measures) { File file = fileStructure.fileFor(FileStructure.Domain.MEASURES, componentRef); - ProtobufUtil.writeStreamToFile(measures, file, false); + Protobuf.writeStream(measures, file, false); return file; } public File writeComponentChangesets(BatchReport.Changesets changesets) { File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, changesets.getComponentRef()); - ProtobufUtil.writeToFile(changesets, file); + Protobuf.write(changesets, file); return file; } public File writeComponentDuplications(int componentRef, Iterable<BatchReport.Duplication> duplications) { File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATIONS, componentRef); - ProtobufUtil.writeStreamToFile(duplications, file, false); + Protobuf.writeStream(duplications, file, false); return file; } public File writeComponentSymbols(int componentRef, Iterable<BatchReport.Symbol> symbols) { File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef); - ProtobufUtil.writeStreamToFile(symbols, file, false); + Protobuf.writeStream(symbols, file, false); return file; } public File writeComponentSyntaxHighlighting(int componentRef, Iterable<BatchReport.SyntaxHighlighting> syntaxHighlightingRules) { File file = fileStructure.fileFor(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef); - ProtobufUtil.writeStreamToFile(syntaxHighlightingRules, file, false); + Protobuf.writeStream(syntaxHighlightingRules, file, false); return file; } public File writeComponentCoverage(int componentRef, Iterable<BatchReport.Coverage> coverageList) { File file = fileStructure.fileFor(FileStructure.Domain.COVERAGES, componentRef); - ProtobufUtil.writeStreamToFile(coverageList, file, false); + Protobuf.writeStream(coverageList, file, false); return file; } public File writeTests(int componentRef, Iterable<BatchReport.Test> tests) { File file = fileStructure.fileFor(FileStructure.Domain.TESTS, componentRef); - ProtobufUtil.writeStreamToFile(tests, file, false); + Protobuf.writeStream(tests, file, false); return file; } public File writeCoverageDetails(int componentRef, Iterable<BatchReport.CoverageDetail> tests) { File file = fileStructure.fileFor(FileStructure.Domain.COVERAGE_DETAILS, componentRef); - ProtobufUtil.writeStreamToFile(tests, file, false); + Protobuf.writeStream(tests, file, false); return file; } diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java deleted file mode 100644 index 04ebcf7f989..00000000000 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java +++ /dev/null @@ -1,71 +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; - -import java.io.File; -import org.apache.commons.io.FileUtils; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.test.TestUtils; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ProtobufUtilTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Test - public void only_utils() { - assertThat(TestUtils.hasOnlyPrivateConstructors(ProtobufUtil.class)); - } - - @Test - public void readFile_fails_if_file_does_not_exist() throws Exception { - thrown.expect(IllegalStateException.class); - - File file = temp.newFile(); - FileUtils.forceDelete(file); - ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER); - } - - @Test - public void readFile_returns_empty_message_if_file_is_empty() throws Exception { - File file = temp.newFile(); - BatchReport.Metadata msg = ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER); - assertThat(msg).isNotNull(); - assertThat(msg.isInitialized()).isTrue(); - } - - @Test - public void readFile_returns_message() throws Exception { - File file = temp.newFile(); - ProtobufUtil.writeToFile(BatchReport.Metadata.getDefaultInstance(), file); - BatchReport.Metadata message = ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER); - assertThat(message).isNotNull(); - assertThat(message.isInitialized()).isTrue(); - } -} 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 index e19e044d2c5..ae1a6cd143e 100644 --- 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 @@ -28,9 +28,9 @@ 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 org.sonar.batch.protocol.output.BatchReport.Range; import org.sonar.core.util.CloseableIterator; +import org.sonar.core.util.Protobuf; import static org.assertj.core.api.Assertions.assertThat; @@ -63,7 +63,7 @@ public class BatchReportWriterTest { .setRootComponentRef(1); underTest.writeMetadata(metadata.build()); - BatchReport.Metadata read = ProtobufUtil.readFile(underTest.getFileStructure().metadataFile(), BatchReport.Metadata.PARSER); + BatchReport.Metadata read = Protobuf.read(underTest.getFileStructure().metadataFile(), BatchReport.Metadata.PARSER); assertThat(read.getAnalysisDate()).isEqualTo(15000000L); assertThat(read.getProjectKey()).isEqualTo("PROJECT_A"); assertThat(read.getRootComponentRef()).isEqualTo(1); @@ -88,7 +88,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.COMPONENT, 1); assertThat(file).exists().isFile(); - BatchReport.Component read = ProtobufUtil.readFile(file, BatchReport.Component.PARSER); + BatchReport.Component read = Protobuf.read(file, BatchReport.Component.PARSER); assertThat(read.getRef()).isEqualTo(1); assertThat(read.getChildRefList()).containsOnly(5, 42); assertThat(read.hasName()).isFalse(); @@ -111,7 +111,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.ISSUES, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.ISSUES, 1); assertThat(file).exists().isFile(); - try (CloseableIterator<BatchReport.Issue> read = ProtobufUtil.readStreamFromFile(file, BatchReport.Issue.PARSER)) { + try (CloseableIterator<BatchReport.Issue> read = Protobuf.readStream(file, BatchReport.Issue.PARSER)) { assertThat(Iterators.size(read)).isEqualTo(1); } } @@ -131,7 +131,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.MEASURES, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.MEASURES, 1); assertThat(file).exists().isFile(); - try (CloseableIterator<BatchReport.Measure> read = ProtobufUtil.readStreamFromFile(file, BatchReport.Measure.PARSER)) { + try (CloseableIterator<BatchReport.Measure> read = Protobuf.readStream(file, BatchReport.Measure.PARSER)) { assertThat(Iterators.size(read)).isEqualTo(1); } } @@ -154,7 +154,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.CHANGESETS, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.CHANGESETS, 1); assertThat(file).exists().isFile(); - BatchReport.Changesets read = ProtobufUtil.readFile(file, BatchReport.Changesets.PARSER); + BatchReport.Changesets read = Protobuf.read(file, BatchReport.Changesets.PARSER); assertThat(read.getComponentRef()).isEqualTo(1); assertThat(read.getChangesetCount()).isEqualTo(1); assertThat(read.getChangesetList()).hasSize(1); @@ -184,7 +184,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATIONS, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.DUPLICATIONS, 1); assertThat(file).exists().isFile(); - try (CloseableIterator<BatchReport.Duplication> duplications = ProtobufUtil.readStreamFromFile(file, BatchReport.Duplication.PARSER)) { + try (CloseableIterator<BatchReport.Duplication> duplications = Protobuf.readStream(file, BatchReport.Duplication.PARSER)) { BatchReport.Duplication dup = duplications.next(); assertThat(dup.getOriginPosition()).isNotNull(); assertThat(dup.getDuplicateList()).hasSize(1); @@ -218,7 +218,7 @@ public class BatchReportWriterTest { File file = underTest.getFileStructure().fileFor(FileStructure.Domain.SYMBOLS, 1); assertThat(file).exists().isFile(); - try (CloseableIterator<BatchReport.Symbol> read = ProtobufUtil.readStreamFromFile(file, BatchReport.Symbol.PARSER)) { + try (CloseableIterator<BatchReport.Symbol> read = Protobuf.readStream(file, BatchReport.Symbol.PARSER)) { assertThat(read).hasSize(1); } } @@ -235,8 +235,7 @@ public class BatchReportWriterTest { .setEndLine(1) .build()) .setType(Constants.HighlightingType.ANNOTATION) - .build() - )); + .build())); assertThat(underTest.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, 1)).isTrue(); } @@ -255,8 +254,7 @@ public class BatchReportWriterTest { .setUtCoveredConditions(1) .setItCoveredConditions(1) .setOverallCoveredConditions(1) - .build() - )); + .build())); assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGES, 1)).isTrue(); } @@ -266,8 +264,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.TESTS, 1)).isFalse(); underTest.writeTests(1, Arrays.asList( - BatchReport.Test.getDefaultInstance() - )); + BatchReport.Test.getDefaultInstance())); assertThat(underTest.hasComponentData(FileStructure.Domain.TESTS, 1)).isTrue(); @@ -278,8 +275,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGE_DETAILS, 1)).isFalse(); underTest.writeCoverageDetails(1, Arrays.asList( - BatchReport.CoverageDetail.getDefaultInstance() - )); + BatchReport.CoverageDetail.getDefaultInstance())); assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGE_DETAILS, 1)).isTrue(); } |