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