diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-01-26 18:24:01 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-02-02 17:27:19 +0100 |
commit | 35f1487144f72e49b73383c0e96d73b53c60c641 (patch) | |
tree | ceb208c0c3d0e631561c9807d394ca97503df1ae /sonar-batch-protocol/src/test | |
parent | 7a15c4d1064bbda06a204427bc71a26d9091647a (diff) | |
download | sonarqube-35f1487144f72e49b73383c0e96d73b53c60c641.tar.gz sonarqube-35f1487144f72e49b73383c0e96d73b53c60c641.zip |
Use protocol buffers format for analysis reports
Diffstat (limited to 'sonar-batch-protocol/src/test')
7 files changed, 305 insertions, 239 deletions
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 new file mode 100644 index 00000000000..8084bb2d947 --- /dev/null +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java @@ -0,0 +1,34 @@ +/* + * 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 org.junit.Test; +import org.sonar.test.TestUtils; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ProtobufUtilTest { + + @Test + public void only_utils() throws Exception { + assertThat(TestUtils.hasOnlyPrivateConstructors(ProtobufUtil.class)); + } + +} diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputReaderTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputReaderTest.java new file mode 100644 index 00000000000..930e3e3a1c8 --- /dev/null +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputReaderTest.java @@ -0,0 +1,77 @@ +/* + * 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.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BatchOutputReaderTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void create_dir_if_does_not_exist() throws Exception { + File dir = temp.newFolder(); + + initFiles(dir); + + BatchOutputReader reader = new BatchOutputReader(dir); + assertThat(reader.readMetadata().getAnalysisDate()).isEqualTo(15000000L); + assertThat(reader.readComponentIssues(1)).hasSize(1); + assertThat(reader.readComponentIssues(200)).isEmpty(); + assertThat(reader.readComponent(1).getUuid()).isEqualTo("UUID_A"); + assertThat(reader.readComponent(200)).isNull(); + + } + + private void initFiles(File dir) { + BatchOutputWriter writer = new BatchOutputWriter(dir); + + BatchOutput.ReportMetadata.Builder metadata = BatchOutput.ReportMetadata.newBuilder() + .setAnalysisDate(15000000L) + .setProjectKey("PROJECT_A") + .setRootComponentRef(1); + writer.writeMetadata(metadata.build()); + + BatchOutput.ReportComponent.Builder component = BatchOutput.ReportComponent.newBuilder() + .setRef(1) + .setUuid("UUID_A"); + writer.writeComponent(component.build()); + + BatchOutput.ReportIssue issue = BatchOutput.ReportIssue.newBuilder() + .setUuid("ISSUE_A") + .setLine(50) + .build(); + + writer.writeComponentIssues(1, Arrays.asList(issue)); + } + + @Test + public void readMetadata() throws Exception { + + } +} 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 new file mode 100644 index 00000000000..9261f1b1eaa --- /dev/null +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchOutputWriterTest.java @@ -0,0 +1,120 @@ +/* + * 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); + BatchOutput.ReportMetadata.Builder metadata = BatchOutput.ReportMetadata.newBuilder() + .setAnalysisDate(15000000L) + .setProjectKey("PROJECT_A") + .setRootComponentRef(1); + writer.writeMetadata(metadata.build()); + + BatchOutput.ReportMetadata read = ProtobufUtil.readFile(writer.getFileStructure().metadataFile(), BatchOutput.ReportMetadata.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 + BatchOutput.ReportComponent.Builder component = BatchOutput.ReportComponent.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(); + BatchOutput.ReportComponent read = ProtobufUtil.readFile(file, BatchOutput.ReportComponent.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 + BatchOutput.ReportIssue issue = BatchOutput.ReportIssue.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(); + BatchOutput.ReportIssues read = ProtobufUtil.readFile(file, BatchOutput.ReportIssues.PARSER); + assertThat(read.getComponentRef()).isEqualTo(1); + assertThat(read.getListCount()).isEqualTo(1); + } +} diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/FileStructureTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/FileStructureTest.java new file mode 100644 index 00000000000..465e5a6301d --- /dev/null +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/FileStructureTest.java @@ -0,0 +1,74 @@ +/* + * 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 java.io.File; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; + +public class FileStructureTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void fail_if_dir_does_not_exist() throws Exception { + File dir = temp.newFolder(); + FileUtils.deleteQuietly(dir); + try { + new FileStructure(dir); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessageContaining("Directory of analysis report does not exist"); + } + } + + @Test + public void fail_if_invalid_dir() throws Exception { + // not a dir but a file + File dir = temp.newFile(); + try { + new FileStructure(dir); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessageContaining("Directory of analysis report does not exist"); + } + } + + @Test + public void locate_files() throws Exception { + File dir = temp.newFolder(); + FileUtils.write(new File(dir, "metadata.pb"), "metadata content"); + FileUtils.write(new File(dir, "issues-3.pb"), "issues of component 3"); + FileUtils.write(new File(dir, "component-42.pb"), "details of component 42"); + + FileStructure structure = new FileStructure(dir); + assertThat(structure.metadataFile()).exists().isFile(); + assertThat(structure.fileFor(FileStructure.Domain.COMPONENT, 42)).exists().isFile(); + assertThat(structure.fileFor(FileStructure.Domain.ISSUES, 3)).exists().isFile(); + assertThat(structure.fileFor(FileStructure.Domain.ISSUES, 42)).doesNotExist(); + } +} diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/ReportHelperTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/ReportHelperTest.java deleted file mode 100644 index d79fb5a96d8..00000000000 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/ReportHelperTest.java +++ /dev/null @@ -1,55 +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.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.batch.protocol.output.component.ReportComponent; -import org.sonar.batch.protocol.output.component.ReportComponents; -import org.sonar.batch.protocol.output.issue.ReportIssue; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ReportHelperTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Test - public void createAndRead() throws IOException { - ReportHelper helper = ReportHelper.create(temp.newFolder()); - - helper.saveComponents(new ReportComponents().setRoot(new ReportComponent().setBatchId(1L))); - - helper.saveIssues(1L, Arrays.asList(new ReportIssue().setRuleKey("foo", "bar"))); - - assertThat(new File(helper.reportRootDir(), "components.json")).exists(); - assertThat(new File(helper.reportRootDir(), "1/issues-1.json")).exists(); - - assertThat(helper.getComponents().root().batchId()).isEqualTo(1L); - assertThat(helper.getIssues(1L)).hasSize(1); - } - -} diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/component/ReportComponentsTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/component/ReportComponentsTest.java deleted file mode 100644 index 0df08d6d0df..00000000000 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/component/ReportComponentsTest.java +++ /dev/null @@ -1,98 +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.component; - -import org.apache.commons.io.IOUtils; -import org.junit.Test; -import org.skyscreamer.jsonassert.JSONAssert; -import org.sonar.batch.protocol.output.component.ReportComponent.Type; - -import java.text.SimpleDateFormat; -import java.util.Date; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ReportComponentsTest { - - @Test - public void to_json() throws Exception { - ReportComponents res = new ReportComponents(); - Date d = new SimpleDateFormat("dd/MM/yyyy").parse("12/12/2012"); - res.setAnalysisDate(d); - ReportComponent root = new ReportComponent() - .setBatchId(1) - .setId(11) - .setName("Root project") - .setSnapshotId(111) - .setType(Type.PRJ); - ReportComponent module = new ReportComponent() - .setBatchId(2) - .setId(22) - .setName("Module") - .setSnapshotId(222) - .setPath("module1") - .setType(Type.MOD); - root.addChild(module); - ReportComponent dir = new ReportComponent() - .setBatchId(3) - .setId(33) - .setName("src") - .setSnapshotId(333) - .setPath("src") - .setType(Type.DIR); - module.addChild(dir); - ReportComponent file = new ReportComponent() - .setBatchId(4) - .setId(44) - .setName("Foo.java") - .setSnapshotId(444) - .setPath("Foo.java") - .setType(Type.FIL) - .setTest(true) - .setLanguageKey("java"); - dir.addChild(file); - res.setRoot(root); - - JSONAssert - .assertEquals( - IOUtils.toString(this.getClass().getResourceAsStream("ReportComponentsTest/expected.json"), "UTF-8"), - res.toJson(), true); - } - - @Test - public void from_json() throws Exception { - ReportComponents res = ReportComponents - .fromJson( - IOUtils.toString(this.getClass().getResourceAsStream("ReportComponentsTest/expected.json"), "UTF-8")); - - assertThat(res.analysisDate()).isEqualTo(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/2012")); - ReportComponent root = res.root(); - assertThat(root.batchId()).isEqualTo(1); - assertThat(root.id()).isEqualTo(11); - assertThat(root.name()).isEqualTo("Root project"); - assertThat(root.snapshotId()).isEqualTo(111); - assertThat(root.path()).isNull(); - assertThat(root.type()).isEqualTo(Type.PRJ); - assertThat(root.children()).hasSize(1); - assertThat(root.isTest()).isNull(); - assertThat(root.languageKey()).isNull(); - - } -} diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/issue/ReportIssueTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/issue/ReportIssueTest.java deleted file mode 100644 index 36770b20e9f..00000000000 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/issue/ReportIssueTest.java +++ /dev/null @@ -1,86 +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.issue; - -import org.junit.Test; - -import java.text.SimpleDateFormat; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ReportIssueTest { - - @Test - public void testGetterSetter() throws Exception { - SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); - ReportIssue issue = new ReportIssue() - .setActionPlanKey("plan") - .setAssignee("assignee") - .setAuthorLogin("author") - .setChanged(true) - .setChecksum("checksum") - .setDebt(3L) - .setDiffFields("diff") - .setEffortToFix(2.0) - .setAttributes("attributes") - .setCloseDate(sdf.parse("11/12/2012")) - .setCreationDate(sdf.parse("12/12/2012")) - .setUpdateDate(sdf.parse("13/12/2012")) - .setKey("key") - .setLine(3) - .setManualSeverity(true) - .setMessage("message") - .setNew(true) - .setReporter("reporter") - .setResolution("resolution") - .setComponentBatchId(4L) - .setRuleKey("repo", "rule") - .setSelectedAt(234L) - .setSeverity("severity") - .setStatus("status"); - - assertThat(issue.actionPlanKey()).isEqualTo("plan"); - assertThat(issue.assignee()).isEqualTo("assignee"); - assertThat(issue.authorLogin()).isEqualTo("author"); - assertThat(issue.isChanged()).isTrue(); - assertThat(issue.checksum()).isEqualTo("checksum"); - assertThat(issue.debt()).isEqualTo(3L); - assertThat(issue.diffFields()).isEqualTo("diff"); - assertThat(issue.effortToFix()).isEqualTo(2.0); - assertThat(issue.issueAttributes()).isEqualTo("attributes"); - assertThat(issue.closeDate()).isEqualTo(sdf.parse("11/12/2012")); - assertThat(issue.creationDate()).isEqualTo(sdf.parse("12/12/2012")); - assertThat(issue.updateDate()).isEqualTo(sdf.parse("13/12/2012")); - assertThat(issue.key()).isEqualTo("key"); - assertThat(issue.line()).isEqualTo(3); - assertThat(issue.isManualSeverity()).isTrue(); - assertThat(issue.message()).isEqualTo("message"); - assertThat(issue.isNew()).isTrue(); - assertThat(issue.reporter()).isEqualTo("reporter"); - assertThat(issue.resolution()).isEqualTo("resolution"); - assertThat(issue.componentBatchId()).isEqualTo(4L); - assertThat(issue.ruleRepo()).isEqualTo("repo"); - assertThat(issue.ruleKey()).isEqualTo("rule"); - assertThat(issue.selectedAt()).isEqualTo(234L); - assertThat(issue.severity()).isEqualTo("severity"); - assertThat(issue.status()).isEqualTo("status"); - } - -} |