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

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

see #9414 fix #9485 - MapCSSTagChecker: add support for set class_name instruction and .class_name condition

File size: 4.3 KB
Line 
1package org.openstreetmap.josm.data.validation.tests;
2
3import org.junit.Before;
4import org.junit.Test;
5import org.openstreetmap.TestUtils;
6import org.openstreetmap.josm.Main;
7import org.openstreetmap.josm.command.ChangePropertyCommand;
8import org.openstreetmap.josm.data.Preferences;
9import org.openstreetmap.josm.data.osm.Node;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.data.osm.Tag;
12import org.openstreetmap.josm.data.validation.Severity;
13import org.openstreetmap.josm.data.validation.TestError;
14import org.openstreetmap.josm.tools.Predicate;
15import org.openstreetmap.josm.tools.Predicates;
16import org.openstreetmap.josm.tools.Utils;
17
18import java.io.StringReader;
19import java.text.MessageFormat;
20import java.util.LinkedHashSet;
21import java.util.List;
22import java.util.Map;
23
24import static org.hamcrest.CoreMatchers.is;
25import static org.hamcrest.CoreMatchers.notNullValue;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertThat;
28import static org.junit.Assert.assertTrue;
29
30public class MapCSSTagCheckerTest {
31
32 @Before
33 public void setUp() throws Exception {
34 Main.pref = new Preferences();
35 }
36
37 @Test
38 public void testNaturalMarsh() throws Exception {
39
40 final List<MapCSSTagChecker.TagCheck> checks = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
41 "*[natural=marsh] {\n" +
42 " throwWarning: tr(\"{0} is deprecated\", \"{0.tag}\");\n" +
43 " fixRemove: \"{0.key}\";\n" +
44 " fixAdd: \"natural=wetland\";\n" +
45 " fixAdd: \"wetland=marsh\";\n" +
46 "}"));
47 assertThat(checks.size(), is(1));
48 final MapCSSTagChecker.TagCheck check = checks.get(0);
49 assertThat(check, notNullValue());
50 assertThat(check.getDescription(), is("{0.tag} is deprecated"));
51 assertThat(check.change.get(0).apply(null), is(new Tag("{0.key}")));
52 assertThat(check.change.get(1).apply(null), is(new Tag("natural", "wetland")));
53 assertThat(check.change.get(2).apply(null), is(new Tag("wetland", "marsh")));
54 final Node n1 = new Node();
55 n1.put("natural", "marsh");
56 assertTrue(check.matchesPrimitive(n1));
57 assertThat(check.getErrorForPrimitive(n1).getMessage(), is("natural=marsh is deprecated"));
58 assertThat(check.getErrorForPrimitive(n1).getSeverity(), is(Severity.WARNING));
59 assertThat(check.fixPrimitive(n1).getDescriptionText(), is("Sequence: Fix of natural=marsh is deprecated"));
60 assertThat(((ChangePropertyCommand) check.fixPrimitive(n1).getChildren().iterator().next()).getTags().toString(),
61 is("{natural=}"));
62 final Node n2 = new Node();
63 n2.put("natural", "wood");
64 assertFalse(check.matchesPrimitive(n2));
65 assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}"),
66 is("The key is natural and the value is marsh"));
67 }
68
69 @Test
70 public void testInit() throws Exception {
71 final MapCSSTagChecker c = new MapCSSTagChecker();
72 c.initialize();
73
74 LinkedHashSet<String> assertionErrors = new LinkedHashSet<String>();
75 for (final MapCSSTagChecker.TagCheck check : c.checks) {
76 for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
77 final OsmPrimitive p = TestUtils.createPrimitive(i.getKey());
78 final boolean isError = Utils.exists(c.getErrorsForPrimitive(p), new Predicate<TestError>() {
79 @Override
80 public boolean evaluate(TestError e) {
81 //noinspection EqualsBetweenInconvertibleTypes
82 return e.getTester().equals(check.rule);
83 }
84 });
85 if (isError != i.getValue()) {
86 final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
87 check.getMessage(), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
88 System.err.println(error);
89 assertionErrors.add(error);
90 }
91 }
92 }
93 assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
94
95 }
96}
Note: See TracBrowser for help on using the repository browser.