1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 | import static org.junit.Assert.fail;
|
---|
9 |
|
---|
10 | import java.io.ByteArrayInputStream;
|
---|
11 | import java.io.FileInputStream;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.nio.charset.StandardCharsets;
|
---|
14 |
|
---|
15 | import org.junit.Rule;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Way;
|
---|
20 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
23 |
|
---|
24 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Unit tests of {@link OsmReader} class.
|
---|
28 | */
|
---|
29 | public class OsmReaderTest {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Setup rule
|
---|
33 | */
|
---|
34 | @Rule
|
---|
35 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
36 | public JOSMTestRules test = new JOSMTestRules();
|
---|
37 |
|
---|
38 | private static final class PostProcessorStub implements OsmServerReadPostprocessor {
|
---|
39 | boolean called;
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void postprocessDataSet(DataSet ds, ProgressMonitor progress) {
|
---|
43 | called = true;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Unit test of {@link OsmReader#registerPostprocessor} / {@link OsmReader#deregisterPostprocessor}.
|
---|
49 | * @throws Exception if any error occurs
|
---|
50 | */
|
---|
51 | @Test
|
---|
52 | public void testPostProcessors() throws Exception {
|
---|
53 | PostProcessorStub registered = new PostProcessorStub();
|
---|
54 | PostProcessorStub unregistered = new PostProcessorStub();
|
---|
55 |
|
---|
56 | OsmReader.registerPostprocessor(registered);
|
---|
57 |
|
---|
58 | OsmReader.registerPostprocessor(unregistered);
|
---|
59 | OsmReader.deregisterPostprocessor(unregistered);
|
---|
60 |
|
---|
61 | try (InputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "empty.osm")) {
|
---|
62 | OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
63 | assertTrue(registered.called);
|
---|
64 | assertFalse(unregistered.called);
|
---|
65 | } finally {
|
---|
66 | OsmReader.deregisterPostprocessor(registered);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static void testUnknown(String osm) throws Exception {
|
---|
71 | try (InputStream in = new ByteArrayInputStream(
|
---|
72 | ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
|
---|
73 | assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE).allPrimitives().isEmpty());
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Unit test of {@link OsmReader#parseUnknown} - root case.
|
---|
79 | * @throws Exception if any error occurs
|
---|
80 | */
|
---|
81 | @Test
|
---|
82 | public void testUnknownRoot() throws Exception {
|
---|
83 | testUnknown("<nonosm/>");
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Unit test of {@link OsmReader#parseUnknown} - meta case from Overpass API.
|
---|
88 | * @throws Exception if any error occurs
|
---|
89 | */
|
---|
90 | @Test
|
---|
91 | public void testUnknownMeta() throws Exception {
|
---|
92 | testUnknown("<osm version='0.6'><meta osm_base='2017-03-29T19:04:03Z'/></osm>");
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Unit test of {@link OsmReader#parseUnknown} - note case from Overpass API.
|
---|
97 | * @throws Exception if any error occurs
|
---|
98 | */
|
---|
99 | @Test
|
---|
100 | public void testUnknownNote() throws Exception {
|
---|
101 | testUnknown("<osm version='0.6'><note>The data included in this document is from www.openstreetmap.org.</note></osm>");
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Unit test of {@link OsmReader#parseUnknown} - other cases.
|
---|
106 | * @throws Exception if any error occurs
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | public void testUnknownTag() throws Exception {
|
---|
110 | testUnknown("<osm version='0.6'><foo>bar</foo></osm>");
|
---|
111 | testUnknown("<osm version='0.6'><foo><bar/></foo></osm>");
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Test valid data.
|
---|
116 | * @param osm OSM data without XML prefix
|
---|
117 | * @throws Exception if any error occurs
|
---|
118 | */
|
---|
119 | private static void testValidData(String osm) throws Exception {
|
---|
120 | try (InputStream in = new ByteArrayInputStream(
|
---|
121 | ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
|
---|
122 | OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Test invalid data.
|
---|
128 | * @param osm OSM data without XML prefix
|
---|
129 | * @param expectedError expected error message
|
---|
130 | * @throws Exception if any error occurs
|
---|
131 | */
|
---|
132 | private static void testInvalidData(String osm, String expectedError) throws Exception {
|
---|
133 | try (InputStream in = new ByteArrayInputStream(
|
---|
134 | ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
|
---|
135 | OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
136 | fail("should throw exception");
|
---|
137 | } catch (IllegalDataException e) {
|
---|
138 | assertEquals(expectedError, e.getMessage());
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Test invalid UID.
|
---|
144 | * @throws Exception if any error occurs
|
---|
145 | */
|
---|
146 | @Test
|
---|
147 | public void testInvalidUid() throws Exception {
|
---|
148 | testInvalidData("<osm version='0.6'><node id='1' uid='nan'/></osm>",
|
---|
149 | "Illegal value for attribute 'uid'. Got 'nan'. (at line 1, column 82). 82 bytes have been read");
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Test missing ID.
|
---|
154 | * @throws Exception if any error occurs
|
---|
155 | */
|
---|
156 | @Test
|
---|
157 | public void testMissingId() throws Exception {
|
---|
158 | testInvalidData("<osm version='0.6'><node/></osm>",
|
---|
159 | "Missing required attribute 'id'. (at line 1, column 65). 64 bytes have been read");
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Test missing ref.
|
---|
164 | * @throws Exception if any error occurs
|
---|
165 | */
|
---|
166 | @Test
|
---|
167 | public void testMissingRef() throws Exception {
|
---|
168 | testInvalidData("<osm version='0.6'><way id='1' version='1'><nd/></way></osm>",
|
---|
169 | "Missing mandatory attribute 'ref' on <nd> of way 1. (at line 1, column 87). 88 bytes have been read");
|
---|
170 | testInvalidData("<osm version='0.6'><relation id='1' version='1'><member/></relation></osm>",
|
---|
171 | "Missing attribute 'ref' on member in relation 1. (at line 1, column 96). 101 bytes have been read");
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Test illegal ref.
|
---|
176 | * @throws Exception if any error occurs
|
---|
177 | */
|
---|
178 | @Test
|
---|
179 | public void testIllegalRef() throws Exception {
|
---|
180 | testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='0'/></way></osm>",
|
---|
181 | "Illegal value of attribute 'ref' of element <nd>. Got 0. (at line 1, column 95). 96 bytes have been read");
|
---|
182 | testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='nan'/></way></osm>",
|
---|
183 | "Illegal long value for attribute 'ref'. Got 'nan'. (at line 1, column 97). 98 bytes have been read");
|
---|
184 |
|
---|
185 | testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='0'/></relation></osm>",
|
---|
186 | "Incomplete <member> specification with ref=0 (at line 1, column 116). 121 bytes have been read");
|
---|
187 | testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='nan'/></relation></osm>",
|
---|
188 | "Illegal value for attribute 'ref' on member in relation 1. Got nan (at line 1, column 118). 123 bytes have been read");
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Test missing member type.
|
---|
193 | * @throws Exception if any error occurs
|
---|
194 | */
|
---|
195 | @Test
|
---|
196 | public void testMissingType() throws Exception {
|
---|
197 | testInvalidData("<osm version='0.6'><relation id='1' version='1'><member ref='1'/></relation></osm>",
|
---|
198 | "Missing attribute 'type' on member 1 in relation 1. (at line 1, column 104). 109 bytes have been read");
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Test illegal member type.
|
---|
203 | * @throws Exception if any error occurs
|
---|
204 | */
|
---|
205 | @Test
|
---|
206 | public void testIllegalType() throws Exception {
|
---|
207 | testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='foo' ref='1'/></relation></osm>",
|
---|
208 | "Illegal value for attribute 'type' on member 1 in relation 1. Got foo. (at line 1, column 115). 120 bytes have been read");
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Test missing key/value.
|
---|
213 | * @throws Exception if any error occurs
|
---|
214 | */
|
---|
215 | @Test
|
---|
216 | public void testMissingKeyValue() throws Exception {
|
---|
217 | testInvalidData("<osm version='0.6'><node id='1' version='1'><tag/></node></osm>",
|
---|
218 | "Missing key or value attribute in tag. (at line 1, column 89). 89 bytes have been read");
|
---|
219 | testInvalidData("<osm version='0.6'><node id='1' version='1'><tag k='foo'/></node></osm>",
|
---|
220 | "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
|
---|
221 | testInvalidData("<osm version='0.6'><node id='1' version='1'><tag v='bar'/></node></osm>",
|
---|
222 | "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Test missing version.
|
---|
227 | * @throws Exception if any error occurs
|
---|
228 | */
|
---|
229 | @Test
|
---|
230 | public void testMissingVersion() throws Exception {
|
---|
231 | testInvalidData("<osm/>",
|
---|
232 | "Missing mandatory attribute 'version'. (at line 1, column 45). 44 bytes have been read");
|
---|
233 | testInvalidData("<osm version='0.6'><node id='1'/></osm>",
|
---|
234 | "Missing attribute 'version' on OSM primitive with ID 1. (at line 1, column 72). 72 bytes have been read");
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Test unsupported version.
|
---|
239 | * @throws Exception if any error occurs
|
---|
240 | */
|
---|
241 | @Test
|
---|
242 | public void testUnsupportedVersion() throws Exception {
|
---|
243 | testInvalidData("<osm version='0.1'/>",
|
---|
244 | "Unsupported version: 0.1 (at line 1, column 59). 58 bytes have been read");
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Test illegal version.
|
---|
249 | * @throws Exception if any error occurs
|
---|
250 | */
|
---|
251 | @Test
|
---|
252 | public void testIllegalVersion() throws Exception {
|
---|
253 | testInvalidData("<osm version='0.6'><node id='1' version='nan'/></osm>",
|
---|
254 | "Illegal value for attribute 'version' on OSM primitive with ID 1. Got nan. (at line 1, column 86). 86 bytes have been read");
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Test illegal changeset.
|
---|
259 | * @throws Exception if any error occurs
|
---|
260 | */
|
---|
261 | @Test
|
---|
262 | public void testIllegalChangeset() throws Exception {
|
---|
263 | testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='nan'/></osm>",
|
---|
264 | "Illegal value for attribute 'changeset'. Got nan. (at line 1, column 100). 100 bytes have been read");
|
---|
265 | testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='-1'/></osm>",
|
---|
266 | "Illegal value for attribute 'changeset'. Got -1. (at line 1, column 99). 99 bytes have been read");
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Test GDPR-compliant changeset.
|
---|
271 | * @throws Exception if any error occurs
|
---|
272 | */
|
---|
273 | @Test
|
---|
274 | public void testGdprChangeset() throws Exception {
|
---|
275 | testValidData("<osm version='0.6'><node id='1' version='1' changeset='0'/></osm>");
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Test invalid bounds.
|
---|
280 | * @throws Exception if any error occurs
|
---|
281 | */
|
---|
282 | @Test
|
---|
283 | public void testInvalidBounds() throws Exception {
|
---|
284 | testInvalidData("<osm version='0.6'><bounds/></osm>",
|
---|
285 | "Missing mandatory attributes on element 'bounds'. " +
|
---|
286 | "Got minlon='null',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 67). 72 bytes have been read");
|
---|
287 | testInvalidData("<osm version='0.6'><bounds minlon='0'/></osm>",
|
---|
288 | "Missing mandatory attributes on element 'bounds'. " +
|
---|
289 | "Got minlon='0',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 78). 83 bytes have been read");
|
---|
290 | testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0'/></osm>",
|
---|
291 | "Missing mandatory attributes on element 'bounds'. " +
|
---|
292 | "Got minlon='0',minlat='0',maxlon='null',maxlat='null', origin='null'. (at line 1, column 89). 94 bytes have been read");
|
---|
293 | testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0' maxlon='1'/></osm>",
|
---|
294 | "Missing mandatory attributes on element 'bounds'. " +
|
---|
295 | "Got minlon='0',minlat='0',maxlon='1',maxlat='null', origin='null'. (at line 1, column 100). 105 bytes have been read");
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14199">Bug #14199</a>.
|
---|
300 | * @throws Exception if any error occurs
|
---|
301 | */
|
---|
302 | @Test
|
---|
303 | public void testTicket14199() throws Exception {
|
---|
304 | try (InputStream in = TestUtils.getRegressionDataStream(14199, "emptytag.osm")) {
|
---|
305 | Way w = OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE).getWays().iterator().next();
|
---|
306 | assertEquals(1, w.getKeys().size());
|
---|
307 | assertNull(w.get(" "));
|
---|
308 | assertTrue(w.isModified());
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14754">Bug #14754</a>.
|
---|
314 | * @throws Exception if any error occurs
|
---|
315 | */
|
---|
316 | @Test
|
---|
317 | public void testTicket14754() throws Exception {
|
---|
318 | try (InputStream in = TestUtils.getRegressionDataStream(14754, "malformed_for_14754.osm")) {
|
---|
319 | OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
320 | fail("should throw exception");
|
---|
321 | } catch (IllegalDataException e) {
|
---|
322 | assertEquals("Illegal value for attributes 'lat', 'lon' on node with ID 1425146006." +
|
---|
323 | " Got '550.3311950157', '10.49428298298'." +
|
---|
324 | " (at line 5, column 179). 578 bytes have been read", e.getMessage());
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14788">Bug #14788</a>.
|
---|
330 | * @throws Exception if any error occurs
|
---|
331 | */
|
---|
332 | @Test
|
---|
333 | public void testTicket14788() throws Exception {
|
---|
334 | try (InputStream in = TestUtils.getRegressionDataStream(14788, "remove_sign_test_4.osm")) {
|
---|
335 | OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
336 | fail("should throw exception");
|
---|
337 | } catch (IllegalDataException e) {
|
---|
338 | assertEquals("Illegal value for attributes 'lat', 'lon' on node with ID 978." +
|
---|
339 | " Got 'nan', 'nan'." +
|
---|
340 | " (at line 4, column 151). 336 bytes have been read", e.getMessage());
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|