source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.java@ 17531

Last change on this file since 17531 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import static org.junit.jupiter.api.Assertions.assertFalse;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6import static org.junit.jupiter.api.Assertions.fail;
7
8import java.io.StringReader;
9
10import org.junit.jupiter.api.BeforeEach;
11import org.junit.jupiter.api.Test;
12import org.junit.jupiter.api.extension.RegisterExtension;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmUtils;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.data.osm.RelationMember;
19import org.openstreetmap.josm.gui.mappaint.Environment;
20import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
21import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition;
22import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.Op;
23import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
24import org.openstreetmap.josm.testutils.JOSMTestRules;
25import org.openstreetmap.josm.tools.Logging;
26
27import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28
29/**
30 * Unit tests of {@link KeyValueCondition}.
31 */
32class KeyValueConditionTest {
33
34 private DataSet ds;
35
36 /**
37 * Setup rule
38 */
39 @RegisterExtension
40 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
41 public JOSMTestRules test = new JOSMTestRules().projection();
42
43 /**
44 * Setup test
45 */
46 @BeforeEach
47 public void setUp() {
48 ds = new DataSet();
49 }
50
51 Relation relation(int id) {
52 Relation r = new Relation(id, 1);
53 ds.addPrimitive(r);
54 return r;
55 }
56
57 Node node(int id) {
58 Node n = new Node(id, 1);
59 n.setCoor(LatLon.ZERO);
60 ds.addPrimitive(n);
61 return n;
62 }
63
64 private static void shouldFail(Runnable r) {
65 try {
66 r.run();
67 fail("should throw exception");
68 } catch (MapCSSException e) {
69 Logging.trace(e);
70 }
71 }
72
73 @Test
74 public void create() {
75 ConditionFactory.createKeyValueCondition("a key", "a value", Op.EQ, Context.PRIMITIVE, false);
76
77 ConditionFactory.createKeyValueCondition("role", "a role", Op.EQ, Context.LINK, false);
78 ConditionFactory.createKeyValueCondition("RoLe", "a role", Op.EQ, Context.LINK, false);
79
80 shouldFail(() ->
81 ConditionFactory.createKeyValueCondition("an arbitry tag", "a role", Op.EQ, Context.LINK, false)
82 );
83 }
84
85 @Test
86 public void applies_1() {
87 Relation r = relation(1);
88 Node n = node(1);
89 r.addMember(new RelationMember("my_role", n));
90
91 Environment e = new Environment(n).withParent(r).withLinkContext().withIndex(0, r.getMembersCount());
92
93 Condition cond = new ConditionFactory.RoleCondition("my_role", Op.EQ);
94 assertTrue(cond.applies(e));
95
96 cond = new ConditionFactory.RoleCondition("another_role", Op.EQ);
97 assertFalse(cond.applies(e));
98 }
99
100 @Test
101 public void applies_2() {
102 Relation r = relation(1);
103 Node n = node(1);
104 r.addMember(new RelationMember("my_role", n));
105
106 Environment e = new Environment(n).withParent(r).withIndex(0, r.getMembersCount()).withLinkContext();
107
108 Condition cond = ConditionFactory.createKeyValueCondition("role", "my_role", Op.NEQ, Context.LINK, false);
109 assertFalse(cond.applies(e));
110
111 cond = ConditionFactory.createKeyValueCondition("role", "another_role", Op.NEQ, Context.LINK, false);
112 assertTrue(cond.applies(e));
113 }
114
115 @Test
116 void testKeyRegexValueRegex() throws Exception {
117 Selector selPos = new MapCSSParser(new StringReader("*[/^source/ =~ /.*,.*/]")).selector();
118 Selector selNeg = new MapCSSParser(new StringReader("*[/^source/ !~ /.*,.*/]")).selector();
119 assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way foo=bar"))));
120 assertTrue(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1,2"))));
121 assertTrue(selPos.matches(new Environment(OsmUtils.createPrimitive("way source_foo_bar=1,2"))));
122 assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1"))));
123 assertFalse(selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1"))));
124 assertFalse(selNeg.matches(new Environment(OsmUtils.createPrimitive("way source=1,2"))));
125 assertFalse(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=1,2"))));
126 assertTrue(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=baz"))));
127 assertTrue(selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar src=1,2"))));
128 }
129
130 @Test
131 void testValueFive() throws Exception {
132 // ticket #5985
133 Selector sel = new MapCSSParser(new StringReader("*[width=5]")).selector();
134 assertTrue(sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=5"))));
135 assertFalse(sel.matches(new Environment(OsmUtils.createPrimitive("way highway=track width=2"))));
136 }
137
138 @Test
139 void testValueZero() throws Exception {
140 // ticket #12267
141 Selector sel = new MapCSSParser(new StringReader("*[frequency=0]")).selector();
142 assertTrue(sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=0"))));
143 assertFalse(sel.matches(new Environment(OsmUtils.createPrimitive("way railway=rail frequency=50"))));
144 }
145}
Note: See TracBrowser for help on using the repository browser.