diff --git a/resources/GranFileBlock1-deflate.osm.pbf b/resources/GranFileBlock1-deflate.osm.pbf new file mode 100644 index 0000000..4b99440 Binary files /dev/null and b/resources/GranFileBlock1-deflate.osm.pbf differ diff --git a/resources/GranFileBlock1-none.osm.pbf b/resources/GranFileBlock1-none.osm.pbf new file mode 100644 index 0000000..035808a Binary files /dev/null and b/resources/GranFileBlock1-none.osm.pbf differ diff --git a/resources/SimpleFileBlock1-deflate.osm.pbf b/resources/SimpleFileBlock1-deflate.osm.pbf new file mode 100644 index 0000000..39c26b2 Binary files /dev/null and b/resources/SimpleFileBlock1-deflate.osm.pbf differ diff --git a/resources/SimpleFileBlock1-none.osm.pbf b/resources/SimpleFileBlock1-none.osm.pbf new file mode 100644 index 0000000..0f8456c Binary files /dev/null and b/resources/SimpleFileBlock1-none.osm.pbf differ diff --git a/test.java/crosby/binary/BuildTestFile.java b/test.java/crosby/binary/WriteFileTest.java similarity index 79% rename from test.java/crosby/binary/BuildTestFile.java rename to test.java/crosby/binary/WriteFileTest.java index edb62e6..b00a584 100644 --- a/test.java/crosby/binary/BuildTestFile.java +++ b/test.java/crosby/binary/WriteFileTest.java @@ -1,19 +1,19 @@ -/** 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 . - -*/ +/** + * 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();