source: josm/trunk/test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java@ 18690

Last change on this file since 18690 was 18690, checked in by taylor.smock, 13 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

File size: 17.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7
8import java.io.IOException;
9import java.util.List;
10import java.util.function.Consumer;
11import java.util.stream.Collectors;
12
13import org.junit.jupiter.api.Assertions;
14import org.junit.jupiter.api.Disabled;
15import org.junit.jupiter.api.Test;
16import org.junit.jupiter.api.extension.RegisterExtension;
17import org.openstreetmap.josm.TestUtils;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.OsmUtils;
20import org.openstreetmap.josm.data.osm.Tag;
21import org.openstreetmap.josm.data.validation.Severity;
22import org.openstreetmap.josm.data.validation.TestError;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24
25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26
27/**
28 * JUnit Test of {@link TagChecker}.
29 */
30class TagCheckerTest {
31
32 /**
33 * Setup test.
34 */
35 @RegisterExtension
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules rule = new JOSMTestRules().presets();
38
39 List<TestError> test(OsmPrimitive primitive) throws IOException {
40 final TagChecker checker = new TagChecker() {
41 @Override
42 protected boolean includeOtherSeverityChecks() {
43 return true;
44 }
45 };
46 checker.initialize();
47 checker.startTest(null);
48 checker.check(TestUtils.addFakeDataSet(primitive));
49 return checker.getErrors();
50 }
51
52 /**
53 * Check for misspelled key.
54 * @throws IOException if any I/O error occurs
55 */
56 @Test
57 void testMisspelledKey1() throws IOException {
58 final List<TestError> errors = test(OsmUtils.createPrimitive("node Name=Main"));
59 assertEquals(1, errors.size());
60 assertEquals("Misspelled property key", errors.get(0).getMessage());
61 assertEquals("Key 'Name' looks like 'name'.", errors.get(0).getDescription());
62 assertTrue(errors.get(0).isFixable());
63 }
64
65 /**
66 * Check for misspelled key.
67 * @throws IOException if any I/O error occurs
68 */
69 @Test
70 void testMisspelledKey2() throws IOException {
71 final List<TestError> errors = test(OsmUtils.createPrimitive("node landuse;=forest"));
72 assertEquals(1, errors.size());
73 assertEquals("Misspelled property key", errors.get(0).getMessage());
74 assertEquals("Key 'landuse;' looks like 'landuse'.", errors.get(0).getDescription());
75 assertTrue(errors.get(0).isFixable());
76 }
77
78 /**
79 * Check for misspelled key where the suggested alternative is in use. The error should not be fixable.
80 * @throws IOException if any I/O error occurs
81 */
82 @Test
83 void testMisspelledKeyButAlternativeInUse() throws IOException {
84 // ticket 12329
85 final List<TestError> errors = test(OsmUtils.createPrimitive("node amenity=fuel brand=bah Brand=foo"));
86 assertEquals(1, errors.size());
87 assertEquals("Misspelled property key", errors.get(0).getMessage());
88 assertEquals("Key 'Brand' looks like 'brand'.", errors.get(0).getDescription());
89 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
90 assertFalse(errors.get(0).isFixable());
91 }
92
93 /**
94 * Check for misspelled key where the suggested alternative is given with prefix E: in ignoreTags.cfg.
95 * The error should be fixable.
96 * @throws IOException if any I/O error occurs
97 */
98 @Test
99 void testUpperCaseIgnoredKey() throws IOException {
100 // ticket 17468
101 final List<TestError> errors = test(OsmUtils.createPrimitive("node wheelchair:Description=bla"));
102 assertEquals(1, errors.size());
103 assertEquals("Misspelled property key", errors.get(0).getMessage());
104 assertEquals("Key 'wheelchair:Description' looks like 'wheelchair:description'.", errors.get(0).getDescription());
105 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
106 assertTrue(errors.get(0).isFixable());
107 }
108
109 /**
110 * Check for misspelled key where the suggested alternative is given with prefix K: in ignoreTags.cfg.
111 * The error should be fixable.
112 * @throws IOException if any I/O error occurs
113 */
114 @Test
115 void testUpperCaseInKeyIgnoredTag() throws IOException {
116 // ticket 17468
117 final List<TestError> errors = test(OsmUtils.createPrimitive("node land_Area=administrative"));
118 assertEquals(1, errors.size());
119 assertEquals("Misspelled property key", errors.get(0).getMessage());
120 assertEquals("Key 'land_Area' looks like 'land_area'.", errors.get(0).getDescription());
121 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
122 assertTrue(errors.get(0).isFixable());
123 }
124
125 /**
126 * Check for unknown key.
127 * @throws IOException if any I/O error occurs
128 */
129 @Test
130 void testTranslatedNameKey() throws IOException {
131 final List<TestError> errors = test(OsmUtils.createPrimitive("node namez=Baz"));
132 assertEquals(1, errors.size());
133 assertEquals("Presets do not contain property key", errors.get(0).getMessage());
134 assertEquals("Key 'namez' not in presets.", errors.get(0).getDescription());
135 assertEquals(Severity.OTHER, errors.get(0).getSeverity());
136 assertFalse(errors.get(0).isFixable());
137 }
138
139 /**
140 * Check for misspelled value.
141 * @throws IOException if any I/O error occurs
142 */
143 @Test
144 void testMisspelledTag() throws IOException {
145 final List<TestError> errors = test(OsmUtils.createPrimitive("node landuse=forrest"));
146 assertEquals(1, errors.size());
147 assertEquals("Unknown property value", errors.get(0).getMessage());
148 assertEquals("Value 'forrest' for key 'landuse' is unknown, maybe 'forest' is meant?", errors.get(0).getDescription());
149 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
150 assertFalse(errors.get(0).isFixable());
151 }
152
153 /**
154 * Check for misspelled value with multiple alternatives in presets.
155 * @throws IOException if any I/O error occurs
156 */
157 @Test
158 void testMisspelledTag2() throws IOException {
159 final List<TestError> errors = test(OsmUtils.createPrimitive("node highway=servics"));
160 assertEquals(1, errors.size());
161 assertEquals("Unknown property value", errors.get(0).getMessage());
162 assertEquals(
163 "Value 'servics' for key 'highway' is unknown, maybe one of [service, services] is meant?",
164 errors.get(0).getDescription());
165 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
166 assertFalse(errors.get(0).isFixable());
167 }
168
169 /**
170 * Check for misspelled value.
171 * @throws IOException if any I/O error occurs
172 */
173 @Test
174 void testMisspelledTag3() throws IOException {
175 final List<TestError> errors = test(OsmUtils.createPrimitive("node highway=residentail"));
176 assertEquals(1, errors.size());
177 assertEquals("Unknown property value", errors.get(0).getMessage());
178 assertEquals("Value 'residentail' for key 'highway' is unknown, maybe 'residential' is meant?",
179 errors.get(0).getDescription());
180 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
181 assertFalse(errors.get(0).isFixable());
182 }
183
184 /**
185 * Check for misspelled value.
186 * @throws IOException if any I/O error occurs
187 */
188 @Test
189 void testShortValNotInPreset2() throws IOException {
190 final List<TestError> errors = test(OsmUtils.createPrimitive("node shop=abs"));
191 assertEquals(1, errors.size());
192 assertEquals("Presets do not contain property value", errors.get(0).getMessage());
193 assertEquals("Value 'abs' for key 'shop' not in presets.", errors.get(0).getDescription());
194 assertEquals(Severity.OTHER, errors.get(0).getSeverity());
195 assertFalse(errors.get(0).isFixable());
196 }
197
198 /**
199 * Checks that tags specifically ignored are effectively not in internal presets.
200 * @throws IOException if any I/O error occurs
201 */
202 @Test
203 void testIgnoredTagsNotInPresets() throws IOException {
204 new TagChecker().initialize();
205 List<String> errors = TagChecker.getIgnoredTags().stream()
206 .filter(tag -> TagChecker.isTagInPresets(tag.getKey(), tag.getValue()))
207 .map(Tag::toString)
208 .collect(Collectors.toList());
209 assertTrue(errors.isEmpty(), errors::toString);
210 }
211
212 /**
213 * Check regression: Don't fix surface=u -> surface=mud.
214 * @throws IOException if any I/O error occurs
215 */
216 @Test
217 void testTooShortToFix() throws IOException {
218 final List<TestError> errors = test(OsmUtils.createPrimitive("node surface=u"));
219 assertEquals(1, errors.size());
220 assertEquals("Presets do not contain property value", errors.get(0).getMessage());
221 assertEquals("Value 'u' for key 'surface' not in presets.", errors.get(0).getDescription());
222 assertEquals(Severity.OTHER, errors.get(0).getSeverity());
223 assertFalse(errors.get(0).isFixable());
224 }
225
226 /**
227 * Check value with upper case
228 * @throws IOException if any I/O error occurs
229 */
230 @Test
231 void testValueDifferentCase() throws IOException {
232 final List<TestError> errors = test(OsmUtils.createPrimitive("node highway=Residential"));
233 assertEquals(1, errors.size());
234 assertEquals("Unknown property value", errors.get(0).getMessage());
235 assertEquals("Value 'Residential' for key 'highway' is unknown, maybe 'residential' is meant?",
236 errors.get(0).getDescription());
237 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
238 assertFalse(errors.get(0).isFixable());
239 }
240
241 /**
242 * Key in presets but not in ignored.cfg. Caused a NPE with r14727.
243 * @throws IOException if any I/O error occurs
244 */
245 @Test
246 void testRegression17246() throws IOException {
247 final List<TestError> errors = test(OsmUtils.createPrimitive("node access=privat"));
248 assertEquals(1, errors.size());
249 assertEquals("Unknown property value", errors.get(0).getMessage());
250 assertEquals("Value 'privat' for key 'access' is unknown, maybe 'private' is meant?",
251 errors.get(0).getDescription());
252 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
253 assertFalse(errors.get(0).isFixable());
254 }
255
256 /**
257 * Checks for unwanted non printing control characters
258 * @param s String to test
259 * @param assertionC assertion on the result (true/false)
260 * @param expected expected fixed value
261 */
262 private static void doTestUnwantedNonprintingControlCharacters(String s, Consumer<Boolean> assertionC, String expected) {
263 assertionC.accept(TagChecker.containsUnwantedNonPrintingControlCharacter(s));
264 assertEquals(expected, TagChecker.removeUnwantedNonPrintingControlCharacters(s));
265 }
266
267 private static void doTestUnwantedNonprintingControlCharacters(String s) {
268 doTestUnwantedNonprintingControlCharacters(s, Assertions::assertTrue, "");
269 }
270
271 /**
272 * Unit test of {@link TagChecker#containsUnwantedNonPrintingControlCharacter}
273 * / {@link TagChecker#removeUnwantedNonPrintingControlCharacters}
274 */
275 @Test
276 void testContainsRemoveUnwantedNonprintingControlCharacters() {
277 // Check empty string is handled
278 doTestUnwantedNonprintingControlCharacters("", Assertions::assertFalse, "");
279 // Check 65 ASCII control characters are removed, except new lines
280 for (char c = 0x0; c < 0x20; c++) {
281 if (c != '\r' && c != '\n') {
282 doTestUnwantedNonprintingControlCharacters(Character.toString(c));
283 } else {
284 doTestUnwantedNonprintingControlCharacters(Character.toString(c), Assertions::assertFalse, Character.toString(c));
285 }
286 }
287 doTestUnwantedNonprintingControlCharacters(Character.toString((char) 0x7F));
288 // Check 7 Unicode bidi control characters are removed
289 for (char c = 0x200e; c <= 0x200f; c++) {
290 doTestUnwantedNonprintingControlCharacters(Character.toString(c));
291 }
292 for (char c = 0x202a; c <= 0x202e; c++) {
293 doTestUnwantedNonprintingControlCharacters(Character.toString(c));
294 }
295 // Check joining characters are removed if located at the beginning or end of the string
296 for (char c = 0x200c; c <= 0x200d; c++) {
297 final String s = Character.toString(c);
298 doTestUnwantedNonprintingControlCharacters(s);
299 doTestUnwantedNonprintingControlCharacters(s + s);
300 doTestUnwantedNonprintingControlCharacters(s + 'a' + s, Assertions::assertTrue, "a");
301 final String ok = 'a' + s + 'b';
302 doTestUnwantedNonprintingControlCharacters(ok, Assertions::assertFalse, ok);
303 doTestUnwantedNonprintingControlCharacters(s + ok, Assertions::assertTrue, ok);
304 doTestUnwantedNonprintingControlCharacters(ok + s, Assertions::assertTrue, ok);
305 doTestUnwantedNonprintingControlCharacters(s + ok + s, Assertions::assertTrue, ok);
306 }
307 }
308
309 /**
310 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17667">Bug #17667</a>.
311 */
312 @Test
313 void testTicket17667() {
314 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name", "Bus 118: Berlin, Rathaus Zehlendorf => Potsdam, Drewitz Stern-Center"));
315 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name", "Καρδίτσα → Λάρισα"));
316 assertFalse(TagChecker.containsUnusualUnicodeCharacter("traffic_sign", "FI:871[← Lippuautomaatti]"));
317 assertFalse(TagChecker.containsUnusualUnicodeCharacter("traffic_sign", "FI:871[↑ Nostopaikka ↑]"));
318 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name", "Cinderella II - Strandvägen ↔ Hagede"));
319 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name", "Tallinn — Narva"));
320 }
321
322 /**
323 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18322">Bug #18322</a>.
324 */
325 @Test
326 void testTicket18322() {
327 assertTrue(TagChecker.containsUnusualUnicodeCharacter("name", "D36ᴬ"));
328 assertFalse(TagChecker.containsUnusualUnicodeCharacter("ref", "D36ᴬ"));
329 assertFalse(TagChecker.containsUnusualUnicodeCharacter("old_ref", "D36ᴬ"));
330 assertFalse(TagChecker.containsUnusualUnicodeCharacter("old_ref", "D36ᵂ"));
331 assertTrue(TagChecker.containsUnusualUnicodeCharacter("old_ref", "D36ᴫ"));
332 assertTrue(TagChecker.containsUnusualUnicodeCharacter("old_ref", "D36ᵃ"));
333 }
334
335 /**
336 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18449">Bug #18449</a>.
337 */
338 @Test
339 void testTicket18449() {
340 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name", "Hökumət Evi"));
341 }
342
343 /**
344 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18740">Bug #18740</a>.
345 */
346 @Test
347 void testTicket18740() {
348 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name:ak", "Frɛnkyeman"));
349 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name:bm", "Esipaɲi"));
350 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name:oym", "Wɨlapaleya Ɨtu"));
351 }
352
353 /**
354 * Detects objects with types not supported by their presets.
355 * @throws IOException in case of I/O error
356 */
357 @Test
358 void testObjectTypeNotSupportedByPreset() throws IOException {
359 List<TestError> errors = test(OsmUtils.createPrimitive("relation waterway=river"));
360 assertEquals(1, errors.size());
361 assertEquals(TagChecker.INVALID_PRESETS_TYPE, errors.get(0).getCode());
362 errors = test(OsmUtils.createPrimitive("relation type=waterway waterway=river"));
363 assertTrue(errors.isEmpty(), errors::toString);
364 }
365
366 /**
367 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/19519">Bug #19519</a>.
368 * @throws IOException ignored
369 */
370 @Test
371 @Disabled("broken, see #19519")
372 void testTicket19519() throws IOException {
373 List<TestError> errors = test(OsmUtils.createPrimitive("node amenity=restaurant cuisine=bavarian;beef_bowl"));
374 assertEquals(0, errors.size());
375 }
376
377 /**
378 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/20437">Bug #20437</a>.
379 */
380 @Test
381 void testTicket20437() {
382 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name:kbp", "Wasɩŋtɔŋ"));
383 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name:kbp", "Kalɩfɔrnii"));
384 }
385
386 /**
387 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/20754">Bug #20754</a>.
388 */
389 @Test
390 void testTicket20754() {
391 assertFalse(TagChecker.containsUnusualUnicodeCharacter("name", "Yuułuʔiłʔatḥ Lands"));
392 }
393
394 /**
395 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/21348">Bug #21348</a>.
396 * Key ref is in presets but without any value.
397 * @throws IOException if any I/O error occurs
398 */
399 @Test
400 void testTicket21348() throws IOException {
401 final List<TestError> errors = test(OsmUtils.createPrimitive("node power=tower ref=12"));
402 assertEquals(0, errors.size());
403 }
404}
Note: See TracBrowser for help on using the repository browser.