source: josm/trunk/test/unit/org/openstreetmap/josm/io/OsmChangesetContentParserTest.groovy@ 6774

Last change on this file since 6774 was 6552, checked in by simon04, 11 years ago

Refactoring: introduce Utils.UTF_8 charset to avoid handling of UnsupportedEncodingException

According to the Javadoc of Charset, every implementation of the Java
platform is required to support UTF-8.

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