1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 |
|
---|
9 | import java.io.StringReader;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Iterator;
|
---|
12 | import java.util.LinkedHashSet;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Set;
|
---|
15 |
|
---|
16 | import org.junit.Rule;
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
20 | import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
|
---|
21 | import org.openstreetmap.josm.command.Command;
|
---|
22 | import org.openstreetmap.josm.command.PseudoCommand;
|
---|
23 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
27 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
28 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
29 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.ParseResult;
|
---|
30 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.TagCheck;
|
---|
31 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
|
---|
32 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
33 |
|
---|
34 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * JUnit Test of {@link MapCSSTagChecker}.
|
---|
38 | */
|
---|
39 | public class MapCSSTagCheckerTest {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Setup test.
|
---|
43 | */
|
---|
44 | @Rule
|
---|
45 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
46 | public JOSMTestRules test = new JOSMTestRules();
|
---|
47 |
|
---|
48 | static MapCSSTagChecker buildTagChecker(String css) throws ParseException {
|
---|
49 | final MapCSSTagChecker test = new MapCSSTagChecker();
|
---|
50 | test.checks.putAll("test", TagCheck.readMapCSS(new StringReader(css)).parseChecks);
|
---|
51 | return test;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Test
|
---|
55 | public void testNaturalMarsh() throws Exception {
|
---|
56 | ParseResult result = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
|
---|
57 | "*[natural=marsh] {\n" +
|
---|
58 | " group: tr(\"deprecated\");\n" +
|
---|
59 | " throwWarning: tr(\"{0}={1} is deprecated\", \"{0.key}\", tag(\"natural\"));\n" +
|
---|
60 | " fixRemove: \"{0.key}\";\n" +
|
---|
61 | " fixAdd: \"natural=wetland\";\n" +
|
---|
62 | " fixAdd: \"wetland=marsh\";\n" +
|
---|
63 | "}"));
|
---|
64 | final List<MapCSSTagChecker.TagCheck> checks = result.parseChecks;
|
---|
65 | assertEquals(1, checks.size());
|
---|
66 | assertTrue(result.parseErrors.isEmpty());
|
---|
67 | final MapCSSTagChecker.TagCheck check = checks.get(0);
|
---|
68 | assertNotNull(check);
|
---|
69 | assertEquals("{0.key}=null is deprecated", check.getDescription(null));
|
---|
70 | assertEquals("fixRemove: {0.key}", check.fixCommands.get(0).toString());
|
---|
71 | assertEquals("fixAdd: natural=wetland", check.fixCommands.get(1).toString());
|
---|
72 | assertEquals("fixAdd: wetland=marsh", check.fixCommands.get(2).toString());
|
---|
73 | final Node n1 = new Node();
|
---|
74 | n1.put("natural", "marsh");
|
---|
75 | assertTrue(check.test(n1));
|
---|
76 | assertEquals("deprecated", check.getErrorForPrimitive(n1).getMessage());
|
---|
77 | assertEquals("natural=marsh is deprecated", check.getErrorForPrimitive(n1).getDescription());
|
---|
78 | assertEquals(Severity.WARNING, check.getErrorForPrimitive(n1).getSeverity());
|
---|
79 | assertEquals("Sequence: Fix of natural=marsh is deprecated", check.fixPrimitive(n1).getDescriptionText());
|
---|
80 | assertEquals("{natural=}", ((ChangePropertyCommand) check.fixPrimitive(n1).getChildren().iterator().next()).getTags().toString());
|
---|
81 | final Node n2 = new Node();
|
---|
82 | n2.put("natural", "wood");
|
---|
83 | assertFalse(check.test(n2));
|
---|
84 | assertEquals("The key is natural and the value is marsh",
|
---|
85 | MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}", null));
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Test
|
---|
89 | public void testTicket10913() throws Exception {
|
---|
90 | final OsmPrimitive p = OsmUtils.createPrimitive("way highway=tertiary construction=yes");
|
---|
91 | final TagCheck check = TagCheck.readMapCSS(new StringReader("way {" +
|
---|
92 | "throwError: \"error\";" +
|
---|
93 | "fixChangeKey: \"highway => construction\";\n" +
|
---|
94 | "fixAdd: \"highway=construction\";\n" +
|
---|
95 | "}")).parseChecks.get(0);
|
---|
96 | final Command command = check.fixPrimitive(p);
|
---|
97 | assertTrue(command instanceof SequenceCommand);
|
---|
98 | final Iterator<PseudoCommand> it = command.getChildren().iterator();
|
---|
99 | assertTrue(it.next() instanceof ChangePropertyKeyCommand);
|
---|
100 | assertTrue(it.next() instanceof ChangePropertyCommand);
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Test
|
---|
104 | public void testTicket9782() throws Exception {
|
---|
105 | final MapCSSTagChecker test = buildTagChecker("*[/.+_name/][!name] {" +
|
---|
106 | "throwWarning: tr(\"has {0} but not {1}\", \"{0.key}\", \"{1.key}\");}");
|
---|
107 | final OsmPrimitive p = OsmUtils.createPrimitive("way alt_name=Foo");
|
---|
108 | final Collection<TestError> errors = test.getErrorsForPrimitive(p, false);
|
---|
109 | assertEquals(1, errors.size());
|
---|
110 | assertEquals("has alt_name but not name", errors.iterator().next().getMessage());
|
---|
111 | assertEquals("3000_*[.+_name][!name]", errors.iterator().next().getIgnoreSubGroup());
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Test
|
---|
115 | public void testTicket10859() throws Exception {
|
---|
116 | final MapCSSTagChecker test = buildTagChecker("way[highway=footway][foot?!] {\n" +
|
---|
117 | " throwWarning: tr(\"{0} used with {1}\", \"{0.value}\", \"{1.tag}\");}");
|
---|
118 | final OsmPrimitive p = OsmUtils.createPrimitive("way highway=footway foot=no");
|
---|
119 | final Collection<TestError> errors = test.getErrorsForPrimitive(p, false);
|
---|
120 | assertEquals(1, errors.size());
|
---|
121 | assertEquals("footway used with foot=no", errors.iterator().next().getMessage());
|
---|
122 | assertEquals("3000_way[highway=footway][foot]", errors.iterator().next().getIgnoreSubGroup());
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Test
|
---|
126 | public void testPreprocessing() throws Exception {
|
---|
127 | final MapCSSTagChecker test = buildTagChecker("" +
|
---|
128 | "@supports (min-josm-version: 1) { *[foo] { throwWarning: \"!\"; } }\n" +
|
---|
129 | "@supports (min-josm-version: 2147483647) { *[bar] { throwWarning: \"!\"; } }\n");
|
---|
130 | assertEquals(1, test.getErrorsForPrimitive(OsmUtils.createPrimitive("way foo=1"), false).size());
|
---|
131 | assertEquals(0, test.getErrorsForPrimitive(OsmUtils.createPrimitive("way bar=1"), false).size());
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Test
|
---|
135 | public void testInit() throws Exception {
|
---|
136 | MapCSSTagChecker c = new MapCSSTagChecker();
|
---|
137 | c.initialize();
|
---|
138 |
|
---|
139 | Set<String> assertionErrors = new LinkedHashSet<>();
|
---|
140 | for (Set<TagCheck> schecks : c.checks.values()) {
|
---|
141 | assertionErrors.addAll(c.checkAsserts(schecks));
|
---|
142 | }
|
---|
143 | for (String msg : assertionErrors) {
|
---|
144 | Main.error(msg);
|
---|
145 | }
|
---|
146 | assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
|
---|
147 | }
|
---|
148 | }
|
---|