source: josm/trunk/test/unit/org/openstreetmap/josm/io/OsmReaderTest.java@ 16159

Last change on this file since 16159 was 15470, checked in by Don-vip, 4 years ago

fix #18249 - Allow unknown xml attributes to be added as tags (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 18.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8import static org.junit.Assert.fail;
9
10import java.io.ByteArrayInputStream;
11import java.io.InputStream;
12import java.nio.charset.StandardCharsets;
13import java.nio.file.Files;
14import java.nio.file.Paths;
15import java.util.Arrays;
16
17import org.junit.Rule;
18import org.junit.Test;
19import org.openstreetmap.josm.TestUtils;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.Way;
23import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.testutils.JOSMTestRules;
26
27import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28
29/**
30 * Unit tests of {@link OsmReader} class.
31 */
32public class OsmReaderTest {
33
34 /**
35 * Setup rule
36 */
37 @Rule
38 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
39 public JOSMTestRules test = new JOSMTestRules();
40
41 private static final class PostProcessorStub implements OsmServerReadPostprocessor {
42 boolean called;
43
44 @Override
45 public void postprocessDataSet(DataSet ds, ProgressMonitor progress) {
46 called = true;
47 }
48 }
49
50 /**
51 * Unit test of {@link OsmReader#registerPostprocessor} / {@link OsmReader#deregisterPostprocessor}.
52 * @throws Exception if any error occurs
53 */
54 @Test
55 public void testPostProcessors() throws Exception {
56 PostProcessorStub registered = new PostProcessorStub();
57 PostProcessorStub unregistered = new PostProcessorStub();
58
59 OsmReader.registerPostprocessor(registered);
60
61 OsmReader.registerPostprocessor(unregistered);
62 OsmReader.deregisterPostprocessor(unregistered);
63
64 try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "empty.osm"))) {
65 OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
66 assertTrue(registered.called);
67 assertFalse(unregistered.called);
68 } finally {
69 OsmReader.deregisterPostprocessor(registered);
70 }
71 }
72
73 private static void testUnknown(String osm) throws Exception {
74 try (InputStream in = new ByteArrayInputStream(
75 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
76 assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE).allPrimitives().isEmpty());
77 }
78 testUnknown(osm, true);
79 testUnknown(osm, false);
80 }
81
82 private static void testUnknown(String osm, boolean parseUnknownAttributes) throws Exception {
83 try (InputStream in = new ByteArrayInputStream(
84 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
85 assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes).allPrimitives()
86 .isEmpty());
87 }
88 }
89
90 /**
91 * Unit test of {@link OsmReader#parseUnknown} - root case.
92 * @throws Exception if any error occurs
93 */
94 @Test
95 public void testUnknownRoot() throws Exception {
96 testUnknown("<nonosm/>");
97 }
98
99 /**
100 * Unit test of {@link OsmReader#parseUnknown} - meta case from Overpass API.
101 * @throws Exception if any error occurs
102 */
103 @Test
104 public void testUnknownMeta() throws Exception {
105 testUnknown("<osm version='0.6'><meta osm_base='2017-03-29T19:04:03Z'/></osm>");
106 }
107
108 /**
109 * Unit test of {@link OsmReader#parseUnknown} - note case from Overpass API.
110 * @throws Exception if any error occurs
111 */
112 @Test
113 public void testUnknownNote() throws Exception {
114 testUnknown("<osm version='0.6'><note>The data included in this document is from www.openstreetmap.org.</note></osm>");
115 }
116
117 /**
118 * Unit test of {@link OsmReader#parseUnknown} - other cases.
119 * @throws Exception if any error occurs
120 */
121 @Test
122 public void testUnknownTag() throws Exception {
123 testUnknown("<osm version='0.6'><foo>bar</foo></osm>");
124 testUnknown("<osm version='0.6'><foo><bar/></foo></osm>");
125 }
126
127 /**
128 * Test valid data.
129 * @param osm OSM data without XML prefix
130 * @return parsed data set
131 * @throws Exception if any error occurs
132 */
133 private static DataSet testValidData(String osm) throws Exception {
134 try (InputStream in = new ByteArrayInputStream(
135 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
136 return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
137 }
138 }
139
140 /**
141 * Test valid data.
142 * @param osm OSM data without XML prefix
143 * @param parseUnknownAttributes if true, attempt to parse unknown xml attributes
144 * @return parsed data set
145 * @throws Exception if any error occurs
146 */
147 private static DataSet testValidData(String osm, boolean parseUnknownAttributes) throws Exception {
148 try (InputStream in = new ByteArrayInputStream(
149 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
150 return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);
151 }
152 }
153
154 /**
155 * Test invalid data.
156 * @param osm OSM data without XML prefix
157 * @param expectedError expected error message
158 * @throws Exception if any error occurs
159 */
160 private static void testInvalidData(String osm, String expectedError) throws Exception {
161 try (InputStream in = new ByteArrayInputStream(
162 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
163 OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
164 fail("should throw exception");
165 } catch (IllegalDataException e) {
166 assertEquals(expectedError, e.getMessage());
167 }
168 testInvalidData(osm, expectedError, true);
169 testInvalidData(osm, expectedError, false);
170 }
171
172 /**
173 * Test invalid data.
174 *
175 * @param osm OSM data without XML prefix
176 * @param expectedError expected error message
177 * @param parseUnknownAttributes if true, attempt to parse unknown xml
178 * attributes
179 * @throws Exception if any error occurs
180 */
181 private static void testInvalidData(String osm, String expectedError, boolean parseUnknownAttributes)
182 throws Exception {
183 try (InputStream in = new ByteArrayInputStream(
184 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
185 OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);
186 fail("should throw exception");
187 } catch (IllegalDataException e) {
188 assertEquals(expectedError, e.getMessage());
189 }
190 }
191
192 /**
193 * Test invalid UID.
194 * @throws Exception if any error occurs
195 */
196 @Test
197 public void testInvalidUid() throws Exception {
198 testInvalidData("<osm version='0.6'><node id='1' uid='nan'/></osm>",
199 "Illegal value for attribute 'uid'. Got 'nan'. (at line 1, column 82). 82 bytes have been read");
200 }
201
202 /**
203 * Test missing ID.
204 * @throws Exception if any error occurs
205 */
206 @Test
207 public void testMissingId() throws Exception {
208 testInvalidData("<osm version='0.6'><node/></osm>",
209 "Missing required attribute 'id'. (at line 1, column 65). 64 bytes have been read");
210 }
211
212 /**
213 * Test missing ref.
214 * @throws Exception if any error occurs
215 */
216 @Test
217 public void testMissingRef() throws Exception {
218 testInvalidData("<osm version='0.6'><way id='1' version='1'><nd/></way></osm>",
219 "Missing mandatory attribute 'ref' on <nd> of way 1. (at line 1, column 87). 88 bytes have been read");
220 testInvalidData("<osm version='0.6'><relation id='1' version='1'><member/></relation></osm>",
221 "Missing attribute 'ref' on member in relation 1. (at line 1, column 96). 101 bytes have been read");
222 }
223
224 /**
225 * Test illegal ref.
226 * @throws Exception if any error occurs
227 */
228 @Test
229 public void testIllegalRef() throws Exception {
230 testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='0'/></way></osm>",
231 "Illegal value of attribute 'ref' of element <nd>. Got 0. (at line 1, column 95). 96 bytes have been read");
232 testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='nan'/></way></osm>",
233 "Illegal long value for attribute 'ref'. Got 'nan'. (at line 1, column 97). 98 bytes have been read");
234
235 testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='0'/></relation></osm>",
236 "Incomplete <member> specification with ref=0 (at line 1, column 116). 121 bytes have been read");
237 testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='nan'/></relation></osm>",
238 "Illegal value for attribute 'ref' on member in relation 1. Got nan (at line 1, column 118). 123 bytes have been read");
239 }
240
241 /**
242 * Test missing member type.
243 * @throws Exception if any error occurs
244 */
245 @Test
246 public void testMissingType() throws Exception {
247 testInvalidData("<osm version='0.6'><relation id='1' version='1'><member ref='1'/></relation></osm>",
248 "Missing attribute 'type' on member 1 in relation 1. (at line 1, column 104). 109 bytes have been read");
249 }
250
251 /**
252 * Test illegal member type.
253 * @throws Exception if any error occurs
254 */
255 @Test
256 public void testIllegalType() throws Exception {
257 testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='foo' ref='1'/></relation></osm>",
258 "Illegal value for attribute 'type' on member 1 in relation 1. Got foo. (at line 1, column 115). 120 bytes have been read");
259 }
260
261 /**
262 * Test missing key/value.
263 * @throws Exception if any error occurs
264 */
265 @Test
266 public void testMissingKeyValue() throws Exception {
267 testInvalidData("<osm version='0.6'><node id='1' version='1'><tag/></node></osm>",
268 "Missing key or value attribute in tag. (at line 1, column 89). 89 bytes have been read");
269 testInvalidData("<osm version='0.6'><node id='1' version='1'><tag k='foo'/></node></osm>",
270 "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
271 testInvalidData("<osm version='0.6'><node id='1' version='1'><tag v='bar'/></node></osm>",
272 "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
273 }
274
275 /**
276 * Test missing version.
277 * @throws Exception if any error occurs
278 */
279 @Test
280 public void testMissingVersion() throws Exception {
281 testInvalidData("<osm/>",
282 "Missing mandatory attribute 'version'. (at line 1, column 45). 44 bytes have been read");
283 testInvalidData("<osm version='0.6'><node id='1'/></osm>",
284 "Missing attribute 'version' on OSM primitive with ID 1. (at line 1, column 72). 72 bytes have been read");
285 }
286
287 /**
288 * Test unsupported version.
289 * @throws Exception if any error occurs
290 */
291 @Test
292 public void testUnsupportedVersion() throws Exception {
293 testInvalidData("<osm version='0.1'/>",
294 "Unsupported version: 0.1 (at line 1, column 59). 58 bytes have been read");
295 }
296
297 /**
298 * Test illegal version.
299 * @throws Exception if any error occurs
300 */
301 @Test
302 public void testIllegalVersion() throws Exception {
303 testInvalidData("<osm version='0.6'><node id='1' version='nan'/></osm>",
304 "Illegal value for attribute 'version' on OSM primitive with ID 1. Got nan. (at line 1, column 86). 86 bytes have been read");
305 }
306
307 /**
308 * Test illegal changeset.
309 * @throws Exception if any error occurs
310 */
311 @Test
312 public void testIllegalChangeset() throws Exception {
313 testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='nan'/></osm>",
314 "Illegal value for attribute 'changeset'. Got nan. (at line 1, column 100). 100 bytes have been read");
315 testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='-1'/></osm>",
316 "Illegal value for attribute 'changeset'. Got -1. (at line 1, column 99). 99 bytes have been read");
317 }
318
319 /**
320 * Test GDPR-compliant changeset.
321 * @throws Exception if any error occurs
322 */
323 @Test
324 public void testGdprChangeset() throws Exception {
325 String gdprChangeset = "<osm version='0.6'><node id='1' version='1' changeset='0'/></osm>";
326 testValidData(gdprChangeset);
327 testValidData(gdprChangeset, true);
328 testValidData(gdprChangeset, false);
329 }
330
331 /**
332 * Test invalid bounds.
333 * @throws Exception if any error occurs
334 */
335 @Test
336 public void testInvalidBounds() throws Exception {
337 testInvalidData("<osm version='0.6'><bounds/></osm>",
338 "Missing mandatory attributes on element 'bounds'. " +
339 "Got minlon='null',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 67). 72 bytes have been read");
340 testInvalidData("<osm version='0.6'><bounds minlon='0'/></osm>",
341 "Missing mandatory attributes on element 'bounds'. " +
342 "Got minlon='0',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 78). 83 bytes have been read");
343 testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0'/></osm>",
344 "Missing mandatory attributes on element 'bounds'. " +
345 "Got minlon='0',minlat='0',maxlon='null',maxlat='null', origin='null'. (at line 1, column 89). 94 bytes have been read");
346 testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0' maxlon='1'/></osm>",
347 "Missing mandatory attributes on element 'bounds'. " +
348 "Got minlon='0',minlat='0',maxlon='1',maxlat='null', origin='null'. (at line 1, column 100). 105 bytes have been read");
349 }
350
351 /**
352 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14199">Bug #14199</a>.
353 * @throws Exception if any error occurs
354 */
355 @Test
356 public void testTicket14199() throws Exception {
357 try (InputStream in = TestUtils.getRegressionDataStream(14199, "emptytag.osm")) {
358 Way w = OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE).getWays().iterator().next();
359 assertEquals(1, w.getKeys().size());
360 assertNull(w.get(" "));
361 assertTrue(w.isModified());
362 }
363 }
364
365 /**
366 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14754">Bug #14754</a>.
367 * @throws Exception if any error occurs
368 */
369 @Test
370 public void testTicket14754() throws Exception {
371 try (InputStream in = TestUtils.getRegressionDataStream(14754, "malformed_for_14754.osm")) {
372 OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
373 fail("should throw exception");
374 } catch (IllegalDataException e) {
375 assertEquals("Illegal value for attributes 'lat', 'lon' on node with ID 1425146006." +
376 " Got '550.3311950157', '10.49428298298'." +
377 " (at line 5, column 179). 578 bytes have been read", e.getMessage());
378 }
379 }
380
381 /**
382 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14788">Bug #14788</a>.
383 * @throws Exception if any error occurs
384 */
385 @Test
386 public void testTicket14788() throws Exception {
387 try (InputStream in = TestUtils.getRegressionDataStream(14788, "remove_sign_test_4.osm")) {
388 OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
389 fail("should throw exception");
390 } catch (IllegalDataException e) {
391 assertEquals("Illegal value for attributes 'lat', 'lon' on node with ID 978." +
392 " Got 'nan', 'nan'." +
393 " (at line 4, column 151). 336 bytes have been read", e.getMessage());
394 }
395 }
396
397 /**
398 * Test reading remark from Overpass API.
399 * @throws Exception if any error occurs
400 */
401 @Test
402 public void testRemark() throws Exception {
403 String query = "<osm version=\"0.6\" generator=\"Overpass API 0.7.55.4 3079d8ea\">\r\n" +
404 "<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>\r\n" +
405 "<meta osm_base=\"2018-08-30T12:46:02Z\" areas=\"2018-08-30T12:40:02Z\"/>\r\n" +
406 "<remark>runtime error: Query ran out of memory in \"query\" at line 5.</remark>\r\n" +
407 "</osm>";
408 for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, true), testValidData(query, false))) {
409 assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
410 }
411 }
412
413 /**
414 * Test reading a file with unknown attributes in osm primitives
415 * @throws Exception if any error occurs
416 */
417 @Test
418 public void testUnknownAttributeTags() throws Exception {
419 String testData = "<osm version=\"0.6\" generator=\"fake generator\">"
420 + "<node id='1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>";
421 DataSet ds = testValidData(testData);
422 assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
423
424 ds = testValidData(testData, true);
425 Node firstNode = ds.getNodes().iterator().next();
426 assertEquals(1, firstNode.getKeys().size());
427 assertEquals("randomvalue", firstNode.get("randomkey"));
428
429 ds = testValidData(testData, false);
430 assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
431 }
432}
Note: See TracBrowser for help on using the repository browser.