source: josm/trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java@ 6538

Last change on this file since 6538 was 6538, checked in by simon04, 10 years ago

see #9414 - MapCSS-based tagchecker: provide {i.key}, {i.value}, {i.tag} variables to messages/fixes which refer to the corresponding match condition

File size: 5.0 KB
Line 
1package org.openstreetmap.josm.data.validation.tests;
2
3import org.junit.Before;
4import org.junit.Test;
5import org.openstreetmap.josm.Main;
6import org.openstreetmap.josm.command.ChangePropertyCommand;
7import org.openstreetmap.josm.data.Preferences;
8import org.openstreetmap.josm.data.osm.Node;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.Tag;
12import org.openstreetmap.josm.data.osm.Way;
13import org.openstreetmap.josm.data.validation.Severity;
14import org.openstreetmap.josm.tools.TextTagParser;
15
16import java.io.StringReader;
17import java.text.MessageFormat;
18import java.util.LinkedHashSet;
19import java.util.List;
20import java.util.Map;
21
22import static org.hamcrest.CoreMatchers.is;
23import static org.hamcrest.CoreMatchers.notNullValue;
24import static org.junit.Assert.assertFalse;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.assertTrue;
27
28public 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(((ChangePropertyCommand) check.fixPrimitive(n1).getChildren().iterator().next()).getTags().toString(),
58 is("{natural=}"));
59 final Node n2 = new Node();
60 n2.put("natural", "wood");
61 assertFalse(check.matchesPrimitive(n2));
62 assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.selector.get(0), "The key is {0.key} and the value is {0.value}"),
63 is("The key is natural and the value is marsh"));
64 }
65
66 OsmPrimitive createPrimitiveForAssertion(String assertion) {
67 final String[] x = assertion.split("\\s+", 2);
68 final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
69 ? new Node()
70 : "w".equals(x[0]) || "way".equals(x[0])
71 ? new Way()
72 : "r".equals(x[0]) || "relation".equals(x[0])
73 ? new Relation()
74 : null;
75 if (p == null) {
76 throw new IllegalArgumentException("Expecting n/node/w/way/r/relation, but got " + x[0]);
77 }
78 for (final Map.Entry<String, String> i : TextTagParser.getValidatedTagsFromText(x[1]).entrySet()) {
79 p.put(i.getKey(), i.getValue());
80 }
81 return p;
82 }
83
84 @Test
85 public void testCreatePrimitiveForAssertion() throws Exception {
86 final OsmPrimitive p = createPrimitiveForAssertion("way name=Foo railway=rail");
87 assertTrue(p instanceof Way);
88 assertThat(p.keySet().size(), is(2));
89 assertThat(p.get("name"), is("Foo"));
90 assertThat(p.get("railway"), is("rail"));
91 }
92
93 @Test(expected = IllegalArgumentException.class)
94 public void testCreatePrimitiveForAssertionFail() throws Exception {
95 final OsmPrimitive p = createPrimitiveForAssertion("noway name=Foo");
96 }
97
98 @Test
99 public void testInit() throws Exception {
100 final MapCSSTagChecker c = new MapCSSTagChecker();
101 c.initialize();
102
103 LinkedHashSet<String> assertionErrors = new LinkedHashSet<String>();
104 for (final MapCSSTagChecker.TagCheck check : c.checks) {
105 for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
106 final OsmPrimitive p = createPrimitiveForAssertion(i.getKey());
107 if (check.matchesPrimitive(p) != i.getValue()) {
108 final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
109 check.getMessage(), check.selector, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
110 System.err.println(error);
111 assertionErrors.add(error);
112 }
113 }
114 }
115 assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
116
117 }
118}
Note: See TracBrowser for help on using the repository browser.