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