From 26d0a8328deab87ebdf98e2e54514143ae8c6058 Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Sun, 3 Jan 2021 20:06:01 +0100 Subject: [PATCH] Convert WriteFileTest to unit test --- resources/GranFileBlock1-deflate.osm.pbf | Bin 0 -> 374 bytes resources/GranFileBlock1-none.osm.pbf | Bin 0 -> 477 bytes resources/SimpleFileBlock1-deflate.osm.pbf | Bin 0 -> 480 bytes resources/SimpleFileBlock1-none.osm.pbf | Bin 0 -> 603 bytes ...{BuildTestFile.java => WriteFileTest.java} | 108 ++++++++++-------- 5 files changed, 59 insertions(+), 49 deletions(-) create mode 100644 resources/GranFileBlock1-deflate.osm.pbf create mode 100644 resources/GranFileBlock1-none.osm.pbf create mode 100644 resources/SimpleFileBlock1-deflate.osm.pbf create mode 100644 resources/SimpleFileBlock1-none.osm.pbf rename test.java/crosby/binary/{BuildTestFile.java => WriteFileTest.java} (79%) diff --git a/resources/GranFileBlock1-deflate.osm.pbf b/resources/GranFileBlock1-deflate.osm.pbf new file mode 100644 index 0000000000000000000000000000000000000000..4b99440a1efb216c8484eb68aa6422c744096e8b GIT binary patch literal 374 zcmZQzVBqEA^bhv+NKH&hEt0Sh(2+8!m=pZyv;H|=zjGdEeXsBuUoZ}SROES9+wbdX ze^1?aNl$on&ib4y(>kSpzWDTd1_l#%zGVyy3_M)yK!aQoOA;j}G76kvlfOF0&joaWyzA;8VrRy4IL$2K2* zD7F8%(NDf(ymN#l`Z>-g)Lh>Ex$70vF=K!EJ)c<_I-=a-fez$GIIvb=1*232!g-%? zJMTn`cMHgQifL|cX_{$jX=3f%f8@BI?A&3|cji&yjwA0kdd%}~cq|dXs=Qi6dG=w` ny8hHpvOV@Y)ePQ=%|E!~U+`<)xvV|GW%++aIT)sYG7tg)1RR%( literal 0 HcmV?d00001 diff --git a/resources/GranFileBlock1-none.osm.pbf b/resources/GranFileBlock1-none.osm.pbf new file mode 100644 index 0000000000000000000000000000000000000000..035808a24ce3dda9108ef772f01d7ef1e27f975e GIT binary patch literal 477 zcmZQzVBqEA^bhv+NKH&hEs`+c(oy2`FU}24&PdHo)D1JxGgIPnNzE%x^~+C5E$(9E z4J^$}&UPxwFVAz!uMp5-U|`_kVh0-Fl30=`agLGe1S3}z7Xufkv!Q=lT5)QLA%bay zU>YNsCJ3e}f@y|enj@GNLLppo9EWD{CkSpzWDTd1_l#%zGVyy3_M)yK!aQoOA;l1F$(NplKR3}G3RmD zqofCqN{qhvdu#jY8oC(t3iu?mTxL0F;%abKz^9nyvXjSD@0Q0Riv^SyizrWaKD=cA zRk{E1@;Rwb9=oyn{{^LAs=;VMH#beV-PQKO&l93UfId?{*jb>Wk`ZG>dUW;VRBj-fSm=PJVHzMf9 zjT=o75)u+K8xB=RD(V#^B_)4PTJ?Y{DJe;4llZ2mkM*^3H+9rP}_PxnX{vo w>BPQc?>F)sv13iS|KNLs;{Ag=8jIN;&%QS|JVe}JUgfU`bM+ZsX_`L)04*}c%m4rY literal 0 HcmV?d00001 diff --git a/resources/SimpleFileBlock1-none.osm.pbf b/resources/SimpleFileBlock1-none.osm.pbf new file mode 100644 index 0000000000000000000000000000000000000000..0f8456c9b32ad8c3453346a66ad1241c5bf4b581 GIT binary patch literal 603 zcmZQzVBqEA^bhv+NKH&hEs`+c(oy2`FU}24&PdHo)D1JxGgIPnNzE%x^~+C5E$(9E z4J^$}&UPxwFVAz!uMp5-U|`_kVh0-Fl30=`v5$#s2NPEm7Xufklc9fFT5)QLA%bay zU>YNsCJ3e}f@y|enj@GNLaJOM9H$tC7#XD)nH(A>{jK)+|N3AnBNs1F)L~BRV{VU? z8`g^nIY|j~oMaSYVq%tJVq|e*JH^PvD55SU!*TiVe+U3dvam`qF|#?bFJlHuUT2Zw z;F!ql#0=y~F>`!n7SdA^<+#b>#KOeP!s^A6!p6oH#KOS9$OIJL%L;LU6B8S|7gGgS z5EB!VkiC#C7w0AZJB;(?KXoxW2{i23QFNepJLCWDJBmQqO8`V3n6{A# - - This program 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. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -*/ +/** + * Copyright (c) 2010 Scott A. Crosby. + *

+ * This program 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. + *

+ * This program 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 General Public License for more details. + *

+ * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package crosby.binary; @@ -23,12 +23,47 @@ import crosby.binary.Osmformat.StringTable; import crosby.binary.Osmformat.Relation.MemberType; import crosby.binary.file.BlockOutputStream; import crosby.binary.file.FileBlock; +import org.junit.Assert; +import org.junit.Test; -import java.io.FileOutputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; -public class BuildTestFile { - BlockOutputStream output; +public class WriteFileTest { + + @Test + public void testSimpleFileBlock1() throws Exception { + test("deflate", "/SimpleFileBlock1-deflate.osm.pbf"); + test("none", "/SimpleFileBlock1-none.osm.pbf"); + } + + @Test + public void testGranFileBlock1() throws Exception { + test("deflate", "/GranFileBlock1-deflate.osm.pbf"); + test("none", "/GranFileBlock1-none.osm.pbf"); + } + + private void test(String compress, String resource) throws IOException, URISyntaxException { + try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) { + BuildTestFile builder = new BuildTestFile(new BlockOutputStream(bytes), compress); + if (resource.startsWith("/SimpleFileBlock1")) { + builder.makeSimpleFileBlock1(); + } else if (resource.startsWith("/GranFileBlock1")) { + builder.makeGranFileBlock1(); + } else { + throw new IllegalArgumentException(); + } + byte[] expected = Files.readAllBytes(Paths.get(WriteFileTest.class.getResource(resource).toURI())); + Assert.assertArrayEquals(expected, bytes.toByteArray()); + } + } +} + +class BuildTestFile { + private final BlockOutputStream output; public static final long BILLION = 1000000000L; StringTable makeStringTable(String prefix) { @@ -123,37 +158,14 @@ public class BuildTestFile { } - BuildTestFile(String name, String compress) throws IOException { - output = new BlockOutputStream(new FileOutputStream(name)); - output.setCompress(compress); + BuildTestFile(BlockOutputStream output, String compress) throws IOException { + this.output = output; + this.output.setCompress(compress); HeaderBlock.Builder b = HeaderBlock.newBuilder(); b.addRequiredFeatures("OsmSchema-V0.6").addRequiredFeatures("DenseNodes").setSource("QuickBrownFox"); - output.write(FileBlock.newInstance("OSMHeader", b.build().toByteString(), null)); + this.output.write(FileBlock.newInstance("OSMHeader", b.build().toByteString(), null)); } - - public static void main(String[] args) { - try { - BuildTestFile out1a = new BuildTestFile("TestFile1-deflate.osm.pbf", "deflate"); - out1a.makeSimpleFileBlock1(); - out1a.output.close(); - - BuildTestFile out1b = new BuildTestFile("TestFile1-none.osm.pbf", "none"); - out1b.makeSimpleFileBlock1(); - out1b.output.close(); - - BuildTestFile out2 = new BuildTestFile("TestFile2-uncom.osm.pbf", "deflate"); - out2.makeGranFileBlock1(); - out2.output.close(); - - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - void makeGranFileBlock1() throws IOException { PrimitiveBlock.Builder b1 = PrimitiveBlock.newBuilder(); b1.setLatOffset(10 * BILLION + 109208300) @@ -193,9 +205,7 @@ public class BuildTestFile { .setUserSid(4) .build()) .build()) - ) - - ; + ); // The same, but with different granularities. PrimitiveBlock.Builder b2 = PrimitiveBlock.newBuilder();