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

Last change on this file since 7065 was 7065, checked in by Don-vip, 10 years ago

update test to match r7064

File size: 4.4 KB
Line 
1package org.openstreetmap.josm.data.validation.tests;
2
3import static org.hamcrest.CoreMatchers.is;
4import static org.hamcrest.CoreMatchers.notNullValue;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertThat;
7import static org.junit.Assert.assertTrue;
8
9import java.io.StringReader;
10import java.text.MessageFormat;
11import java.util.LinkedHashSet;
12import java.util.List;
13import java.util.Map;
14
15import org.junit.Before;
16import org.junit.Test;
17import org.openstreetmap.TestUtils;
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.command.ChangePropertyCommand;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.Tag;
23import org.openstreetmap.josm.data.validation.Severity;
24import org.openstreetmap.josm.data.validation.TestError;
25import org.openstreetmap.josm.tools.Predicate;
26import org.openstreetmap.josm.tools.Utils;
27
28public class MapCSSTagCheckerTest {
29
30 /**
31 * Setup test.
32 */
33 @Before
34 public void setUp() throws Exception {
35 Main.initApplicationPreferences();
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.selector, "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.selector, 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}
Note: See TracBrowser for help on using the repository browser.