1 | package org.openstreetmap.josm.data.validation.tests;
|
---|
2 |
|
---|
3 | import org.junit.Before;
|
---|
4 | import org.junit.Test;
|
---|
5 | import org.openstreetmap.josm.Main;
|
---|
6 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
7 | import org.openstreetmap.josm.data.Preferences;
|
---|
8 | import org.openstreetmap.josm.data.osm.Node;
|
---|
9 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
10 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
11 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
14 | import org.openstreetmap.josm.tools.TextTagParser;
|
---|
15 |
|
---|
16 | import java.io.StringReader;
|
---|
17 | import java.text.MessageFormat;
|
---|
18 | import java.util.LinkedHashSet;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Map;
|
---|
21 |
|
---|
22 | import static org.hamcrest.CoreMatchers.is;
|
---|
23 | import static org.hamcrest.CoreMatchers.notNullValue;
|
---|
24 | import static org.junit.Assert.assertFalse;
|
---|
25 | import static org.junit.Assert.assertThat;
|
---|
26 | import static org.junit.Assert.assertTrue;
|
---|
27 |
|
---|
28 | public class MapCSSTagCheckerTest {
|
---|
29 |
|
---|
30 | @Before
|
---|
31 | public void setUp() throws Exception {
|
---|
32 | Main.pref = new Preferences();
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Test
|
---|
36 | public void testNaturalMarsh() throws Exception {
|
---|
37 |
|
---|
38 | final List<MapCSSTagChecker.TagCheck> checks = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
|
---|
39 | "*[natural=marsh] {\n" +
|
---|
40 | " throwWarning: tr(\"{0} is deprecated\", \"{0.tag}\");\n" +
|
---|
41 | " fixRemove: \"{0.key}\";\n" +
|
---|
42 | " fixAdd: \"natural=wetland\";\n" +
|
---|
43 | " fixAdd: \"wetland=marsh\";\n" +
|
---|
44 | "}"));
|
---|
45 | assertThat(checks.size(), is(1));
|
---|
46 | final MapCSSTagChecker.TagCheck check = checks.get(0);
|
---|
47 | assertThat(check, notNullValue());
|
---|
48 | assertThat(check.getDescription(), is("{0.tag} is deprecated"));
|
---|
49 | assertThat(check.change.get(0).apply(null), is(new Tag("{0.key}")));
|
---|
50 | assertThat(check.change.get(1).apply(null), is(new Tag("natural", "wetland")));
|
---|
51 | assertThat(check.change.get(2).apply(null), is(new Tag("wetland", "marsh")));
|
---|
52 | final Node n1 = new Node();
|
---|
53 | n1.put("natural", "marsh");
|
---|
54 | assertTrue(check.matchesPrimitive(n1));
|
---|
55 | assertThat(check.getErrorForPrimitive(n1).getMessage(), is("natural=marsh is deprecated"));
|
---|
56 | assertThat(check.getErrorForPrimitive(n1).getSeverity(), is(Severity.WARNING));
|
---|
57 | assertThat(check.fixPrimitive(n1).getDescriptionText(), is("Sequence: Fix of natural=marsh is deprecated"));
|
---|
58 | assertThat(((ChangePropertyCommand) check.fixPrimitive(n1).getChildren().iterator().next()).getTags().toString(),
|
---|
59 | is("{natural=}"));
|
---|
60 | final Node n2 = new Node();
|
---|
61 | n2.put("natural", "wood");
|
---|
62 | assertFalse(check.matchesPrimitive(n2));
|
---|
63 | assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.selector.get(0), "The key is {0.key} and the value is {0.value}"),
|
---|
64 | is("The key is natural and the value is marsh"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | OsmPrimitive createPrimitiveForAssertion(String assertion) {
|
---|
68 | final String[] x = assertion.split("\\s+", 2);
|
---|
69 | final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
|
---|
70 | ? new Node()
|
---|
71 | : "w".equals(x[0]) || "way".equals(x[0])
|
---|
72 | ? new Way()
|
---|
73 | : "r".equals(x[0]) || "relation".equals(x[0])
|
---|
74 | ? new Relation()
|
---|
75 | : null;
|
---|
76 | if (p == null) {
|
---|
77 | throw new IllegalArgumentException("Expecting n/node/w/way/r/relation, but got " + x[0]);
|
---|
78 | }
|
---|
79 | for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
|
---|
80 | p.put(i.getKey(), i.getValue());
|
---|
81 | }
|
---|
82 | return p;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Test
|
---|
86 | public void testCreatePrimitiveForAssertion() throws Exception {
|
---|
87 | final OsmPrimitive p = createPrimitiveForAssertion("way name=Foo railway=rail");
|
---|
88 | assertTrue(p instanceof Way);
|
---|
89 | assertThat(p.keySet().size(), is(2));
|
---|
90 | assertThat(p.get("name"), is("Foo"));
|
---|
91 | assertThat(p.get("railway"), is("rail"));
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test(expected = IllegalArgumentException.class)
|
---|
95 | public void testCreatePrimitiveForAssertionFail() throws Exception {
|
---|
96 | final OsmPrimitive p = createPrimitiveForAssertion("noway name=Foo");
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void testInit() throws Exception {
|
---|
101 | final MapCSSTagChecker c = new MapCSSTagChecker();
|
---|
102 | c.initialize();
|
---|
103 |
|
---|
104 | LinkedHashSet<String> assertionErrors = new LinkedHashSet<String>();
|
---|
105 | for (final MapCSSTagChecker.TagCheck check : c.checks) {
|
---|
106 | for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
|
---|
107 | final OsmPrimitive p = createPrimitiveForAssertion(i.getKey());
|
---|
108 | if (check.matchesPrimitive(p) != i.getValue()) {
|
---|
109 | final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
|
---|
110 | check.getMessage(), check.selector, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
|
---|
111 | System.err.println(error);
|
---|
112 | assertionErrors.add(error);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
|
---|
117 |
|
---|
118 | }
|
---|
119 | }
|
---|