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

Last change on this file since 13733 was 12138, checked in by Don-vip, 7 years ago

improve unit tests of OsmReader

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