1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.is;
|
---|
5 | import static org.hamcrest.CoreMatchers.notNullValue;
|
---|
6 | import static org.junit.Assert.assertFalse;
|
---|
7 | import static org.junit.Assert.assertThat;
|
---|
8 | import static org.junit.Assert.assertTrue;
|
---|
9 |
|
---|
10 | import java.io.StringReader;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Iterator;
|
---|
13 | import java.util.LinkedHashSet;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Set;
|
---|
16 |
|
---|
17 | import org.junit.BeforeClass;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.JOSMFixture;
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
22 | import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
|
---|
23 | import org.openstreetmap.josm.command.Command;
|
---|
24 | import org.openstreetmap.josm.command.PseudoCommand;
|
---|
25 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
29 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
30 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
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 MapCSS TagChecker.
|
---|
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)));
|
---|
50 | return test;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Test
|
---|
54 | public void testNaturalMarsh() throws Exception {
|
---|
55 |
|
---|
56 | final List<MapCSSTagChecker.TagCheck> checks = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
|
---|
57 | "*[natural=marsh] {\n" +
|
---|
58 | " throwWarning: tr(\"{0}={1} is deprecated\", \"{0.key}\", tag(\"natural\"));\n" +
|
---|
59 | " fixRemove: \"{0.key}\";\n" +
|
---|
60 | " fixAdd: \"natural=wetland\";\n" +
|
---|
61 | " fixAdd: \"wetland=marsh\";\n" +
|
---|
62 | "}"));
|
---|
63 | assertThat(checks.size(), is(1));
|
---|
64 | final MapCSSTagChecker.TagCheck check = checks.get(0);
|
---|
65 | assertThat(check, notNullValue());
|
---|
66 | assertThat(check.getDescription(null), is("{0.key}=null is deprecated"));
|
---|
67 | assertThat(check.fixCommands.get(0).toString(), is("fixRemove: {0.key}"));
|
---|
68 | assertThat(check.fixCommands.get(1).toString(), is("fixAdd: natural=wetland"));
|
---|
69 | assertThat(check.fixCommands.get(2).toString(), is("fixAdd: wetland=marsh"));
|
---|
70 | final Node n1 = new Node();
|
---|
71 | n1.put("natural", "marsh");
|
---|
72 | assertTrue(check.evaluate(n1));
|
---|
73 | assertThat(check.getErrorForPrimitive(n1).getMessage(), is("natural=marsh is deprecated"));
|
---|
74 | assertThat(check.getErrorForPrimitive(n1).getSeverity(), is(Severity.WARNING));
|
---|
75 | assertThat(check.fixPrimitive(n1).getDescriptionText(), is("Sequence: Fix of natural=marsh is deprecated"));
|
---|
76 | assertThat(((ChangePropertyCommand) check.fixPrimitive(n1).getChildren().iterator().next()).getTags().toString(),
|
---|
77 | is("{natural=}"));
|
---|
78 | final Node n2 = new Node();
|
---|
79 | n2.put("natural", "wood");
|
---|
80 | assertFalse(check.evaluate(n2));
|
---|
81 | assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}", null),
|
---|
82 | is("The key is natural and the value is marsh"));
|
---|
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 | "}")).get(0);
|
---|
93 | final Command command = check.fixPrimitive(p);
|
---|
94 | assertThat(command instanceof SequenceCommand, is(true));
|
---|
95 | final Iterator<PseudoCommand> it = command.getChildren().iterator();
|
---|
96 | assertThat(it.next() instanceof ChangePropertyKeyCommand, is(true));
|
---|
97 | assertThat(it.next() instanceof ChangePropertyCommand, is(true));
|
---|
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 | assertThat(errors.size(), is(1));
|
---|
107 | assertThat(errors.iterator().next().getMessage(), is("has alt_name but not name"));
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Test
|
---|
111 | public void test10859() throws Exception {
|
---|
112 | final MapCSSTagChecker test = buildTagChecker("way[highway=footway][foot?!] {\n" +
|
---|
113 | " throwWarning: tr(\"{0} used with {1}\", \"{0.value}\", \"{1.tag}\");}");
|
---|
114 | final OsmPrimitive p = OsmUtils.createPrimitive("way highway=footway foot=no");
|
---|
115 | final Collection<TestError> errors = test.getErrorsForPrimitive(p, false);
|
---|
116 | assertThat(errors.size(), is(1));
|
---|
117 | assertThat(errors.iterator().next().getMessage(), is("footway used with foot=no"));
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Test
|
---|
121 | public void testPreprocessing() throws Exception {
|
---|
122 | final MapCSSTagChecker test = buildTagChecker("" +
|
---|
123 | "@media (min-josm-version: 1) { *[foo] { throwWarning: \"!\"; } }\n" +
|
---|
124 | "@media (min-josm-version: 2147483647) { *[bar] { throwWarning: \"!\"; } }\n");
|
---|
125 | assertThat(test.getErrorsForPrimitive(OsmUtils.createPrimitive("way foo=1"), false).size(), is(1));
|
---|
126 | assertThat(test.getErrorsForPrimitive(OsmUtils.createPrimitive("way bar=1"), false).size(), is(0));
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test
|
---|
130 | public void testInit() throws Exception {
|
---|
131 | MapCSSTagChecker c = new MapCSSTagChecker();
|
---|
132 | c.initialize();
|
---|
133 |
|
---|
134 | Set<String> assertionErrors = new LinkedHashSet<>();
|
---|
135 | for (Set<TagCheck> schecks : c.checks.values()) {
|
---|
136 | assertionErrors.addAll(c.checkAsserts(schecks));
|
---|
137 | }
|
---|
138 | for (String msg : assertionErrors) {
|
---|
139 | Main.error(msg);
|
---|
140 | }
|
---|
141 | assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
|
---|
142 | }
|
---|
143 | }
|
---|