1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import org.junit.Test
|
---|
5 |
|
---|
6 | import static org.junit.Assert.*;
|
---|
7 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
9 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
10 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
|
---|
11 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.history.HistoryRelation;
|
---|
13 | import org.openstreetmap.josm.data.osm.history.HistoryWay;
|
---|
14 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
15 | import org.openstreetmap.josm.io.OsmChangesetContentParser;
|
---|
16 |
|
---|
17 |
|
---|
18 | class OsmChangesetContentParserTest {
|
---|
19 | final shouldFail = new GroovyTestCase().&shouldFail
|
---|
20 |
|
---|
21 | @Test
|
---|
22 | public void test_Constructor() {
|
---|
23 | OsmChangesetContentParser parser
|
---|
24 |
|
---|
25 | // should be OK
|
---|
26 | parser = new OsmChangesetContentParser(new ByteArrayInputStream("".bytes))
|
---|
27 |
|
---|
28 | shouldFail(IllegalArgumentException) {
|
---|
29 | parser = new OsmChangesetContentParser(null)
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | @Test
|
---|
35 | public void test_parse_arguments() {
|
---|
36 | OsmChangesetContentParser parser
|
---|
37 |
|
---|
38 | def String doc = """
|
---|
39 | <osmChange version="0.6" generator="OpenStreetMap server">
|
---|
40 | </osmChange>
|
---|
41 | """
|
---|
42 |
|
---|
43 | // should be OK
|
---|
44 | parser = new OsmChangesetContentParser(new ByteArrayInputStream(doc.getBytes("UTF-8")))
|
---|
45 | parser.parse null
|
---|
46 |
|
---|
47 | // should be OK
|
---|
48 | parser = new OsmChangesetContentParser(new ByteArrayInputStream(doc.getBytes("UTF-8")))
|
---|
49 | parser.parse NullProgressMonitor.INSTANCE
|
---|
50 |
|
---|
51 | // should be OK
|
---|
52 | parser = new OsmChangesetContentParser(doc)
|
---|
53 | parser.parse null
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * A simple changeset content document with one created node
|
---|
58 | *
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | public void test_OK_OneCreatedNode() {
|
---|
62 | OsmChangesetContentParser parser
|
---|
63 |
|
---|
64 | def String doc = """
|
---|
65 | <osmChange version="0.6" generator="OpenStreetMap server">
|
---|
66 | <create>
|
---|
67 | <node id="1" version="1" visible="true" changeset="1" lat="1.0" lon="1.0" timestamp="2009-12-22" />
|
---|
68 | </create>
|
---|
69 | </osmChange>
|
---|
70 | """
|
---|
71 |
|
---|
72 | // should be OK
|
---|
73 | parser = new OsmChangesetContentParser(doc)
|
---|
74 | ChangesetDataSet ds = parser.parse()
|
---|
75 |
|
---|
76 | assert ds.size() == 1
|
---|
77 | HistoryOsmPrimitive p = ds.getPrimitive(new SimplePrimitiveId(1, OsmPrimitiveType.NODE));
|
---|
78 | assert p != null
|
---|
79 | assert p.getId() == 1
|
---|
80 | assert p.getVersion() == 1
|
---|
81 | assert p.getChangesetId() == 1
|
---|
82 | assert p.getTimestamp() != null
|
---|
83 | assert ds.getModificationType(p.getPrimitiveId()) == ChangesetModificationType.CREATED
|
---|
84 | assert ds.isCreated(p.getPrimitiveId())
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * A simple changeset content document with one updated node
|
---|
89 | *
|
---|
90 | */
|
---|
91 | @Test
|
---|
92 | public void test_OK_OneUpdatedNode() {
|
---|
93 | OsmChangesetContentParser parser
|
---|
94 |
|
---|
95 | def String doc = """
|
---|
96 | <osmChange version="0.6" generator="OpenStreetMap server">
|
---|
97 | <modify>
|
---|
98 | <node id="1" version="1" visible="true" changeset="1" lat="1.0" lon="1.0" timestamp="2009-12-22" />
|
---|
99 | </modify>
|
---|
100 | </osmChange>
|
---|
101 | """
|
---|
102 |
|
---|
103 | // should be OK
|
---|
104 | parser = new OsmChangesetContentParser(doc)
|
---|
105 | ChangesetDataSet ds = parser.parse()
|
---|
106 |
|
---|
107 | assert ds.size() == 1
|
---|
108 | HistoryOsmPrimitive p = ds.getPrimitive(new SimplePrimitiveId(1, OsmPrimitiveType.NODE));
|
---|
109 | assert p != null
|
---|
110 | assert p.getId() == 1
|
---|
111 | assert p.getVersion() == 1
|
---|
112 | assert p.getChangesetId() == 1
|
---|
113 | assert p.getTimestamp() != null
|
---|
114 | assert ds.getModificationType(p.getPrimitiveId()) == ChangesetModificationType.UPDATED
|
---|
115 | assert ds.isUpdated(p.getPrimitiveId())
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * A simple changeset content document with one deleted node
|
---|
120 | *
|
---|
121 | */
|
---|
122 | @Test
|
---|
123 | public void test_OK_OneDeletedNode() {
|
---|
124 | OsmChangesetContentParser parser
|
---|
125 |
|
---|
126 | def String doc = """
|
---|
127 | <osmChange version="0.6" generator="OpenStreetMap server">
|
---|
128 | <delete>
|
---|
129 | <node id="1" version="1" visible="true" changeset="1" lat="1.0" lon="1.0" timestamp="2009-12-22" />
|
---|
130 | </delete>
|
---|
131 | </osmChange>
|
---|
132 | """
|
---|
133 |
|
---|
134 | // should be OK
|
---|
135 | parser = new OsmChangesetContentParser(doc)
|
---|
136 | ChangesetDataSet ds = parser.parse()
|
---|
137 |
|
---|
138 | assert ds.size() == 1
|
---|
139 | HistoryOsmPrimitive p = ds.getPrimitive(new SimplePrimitiveId(1, OsmPrimitiveType.NODE));
|
---|
140 | assert p != null
|
---|
141 | assert p.getId() == 1
|
---|
142 | assert p.getVersion() == 1
|
---|
143 | assert p.getChangesetId() == 1
|
---|
144 | assert p.getTimestamp() != null
|
---|
145 | assert ds.getModificationType(p.getPrimitiveId()) == ChangesetModificationType.DELETED
|
---|
146 | assert ds.isDeleted(p.getPrimitiveId())
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * A more complex test with a document including nodes, ways, and relations.
|
---|
151 | *
|
---|
152 | */
|
---|
153 | @Test
|
---|
154 | public void test_OK_ComplexTestCase() {
|
---|
155 | OsmChangesetContentParser parser
|
---|
156 |
|
---|
157 | def String doc = """
|
---|
158 | <osmChange version="0.6" generator="OpenStreetMap server">
|
---|
159 | <create>
|
---|
160 | <node id="1" version="1" visible="true" changeset="1" lat="1.0" lon="1.0" timestamp="2009-12-22">
|
---|
161 | <tag k="a.key" v="a.value" />
|
---|
162 | </node>
|
---|
163 | </create>
|
---|
164 | <modify>
|
---|
165 | <way id="2" version="2" visible="true" changeset="1" timestamp="2009-12-22">
|
---|
166 | <nd ref="21"/>
|
---|
167 | <nd ref="22"/>
|
---|
168 | </way>
|
---|
169 | </modify>
|
---|
170 | <delete>
|
---|
171 | <relation id="3" version="3" visible="true" changeset="1" timestamp="2009-12-22" />
|
---|
172 | </delete>
|
---|
173 | </osmChange>
|
---|
174 | """
|
---|
175 |
|
---|
176 | // should be OK
|
---|
177 | parser = new OsmChangesetContentParser(doc)
|
---|
178 | ChangesetDataSet ds = parser.parse()
|
---|
179 |
|
---|
180 | assert ds.size() == 3
|
---|
181 |
|
---|
182 | HistoryOsmPrimitive p = ds.getPrimitive(new SimplePrimitiveId(1, OsmPrimitiveType.NODE));
|
---|
183 | assert p != null
|
---|
184 | assert p.getId() == 1
|
---|
185 | assert p.getVersion() == 1
|
---|
186 | assert p.getChangesetId() == 1
|
---|
187 | assert p.getTimestamp() != null
|
---|
188 | assert ds.getModificationType(p.getPrimitiveId()) == ChangesetModificationType.CREATED
|
---|
189 | assert ds.isCreated(p.getPrimitiveId())
|
---|
190 | assert p.get("a.key") == "a.value"
|
---|
191 |
|
---|
192 | HistoryWay w = (HistoryWay)ds.getPrimitive(new SimplePrimitiveId(2, OsmPrimitiveType.WAY));
|
---|
193 | assert w != null
|
---|
194 | assert w.getId() == 2
|
---|
195 | assert w.getVersion() == 2
|
---|
196 | assert w.getChangesetId() == 1
|
---|
197 | assert w.getTimestamp() != null
|
---|
198 | assert ds.getModificationType(w.getPrimitiveId()) == ChangesetModificationType.UPDATED
|
---|
199 | assert ds.isUpdated(w.getPrimitiveId())
|
---|
200 | assert w.getNumNodes() == 2
|
---|
201 | assert w.getNodes() == [21,22]
|
---|
202 |
|
---|
203 | HistoryRelation r = (HistoryRelation)ds.getPrimitive(new SimplePrimitiveId(3, OsmPrimitiveType.RELATION));
|
---|
204 | assert r != null
|
---|
205 | assert r.getId() == 3
|
---|
206 | assert r.getVersion() == 3
|
---|
207 | assert r.getChangesetId() == 1
|
---|
208 | assert r.getTimestamp() != null
|
---|
209 | assert ds.getModificationType(r.getPrimitiveId()) == ChangesetModificationType.DELETED
|
---|
210 | assert ds.isDeleted(r.getPrimitiveId())
|
---|
211 | }
|
---|
212 | }
|
---|