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

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

sonar - squid:S3578 - Test methods should comply with a naming convention

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertTrue;
8
9import java.io.StringReader;
10import java.util.Collection;
11import java.util.Iterator;
12import java.util.LinkedHashSet;
13import java.util.List;
14import java.util.Set;
15
16import org.junit.BeforeClass;
17import org.junit.Test;
18import org.openstreetmap.josm.JOSMFixture;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.command.ChangePropertyCommand;
21import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
22import org.openstreetmap.josm.command.Command;
23import org.openstreetmap.josm.command.PseudoCommand;
24import org.openstreetmap.josm.command.SequenceCommand;
25import org.openstreetmap.josm.data.osm.Node;
26import org.openstreetmap.josm.data.osm.OsmPrimitive;
27import org.openstreetmap.josm.data.osm.OsmUtils;
28import org.openstreetmap.josm.data.validation.Severity;
29import org.openstreetmap.josm.data.validation.TestError;
30import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.ParseResult;
31import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.TagCheck;
32import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
33
34/**
35 * JUnit Test of {@link MapCSSTagChecker}.
36 */
37public class MapCSSTagCheckerTest {
38
39 /**
40 * Setup test.
41 */
42 @BeforeClass
43 public static void setUp() {
44 JOSMFixture.createUnitTestFixture().init();
45 }
46
47 static MapCSSTagChecker buildTagChecker(String css) throws ParseException {
48 final MapCSSTagChecker test = new MapCSSTagChecker();
49 test.checks.putAll("test", TagCheck.readMapCSS(new StringReader(css)).parseChecks);
50 return test;
51 }
52
53 @Test
54 public void testNaturalMarsh() throws Exception {
55 ParseResult result = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
56 "*[natural=marsh] {\n" +
57 " group: tr(\"deprecated\");\n" +
58 " throwWarning: tr(\"{0}={1} is deprecated\", \"{0.key}\", tag(\"natural\"));\n" +
59 " fixRemove: \"{0.key}\";\n" +
60 " fixAdd: \"natural=wetland\";\n" +
61 " fixAdd: \"wetland=marsh\";\n" +
62 "}"));
63 final List<MapCSSTagChecker.TagCheck> checks = result.parseChecks;
64 assertEquals(1, checks.size());
65 assertTrue(result.parseErrors.isEmpty());
66 final MapCSSTagChecker.TagCheck check = checks.get(0);
67 assertNotNull(check);
68 assertEquals("{0.key}=null is deprecated", check.getDescription(null));
69 assertEquals("fixRemove: {0.key}", check.fixCommands.get(0).toString());
70 assertEquals("fixAdd: natural=wetland", check.fixCommands.get(1).toString());
71 assertEquals("fixAdd: wetland=marsh", check.fixCommands.get(2).toString());
72 final Node n1 = new Node();
73 n1.put("natural", "marsh");
74 assertTrue(check.test(n1));
75 assertEquals("deprecated", check.getErrorForPrimitive(n1).getMessage());
76 assertEquals("natural=marsh is deprecated", check.getErrorForPrimitive(n1).getDescription());
77 assertEquals(Severity.WARNING, check.getErrorForPrimitive(n1).getSeverity());
78 assertEquals("Sequence: Fix of natural=marsh is deprecated", check.fixPrimitive(n1).getDescriptionText());
79 assertEquals("{natural=}", ((ChangePropertyCommand) check.fixPrimitive(n1).getChildren().iterator().next()).getTags().toString());
80 final Node n2 = new Node();
81 n2.put("natural", "wood");
82 assertFalse(check.test(n2));
83 assertEquals("The key is natural and the value is marsh",
84 MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}", null));
85 }
86
87 @Test
88 public void testTicket10913() throws Exception {
89 final OsmPrimitive p = OsmUtils.createPrimitive("way highway=tertiary construction=yes");
90 final TagCheck check = TagCheck.readMapCSS(new StringReader("way {" +
91 "throwError: \"error\";" +
92 "fixChangeKey: \"highway => construction\";\n" +
93 "fixAdd: \"highway=construction\";\n" +
94 "}")).parseChecks.get(0);
95 final Command command = check.fixPrimitive(p);
96 assertTrue(command instanceof SequenceCommand);
97 final Iterator<PseudoCommand> it = command.getChildren().iterator();
98 assertTrue(it.next() instanceof ChangePropertyKeyCommand);
99 assertTrue(it.next() instanceof ChangePropertyCommand);
100 }
101
102 @Test
103 public void testTicket9782() throws Exception {
104 final MapCSSTagChecker test = buildTagChecker("*[/.+_name/][!name] {" +
105 "throwWarning: tr(\"has {0} but not {1}\", \"{0.key}\", \"{1.key}\");}");
106 final OsmPrimitive p = OsmUtils.createPrimitive("way alt_name=Foo");
107 final Collection<TestError> errors = test.getErrorsForPrimitive(p, false);
108 assertEquals(1, errors.size());
109 assertEquals("has alt_name but not name", errors.iterator().next().getMessage());
110 assertEquals("3000_*[.+_name][!name]", errors.iterator().next().getIgnoreSubGroup());
111 }
112
113 @Test
114 public void testTicket10859() throws Exception {
115 final MapCSSTagChecker test = buildTagChecker("way[highway=footway][foot?!] {\n" +
116 " throwWarning: tr(\"{0} used with {1}\", \"{0.value}\", \"{1.tag}\");}");
117 final OsmPrimitive p = OsmUtils.createPrimitive("way highway=footway foot=no");
118 final Collection<TestError> errors = test.getErrorsForPrimitive(p, false);
119 assertEquals(1, errors.size());
120 assertEquals("footway used with foot=no", errors.iterator().next().getMessage());
121 assertEquals("3000_way[highway=footway][foot]", errors.iterator().next().getIgnoreSubGroup());
122 }
123
124 @Test
125 public void testPreprocessing() throws Exception {
126 final MapCSSTagChecker test = buildTagChecker("" +
127 "@supports (min-josm-version: 1) { *[foo] { throwWarning: \"!\"; } }\n" +
128 "@supports (min-josm-version: 2147483647) { *[bar] { throwWarning: \"!\"; } }\n");
129 assertEquals(1, test.getErrorsForPrimitive(OsmUtils.createPrimitive("way foo=1"), false).size());
130 assertEquals(0, test.getErrorsForPrimitive(OsmUtils.createPrimitive("way bar=1"), false).size());
131 }
132
133 @Test
134 public void testInit() throws Exception {
135 MapCSSTagChecker c = new MapCSSTagChecker();
136 c.initialize();
137
138 Set<String> assertionErrors = new LinkedHashSet<>();
139 for (Set<TagCheck> schecks : c.checks.values()) {
140 assertionErrors.addAll(c.checkAsserts(schecks));
141 }
142 for (String msg : assertionErrors) {
143 Main.error(msg);
144 }
145 assertTrue("not all assertions included in the tests are met", assertionErrors.isEmpty());
146 }
147}
Note: See TracBrowser for help on using the repository browser.