1 | package org.openstreetmap.josm.test;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.StringReader;
|
---|
5 | import java.io.StringWriter;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import junit.framework.TestCase;
|
---|
11 |
|
---|
12 | import org.jdom.Element;
|
---|
13 | import org.jdom.JDOMException;
|
---|
14 | import org.jdom.input.SAXBuilder;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.Key;
|
---|
17 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.Track;
|
---|
21 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
22 | import org.openstreetmap.josm.test.framework.Bug;
|
---|
23 | import org.openstreetmap.josm.test.framework.DataSetTestCaseHelper;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Test various problems with generation of OSM-XML
|
---|
27 | * @author Imi
|
---|
28 | */
|
---|
29 | public class OsmWriterTest extends TestCase {
|
---|
30 |
|
---|
31 | private Node n1;
|
---|
32 | private Node n2;
|
---|
33 | private Node n3;
|
---|
34 | private Node n4;
|
---|
35 | private Node n5;
|
---|
36 | private LineSegment ls1;
|
---|
37 | private LineSegment ls2;
|
---|
38 | private LineSegment ls3;
|
---|
39 | private Track w;
|
---|
40 |
|
---|
41 | private DataSet ds;
|
---|
42 | private Element osm;
|
---|
43 | private List<Element> nodes;
|
---|
44 | private List<Element> lineSegments;
|
---|
45 | private List<Element> ways;
|
---|
46 | private StringWriter out;
|
---|
47 |
|
---|
48 |
|
---|
49 | public void testNode() throws Exception {
|
---|
50 | ds = new DataSet();
|
---|
51 | Node n = DataSetTestCaseHelper.createNode(ds);
|
---|
52 | n.id = 42;
|
---|
53 | reparse();
|
---|
54 | assertEquals(42, Long.parseLong(getAttr(osm, "node", 0, "id")));
|
---|
55 | assertEquals(n.coor.lat, Double.parseDouble(getAttr(osm, "node", 0, "lat")));
|
---|
56 | assertEquals(n.coor.lon, Double.parseDouble(getAttr(osm, "node", 0, "lon")));
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | public void testLineSegment() throws Exception {
|
---|
61 | ds = new DataSet();
|
---|
62 | LineSegment ls = DataSetTestCaseHelper.createLineSegment(ds, DataSetTestCaseHelper.createNode(ds), DataSetTestCaseHelper.createNode(ds));
|
---|
63 | ls.put(Key.get("foo"), "bar");
|
---|
64 | reparse();
|
---|
65 | assertEquals(1, lineSegments.size());
|
---|
66 | assertEquals("foo", getAttr(osm.getChild("segment"), "tag", 0, "k"));
|
---|
67 | assertEquals("bar", getAttr(osm.getChild("segment"), "tag", 0, "v"));
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Test that the id generation creates unique ids and all are negative
|
---|
73 | */
|
---|
74 | @SuppressWarnings("unchecked")
|
---|
75 | public void testIDGenerationUniqueNegative() {
|
---|
76 | Set<Long> ids = new HashSet<Long>();
|
---|
77 | for (Element e : (List<Element>)osm.getChildren()) {
|
---|
78 | long id = Long.parseLong(e.getAttributeValue("id"));
|
---|
79 | assertTrue("id "+id+" is negative", id < 0);
|
---|
80 | ids.add(id);
|
---|
81 | }
|
---|
82 | assertEquals(nodes.size()+lineSegments.size()+ways.size(), ids.size());
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Verify that generated ids of higher level primitives point to the
|
---|
87 | * generated lower level ids (ways point to segments which point to nodes).
|
---|
88 | */
|
---|
89 | @Bug(47)
|
---|
90 | public void testIDGenerationReferences() {
|
---|
91 | long id1 = Long.parseLong(getAttr(osm, "node", 0, "id"));
|
---|
92 | long id2 = Long.parseLong(getAttr(osm, "node", 1, "id"));
|
---|
93 | long lsFrom = Long.parseLong(getAttr(osm, "segment", 0, "from"));
|
---|
94 | long lsTo = Long.parseLong(getAttr(osm, "segment", 0, "to"));
|
---|
95 | assertEquals(id1, lsFrom);
|
---|
96 | assertEquals(id2, lsTo);
|
---|
97 | assertEquals(id2, lsTo);
|
---|
98 |
|
---|
99 | long ls1 = Long.parseLong(getAttr(osm, "segment", 0, "id"));
|
---|
100 | long ls2 = Long.parseLong(getAttr(osm, "segment", 1, "id"));
|
---|
101 | long t1 = Long.parseLong(getAttr(osm.getChild("way"), "seg", 0, "id"));
|
---|
102 | long t2 = Long.parseLong(getAttr(osm.getChild("way"), "seg", 1, "id"));
|
---|
103 | assertEquals(ls1, t1);
|
---|
104 | assertEquals(ls2, t2);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Verify that deleted objects that are not uploaded to the server does not show up
|
---|
109 | * in xml save output at all.
|
---|
110 | */
|
---|
111 | @Bug(47)
|
---|
112 | public void testDeleteNewDoesReallyRemove() throws IOException, JDOMException {
|
---|
113 | ds.tracks.iterator().next().setDeleted(true);
|
---|
114 | reparse();
|
---|
115 | //assertEquals(0, deleted.size());
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Verify that action tag is set correctly.
|
---|
121 | */
|
---|
122 | public void testActionTag() throws IOException, JDOMException {
|
---|
123 | int id = 1;
|
---|
124 | for (OsmPrimitive osm : ds.allPrimitives())
|
---|
125 | osm.id = id++; // make all objects "old".
|
---|
126 | n1.setDeleted(true);
|
---|
127 | ls1.modified = true;
|
---|
128 | ls1.modifiedProperties = true;
|
---|
129 | ls3.modified = true;
|
---|
130 | w.modifiedProperties = true;
|
---|
131 | reparse();
|
---|
132 |
|
---|
133 | boolean foundNode = false;
|
---|
134 | for (Element n : nodes) {
|
---|
135 | if (n.getAttributeValue("id").equals(""+n1.id)) {
|
---|
136 | assertEquals("delete", n.getAttributeValue("action"));
|
---|
137 | foundNode = true;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | assertTrue("Node found in output", foundNode);
|
---|
141 |
|
---|
142 | boolean foundLs1 = false;
|
---|
143 | boolean foundLs3 = false;
|
---|
144 | for (Element lsElem : lineSegments) {
|
---|
145 | String idStr = lsElem.getAttributeValue("id");
|
---|
146 | String action = lsElem.getAttributeValue("action");
|
---|
147 | if (idStr.equals(""+ls1.id)) {
|
---|
148 | assertEquals("Attribute action on modified data is ok", "modify", action);
|
---|
149 | foundLs1 = true;
|
---|
150 | } else if (idStr.equals(""+ls3.id)) {
|
---|
151 | assertEquals("Attribute action on modified/object data is ok", "modify/object", action);
|
---|
152 | foundLs3 = true;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | assertTrue("LineSegments found in output", foundLs1 && foundLs3);
|
---|
156 |
|
---|
157 | assertEquals("Way found in output", 1, ways.size());
|
---|
158 | assertEquals("Attribute action on modifiedProperty data is ok", "modify/property", ways.get(0).getAttributeValue("action"));
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Override
|
---|
162 | protected void setUp() throws Exception {
|
---|
163 | super.setUp();
|
---|
164 |
|
---|
165 | // create some data
|
---|
166 | ds = new DataSet();
|
---|
167 | n1 = DataSetTestCaseHelper.createNode(ds);
|
---|
168 | n2 = DataSetTestCaseHelper.createNode(ds);
|
---|
169 | n3 = DataSetTestCaseHelper.createNode(ds);
|
---|
170 | n4 = DataSetTestCaseHelper.createNode(ds);
|
---|
171 | n5 = DataSetTestCaseHelper.createNode(ds);
|
---|
172 | ls1 = DataSetTestCaseHelper.createLineSegment(ds, n1, n2);
|
---|
173 | ls2 = DataSetTestCaseHelper.createLineSegment(ds, n2, n3);
|
---|
174 | ls3 = DataSetTestCaseHelper.createLineSegment(ds, n4, n5);
|
---|
175 | w = DataSetTestCaseHelper.createWay(ds, ls1, ls2);
|
---|
176 |
|
---|
177 | reparse();
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Get an attribute out of an object of the root element.
|
---|
182 | */
|
---|
183 | private String getAttr(Element root, String objName, int objPos, String attrName) {
|
---|
184 | Element e = (Element)root.getChildren(objName).get(objPos);
|
---|
185 | return e.getAttributeValue(attrName);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Reparse the dataset into the lists members..
|
---|
190 | */
|
---|
191 | @SuppressWarnings("unchecked")
|
---|
192 | private void reparse() throws IOException, JDOMException {
|
---|
193 | out = new StringWriter();
|
---|
194 | OsmWriter.output(out, ds, false);
|
---|
195 |
|
---|
196 | // reparse
|
---|
197 | osm = new SAXBuilder().build(new StringReader(out.toString())).getRootElement();
|
---|
198 | nodes = osm.getChildren("node");
|
---|
199 | lineSegments = osm.getChildren("segment");
|
---|
200 | ways = osm.getChildren("way");
|
---|
201 | }
|
---|
202 | }
|
---|