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

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

javadoc/code style/minor refactorization

File size: 4.3 KB
RevLine 
[6506]1package org.openstreetmap.josm.data.validation.tests;
2
[6603]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
[6506]15import org.junit.Before;
16import org.junit.Test;
[6592]17import org.openstreetmap.TestUtils;
[6506]18import org.openstreetmap.josm.Main;
[6538]19import org.openstreetmap.josm.command.ChangePropertyCommand;
[6506]20import org.openstreetmap.josm.data.osm.Node;
[6512]21import org.openstreetmap.josm.data.osm.OsmPrimitive;
[6506]22import org.openstreetmap.josm.data.osm.Tag;
[6538]23import org.openstreetmap.josm.data.validation.Severity;
[6601]24import org.openstreetmap.josm.data.validation.TestError;
25import org.openstreetmap.josm.tools.Predicate;
26import org.openstreetmap.josm.tools.Utils;
[6506]27
28public class MapCSSTagCheckerTest {
29
[6881]30 /**
31 * Setup test.
32 */
[6506]33 @Before
34 public void setUp() throws Exception {
[6603]35 Main.initApplicationPreferences();
[6506]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" +
[6677]43 " throwWarning: tr(\"{0}={1} is deprecated\", \"{0.key}\", tag(\"natural\"));\n" +
[6538]44 " fixRemove: \"{0.key}\";\n" +
[6506]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());
[6677]51 assertThat(check.getDescription(null), is("{0.key}=null is deprecated"));
[6538]52 assertThat(check.change.get(0).apply(null), is(new Tag("{0.key}")));
[6534]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")));
[6506]55 final Node n1 = new Node();
56 n1.put("natural", "marsh");
57 assertTrue(check.matchesPrimitive(n1));
[6538]58 assertThat(check.getErrorForPrimitive(n1).getMessage(), is("natural=marsh is deprecated"));
59 assertThat(check.getErrorForPrimitive(n1).getSeverity(), is(Severity.WARNING));
[6548]60 assertThat(check.fixPrimitive(n1).getDescriptionText(), is("Sequence: Fix of natural=marsh is deprecated"));
[6538]61 assertThat(((ChangePropertyCommand) check.fixPrimitive(n1).getChildren().iterator().next()).getTags().toString(),
62 is("{natural=}"));
[6506]63 final Node n2 = new Node();
64 n2.put("natural", "wood");
65 assertFalse(check.matchesPrimitive(n2));
[6601]66 assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}"),
[6538]67 is("The key is natural and the value is marsh"));
[6506]68 }
69
[6512]70 @Test
[6506]71 public void testInit() throws Exception {
72 final MapCSSTagChecker c = new MapCSSTagChecker();
73 c.initialize();
[6512]74
75 LinkedHashSet<String> assertionErrors = new LinkedHashSet<String>();
76 for (final MapCSSTagChecker.TagCheck check : c.checks) {
77 for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
[6592]78 final OsmPrimitive p = TestUtils.createPrimitive(i.getKey());
[6636]79 final boolean isError = Utils.exists(c.getErrorsForPrimitive(p, true), new Predicate<TestError>() {
[6601]80 @Override
81 public boolean evaluate(TestError e) {
82 //noinspection EqualsBetweenInconvertibleTypes
83 return e.getTester().equals(check.rule);
84 }
85 });
86 if (isError != i.getValue()) {
[6538]87 final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
[6677]88 check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
[6512]89 System.err.println(error);
90 assertionErrors.add(error);
91 }
92 }
93 }
94 assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
95
[6506]96 }
97}
Note: See TracBrowser for help on using the repository browser.