NodeWriter extends EntityWriter { public void process(Node node) { Collection<Tag> tags; beginOpenElement(); addCommonAttributes(node); if (!Double.isNaN(node.getLatitude())) { addAttribute("lat", numberFormat.format(node.getLatitude())); } if (!Double.isNaN(node.getLongitude())) { addAttribute("lon", numberFormat.format(node.getLongitude())); } addMetatags(node); tags = node.getTags(); if (tags.size() > 0) { endOpenElement(false); for (Tag tag : tags) { tagWriter.process(tag); } closeElement(); } else { endOpenElement(true); } } NodeWriter(String elementName, int indentLevel); void process(Node node); @Override void setWriter(final Writer writer);  }
@Test public final void testProcessNormalNode() { Node node = new Node( new CommonEntityData(1234, 2, timestamp, new OsmUser(23, "someuser"), 0), 20.12345678, -21.98765432); node.getTags().add(new Tag("nodekey", "nodevalue")); testNodeWriter.process(node); try { testBufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); fail("IOException"); } String[] strArray = testWriter.toString().split("\\n", 3); assertTrue("Node opening element does not match.", strArray[0].matches(nodeOpeningMatch)); assertTrue("Node tag does not match.", strArray[1].matches(nodeTagMatch)); assertTrue("Node closing element does not match.", strArray[2].matches(nodeClosingMatch)); }
@Test public final void testProcessNodeNoTags() { testNodeWriter.process( new Node( new CommonEntityData( 1234, 2, timestamp, new OsmUser(23, "someuser"), 0, new ArrayList<Tag>()), 20.12345678, -21.98765432)); try { testBufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); fail("IOException"); } String regexMatch = "^\\s*<node\\s*" + "id=['\"]1234['\"]\\s*" + "version=['\"]2['\"]\\s*" + "timestamp=['\"]2013-10-07T10:24:31Z?['\"]\\s*" + "uid=['\"]23['\"]\\s*" + "user=['\"]someuser['\"]\\s*" + "lat=['\"]20.1234568['\"]\\s*" + "lon=['\"]-21.9876543['\"]\\s*" + "/>\\s*$"; assertTrue(testWriter.toString().matches(regexMatch)); }
@Test public final void testProcessNodeWithNoUser() { Node node = new Node(new CommonEntityData(1234, 2, timestamp, OsmUser.NONE, 0), 20.12345678, -21.98765432); node.getTags().add(new Tag("nodekey", "nodevalue")); testNodeWriter.process(node); try { testBufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); fail("IOException"); } String nodeOpeningNoUserMatch = "^\\s*<node\\s*" + "id=['\"]1234['\"]\\s*" + "version=['\"]2['\"]\\s*" + "timestamp=['\"]2013-10-07T10:24:31Z?['\"]\\s*" + "lat=['\"]20.1234568['\"]\\s*" + "lon=['\"]-21.9876543['\"]\\s*" + ">\\s*"; String[] strArray = testWriter.toString().split("\\n", 3); assertTrue("Node opening element does not match.", strArray[0].matches(nodeOpeningNoUserMatch)); assertTrue("Node tag does not match.", strArray[1].matches(nodeTagMatch)); assertTrue("Node closing element does not match.", strArray[2].matches(nodeClosingMatch)); }