svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / test / java / org / gvsig / expresionevaluator / impl / TestExpressionBuilder.java @ 44364
History | View | Annotate | Download (18.4 KB)
1 |
package org.gvsig.expresionevaluator.impl; |
---|---|
2 |
|
3 |
import junit.framework.TestCase; |
4 |
import org.apache.commons.lang3.ArrayUtils; |
5 |
import org.cresques.cts.IProjection; |
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType; |
8 |
import org.gvsig.expressionevaluator.impl.DefaultExpressionBuilder; |
9 |
import org.gvsig.fmap.crs.CRSFactory; |
10 |
import org.gvsig.fmap.geom.Geometry; |
11 |
import org.gvsig.fmap.geom.GeometryLocator; |
12 |
import org.gvsig.fmap.geom.GeometryManager; |
13 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
14 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
15 |
import org.gvsig.fmap.geom.primitive.Point; |
16 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
17 |
|
18 |
public class TestExpressionBuilder extends TestCase { |
19 |
|
20 |
public TestExpressionBuilder(String testName) { |
21 |
super(testName);
|
22 |
} |
23 |
|
24 |
@Override
|
25 |
protected void setUp() throws Exception { |
26 |
super.setUp();
|
27 |
new DefaultLibrariesInitializer().fullInitialize();
|
28 |
} |
29 |
|
30 |
@Override
|
31 |
protected void tearDown() throws Exception { |
32 |
super.tearDown();
|
33 |
} |
34 |
|
35 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
36 |
// public void testHello() {}
|
37 |
|
38 |
public void test2() { |
39 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
40 |
|
41 |
builder.and( |
42 |
builder.eq( |
43 |
builder.lower(builder.variable("colum_name_c")),
|
44 |
builder.parameter("colum_name_p")
|
45 |
) |
46 |
); |
47 |
builder.and( |
48 |
builder.group( |
49 |
builder.or( |
50 |
builder.like( |
51 |
builder.lower( builder.variable("uno")),
|
52 |
builder.constant("%10")
|
53 |
), |
54 |
builder.lt( |
55 |
builder.variable("dos"),
|
56 |
builder.constant(-3.5)
|
57 |
) |
58 |
) |
59 |
) |
60 |
); |
61 |
builder.and( |
62 |
builder.ST_Intersects( |
63 |
builder.variable("geom1"),
|
64 |
builder.ST_Envelope( |
65 |
builder.ST_GeomFromWKB( |
66 |
builder.parameter("geom2"),
|
67 |
builder.parameter().value(4326).as_constant()
|
68 |
) |
69 |
) |
70 |
) |
71 |
); |
72 |
builder.and( |
73 |
builder.gt( |
74 |
builder.variable("tres"),
|
75 |
builder.constant(123456789)
|
76 |
) |
77 |
); |
78 |
assertEquals( |
79 |
"(((( (LOWER(\"colum_name_c\")) = (?) ) AND ( ( (LOWER(\"uno\")) LIKE ('%10') ) OR ( (\"dos\") < (-3.5) ) )) AND ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?)))))) AND ( (\"tres\") > (123456789) ))",
|
80 |
builder.toString() |
81 |
); |
82 |
assertEquals( |
83 |
"[colum_name_c, dos, geom1, tres, uno]",
|
84 |
ArrayUtils.toString(builder.variables_names()) |
85 |
); |
86 |
assertEquals( |
87 |
"[\"colum_name_p\", \"geom2\", 4326]",
|
88 |
ArrayUtils.toString(builder.parameters_names()) |
89 |
); |
90 |
} |
91 |
|
92 |
public void test3() throws CreateGeometryException { |
93 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
94 |
|
95 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
96 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
97 |
|
98 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
99 |
builder.set( |
100 |
builder.ST_Intersects( |
101 |
builder.geometry(point,proj), |
102 |
builder.variable("the_geom")
|
103 |
) |
104 |
); |
105 |
builder.geometry_support_type(GeometrySupportType.WKT); |
106 |
System.out.println(builder.toString());
|
107 |
assertEquals( |
108 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (\"the_geom\"))",
|
109 |
builder.toString() |
110 |
); |
111 |
builder.geometry_support_type(GeometrySupportType.WKB); |
112 |
System.out.println(builder.toString());
|
113 |
assertEquals( |
114 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (\"the_geom\"))",
|
115 |
builder.toString() |
116 |
); |
117 |
assertEquals( |
118 |
"[the_geom]",
|
119 |
ArrayUtils.toString(builder.variables_names()) |
120 |
); |
121 |
assertEquals( |
122 |
"[]",
|
123 |
ArrayUtils.toString(builder.parameters_names()) |
124 |
); |
125 |
} |
126 |
|
127 |
public void testParameter() throws CreateGeometryException { |
128 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
129 |
|
130 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
131 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
132 |
|
133 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
134 |
builder.set( |
135 |
builder.ST_Intersects( |
136 |
builder.geometry(point,proj), |
137 |
builder.parameter("the_geom")
|
138 |
) |
139 |
); |
140 |
builder.geometry_support_type(GeometrySupportType.WKT); |
141 |
System.out.println(builder.toString());
|
142 |
assertEquals( |
143 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (?))",
|
144 |
builder.toString() |
145 |
); |
146 |
builder.geometry_support_type(GeometrySupportType.WKB); |
147 |
System.out.println(builder.toString());
|
148 |
assertEquals( |
149 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (?))",
|
150 |
builder.toString() |
151 |
); |
152 |
assertEquals( |
153 |
"[]",
|
154 |
ArrayUtils.toString(builder.variables_names()) |
155 |
); |
156 |
assertEquals( |
157 |
"[\"the_geom\"]",
|
158 |
ArrayUtils.toString(builder.parameters_names()) |
159 |
); |
160 |
} |
161 |
|
162 |
public void testParameterGeomWhitoutSRS() throws CreateGeometryException { |
163 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
164 |
|
165 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
166 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
167 |
|
168 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
169 |
builder.set( |
170 |
builder.ST_Intersects( |
171 |
builder.geometry(point,proj), |
172 |
builder.parameter("the_geom").as_geometry_variable()
|
173 |
) |
174 |
); |
175 |
try {
|
176 |
builder.toString(); |
177 |
fail("Geometry parameter without SRS not detected.");
|
178 |
} catch(IllegalArgumentException ex) { |
179 |
// Do nothing, it's ok that fail if not SRS is specified.
|
180 |
} |
181 |
} |
182 |
|
183 |
public void testPatameterGeomWithSRS1() throws CreateGeometryException { |
184 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
185 |
|
186 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
187 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
188 |
|
189 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
190 |
builder.set( |
191 |
builder.ST_Intersects( |
192 |
builder.geometry(point,proj), |
193 |
builder.parameter("the_geom").srs(proj)
|
194 |
) |
195 |
); |
196 |
builder.geometry_support_type(GeometrySupportType.WKT); |
197 |
System.out.println(builder.toString());
|
198 |
assertEquals( |
199 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (4326))))",
|
200 |
builder.toString() |
201 |
); |
202 |
builder.geometry_support_type(GeometrySupportType.WKB); |
203 |
System.out.println(builder.toString());
|
204 |
assertEquals( |
205 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (4326))))",
|
206 |
builder.toString() |
207 |
); |
208 |
assertEquals( |
209 |
"[]",
|
210 |
ArrayUtils.toString(builder.variables_names()) |
211 |
); |
212 |
assertEquals( |
213 |
"[\"the_geom\"]",
|
214 |
ArrayUtils.toString(builder.parameters_names()) |
215 |
); |
216 |
} |
217 |
|
218 |
public void testParameterGeomWithSRS2() throws CreateGeometryException { |
219 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
220 |
|
221 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
222 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
223 |
|
224 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
225 |
builder.set( |
226 |
builder.ST_Intersects( |
227 |
builder.geometry(point,proj), |
228 |
builder.parameter("the_geom").srs(builder.constant(4326)) |
229 |
) |
230 |
); |
231 |
builder.geometry_support_type(GeometrySupportType.WKT); |
232 |
System.out.println(builder.toString());
|
233 |
assertEquals( |
234 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (4326))))",
|
235 |
builder.toString() |
236 |
); |
237 |
builder.geometry_support_type(GeometrySupportType.WKB); |
238 |
System.out.println(builder.toString());
|
239 |
assertEquals( |
240 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (4326))))",
|
241 |
builder.toString() |
242 |
); |
243 |
assertEquals( |
244 |
"[]",
|
245 |
ArrayUtils.toString(builder.variables_names()) |
246 |
); |
247 |
assertEquals( |
248 |
"[\"the_geom\"]",
|
249 |
ArrayUtils.toString(builder.parameters_names()) |
250 |
); |
251 |
} |
252 |
|
253 |
public void testParameterGeomWithParameterSRS() throws CreateGeometryException { |
254 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
255 |
|
256 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
257 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
258 |
|
259 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
260 |
builder.set( |
261 |
builder.ST_Intersects( |
262 |
builder.geometry(point,proj), |
263 |
builder.parameter("the_geom").srs(
|
264 |
builder.parameter().value(proj) |
265 |
) |
266 |
) |
267 |
); |
268 |
builder.geometry_support_type(GeometrySupportType.WKT); |
269 |
System.out.println(builder.toString());
|
270 |
assertEquals( |
271 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (?))))",
|
272 |
builder.toString() |
273 |
); |
274 |
builder.geometry_support_type(GeometrySupportType.WKB); |
275 |
System.out.println(builder.toString());
|
276 |
assertEquals( |
277 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (?))))",
|
278 |
builder.toString() |
279 |
); |
280 |
assertEquals( |
281 |
"[]",
|
282 |
ArrayUtils.toString(builder.variables_names()) |
283 |
); |
284 |
assertEquals( |
285 |
"[\"the_geom\", 4326]",
|
286 |
ArrayUtils.toString(builder.parameters_names()) |
287 |
); |
288 |
} |
289 |
|
290 |
public void testParameterGeomConstant() throws CreateGeometryException { |
291 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
292 |
|
293 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
294 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
295 |
|
296 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
297 |
Point point2 = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
298 |
point2.setProjection(proj); |
299 |
builder.set( |
300 |
builder.ST_Intersects( |
301 |
builder.geometry(point,proj), |
302 |
builder.parameter().value(point2) |
303 |
) |
304 |
); |
305 |
builder.geometry_support_type(GeometrySupportType.WKT); |
306 |
System.out.println(builder.toString());
|
307 |
assertEquals( |
308 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (?))))",
|
309 |
builder.toString() |
310 |
); |
311 |
builder.geometry_support_type(GeometrySupportType.WKB); |
312 |
System.out.println(builder.toString());
|
313 |
assertEquals( |
314 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (?))))",
|
315 |
builder.toString() |
316 |
); |
317 |
assertEquals( |
318 |
"[]",
|
319 |
ArrayUtils.toString(builder.variables_names()) |
320 |
); |
321 |
assertEquals( |
322 |
"[0x000000000140240000000000004034000000000000, 4326]",
|
323 |
ArrayUtils.toString(builder.parameters_names()) |
324 |
); |
325 |
} |
326 |
|
327 |
public void test4() throws CreateGeometryException { |
328 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
329 |
|
330 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
331 |
|
332 |
builder.and( |
333 |
builder.ST_Intersects( |
334 |
builder.variable("geom1"),
|
335 |
builder.ST_Envelope( |
336 |
builder.ST_GeomFromWKB( |
337 |
builder.parameter("geom2"),
|
338 |
builder.parameter().value(proj).as_constant() |
339 |
) |
340 |
) |
341 |
) |
342 |
); |
343 |
|
344 |
System.out.println(builder.toString());
|
345 |
assertEquals( |
346 |
"ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?)))))",
|
347 |
builder.toString() |
348 |
); |
349 |
assertEquals( |
350 |
"[geom1]",
|
351 |
ArrayUtils.toString(builder.variables_names()) |
352 |
); |
353 |
assertEquals( |
354 |
"[\"geom2\", 4326]",
|
355 |
ArrayUtils.toString(builder.parameters_names()) |
356 |
); |
357 |
} |
358 |
|
359 |
public void test5() throws CreateGeometryException { |
360 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
361 |
|
362 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
363 |
|
364 |
builder.and( |
365 |
builder.eq( |
366 |
builder.ST_SRID( builder.variable("geom") ),
|
367 |
builder.srs(proj) |
368 |
) |
369 |
); |
370 |
|
371 |
System.out.println(builder.toString());
|
372 |
assertEquals( |
373 |
"( (ST_SRID(\"geom\")) = (4326) )",
|
374 |
builder.toString() |
375 |
); |
376 |
assertEquals( |
377 |
"[geom]",
|
378 |
ArrayUtils.toString(builder.variables_names()) |
379 |
); |
380 |
assertEquals( |
381 |
"[]",
|
382 |
ArrayUtils.toString(builder.parameters_names()) |
383 |
); |
384 |
} |
385 |
|
386 |
public void test6() throws Exception { |
387 |
ExpressionBuilder builder = new DefaultExpressionBuilder();
|
388 |
|
389 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
390 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
391 |
|
392 |
Point point = geometryManager.createPoint(-0.1779813, 51.6122315, Geometry.SUBTYPES.GEOM2D); |
393 |
Geometry point_buffer = point.buffer(0.001);
|
394 |
|
395 |
builder.or( |
396 |
builder.ST_Intersects( |
397 |
builder.function("ST_StartPoint", builder.column("geometry")), |
398 |
builder.geometry(point_buffer, proj) |
399 |
), |
400 |
builder.ST_Intersects( |
401 |
builder.function("ST_EndPoint", builder.column("geometry")), |
402 |
builder.geometry(point_buffer, proj) |
403 |
) |
404 |
); |
405 |
|
406 |
System.out.println(builder.toString());
|
407 |
assertEquals( |
408 |
"ST_Intersects((ST_StartPoint(\"geometry\")), (ST_GeomFromWKB((DECODE('00000000030000000100000021bfc6a752bfc074d04049ce5d9a0f0a5fbfc6a7f3ef0c44504049ce573585c39cbfc6a9d14b36039c4049ce510fe0a08dbfc6acd87c00e8e74049ce4b65990cbfbfc6b0ebb72658ed4049ce466e6aca60bfc6b5e2e568b7fa4049ce425b2fa4f0bfc6bb8d2cfc85e24049ce3f53feda0abfc6c1b2d21f95294049ce3d76a2b04bbfc6c8175b6658244049ce3cd573647cbfc6ce7be4ad1b1f4049ce3d76a2b04bbfc6d4a189d02a664049ce3f53feda0abfc6da4bd163f84e4049ce425b2fa4f0bfc6df42ffa6575b4049ce466e6aca60bfc6e3563acbc7614049ce4b65990cbfbfc6e65d6b96acac4049ce510fe0a08dbfc6e83ac7c06bf84049ce573585c39cbfc6e8dbf70c3b784049ce5d9a0f0a5fbfc6e83ac7c06bf84049ce63fe985122bfc6e65d6b96acac4049ce6a243d7431bfc6e3563acbc7614049ce6fce8507ffbfc6df42ffa6575b4049ce74c5b34a5ebfc6da4bd163f84e4049ce78d8ee6fcebfc6d4a189d02a664049ce7be01f3ab4bfc6ce7be4ad1b1f4049ce7dbd7b6473bfc6c8175b6658244049ce7e5eaab042bfc6c1b2d21f95294049ce7dbd7b6473bfc6bb8d2cfc85e24049ce7be01f3ab4bfc6b5e2e568b7fa4049ce78d8ee6fcebfc6b0ebb72658ed4049ce74c5b34a5ebfc6acd87c00e8e74049ce6fce8507ffbfc6a9d14b36039b4049ce6a243d7431bfc6a7f3ef0c44504049ce63fe985122bfc6a752bfc074d04049ce5d9a0f0a5f','hex')), (4326)))) OR ST_Intersects((ST_EndPoint(\"geometry\")), (ST_GeomFromWKB((DECODE('00000000030000000100000021bfc6a752bfc074d04049ce5d9a0f0a5fbfc6a7f3ef0c44504049ce573585c39cbfc6a9d14b36039c4049ce510fe0a08dbfc6acd87c00e8e74049ce4b65990cbfbfc6b0ebb72658ed4049ce466e6aca60bfc6b5e2e568b7fa4049ce425b2fa4f0bfc6bb8d2cfc85e24049ce3f53feda0abfc6c1b2d21f95294049ce3d76a2b04bbfc6c8175b6658244049ce3cd573647cbfc6ce7be4ad1b1f4049ce3d76a2b04bbfc6d4a189d02a664049ce3f53feda0abfc6da4bd163f84e4049ce425b2fa4f0bfc6df42ffa6575b4049ce466e6aca60bfc6e3563acbc7614049ce4b65990cbfbfc6e65d6b96acac4049ce510fe0a08dbfc6e83ac7c06bf84049ce573585c39cbfc6e8dbf70c3b784049ce5d9a0f0a5fbfc6e83ac7c06bf84049ce63fe985122bfc6e65d6b96acac4049ce6a243d7431bfc6e3563acbc7614049ce6fce8507ffbfc6df42ffa6575b4049ce74c5b34a5ebfc6da4bd163f84e4049ce78d8ee6fcebfc6d4a189d02a664049ce7be01f3ab4bfc6ce7be4ad1b1f4049ce7dbd7b6473bfc6c8175b6658244049ce7e5eaab042bfc6c1b2d21f95294049ce7dbd7b6473bfc6bb8d2cfc85e24049ce7be01f3ab4bfc6b5e2e568b7fa4049ce78d8ee6fcebfc6b0ebb72658ed4049ce74c5b34a5ebfc6acd87c00e8e74049ce6fce8507ffbfc6a9d14b36039b4049ce6a243d7431bfc6a7f3ef0c44504049ce63fe985122bfc6a752bfc074d04049ce5d9a0f0a5f','hex')), (4326))))",
|
409 |
builder.toString() |
410 |
); |
411 |
assertEquals( |
412 |
"[geometry]",
|
413 |
ArrayUtils.toString(builder.variables_names()) |
414 |
); |
415 |
assertEquals( |
416 |
"[]",
|
417 |
ArrayUtils.toString(builder.parameters_names()) |
418 |
); |
419 |
} |
420 |
} |