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

Last change on this file since 14508 was 14508, checked in by GerdP, 5 years ago

see #17055 improve TagChecker

  • allow Levenshtein distance of 2 so that residentail is found as typo
  • disable Fix for typos where the correct value is not known, e.g. services can be service or services
  • don't suggest values if all preset values are short, e.g.

layer=6 should not say "6 looks like one of [1,2,3,4,5]"

File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertTrue;
7
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.List;
11
12import org.junit.Rule;
13import org.junit.Test;
14import org.openstreetmap.josm.TestUtils;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmUtils;
17import org.openstreetmap.josm.data.osm.Tag;
18import org.openstreetmap.josm.data.validation.Severity;
19import org.openstreetmap.josm.data.validation.TestError;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * JUnit Test of {@link TagChecker}.
26 */
27public class TagCheckerTest {
28
29 /**
30 * Setup test.
31 */
32 @Rule
33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
34 public JOSMTestRules rule = new JOSMTestRules().presets();
35
36 List<TestError> test(OsmPrimitive primitive) throws IOException {
37 final TagChecker checker = new TagChecker();
38 checker.initialize();
39 checker.startTest(null);
40 checker.check(TestUtils.addFakeDataSet(primitive));
41 return checker.getErrors();
42 }
43
44 /**
45 * Check for misspelled key.
46 * @throws IOException if any I/O error occurs
47 */
48 @Test
49 public void testMisspelledKey1() throws IOException {
50 final List<TestError> errors = test(OsmUtils.createPrimitive("node Name=Main"));
51 assertEquals(1, errors.size());
52 assertEquals("Misspelled property key", errors.get(0).getMessage());
53 assertEquals("Key 'Name' looks like 'name'.", errors.get(0).getDescription());
54 assertTrue(errors.get(0).isFixable());
55 }
56
57 /**
58 * Check for misspelled key.
59 * @throws IOException if any I/O error occurs
60 */
61 @Test
62 public void testMisspelledKey2() throws IOException {
63 final List<TestError> errors = test(OsmUtils.createPrimitive("node landuse;=forest"));
64 assertEquals(1, errors.size());
65 assertEquals("Misspelled property key", errors.get(0).getMessage());
66 assertEquals("Key 'landuse;' looks like 'landuse'.", errors.get(0).getDescription());
67 assertTrue(errors.get(0).isFixable());
68 }
69
70 /**
71 * Check for misspelled key where the suggested alternative is in use. The error should not be fixable.
72 * @throws IOException if any I/O error occurs
73 */
74 @Test
75 public void testMisspelledKeyButAlternativeInUse() throws IOException {
76 // ticket 12329
77 final List<TestError> errors = test(OsmUtils.createPrimitive("node amenity=fuel brand=bah Brand=foo"));
78 assertEquals(1, errors.size());
79 assertEquals("Misspelled property key", errors.get(0).getMessage());
80 assertEquals("Key 'Brand' looks like 'brand'.", errors.get(0).getDescription());
81 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
82 assertFalse(errors.get(0).isFixable());
83 }
84
85 /**
86 * Check for unknown key.
87 * @throws IOException if any I/O error occurs
88 */
89 @Test
90 public void testTranslatedNameKey() throws IOException {
91 final List<TestError> errors = test(OsmUtils.createPrimitive("node namez=Baz"));
92 assertEquals(1, errors.size());
93 assertEquals("Presets do not contain property key", errors.get(0).getMessage());
94 assertEquals("Key 'namez' not in presets.", errors.get(0).getDescription());
95 assertEquals(Severity.OTHER, errors.get(0).getSeverity());
96 assertFalse(errors.get(0).isFixable());
97 }
98
99 /**
100 * Check for misspelled value.
101 * @throws IOException if any I/O error occurs
102 */
103 @Test
104 public void testMisspelledTag() throws IOException {
105 final List<TestError> errors = test(OsmUtils.createPrimitive("node landuse=forrest"));
106 assertEquals(1, errors.size());
107 assertEquals("Misspelled property value", errors.get(0).getMessage());
108 assertEquals("Value 'forrest' for key 'landuse' looks like 'forest'.", errors.get(0).getDescription());
109 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
110 assertTrue(errors.get(0).isFixable());
111 }
112
113 /**
114 * Check for misspelled value with multiple alternatives in presets.
115 * @throws IOException if any I/O error occurs
116 */
117 @Test
118 public void testMisspelledTag2() throws IOException {
119 final List<TestError> errors = test(OsmUtils.createPrimitive("node highway=servics"));
120 assertEquals(1, errors.size());
121 assertEquals("Misspelled property value", errors.get(0).getMessage());
122 assertEquals("Value 'servics' for key 'highway' looks like one of [service, services].", errors.get(0).getDescription());
123 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
124 assertFalse(errors.get(0).isFixable());
125 }
126
127 /**
128 * Check for misspelled value.
129 * @throws IOException if any I/O error occurs
130 */
131 @Test
132 public void testMisspelledTag3() throws IOException {
133 final List<TestError> errors = test(OsmUtils.createPrimitive("node highway=residentail"));
134 assertEquals(1, errors.size());
135 assertEquals("Misspelled property value", errors.get(0).getMessage());
136 assertEquals("Value 'residentail' for key 'highway' looks like 'residential'.", errors.get(0).getDescription());
137 assertEquals(Severity.WARNING, errors.get(0).getSeverity());
138 assertTrue(errors.get(0).isFixable());
139 }
140 /**
141 * Check for misspelled value.
142 * @throws IOException if any I/O error occurs
143 */
144 @Test
145 public void testShortValNotInPreset() throws IOException {
146 final List<TestError> errors = test(OsmUtils.createPrimitive("node layer=6"));
147 assertEquals(1, errors.size());
148 assertEquals("Presets do not contain property value", errors.get(0).getMessage());
149 assertEquals("Value '6' for key 'layer' not in presets.", errors.get(0).getDescription());
150 assertEquals(Severity.OTHER, errors.get(0).getSeverity());
151 assertFalse(errors.get(0).isFixable());
152 }
153
154 /**
155 * Checks that tags specifically ignored are effectively not in internal presets.
156 * @throws IOException if any I/O error occurs
157 */
158 @Test
159 public void testIgnoredTagsNotInPresets() throws IOException {
160 List<String> errors = new ArrayList<>();
161 new TagChecker().initialize();
162 for (Tag tag : TagChecker.getIgnoredTags()) {
163 if (TagChecker.isTagInPresets(tag.getKey(), tag.getValue())) {
164 errors.add(tag.toString());
165 }
166 }
167 assertTrue(errors.toString(), errors.isEmpty());
168 }
169}
Note: See TracBrowser for help on using the repository browser.