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

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

see #9414 - MapCSS-based tagchecker: migrate remaining tagchecks

All former tagchecker.cfg checks are now located in data/validator/*mapcss :-)

File size: 5.1 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(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}
Note: See TracBrowser for help on using the repository browser.