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