RelationWriter extends EntityWriter { public void process(Relation relation) { List<RelationMember> relationMembers; Collection<Tag> tags; beginOpenElement(); addCommonAttributes(relation); addMetatags(relation); relationMembers = relation.getMembers(); tags = relation.getTags(); if (relationMembers.size() > 0 || tags.size() > 0) { endOpenElement(false); for (RelationMember relationMember : relationMembers) { relationMemberWriter.processRelationMember(relationMember); } for (Tag tag : tags) { tagWriter.process(tag); } closeElement(); } else { endOpenElement(true); } } RelationWriter(String elementName, int indentLevel); void process(Relation relation); @Override void setWriter(final Writer writer);  }
@Test public final void testProcessNormalRelation() { Relation relation = new Relation(new CommonEntityData(1234, 2, timestamp, new OsmUser(23, "someuser"), 0)); relation.getMembers().add(new RelationMember(2345, EntityType.Node, "noderole")); relation.getMembers().add(new RelationMember(3456, EntityType.Way, "wayrole")); relation.getMembers().add(new RelationMember(4567, EntityType.Relation, "relationrole")); relation.getTags().add(new Tag("relationkey", "relationvalue")); testRelationWriter.process(relation); try { testBufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); fail("IOException"); } String[] strArray = testWriter.toString().split("\\n", 6); assertTrue("Relation opening element does not match.", strArray[0].matches(relationOpeningMatch)); assertTrue("Relation member node does not match.", strArray[1].matches(nodeMemberMatch)); assertTrue("Relation member way does not match.", strArray[2].matches(wayMemberMatch)); assertTrue("Relation member relation does not match.", strArray[3].matches(relationMemberMatch)); assertTrue("Relation tag does not match.", strArray[4].matches(relationTagMatch)); assertTrue("Relation closing element does not match.", strArray[5].matches(relationClosingMatch)); }
@Test public final void testProcessRelationWithNoUser() { Relation relation = new Relation(new CommonEntityData(1234, 2, timestamp, OsmUser.NONE, 0)); relation.getMembers().add(new RelationMember(2345, EntityType.Node, "noderole")); relation.getMembers().add(new RelationMember(3456, EntityType.Way, "wayrole")); relation.getMembers().add(new RelationMember(4567, EntityType.Relation, "relationrole")); relation.getTags().add(new Tag("relationkey", "relationvalue")); testRelationWriter.process(relation); try { testBufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); fail("IOException"); } String relationOpeningNoUserMatch = "^\\s*<relation\\s*" + "id=['\"]1234['\"]\\s*" + "version=['\"]2['\"]\\s*" + "timestamp=['\"]2013-10-07T10:24:31Z?['\"]\\s*" + ">\\s*"; String[] strArray = testWriter.toString().split("\\n", 6); assertTrue(strArray[0].matches(relationOpeningNoUserMatch)); assertTrue(strArray[1].matches(nodeMemberMatch)); assertTrue(strArray[2].matches(wayMemberMatch)); assertTrue(strArray[3].matches(relationMemberMatch)); assertTrue(strArray[4].matches(relationTagMatch)); assertTrue(strArray[5].matches(relationClosingMatch)); }
@Test public final void testProcessRelationNoTags() { Relation relation = new Relation(new CommonEntityData(1234, 2, timestamp, new OsmUser(23, "someuser"), 0)); relation.getMembers().add(new RelationMember(2345, EntityType.Node, "noderole")); relation.getMembers().add(new RelationMember(3456, EntityType.Way, "wayrole")); relation.getMembers().add(new RelationMember(4567, EntityType.Relation, "relationrole")); testRelationWriter.process(relation); try { testBufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); fail("IOException"); } String[] strArray = testWriter.toString().split("\\n", 5); assertTrue(strArray[0].matches(relationOpeningMatch)); assertTrue(strArray[1].matches(nodeMemberMatch)); assertTrue(strArray[2].matches(wayMemberMatch)); assertTrue(strArray[3].matches(relationMemberMatch)); assertTrue(strArray[4].matches(relationClosingMatch)); }