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