1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.search;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
10 |
|
---|
11 | import java.lang.reflect.Field;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.nio.file.Files;
|
---|
14 | import java.nio.file.Paths;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.Collections;
|
---|
18 | import java.util.Set;
|
---|
19 |
|
---|
20 | import org.junit.Assert;
|
---|
21 | import org.junit.jupiter.api.Test;
|
---|
22 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
23 | import org.openstreetmap.josm.TestUtils;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
30 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
31 | import org.openstreetmap.josm.data.osm.RelationData;
|
---|
32 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
33 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
34 | import org.openstreetmap.josm.data.osm.User;
|
---|
35 | import org.openstreetmap.josm.data.osm.Way;
|
---|
36 | import org.openstreetmap.josm.data.osm.WayData;
|
---|
37 | import org.openstreetmap.josm.data.osm.search.SearchCompiler.ExactKeyValue;
|
---|
38 | import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
|
---|
39 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
40 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetMenu;
|
---|
41 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
|
---|
42 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
43 | import org.openstreetmap.josm.gui.tagging.presets.items.Key;
|
---|
44 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
45 | import org.openstreetmap.josm.tools.Logging;
|
---|
46 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
47 |
|
---|
48 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
49 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
50 | import nl.jqno.equalsverifier.Warning;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Unit tests for class {@link SearchCompiler}.
|
---|
54 | */
|
---|
55 | class SearchCompilerTest {
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * We need prefs for this. We access preferences when creating OSM primitives.
|
---|
59 | */
|
---|
60 | @RegisterExtension
|
---|
61 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
62 | public JOSMTestRules test = new JOSMTestRules().preferences().timeout(30000);
|
---|
63 |
|
---|
64 | private static final class SearchContext {
|
---|
65 | final DataSet ds = new DataSet();
|
---|
66 | final Node n1 = new Node(LatLon.ZERO);
|
---|
67 | final Node n2 = new Node(new LatLon(5, 5));
|
---|
68 | final Way w1 = new Way();
|
---|
69 | final Way w2 = new Way();
|
---|
70 | final Relation r1 = new Relation();
|
---|
71 | final Relation r2 = new Relation();
|
---|
72 |
|
---|
73 | private final Match m;
|
---|
74 | private final Match n;
|
---|
75 |
|
---|
76 | private SearchContext(String state) throws SearchParseError {
|
---|
77 | m = SearchCompiler.compile(state);
|
---|
78 | n = SearchCompiler.compile('-' + state);
|
---|
79 | ds.addPrimitive(n1);
|
---|
80 | ds.addPrimitive(n2);
|
---|
81 | w1.addNode(n1);
|
---|
82 | w1.addNode(n2);
|
---|
83 | w2.addNode(n1);
|
---|
84 | w2.addNode(n2);
|
---|
85 | ds.addPrimitive(w1);
|
---|
86 | ds.addPrimitive(w2);
|
---|
87 | r1.addMember(new RelationMember("", w1));
|
---|
88 | r1.addMember(new RelationMember("", w2));
|
---|
89 | r2.addMember(new RelationMember("", w1));
|
---|
90 | r2.addMember(new RelationMember("", w2));
|
---|
91 | ds.addPrimitive(r1);
|
---|
92 | ds.addPrimitive(r2);
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void match(OsmPrimitive p, boolean cond) {
|
---|
96 | if (cond) {
|
---|
97 | assertTrue(m.match(p), p::toString);
|
---|
98 | assertFalse(n.match(p), p::toString);
|
---|
99 | } else {
|
---|
100 | assertFalse(m.match(p), p::toString);
|
---|
101 | assertTrue(n.match(p), p::toString);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Search anything.
|
---|
108 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
109 | */
|
---|
110 | @Test
|
---|
111 | void testAny() throws SearchParseError {
|
---|
112 | final SearchCompiler.Match c = SearchCompiler.compile("foo");
|
---|
113 | assertTrue(c.match(OsmUtils.createPrimitive("node foobar=true")));
|
---|
114 | assertTrue(c.match(OsmUtils.createPrimitive("node name=hello-foo-xy")));
|
---|
115 | assertFalse(c.match(OsmUtils.createPrimitive("node name=X")));
|
---|
116 | assertEquals("foo", c.toString());
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Search by equality key=value.
|
---|
121 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
122 | */
|
---|
123 | @Test
|
---|
124 | void testEquals() throws SearchParseError {
|
---|
125 | final SearchCompiler.Match c = SearchCompiler.compile("foo=bar");
|
---|
126 | assertFalse(c.match(OsmUtils.createPrimitive("node foobar=true")));
|
---|
127 | assertTrue(c.match(OsmUtils.createPrimitive("node foo=bar")));
|
---|
128 | assertFalse(c.match(OsmUtils.createPrimitive("node fooX=bar")));
|
---|
129 | assertFalse(c.match(OsmUtils.createPrimitive("node foo=barX")));
|
---|
130 | assertEquals("foo=bar", c.toString());
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Search by regular expression: key~value.
|
---|
135 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
136 | */
|
---|
137 | @Test
|
---|
138 | void testRegexp() throws SearchParseError {
|
---|
139 | final SearchCompiler.Match c = SearchCompiler.compile("foo~[Bb]a[rz]");
|
---|
140 | assertFalse(c.match(OsmUtils.createPrimitive("node foobar=true")));
|
---|
141 | assertFalse(c.match(OsmUtils.createPrimitive("node foo=foo")));
|
---|
142 | assertTrue(c.match(OsmUtils.createPrimitive("node foo=bar")));
|
---|
143 | assertTrue(c.match(OsmUtils.createPrimitive("node foo=baz")));
|
---|
144 | assertTrue(c.match(OsmUtils.createPrimitive("node foo=Baz")));
|
---|
145 | assertEquals("foo=[Bb]a[rz]", c.toString());
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Search by comparison.
|
---|
150 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
151 | */
|
---|
152 | @Test
|
---|
153 | void testCompare() throws SearchParseError {
|
---|
154 | final SearchCompiler.Match c1 = SearchCompiler.compile("start_date>1950");
|
---|
155 | assertTrue(c1.match(OsmUtils.createPrimitive("node start_date=1950-01-01")));
|
---|
156 | assertTrue(c1.match(OsmUtils.createPrimitive("node start_date=1960")));
|
---|
157 | assertFalse(c1.match(OsmUtils.createPrimitive("node start_date=1950")));
|
---|
158 | assertFalse(c1.match(OsmUtils.createPrimitive("node start_date=1000")));
|
---|
159 | assertTrue(c1.match(OsmUtils.createPrimitive("node start_date=101010")));
|
---|
160 |
|
---|
161 | final SearchCompiler.Match c2 = SearchCompiler.compile("start_date<1960");
|
---|
162 | assertTrue(c2.match(OsmUtils.createPrimitive("node start_date=1950-01-01")));
|
---|
163 | assertFalse(c2.match(OsmUtils.createPrimitive("node start_date=1960")));
|
---|
164 | assertTrue(c2.match(OsmUtils.createPrimitive("node start_date=1950")));
|
---|
165 | assertTrue(c2.match(OsmUtils.createPrimitive("node start_date=1000")));
|
---|
166 | assertTrue(c2.match(OsmUtils.createPrimitive("node start_date=200")));
|
---|
167 |
|
---|
168 | final SearchCompiler.Match c3 = SearchCompiler.compile("name<I");
|
---|
169 | assertTrue(c3.match(OsmUtils.createPrimitive("node name=Alpha")));
|
---|
170 | assertFalse(c3.match(OsmUtils.createPrimitive("node name=Sigma")));
|
---|
171 |
|
---|
172 | final SearchCompiler.Match c4 = SearchCompiler.compile("\"start_date\"<1960");
|
---|
173 | assertTrue(c4.match(OsmUtils.createPrimitive("node start_date=1950-01-01")));
|
---|
174 | assertFalse(c4.match(OsmUtils.createPrimitive("node start_date=2000")));
|
---|
175 |
|
---|
176 | final SearchCompiler.Match c5 = SearchCompiler.compile("height>180");
|
---|
177 | assertTrue(c5.match(OsmUtils.createPrimitive("node height=200")));
|
---|
178 | assertTrue(c5.match(OsmUtils.createPrimitive("node height=99999")));
|
---|
179 | assertFalse(c5.match(OsmUtils.createPrimitive("node height=50")));
|
---|
180 | assertFalse(c5.match(OsmUtils.createPrimitive("node height=-9999")));
|
---|
181 | assertFalse(c5.match(OsmUtils.createPrimitive("node height=fixme")));
|
---|
182 |
|
---|
183 | final SearchCompiler.Match c6 = SearchCompiler.compile("name>C");
|
---|
184 | assertTrue(c6.match(OsmUtils.createPrimitive("node name=Delta")));
|
---|
185 | assertFalse(c6.match(OsmUtils.createPrimitive("node name=Alpha")));
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Search by nth.
|
---|
190 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
191 | */
|
---|
192 | @Test
|
---|
193 | void testNth() throws SearchParseError {
|
---|
194 | final DataSet dataSet = new DataSet();
|
---|
195 | final Way way = new Way();
|
---|
196 | final Node node0 = new Node(new LatLon(1, 1));
|
---|
197 | final Node node1 = new Node(new LatLon(2, 2));
|
---|
198 | final Node node2 = new Node(new LatLon(3, 3));
|
---|
199 | dataSet.addPrimitive(way);
|
---|
200 | dataSet.addPrimitive(node0);
|
---|
201 | dataSet.addPrimitive(node1);
|
---|
202 | dataSet.addPrimitive(node2);
|
---|
203 | way.addNode(node0);
|
---|
204 | way.addNode(node1);
|
---|
205 | way.addNode(node2);
|
---|
206 | assertFalse(SearchCompiler.compile("nth:2").match(node1));
|
---|
207 | assertTrue(SearchCompiler.compile("nth:1").match(node1));
|
---|
208 | assertFalse(SearchCompiler.compile("nth:0").match(node1));
|
---|
209 | assertTrue(SearchCompiler.compile("nth:0").match(node0));
|
---|
210 | assertTrue(SearchCompiler.compile("nth:2").match(node2));
|
---|
211 | assertTrue(SearchCompiler.compile("nth:-1").match(node2));
|
---|
212 | assertTrue(SearchCompiler.compile("nth:-2").match(node1));
|
---|
213 | assertTrue(SearchCompiler.compile("nth:-3").match(node0));
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Search by negative nth.
|
---|
218 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
219 | */
|
---|
220 | @Test
|
---|
221 | void testNthParseNegative() throws SearchParseError {
|
---|
222 | assertEquals("Nth{nth=-1, modulo=false}", SearchCompiler.compile("nth:-1").toString());
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Search by modified status.
|
---|
227 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
228 | */
|
---|
229 | @Test
|
---|
230 | void testModified() throws SearchParseError {
|
---|
231 | SearchContext sc = new SearchContext("modified");
|
---|
232 | // Not modified but new
|
---|
233 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
|
---|
234 | assertFalse(p.isModified(), p::toString);
|
---|
235 | assertTrue(p.isNewOrUndeleted(), p::toString);
|
---|
236 | sc.match(p, true);
|
---|
237 | }
|
---|
238 | // Modified and new
|
---|
239 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
|
---|
240 | p.setModified(true);
|
---|
241 | assertTrue(p.isModified(), p::toString);
|
---|
242 | assertTrue(p.isNewOrUndeleted(), p::toString);
|
---|
243 | sc.match(p, true);
|
---|
244 | }
|
---|
245 | // Modified but not new
|
---|
246 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
|
---|
247 | p.setOsmId(1, 1);
|
---|
248 | assertTrue(p.isModified(), p::toString);
|
---|
249 | assertFalse(p.isNewOrUndeleted(), p::toString);
|
---|
250 | sc.match(p, true);
|
---|
251 | }
|
---|
252 | // Not modified nor new
|
---|
253 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
|
---|
254 | p.setOsmId(2, 2);
|
---|
255 | assertFalse(p.isModified(), p::toString);
|
---|
256 | assertFalse(p.isNewOrUndeleted(), p::toString);
|
---|
257 | sc.match(p, false);
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Search by selected status.
|
---|
263 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
264 | */
|
---|
265 | @Test
|
---|
266 | void testSelected() throws SearchParseError {
|
---|
267 | SearchContext sc = new SearchContext("selected");
|
---|
268 | // Not selected
|
---|
269 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
|
---|
270 | assertFalse(p.isSelected(), p::toString);
|
---|
271 | sc.match(p, false);
|
---|
272 | }
|
---|
273 | // Selected
|
---|
274 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
|
---|
275 | sc.ds.addSelected(p);
|
---|
276 | assertTrue(p.isSelected(), p::toString);
|
---|
277 | sc.match(p, true);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Search by incomplete status.
|
---|
283 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
284 | */
|
---|
285 | @Test
|
---|
286 | void testIncomplete() throws SearchParseError {
|
---|
287 | SearchContext sc = new SearchContext("incomplete");
|
---|
288 | // Not incomplete
|
---|
289 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
|
---|
290 | assertFalse(p.isIncomplete(), p::toString);
|
---|
291 | sc.match(p, false);
|
---|
292 | }
|
---|
293 | // Incomplete
|
---|
294 | sc.n2.setCoor(null);
|
---|
295 | WayData wd = new WayData();
|
---|
296 | wd.setIncomplete(true);
|
---|
297 | sc.w2.load(wd);
|
---|
298 | RelationData rd = new RelationData();
|
---|
299 | rd.setIncomplete(true);
|
---|
300 | sc.r2.load(rd);
|
---|
301 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
|
---|
302 | assertTrue(p.isIncomplete(), p::toString);
|
---|
303 | sc.match(p, true);
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Search by untagged status.
|
---|
309 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
310 | */
|
---|
311 | @Test
|
---|
312 | void testUntagged() throws SearchParseError {
|
---|
313 | SearchContext sc = new SearchContext("untagged");
|
---|
314 | // Untagged
|
---|
315 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
|
---|
316 | assertFalse(p.isTagged(), p::toString);
|
---|
317 | sc.match(p, true);
|
---|
318 | }
|
---|
319 | // Tagged
|
---|
320 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
|
---|
321 | p.put("foo", "bar");
|
---|
322 | assertTrue(p.isTagged(), p::toString);
|
---|
323 | sc.match(p, false);
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Search by closed status.
|
---|
329 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
330 | */
|
---|
331 | @Test
|
---|
332 | void testClosed() throws SearchParseError {
|
---|
333 | SearchContext sc = new SearchContext("closed");
|
---|
334 | // Closed
|
---|
335 | sc.w1.addNode(sc.n1);
|
---|
336 | for (Way w : new Way[]{sc.w1}) {
|
---|
337 | assertTrue(w.isClosed(), w::toString);
|
---|
338 | sc.match(w, true);
|
---|
339 | }
|
---|
340 | // Unclosed
|
---|
341 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.n2, sc.w2, sc.r1, sc.r2}) {
|
---|
342 | sc.match(p, false);
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Search by new status.
|
---|
348 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
349 | */
|
---|
350 | @Test
|
---|
351 | void testNew() throws SearchParseError {
|
---|
352 | SearchContext sc = new SearchContext("new");
|
---|
353 | // New
|
---|
354 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
|
---|
355 | assertTrue(p.isNew(), p::toString);
|
---|
356 | sc.match(p, true);
|
---|
357 | }
|
---|
358 | // Not new
|
---|
359 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
|
---|
360 | p.setOsmId(2, 2);
|
---|
361 | assertFalse(p.isNew(), p::toString);
|
---|
362 | sc.match(p, false);
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Search for node objects.
|
---|
368 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
369 | */
|
---|
370 | @Test
|
---|
371 | void testTypeNode() throws SearchParseError {
|
---|
372 | final SearchContext sc = new SearchContext("type:node");
|
---|
373 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.n2, sc.w1, sc.w2, sc.r1, sc.r2}) {
|
---|
374 | sc.match(p, OsmPrimitiveType.NODE.equals(p.getType()));
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Search for way objects.
|
---|
380 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
381 | */
|
---|
382 | @Test
|
---|
383 | void testTypeWay() throws SearchParseError {
|
---|
384 | final SearchContext sc = new SearchContext("type:way");
|
---|
385 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.n2, sc.w1, sc.w2, sc.r1, sc.r2}) {
|
---|
386 | sc.match(p, OsmPrimitiveType.WAY.equals(p.getType()));
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Search for relation objects.
|
---|
392 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
393 | */
|
---|
394 | @Test
|
---|
395 | void testTypeRelation() throws SearchParseError {
|
---|
396 | final SearchContext sc = new SearchContext("type:relation");
|
---|
397 | for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.n2, sc.w1, sc.w2, sc.r1, sc.r2}) {
|
---|
398 | sc.match(p, OsmPrimitiveType.RELATION.equals(p.getType()));
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Search for users.
|
---|
404 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
405 | */
|
---|
406 | @Test
|
---|
407 | void testUser() throws SearchParseError {
|
---|
408 | final SearchContext foobar = new SearchContext("user:foobar");
|
---|
409 | foobar.n1.setUser(User.createLocalUser("foobar"));
|
---|
410 | foobar.match(foobar.n1, true);
|
---|
411 | foobar.match(foobar.n2, false);
|
---|
412 | final SearchContext anonymous = new SearchContext("user:anonymous");
|
---|
413 | anonymous.n1.setUser(User.createLocalUser("foobar"));
|
---|
414 | anonymous.match(anonymous.n1, false);
|
---|
415 | anonymous.match(anonymous.n2, true);
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Compiles "foo type bar" and tests the parse error message
|
---|
420 | * @throws SearchParseError always
|
---|
421 | */
|
---|
422 | @Test
|
---|
423 | void testFooTypeBar() throws SearchParseError {
|
---|
424 | Exception e = assertThrows(SearchParseError.class, () -> SearchCompiler.compile("foo type bar"));
|
---|
425 | assertEquals("<html>Expecting <code>:</code> after <i>type</i></html>", e.getMessage());
|
---|
426 | }
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Search for primitive timestamps.
|
---|
430 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
431 | */
|
---|
432 | @Test
|
---|
433 | void testTimestamp() throws SearchParseError {
|
---|
434 | final Node n1 = new Node();
|
---|
435 | n1.setTimestamp(DateUtils.fromString("2010-01-22"));
|
---|
436 | assertTrue(SearchCompiler.compile("timestamp:2010/2011").match(n1));
|
---|
437 | assertTrue(SearchCompiler.compile("timestamp:2010-01/2011").match(n1));
|
---|
438 | assertTrue(SearchCompiler.compile("timestamp:2010-01-22/2011").match(n1));
|
---|
439 | assertFalse(SearchCompiler.compile("timestamp:2010-01-23/2011").match(n1));
|
---|
440 | assertFalse(SearchCompiler.compile("timestamp:2010/2010-01-21").match(n1));
|
---|
441 | n1.setTimestamp(DateUtils.fromString("2016-01-22"));
|
---|
442 | assertFalse(SearchCompiler.compile("timestamp:2010/2011").match(n1));
|
---|
443 | }
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Tests the implementation of the Boolean logic.
|
---|
447 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
448 | */
|
---|
449 | @Test
|
---|
450 | void testBooleanLogic() throws SearchParseError {
|
---|
451 | final SearchCompiler.Match c1 = SearchCompiler.compile("foo AND bar AND baz");
|
---|
452 | assertTrue(c1.match(OsmUtils.createPrimitive("node foobar=baz")));
|
---|
453 | assertEquals("foo && bar && baz", c1.toString());
|
---|
454 | final SearchCompiler.Match c2 = SearchCompiler.compile("foo AND (bar OR baz)");
|
---|
455 | assertTrue(c2.match(OsmUtils.createPrimitive("node foobar=yes")));
|
---|
456 | assertTrue(c2.match(OsmUtils.createPrimitive("node foobaz=yes")));
|
---|
457 | assertEquals("foo && (bar || baz)", c2.toString());
|
---|
458 | final SearchCompiler.Match c3 = SearchCompiler.compile("foo OR (bar baz)");
|
---|
459 | assertEquals("foo || (bar && baz)", c3.toString());
|
---|
460 | final SearchCompiler.Match c4 = SearchCompiler.compile("foo1 OR (bar1 bar2 baz1 XOR baz2) OR foo2");
|
---|
461 | assertEquals("foo1 || (bar1 && bar2 && (baz1 ^ baz2)) || foo2", c4.toString());
|
---|
462 | final SearchCompiler.Match c5 = SearchCompiler.compile("foo1 XOR (baz1 XOR (bar baz))");
|
---|
463 | assertEquals("foo1 ^ baz1 ^ (bar && baz)", c5.toString());
|
---|
464 | final SearchCompiler.Match c6 = SearchCompiler.compile("foo1 XOR ((baz1 baz2) XOR (bar OR baz))");
|
---|
465 | assertEquals("foo1 ^ (baz1 && baz2) ^ (bar || baz)", c6.toString());
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Tests {@code buildSearchStringForTag}.
|
---|
470 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
471 | */
|
---|
472 | @Test
|
---|
473 | void testBuildSearchStringForTag() throws SearchParseError {
|
---|
474 | final Tag tag1 = new Tag("foo=", "bar\"");
|
---|
475 | final Tag tag2 = new Tag("foo=", "=bar");
|
---|
476 | final String search1 = SearchCompiler.buildSearchStringForTag(tag1.getKey(), tag1.getValue());
|
---|
477 | assertEquals("\"foo=\"=\"bar\\\"\"", search1);
|
---|
478 | assertTrue(SearchCompiler.compile(search1).match(tag1));
|
---|
479 | assertFalse(SearchCompiler.compile(search1).match(tag2));
|
---|
480 | final String search2 = SearchCompiler.buildSearchStringForTag(tag1.getKey(), "");
|
---|
481 | assertEquals("\"foo=\"=*", search2);
|
---|
482 | assertTrue(SearchCompiler.compile(search2).match(tag1));
|
---|
483 | assertTrue(SearchCompiler.compile(search2).match(tag2));
|
---|
484 | }
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/13870">Bug #13870</a>.
|
---|
488 | */
|
---|
489 | @Test
|
---|
490 | void testPattern13870() {
|
---|
491 | // https://bugs.openjdk.java.net/browse/JI-9044959
|
---|
492 | SearchSetting setting = new SearchSetting();
|
---|
493 | setting.regexSearch = true;
|
---|
494 | setting.text = "[";
|
---|
495 | assertThrows(SearchParseError.class, () -> SearchCompiler.compile(setting));
|
---|
496 | }
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14217">Bug #14217</a>.
|
---|
500 | * @throws Exception never
|
---|
501 | */
|
---|
502 | @Test
|
---|
503 | void testTicket14217() throws Exception {
|
---|
504 | assertNotNull(SearchCompiler.compile(new String(Files.readAllBytes(
|
---|
505 | Paths.get(TestUtils.getRegressionDataFile(14217, "filter.txt"))), StandardCharsets.UTF_8)));
|
---|
506 | }
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17112">Bug #17112</a>.
|
---|
510 | */
|
---|
511 | @Test
|
---|
512 | void testTicket17112() {
|
---|
513 | SearchSetting setting = new SearchSetting();
|
---|
514 | setting.mapCSSSearch = true;
|
---|
515 | setting.text = "w"; // partial input
|
---|
516 | assertThrows(SearchParseError.class, () -> SearchCompiler.compile(setting));
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Test empty values.
|
---|
521 | * @throws SearchParseError never
|
---|
522 | */
|
---|
523 | @Test
|
---|
524 | void testEmptyValues15943() throws SearchParseError {
|
---|
525 | Match matcher = SearchCompiler.compile("access=");
|
---|
526 | assertTrue(matcher.match(new Tag("access", null)));
|
---|
527 | assertTrue(matcher.match(new Tag("access", "")));
|
---|
528 | assertFalse(matcher.match(new Tag("access", "private")));
|
---|
529 | }
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Test whether a key exists.
|
---|
533 | * @throws SearchParseError never
|
---|
534 | */
|
---|
535 | @Test
|
---|
536 | void testKeyExists15943() throws SearchParseError {
|
---|
537 | Match matcher = SearchCompiler.compile("surface:");
|
---|
538 | assertTrue(matcher.match(new Tag("surface", "")));
|
---|
539 | assertTrue(matcher.match(new Tag("surface", "wood")));
|
---|
540 | assertFalse(matcher.match(new Tag("surface:source", "xxx")));
|
---|
541 | assertFalse(matcher.match(new Tag("foo", "bar")));
|
---|
542 | assertFalse(matcher.match(new Tag("name", "foo:surface:bar")));
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Unit test of {@link SearchCompiler.ExactKeyValue.Mode} enum.
|
---|
547 | */
|
---|
548 | @Test
|
---|
549 | void testEnumExactKeyValueMode() {
|
---|
550 | assertDoesNotThrow(() -> TestUtils.superficialEnumCodeCoverage(ExactKeyValue.Mode.class));
|
---|
551 | }
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Robustness test for preset searching. Ensures that the query 'preset:' is not accepted.
|
---|
555 | * @since 12464
|
---|
556 | */
|
---|
557 | @Test
|
---|
558 | void testPresetSearchMissingValue() {
|
---|
559 | SearchSetting settings = new SearchSetting();
|
---|
560 | settings.text = "preset:";
|
---|
561 | settings.mapCSSSearch = false;
|
---|
562 |
|
---|
563 | TaggingPresets.readFromPreferences();
|
---|
564 |
|
---|
565 | assertThrows(SearchParseError.class, () -> SearchCompiler.compile(settings));
|
---|
566 | }
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * Robustness test for preset searching. Validates that it is not possible to search for
|
---|
570 | * non existing presets.
|
---|
571 | * @since 12464
|
---|
572 | */
|
---|
573 | @Test
|
---|
574 | void testPresetNotExist() {
|
---|
575 | String testPresetName = "groupnamethatshouldnotexist/namethatshouldnotexist";
|
---|
576 | SearchSetting settings = new SearchSetting();
|
---|
577 | settings.text = "preset:" + testPresetName;
|
---|
578 | settings.mapCSSSearch = false;
|
---|
579 |
|
---|
580 | // load presets
|
---|
581 | TaggingPresets.readFromPreferences();
|
---|
582 |
|
---|
583 | assertThrows(SearchParseError.class, () -> SearchCompiler.compile(settings));
|
---|
584 | }
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Robustness tests for preset searching. Ensures that combined preset names (having more than
|
---|
588 | * 1 word) must be enclosed with " .
|
---|
589 | * @since 12464
|
---|
590 | */
|
---|
591 | @Test
|
---|
592 | void testPresetMultipleWords() {
|
---|
593 | TaggingPreset testPreset = new TaggingPreset();
|
---|
594 | testPreset.name = "Test Combined Preset Name";
|
---|
595 | testPreset.group = new TaggingPresetMenu();
|
---|
596 | testPreset.group.name = "TestGroupName";
|
---|
597 |
|
---|
598 | String combinedPresetname = testPreset.getRawName();
|
---|
599 | SearchSetting settings = new SearchSetting();
|
---|
600 | settings.text = "preset:" + combinedPresetname;
|
---|
601 | settings.mapCSSSearch = false;
|
---|
602 |
|
---|
603 | // load presets
|
---|
604 | TaggingPresets.readFromPreferences();
|
---|
605 |
|
---|
606 | assertThrows(SearchParseError.class, () -> SearchCompiler.compile(settings));
|
---|
607 | }
|
---|
608 |
|
---|
609 | /**
|
---|
610 | * Ensures that correct presets are stored in the {@link org.openstreetmap.josm.data.osm.search.SearchCompiler.Preset}
|
---|
611 | * class against which the osm primitives are tested.
|
---|
612 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
613 | * @throws NoSuchFieldException if there is no field called 'presets'
|
---|
614 | * @throws IllegalAccessException if cannot access the field where all matching presets are stored
|
---|
615 | * @since 12464
|
---|
616 | */
|
---|
617 | @Test
|
---|
618 | void testPresetLookup() throws SearchParseError, NoSuchFieldException, IllegalAccessException {
|
---|
619 | TaggingPreset testPreset = new TaggingPreset();
|
---|
620 | testPreset.name = "Test Preset Name";
|
---|
621 | testPreset.group = new TaggingPresetMenu();
|
---|
622 | testPreset.group.name = "Test Preset Group Name";
|
---|
623 |
|
---|
624 | String query = "preset:" +
|
---|
625 | "\"" + testPreset.getRawName() + "\"";
|
---|
626 | SearchSetting settings = new SearchSetting();
|
---|
627 | settings.text = query;
|
---|
628 | settings.mapCSSSearch = false;
|
---|
629 |
|
---|
630 | // load presets and add the test preset
|
---|
631 | TaggingPresets.readFromPreferences();
|
---|
632 | TaggingPresets.addTaggingPresets(Collections.singletonList(testPreset));
|
---|
633 |
|
---|
634 | Match match = SearchCompiler.compile(settings);
|
---|
635 |
|
---|
636 | // access the private field where all matching presets are stored
|
---|
637 | // and ensure that indeed the correct ones are there
|
---|
638 | Field field = match.getClass().getDeclaredField("presets");
|
---|
639 | field.setAccessible(true);
|
---|
640 | @SuppressWarnings("unchecked")
|
---|
641 | Collection<TaggingPreset> foundPresets = (Collection<TaggingPreset>) field.get(match);
|
---|
642 |
|
---|
643 | assertEquals(1, foundPresets.size());
|
---|
644 | assertTrue(foundPresets.contains(testPreset));
|
---|
645 | }
|
---|
646 |
|
---|
647 | /**
|
---|
648 | * Ensures that the wildcard search works and that correct presets are stored in
|
---|
649 | * the {@link org.openstreetmap.josm.data.osm.search.SearchCompiler.Preset} class against which
|
---|
650 | * the osm primitives are tested.
|
---|
651 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
652 | * @throws NoSuchFieldException if there is no field called 'presets'
|
---|
653 | * @throws IllegalAccessException if cannot access the field where all matching presets are stored
|
---|
654 | * @since 12464
|
---|
655 | */
|
---|
656 | @Test
|
---|
657 | void testPresetLookupWildcard() throws SearchParseError, NoSuchFieldException, IllegalAccessException {
|
---|
658 | TaggingPresetMenu group = new TaggingPresetMenu();
|
---|
659 | group.name = "TestPresetGroup";
|
---|
660 |
|
---|
661 | TaggingPreset testPreset1 = new TaggingPreset();
|
---|
662 | testPreset1.name = "TestPreset1";
|
---|
663 | testPreset1.group = group;
|
---|
664 |
|
---|
665 | TaggingPreset testPreset2 = new TaggingPreset();
|
---|
666 | testPreset2.name = "TestPreset2";
|
---|
667 | testPreset2.group = group;
|
---|
668 |
|
---|
669 | TaggingPreset testPreset3 = new TaggingPreset();
|
---|
670 | testPreset3.name = "TestPreset3";
|
---|
671 | testPreset3.group = group;
|
---|
672 |
|
---|
673 | String query = "preset:" + "\"" + group.getRawName() + "/*\"";
|
---|
674 | SearchSetting settings = new SearchSetting();
|
---|
675 | settings.text = query;
|
---|
676 | settings.mapCSSSearch = false;
|
---|
677 |
|
---|
678 | TaggingPresets.readFromPreferences();
|
---|
679 | TaggingPresets.addTaggingPresets(Arrays.asList(testPreset1, testPreset2, testPreset3));
|
---|
680 |
|
---|
681 | Match match = SearchCompiler.compile(settings);
|
---|
682 |
|
---|
683 | // access the private field where all matching presets are stored
|
---|
684 | // and ensure that indeed the correct ones are there
|
---|
685 | Field field = match.getClass().getDeclaredField("presets");
|
---|
686 | field.setAccessible(true);
|
---|
687 | @SuppressWarnings("unchecked")
|
---|
688 | Collection<TaggingPreset> foundPresets = (Collection<TaggingPreset>) field.get(match);
|
---|
689 |
|
---|
690 | assertEquals(3, foundPresets.size());
|
---|
691 | assertTrue(foundPresets.contains(testPreset1));
|
---|
692 | assertTrue(foundPresets.contains(testPreset2));
|
---|
693 | assertTrue(foundPresets.contains(testPreset3));
|
---|
694 | }
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Ensures that correct primitives are matched against the specified preset.
|
---|
698 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
699 | * @since 12464
|
---|
700 | */
|
---|
701 | @Test
|
---|
702 | void testPreset() throws SearchParseError {
|
---|
703 | final String presetName = "Test Preset Name";
|
---|
704 | final String presetGroupName = "Test Preset Group";
|
---|
705 | final String key = "test_key1";
|
---|
706 | final String val = "test_val1";
|
---|
707 |
|
---|
708 | Key key1 = new Key();
|
---|
709 | key1.key = key;
|
---|
710 | key1.value = val;
|
---|
711 |
|
---|
712 | TaggingPreset testPreset = new TaggingPreset();
|
---|
713 | testPreset.name = presetName;
|
---|
714 | testPreset.types = Collections.singleton(TaggingPresetType.NODE);
|
---|
715 | testPreset.data.add(key1);
|
---|
716 | testPreset.group = new TaggingPresetMenu();
|
---|
717 | testPreset.group.name = presetGroupName;
|
---|
718 |
|
---|
719 | TaggingPresets.readFromPreferences();
|
---|
720 | TaggingPresets.addTaggingPresets(Collections.singleton(testPreset));
|
---|
721 |
|
---|
722 | String query = "preset:" + "\"" + testPreset.getRawName() + "\"";
|
---|
723 |
|
---|
724 | SearchContext ctx = new SearchContext(query);
|
---|
725 | ctx.n1.put(key, val);
|
---|
726 | ctx.n2.put(key, val);
|
---|
727 |
|
---|
728 | for (OsmPrimitive osm : new OsmPrimitive[] {ctx.n1, ctx.n2}) {
|
---|
729 | ctx.match(osm, true);
|
---|
730 | }
|
---|
731 |
|
---|
732 | for (OsmPrimitive osm : new OsmPrimitive[] {ctx.r1, ctx.r2, ctx.w1, ctx.w2}) {
|
---|
733 | ctx.match(osm, false);
|
---|
734 | }
|
---|
735 | }
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Unit test of methods {@link Match#equals} and {@link Match#hashCode}, including all subclasses.
|
---|
739 | */
|
---|
740 | @Test
|
---|
741 | void testEqualsContract() {
|
---|
742 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
743 | Set<Class<? extends Match>> matchers = TestUtils.getJosmSubtypes(Match.class);
|
---|
744 | Assert.assertTrue(matchers.size() >= 10); // if it finds less than 10 classes, something is broken
|
---|
745 | for (Class<?> c : matchers) {
|
---|
746 | Logging.debug(c.toString());
|
---|
747 | EqualsVerifier.forClass(c).usingGetClass()
|
---|
748 | .suppress(Warning.NONFINAL_FIELDS, Warning.INHERITED_DIRECTLY_FROM_OBJECT)
|
---|
749 | .withPrefabValues(TaggingPreset.class, newTaggingPreset("foo"), newTaggingPreset("bar"))
|
---|
750 | .verify();
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | private static TaggingPreset newTaggingPreset(String name) {
|
---|
755 | TaggingPreset result = new TaggingPreset();
|
---|
756 | result.name = name;
|
---|
757 | return result;
|
---|
758 | }
|
---|
759 |
|
---|
760 | /**
|
---|
761 | * Search for {@code nodes:2}.
|
---|
762 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
763 | */
|
---|
764 | @Test
|
---|
765 | void testNodeCount() throws SearchParseError {
|
---|
766 | final SearchContext sc = new SearchContext("nodes:2");
|
---|
767 | sc.match(sc.n1, false);
|
---|
768 | sc.match(sc.w1, true);
|
---|
769 | Node n3 = new Node(new LatLon(0, 5));
|
---|
770 | sc.ds.addPrimitive(n3);
|
---|
771 | sc.w1.addNode(n3);
|
---|
772 | sc.match(sc.w1, false);
|
---|
773 | sc.match(sc.r1, false);
|
---|
774 | }
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Search for {@code ways:2}.
|
---|
778 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
779 | */
|
---|
780 | @Test
|
---|
781 | void testWayCount() throws SearchParseError {
|
---|
782 | final SearchContext sc = new SearchContext("ways:2");
|
---|
783 | sc.match(sc.n1, true);
|
---|
784 | sc.ds.addPrimitive(new Way(sc.w2, true));
|
---|
785 | sc.match(sc.n1, false);
|
---|
786 | sc.match(sc.w1, false);
|
---|
787 | sc.match(sc.r1, true);
|
---|
788 | sc.r1.addMember(new RelationMember("", sc.n1));
|
---|
789 | sc.match(sc.r1, true);
|
---|
790 | }
|
---|
791 |
|
---|
792 | /**
|
---|
793 | * Search for {@code members:2}.
|
---|
794 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
795 | */
|
---|
796 | @Test
|
---|
797 | void testMemberCount() throws SearchParseError {
|
---|
798 | final SearchContext sc = new SearchContext("members:2");
|
---|
799 | sc.match(sc.n1, false);
|
---|
800 | sc.match(sc.w1, false);
|
---|
801 | sc.match(sc.r1, true);
|
---|
802 | sc.r1.addMember(new RelationMember("", sc.n1));
|
---|
803 | sc.match(sc.r1, false);
|
---|
804 | }
|
---|
805 |
|
---|
806 | /**
|
---|
807 | * Search for {@code role:foo}.
|
---|
808 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
809 | */
|
---|
810 | @Test
|
---|
811 | void testRole() throws SearchParseError {
|
---|
812 | final SearchContext sc = new SearchContext("role:foo");
|
---|
813 | sc.match(sc.r1, false);
|
---|
814 | sc.match(sc.w1, false);
|
---|
815 | sc.match(sc.n1, false);
|
---|
816 | sc.match(sc.n2, false);
|
---|
817 | sc.r1.addMember(new RelationMember("foo", sc.n1));
|
---|
818 | sc.match(sc.n1, true);
|
---|
819 | sc.match(sc.n2, false);
|
---|
820 | }
|
---|
821 | }
|
---|