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

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

fix unit test

File size: 4.4 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.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.OsmUtils;
16import org.openstreetmap.josm.data.osm.Tag;
17import org.openstreetmap.josm.data.validation.TestError;
18import org.openstreetmap.josm.testutils.JOSMTestRules;
19
20import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21
22/**
23 * JUnit Test of {@link TagChecker}.
24 */
25public class TagCheckerTest {
26
27 /**
28 * Setup test.
29 */
30 @Rule
31 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
32 public JOSMTestRules rule = new JOSMTestRules().presets();
33
34 List<TestError> test(OsmPrimitive primitive) throws IOException {
35 final TagChecker checker = new TagChecker();
36 checker.initialize();
37 checker.startTest(null);
38 checker.check(primitive);
39 return checker.getErrors();
40 }
41
42 /**
43 * Check for mispelled key.
44 * @throws IOException if any I/O error occurs
45 */
46 @Test
47 public void testMisspelledKey1() throws IOException {
48 final List<TestError> errors = test(OsmUtils.createPrimitive("node Name=Main"));
49 assertEquals(1, errors.size());
50 assertEquals("Misspelled property key", errors.get(0).getMessage());
51 assertEquals("Key 'Name' looks like 'name'.", errors.get(0).getDescription());
52 assertTrue(errors.get(0).isFixable());
53 }
54
55 /**
56 * Check for mispelled key.
57 * @throws IOException if any I/O error occurs
58 */
59 @Test
60 public void testMisspelledKey2() throws IOException {
61 final List<TestError> errors = test(OsmUtils.createPrimitive("node landuse;=forest"));
62 assertEquals(1, errors.size());
63 assertEquals("Misspelled property key", errors.get(0).getMessage());
64 assertEquals("Key 'landuse;' looks like 'landuse'.", errors.get(0).getDescription());
65 assertTrue(errors.get(0).isFixable());
66 }
67
68 /**
69 * Check for mispelled key where the suggested alternative is in use. The error should not be fixable.
70 * @throws IOException if any I/O error occurs
71 */
72 @Test
73 public void testMisspelledKeyButAlternativeInUse() throws IOException {
74 // ticket 12329
75 final List<TestError> errors = test(OsmUtils.createPrimitive("node amenity=fuel brand=bah Brand=foo"));
76 assertEquals(1, errors.size());
77 assertEquals("Misspelled property key", errors.get(0).getMessage());
78 assertEquals("Key 'Brand' looks like 'brand'.", errors.get(0).getDescription());
79 assertFalse(errors.get(0).isFixable());
80 }
81
82 /**
83 * Check for unknown key.
84 * @throws IOException if any I/O error occurs
85 */
86 @Test
87 public void testTranslatedNameKey() throws IOException {
88 final List<TestError> errors = test(OsmUtils.createPrimitive("node namez=Baz"));
89 assertEquals(1, errors.size());
90 assertEquals("Presets do not contain property key", errors.get(0).getMessage());
91 assertEquals("Key 'namez' not in presets.", errors.get(0).getDescription());
92 }
93
94 /**
95 * Check for mispelled value.
96 * @throws IOException if any I/O error occurs
97 */
98 @Test
99 public void testMisspelledTag() throws IOException {
100 final List<TestError> errors = test(OsmUtils.createPrimitive("node landuse=forrest"));
101 assertEquals(1, errors.size());
102 assertEquals("Presets do not contain property value", errors.get(0).getMessage());
103 assertEquals("Value 'forrest' for key 'landuse' not in presets.", errors.get(0).getDescription());
104 }
105
106 /**
107 * Checks that tags specifically ignored are effectively not in internal presets.
108 * @throws IOException if any I/O error occurs
109 */
110 @Test
111 public void testIgnoredTagsNotInPresets() throws IOException {
112 List<String> errors = new ArrayList<>();
113 new TagChecker().initialize();
114 for (Tag tag : TagChecker.getIgnoredTags()) {
115 if (TagChecker.isTagInPresets(tag.getKey(), tag.getValue())) {
116 errors.add(tag.toString());
117 }
118 }
119 assertTrue(errors.toString(), errors.isEmpty());
120 }
121}
Note: See TracBrowser for help on using the repository browser.