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