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