source: josm/trunk/test/unit/org/openstreetmap/josm/tools/SearchCompilerQueryWizardTest.java@ 17360

Last change on this file since 17360 was 17336, checked in by GerdP, 3 years ago

fix #20037: Overpass query wizard generates wrong query with "*=value"

  • remove obsolete getInstance()
  • fix bug and add unit test
  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertThrows;
6
7import org.junit.jupiter.api.Disabled;
8import org.junit.jupiter.api.Test;
9import org.junit.jupiter.api.extension.RegisterExtension;
10import org.openstreetmap.josm.testutils.JOSMTestRules;
11
12import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13
14/**
15 * Unit tests of {@link SearchCompilerQueryWizard} class.
16 */
17class SearchCompilerQueryWizardTest {
18 /**
19 * Base test environment is enough
20 */
21 @RegisterExtension
22 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
23 public JOSMTestRules test = new JOSMTestRules().i18n("de");
24
25 private static String constructQuery(String s) {
26 return SearchCompilerQueryWizard.constructQuery(s);
27 }
28
29 private void assertQueryEquals(String expectedQueryPart, String input) {
30 final String query = constructQuery(input);
31 assertEquals("" +
32 "[out:xml][timeout:90][bbox:{{bbox}}];\n" +
33 "(\n" +
34 expectedQueryPart +
35 ");\n" +
36 "(._;>;);\n" +
37 "out meta;", query);
38 }
39
40 /**
41 * Test {@code key=value}.
42 */
43 @Test
44 void testKeyValue() {
45 assertQueryEquals(" nwr[\"amenity\"=\"drinking_water\"];\n", "amenity=drinking_water");
46 assertQueryEquals(" nwr[\"amenity\"];\n", "amenity=*");
47 }
48
49 /**
50 * Test {@code key!=value} and {@code key<>value}.
51 */
52 @Test
53 void testKeyNotValue() {
54 assertQueryEquals(" nwr[\"amenity\"!=\"drinking_water\"];\n", "-amenity=drinking_water");
55 assertQueryEquals(" nwr[!\"amenity\"];\n", "-amenity=*");
56 }
57
58 /**
59 * Test {@code key~value} and similar.
60 */
61 @Test
62 void testKeyLikeValue() {
63 assertQueryEquals(" nwr[\"foo\"~\"bar\"];\n", "foo~bar");
64 assertQueryEquals(" nwr[\"foo\"~\"bar\"];\n", "foo~/bar/");
65 // case insensitive
66 assertQueryEquals(" nwr[\"foo\"~\"bar\",i];\n", "foo~/bar/i");
67 // negated
68 assertQueryEquals(" nwr[\"foo\"!~\"bar\"];\n", "-foo~bar");
69 assertQueryEquals(" nwr[\"foo\"!~\"bar\",i];\n", "-foo~/bar/i");
70 }
71
72 /**
73 * Test OSM boolean true/false.
74 */
75 @Test
76 void testOsmBoolean() {
77 assertQueryEquals(" nwr[\"highway\"][\"oneway\"~\"true|yes|1|on\"];\n", "highway=* AND oneway?");
78 assertQueryEquals(" nwr[\"highway\"][\"oneway\"~\"false|no|0|off\"];\n", "highway=* AND -oneway?");
79 }
80
81 /**
82 * Test {@code foo=bar and baz=42}.
83 */
84 @Test
85 void testBooleanAnd() {
86 assertQueryEquals(" nwr[\"foo\"=\"bar\"][\"baz\"=\"42\"];\n", "foo=bar and baz=42");
87 assertQueryEquals(" nwr[\"foo\"=\"bar\"][\"baz\"=\"42\"];\n", "foo=bar && baz=42");
88 assertQueryEquals(" nwr[\"foo\"=\"bar\"][\"baz\"=\"42\"];\n", "foo=bar & baz=42");
89 }
90
91 /**
92 * Test {@code foo=bar or baz=42}.
93 */
94 @Test
95 void testBooleanOr() {
96 assertQueryEquals(" nwr[\"foo\"=\"bar\"];\n nwr[\"baz\"=\"42\"];\n", "foo=bar or baz=42");
97 assertQueryEquals(" nwr[\"foo\"=\"bar\"];\n nwr[\"baz\"=\"42\"];\n", "foo=bar | baz=42");
98 }
99
100 /**
101 * Test {@code (foo=* or bar=*) and (asd=* or fasd=*)}.
102 */
103 @Test
104 void testBoolean() {
105 assertQueryEquals("" +
106 " nwr[\"foo\"][\"baz1\"];\n" +
107 " nwr[\"foo\"][\"baz2\"];\n" +
108 " nwr[\"foo\"][\"baz3\"][\"baz4\"];\n" +
109 " nwr[\"foo\"][\"baz3\"][\"baz5\"];\n" +
110 " nwr[\"bar\"][\"baz1\"];\n" +
111 " nwr[\"bar\"][\"baz2\"];\n" +
112 " nwr[\"bar\"][\"baz3\"][\"baz4\"];\n" +
113 " nwr[\"bar\"][\"baz3\"][\"baz5\"];\n",
114 "(foo=* or bar=*) and (baz1=* or baz2=* or (baz3=* and (baz4=* or baz5=*)))");
115 }
116
117 /**
118 * Test {@code foo=bar and (type:node or type:way)}.
119 */
120 @Test
121 void testType() {
122 assertQueryEquals(" node[\"foo\"=\"bar\"];\n way[\"foo\"=\"bar\"];\n", "foo=bar and (type:node or type:way)");
123 }
124
125 /**
126 * Test {@code user:foo or uid:42}.
127 */
128 @Test
129 void testUser() {
130 assertQueryEquals(" nwr(user:\"foo\");\n nwr(uid:42);\n", "user:foo or user:42");
131 }
132
133 /**
134 * Test {@code foo=bar and (type:node or type:way)}.
135 */
136 @Test
137 void testEmpty() {
138 assertQueryEquals(" way[\"foo\"~\"^$\"];\n", "foo=\"\" and type:way");
139 }
140
141 /**
142 * Test geocodeArea.
143 */
144 @Test
145 void testInArea() {
146 String query = constructQuery("foo=bar | foo=baz in Innsbruck");
147 assertEquals("" +
148 "[out:xml][timeout:90];\n" +
149 "{{geocodeArea:Innsbruck}}->.searchArea;\n" +
150 "(\n" +
151 " nwr[\"foo\"=\"bar\"](area.searchArea);\n" +
152 " nwr[\"foo\"=\"baz\"](area.searchArea);\n" +
153 ");\n" +
154 "(._;>;);\n" +
155 "out meta;", query);
156 query = constructQuery("foo=bar | foo=baz in \"Sankt Sigmund im Sellrain\"");
157 assertEquals("" +
158 "[out:xml][timeout:90];\n" +
159 "{{geocodeArea:Sankt Sigmund im Sellrain}}->.searchArea;\n" +
160 "(\n" +
161 " nwr[\"foo\"=\"bar\"](area.searchArea);\n" +
162 " nwr[\"foo\"=\"baz\"](area.searchArea);\n" +
163 ");\n" +
164 "(._;>;);\n" +
165 "out meta;", query);
166 query = constructQuery("foo=bar | foo=baz in \"Новосибирск\"");
167 assertEquals("" +
168 "[out:xml][timeout:90];\n" +
169 "{{geocodeArea:Новосибирск}}->.searchArea;\n" +
170 "(\n" +
171 " nwr[\"foo\"=\"bar\"](area.searchArea);\n" +
172 " nwr[\"foo\"=\"baz\"](area.searchArea);\n" +
173 ");\n" +
174 "(._;>;);\n" +
175 "out meta;", query);
176 }
177
178 /**
179 * Test geocodeArea.
180 */
181 @Test
182 void testAroundArea() {
183 final String query = constructQuery("foo=bar | foo=baz around \"Sankt Sigmund im Sellrain\"");
184 assertEquals("" +
185 "[out:xml][timeout:90];\n" +
186 "{{radius=1000}}\n" +
187 "(\n" +
188 " nwr[\"foo\"=\"bar\"](around:{{radius}},{{geocodeCoords:Sankt Sigmund im Sellrain}});\n" +
189 " nwr[\"foo\"=\"baz\"](around:{{radius}},{{geocodeCoords:Sankt Sigmund im Sellrain}});\n" +
190 ");\n" +
191 "(._;>;);\n" +
192 "out meta;", query);
193 }
194
195 /**
196 * Test global query.
197 */
198 @Test
199 void testGlobal() {
200 final String query = constructQuery("foo=bar global");
201 assertEquals("" +
202 "[out:xml][timeout:90];\n" +
203 "(\n" +
204 " nwr[\"foo\"=\"bar\"];\n" +
205 ");\n" +
206 "(._;>;);\n" +
207 "out meta;", query);
208 }
209
210 /**
211 * Test "in bbox" query.
212 */
213 @Test
214 void testInBbox() {
215 assertQueryEquals(" nwr[\"foo\"=\"bar\"];\n", "foo=bar IN BBOX");
216 }
217
218 /**
219 * Test building an Overpass query based on a preset name.
220 */
221 @Test
222 @Disabled("preset handling not implemented")
223 void testPreset() {
224 assertQueryEquals(" nwr[\"amenity\"=\"hospital\"];\n", "Hospital");
225 }
226
227 /**
228 * Test erroneous value.
229 */
230 @Test
231 void testErroneous() {
232 assertThrows(UncheckedParseException.class, () -> constructQuery("-(foo or bar)"));
233 }
234
235 /**
236 * Test for ticket <a href="https://josm.openstreetmap.de/ticket/19151>#19151</a>
237 */
238 @Test
239 void testTicket19151() {
240 assertQueryEquals(" relation[\"type\"=\"multipolygon\"][!\"landuse\"][!\"area:highway\"];\n",
241 "type:relation and type=multipolygon and -landuse=* and -\"area:highway\"=*");
242 }
243
244 /**
245 * Test for ticket <a href="https://josm.openstreetmap.de/ticket/20037>#20037</a>
246 */
247 @Test
248 void testTicket20037() {
249 assertQueryEquals(
250 " node[~\"^.*$\"~\"forward\"];\n" +
251 " node[~\"^.*$\"~\"backward\"];\n",
252 "type:node AND (*=forward OR *=backward)");
253 }
254}
Note: See TracBrowser for help on using the repository browser.