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