From 7b8354aadb23b8804f8cc14a21bef9f5ea4c49e3 Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Sun, 3 Jan 2021 20:06:01 +0100 Subject: [PATCH] Convert ReadFileTest to unit test --- test.java/crosby/binary/ReadFileExample.java | 86 --- test.java/crosby/binary/ReadFileTest.java | 528 +++++++++++++++++++ 2 files changed, 528 insertions(+), 86 deletions(-) delete mode 100644 test.java/crosby/binary/ReadFileExample.java create mode 100644 test.java/crosby/binary/ReadFileTest.java diff --git a/test.java/crosby/binary/ReadFileExample.java b/test.java/crosby/binary/ReadFileExample.java deleted file mode 100644 index aed1fc0..0000000 --- a/test.java/crosby/binary/ReadFileExample.java +++ /dev/null @@ -1,86 +0,0 @@ -package crosby.binary; - -import crosby.binary.*; -import crosby.binary.Osmformat.*; -import crosby.binary.file.*; -import java.io.*; -import java.util.List; - -/** - * Demonstrates how to read a file. Reads sample.pbf from the resources folder - * and prints details about it to the standard output. - * - * @author Michael Tandy - */ -public class ReadFileExample { - - public static void main(String[] args) throws Exception { - InputStream input = ReadFileExample.class.getResourceAsStream("/sample.pbf"); - BlockReaderAdapter brad = new TestBinaryParser(); - new BlockInputStream(input, brad).process(); - } - - private static class TestBinaryParser extends BinaryParser { - - @Override - protected void parseRelations(List rels) { - if (!rels.isEmpty()) - System.out.println("Got some relations to parse."); - Relation r = null; - } - - @Override - protected void parseDense(DenseNodes nodes) { - long lastId=0; - long lastLat=0; - long lastLon=0; - - for (int i=0 ; i nodes) { - for (Node n : nodes) { - System.out.printf("Regular node, ID %d @ %.6f,%.6f\n", - n.getId(),parseLat(n.getLat()),parseLon(n.getLon())); - } - } - - @Override - protected void parseWays(List ways) { - for (Way w : ways) { - System.out.println("Way ID " + w.getId()); - StringBuilder sb = new StringBuilder(); - sb.append(" Nodes: "); - long lastRef = 0; - for (Long ref : w.getRefsList()) { - lastRef+= ref; - sb.append(lastRef).append(" "); - } - sb.append("\n Key=value pairs: "); - for (int i=0 ; i rels) { + if (!rels.isEmpty()) + writer.println("Got some relations to parse."); + Relation r = null; + } + + @Override + protected void parseDense(DenseNodes nodes) { + long lastId = 0; + long lastLat = 0; + long lastLon = 0; + + for (int i = 0; i < nodes.getIdCount(); i++) { + lastId += nodes.getId(i); + lastLat += nodes.getLat(i); + lastLon += nodes.getLon(i); + writer.printf("Dense node, ID %d @ %.6f,%.6f%n", + lastId, parseLat(lastLat), parseLon(lastLon)); + } + } + + @Override + protected void parseNodes(List nodes) { + for (Node n : nodes) { + writer.printf("Regular node, ID %d @ %.6f,%.6f%n", + n.getId(), parseLat(n.getLat()), parseLon(n.getLon())); + } + } + + @Override + protected void parseWays(List ways) { + for (Way w : ways) { + writer.println("Way ID " + w.getId()); + StringBuilder sb = new StringBuilder(); + sb.append(" Nodes: "); + long lastRef = 0; + for (Long ref : w.getRefsList()) { + lastRef += ref; + sb.append(lastRef).append(" "); + } + sb.append("\n Key=value pairs: "); + for (int i = 0; i < w.getKeysCount(); i++) { + sb.append(getStringById(w.getKeys(i))).append("=") + .append(getStringById(w.getVals(i))).append(" "); + } + writer.println(sb.toString()); + } + } + + @Override + protected void parse(HeaderBlock header) { + writer.println("Got header block."); + } + + public void complete() { + writer.println("Complete!"); + } + + } + +}