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 / TestCodeFormatter.java @ 44644
History | View | Annotate | Download (15.9 KB)
1 |
package org.gvsig.expresionevaluator.impl; |
---|---|
2 |
|
3 |
import java.text.MessageFormat; |
4 |
import junit.framework.TestCase; |
5 |
import org.apache.commons.lang3.StringUtils; |
6 |
import org.gvsig.expressionevaluator.Code; |
7 |
import org.gvsig.expressionevaluator.Code.Caller; |
8 |
import org.gvsig.expressionevaluator.Code.Constant; |
9 |
import org.gvsig.expressionevaluator.Codes; |
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
11 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
12 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
13 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
14 |
import org.gvsig.expressionevaluator.Formatter; |
15 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
16 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
17 |
|
18 |
public class TestCodeFormatter extends TestCase { |
19 |
|
20 |
private static class MyFormatter implements Formatter<Code> { |
21 |
|
22 |
private class Formatter_constant_bytearray implements Formatter<Code> { |
23 |
|
24 |
@Override
|
25 |
public boolean canApply(Code code) { |
26 |
if( code instanceof Constant ) { |
27 |
return ((Constant)code).value() instanceof byte[]; |
28 |
} |
29 |
return false; |
30 |
} |
31 |
|
32 |
@Override
|
33 |
public String format(Code code) { |
34 |
return builder.bytearray_x((byte[]) ((Constant)code).value()); |
35 |
} |
36 |
} |
37 |
|
38 |
private class Formatter_ST_intersects_H2Spatial implements Formatter<Code> { |
39 |
// Need for H2Spatial
|
40 |
@Override
|
41 |
public boolean canApply(Code code) { |
42 |
if( code instanceof Caller ) { |
43 |
return StringUtils.equalsIgnoreCase("ST_intersects",((Caller)code).name()); |
44 |
} |
45 |
return false; |
46 |
} |
47 |
|
48 |
@Override
|
49 |
public String format(Code code) { |
50 |
Codes parameters = ((Caller)code).parameters(); |
51 |
String p1 = parameters.get(0).toString(MyFormatter.this); |
52 |
String p2 = parameters.get(1).toString(MyFormatter.this); |
53 |
String r = MessageFormat.format( |
54 |
"( (({0}) && ({1})) AND ST_Intersects(({0}),({1}) ))",
|
55 |
p1, |
56 |
p2 |
57 |
); |
58 |
return r;
|
59 |
} |
60 |
} |
61 |
|
62 |
private class Formatter_ST_intersects_SpatiaLite implements Formatter<Code> { |
63 |
// Need for SpatiaLite
|
64 |
|
65 |
private final String table; |
66 |
private final String geomcolumn; |
67 |
|
68 |
public Formatter_ST_intersects_SpatiaLite(String table, String geomcolumn) { |
69 |
this.table = table;
|
70 |
this.geomcolumn = geomcolumn;
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public boolean canApply(Code code) { |
75 |
if( code instanceof Caller ) { |
76 |
return StringUtils.equalsIgnoreCase("ST_intersects2",((Caller)code).name()); |
77 |
} |
78 |
return false; |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public String format(Code code) { |
83 |
Codes parameters = ((Caller)code).parameters(); |
84 |
String p1 = parameters.get(0).toString(MyFormatter.this); |
85 |
String p2 = parameters.get(1).toString(MyFormatter.this); |
86 |
String r = MessageFormat.format( |
87 |
"(ST_Intersects({0},{1}) AND ROWID IN ( SELECT ROWID FROM SpatialIndex WHERE f_table_name = ''{2}'' AND f_geometry_column = ''{3}'' AND search_frame = \"{2}\".\"{3}\"))",
|
88 |
p1, |
89 |
p2, |
90 |
table, |
91 |
geomcolumn |
92 |
); |
93 |
return r;
|
94 |
} |
95 |
} |
96 |
|
97 |
private class Formatter_DECODE implements Formatter<Code> { |
98 |
// Need for SpatiaLite
|
99 |
@Override
|
100 |
public boolean canApply(Code code) { |
101 |
if( code instanceof Caller ) { |
102 |
if( StringUtils.equalsIgnoreCase("DECODE",((Caller)code).name()) ) { |
103 |
Codes parameters = ((Caller)code).parameters(); |
104 |
Code code_p1 = parameters.get(0);
|
105 |
Code code_p2 = parameters.get(1);
|
106 |
if( code_p1 instanceof Constant && code_p2 instanceof Constant ) { |
107 |
Object p1 = ((Constant)code_p1).value();
|
108 |
Object p2 = ((Constant)code_p2).value();
|
109 |
if( p1 instanceof String && p1 instanceof String && |
110 |
StringUtils.equalsIgnoreCase((CharSequence) p2,"hex") ) { |
111 |
return true; |
112 |
} |
113 |
} |
114 |
} |
115 |
} |
116 |
return false; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public String format(Code code) { |
121 |
Codes parameters = ((Caller)code).parameters(); |
122 |
Object p1 = ((Constant)parameters.get(0)).value(); |
123 |
return "x'"+ (String)p1 + "'"; |
124 |
} |
125 |
} |
126 |
|
127 |
private class Formatter_IFNULL implements Formatter<Code> { |
128 |
// Need for H2Spatial
|
129 |
@Override
|
130 |
public boolean canApply(Code code) { |
131 |
if( code instanceof Caller ) { |
132 |
return StringUtils.equalsIgnoreCase("IFNULL",((Caller)code).name()); |
133 |
} |
134 |
return false; |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public String format(Code code) { |
139 |
Codes parameters = ((Caller)code).parameters(); |
140 |
String p1 = parameters.get(0).toString(MyFormatter.this); |
141 |
String p2 = parameters.get(1).toString(MyFormatter.this); |
142 |
String p3 = parameters.get(2).toString(MyFormatter.this); |
143 |
String r = MessageFormat.format( |
144 |
"NVL2({0}, {1}, {2})",
|
145 |
p1, |
146 |
p3, |
147 |
p2 |
148 |
); |
149 |
return r;
|
150 |
} |
151 |
} |
152 |
|
153 |
private class Formatter_ILIKE implements Formatter<Code> { |
154 |
// Need for SpatiaLite
|
155 |
@Override
|
156 |
public boolean canApply(Code code) { |
157 |
if( code instanceof Caller ) { |
158 |
return StringUtils.equalsIgnoreCase("ILIKE",((Caller)code).name()); |
159 |
} |
160 |
return false; |
161 |
} |
162 |
|
163 |
@Override
|
164 |
public String format(Code code) { |
165 |
Codes parameters = ((Caller)code).parameters(); |
166 |
String p1 = parameters.get(0).toString(MyFormatter.this); |
167 |
String p2 = parameters.get(1).toString(MyFormatter.this); |
168 |
String r = MessageFormat.format( |
169 |
"LOWER({0}) LIKE LOWER({1})",
|
170 |
p1, |
171 |
p2 |
172 |
); |
173 |
return r;
|
174 |
} |
175 |
} |
176 |
|
177 |
private class Formatter_NOT_IS_NULL implements Formatter<Code> { |
178 |
// Need for H2Spatial
|
179 |
@Override
|
180 |
public boolean canApply(Code code) { |
181 |
if( code instanceof Caller ) { |
182 |
return StringUtils.equalsIgnoreCase("NOT_IS_NULL",((Caller)code).name()); |
183 |
} |
184 |
return false; |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public String format(Code code) { |
189 |
Codes parameters = ((Caller)code).parameters(); |
190 |
String p1 = parameters.get(0).toString(MyFormatter.this); |
191 |
String r = MessageFormat.format( |
192 |
"( ({0}) IS NOT NULL )",
|
193 |
p1 |
194 |
); |
195 |
return r;
|
196 |
} |
197 |
} |
198 |
|
199 |
private final Formatter<Code>[] formatters; |
200 |
private final ExpressionBuilder builder; |
201 |
|
202 |
public MyFormatter() {
|
203 |
this.builder = ExpressionUtils.createExpressionBuilder();
|
204 |
this.formatters = new Formatter[] { |
205 |
// new Formatter_constant_bytearray(),
|
206 |
// new Formatter_constant_geometry(),
|
207 |
new Formatter_IFNULL(),
|
208 |
new Formatter_NOT_IS_NULL(),
|
209 |
new Formatter_ST_intersects_H2Spatial(),
|
210 |
new Formatter_ST_intersects_SpatiaLite("mytable", "the_geom"), |
211 |
new Formatter_ILIKE(),
|
212 |
new Formatter_DECODE(),
|
213 |
}; |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public boolean canApply(Code code) { |
218 |
for (Formatter<Code> formatter : formatters) { |
219 |
if( formatter.canApply(code) ) {
|
220 |
return true; |
221 |
} |
222 |
} |
223 |
return false; |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public String format(Code code) { |
228 |
for (Formatter<Code> formatter : formatters) { |
229 |
if( formatter.canApply(code) ) {
|
230 |
return formatter.format(code);
|
231 |
} |
232 |
} |
233 |
return code.toString(this); |
234 |
} |
235 |
|
236 |
} |
237 |
|
238 |
public TestCodeFormatter(String testName) { |
239 |
super(testName);
|
240 |
} |
241 |
|
242 |
@Override
|
243 |
protected void setUp() throws Exception { |
244 |
super.setUp();
|
245 |
new DefaultLibrariesInitializer().fullInitialize();
|
246 |
} |
247 |
|
248 |
@Override
|
249 |
protected void tearDown() throws Exception { |
250 |
super.tearDown();
|
251 |
} |
252 |
|
253 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
254 |
// public void testHello() {}
|
255 |
|
256 |
protected LexicalAnalyzer createLexicalAnalyzer() {
|
257 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
258 |
LexicalAnalyzer lexer = manager.createLexicalAnalyzer(); |
259 |
return lexer;
|
260 |
} |
261 |
|
262 |
protected org.gvsig.expressionevaluator.Compiler createCompiler() {
|
263 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
264 |
org.gvsig.expressionevaluator.Compiler compiler = manager.createCompiler(); |
265 |
compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
266 |
return compiler;
|
267 |
} |
268 |
|
269 |
private Formatter<Code> formatter() { |
270 |
return new MyFormatter(); |
271 |
} |
272 |
|
273 |
public Code compileExpression(String source) { |
274 |
org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
275 |
Code code = compiler.compileExpression(source); |
276 |
return code;
|
277 |
} |
278 |
|
279 |
public void testIdentifier1() { |
280 |
String source = "precio"; |
281 |
|
282 |
Code code = compileExpression(source); |
283 |
assertEquals("\"precio\"", code.toString(formatter()));
|
284 |
} |
285 |
|
286 |
public void testIdentifier2() { |
287 |
org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
288 |
compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(true);
|
289 |
|
290 |
String source = "[precio]"; |
291 |
|
292 |
Code code = compiler.compileExpression(source); |
293 |
assertEquals("\"precio\"", code.toString(formatter()));
|
294 |
} |
295 |
|
296 |
public void testIdentifier3() { |
297 |
String source = "\"precio\""; |
298 |
|
299 |
Code code = compileExpression(source); |
300 |
assertEquals("\"precio\"", code.toString(formatter()));
|
301 |
} |
302 |
|
303 |
|
304 |
public void testTrue() { |
305 |
String source = "true"; |
306 |
|
307 |
Code code = compileExpression(source); |
308 |
assertEquals("TRUE", code.toString(formatter()));
|
309 |
} |
310 |
|
311 |
public void testFalse() { |
312 |
String source = "false"; |
313 |
|
314 |
Code code = compileExpression(source); |
315 |
assertEquals("FALSE", code.toString(formatter()));
|
316 |
} |
317 |
|
318 |
public void testNull() { |
319 |
String source = "null"; |
320 |
|
321 |
Code code = compileExpression(source); |
322 |
assertEquals("NULL", code.toString(formatter()));
|
323 |
} |
324 |
|
325 |
public void testNotTrue() { |
326 |
String source = "not true"; |
327 |
|
328 |
Code code = compileExpression(source); |
329 |
assertEquals("NOT(TRUE)", code.toString(formatter()));
|
330 |
} |
331 |
|
332 |
public void testInteger() { |
333 |
String source = "23"; |
334 |
|
335 |
Code code = compileExpression(source); |
336 |
assertEquals("23", code.toString(formatter()));
|
337 |
} |
338 |
|
339 |
public void operator(String operatorName) { |
340 |
String source = "precio " + operatorName + " 23"; |
341 |
|
342 |
Code code = compileExpression(source); |
343 |
assertEquals("(\"precio\" "+ operatorName + " 23)", code.toString(formatter())); |
344 |
} |
345 |
|
346 |
public void testOperators() { |
347 |
|
348 |
operator("=");
|
349 |
operator("<>");
|
350 |
operator(">");
|
351 |
operator(">=");
|
352 |
operator("<");
|
353 |
operator("<=");
|
354 |
operator("LIKE");
|
355 |
// operator("ILIKE");
|
356 |
operator("||");
|
357 |
operator("+");
|
358 |
operator("-");
|
359 |
operator("*");
|
360 |
operator("OR");
|
361 |
operator("AND");
|
362 |
operator("%");
|
363 |
operator("IS");
|
364 |
|
365 |
operator("~");
|
366 |
} |
367 |
|
368 |
public void testILike() { |
369 |
String source = "precio ILike 23"; |
370 |
|
371 |
Code code = compileExpression(source); |
372 |
assertEquals("LOWER(\"precio\") LIKE LOWER(23)", code.toString(formatter()));
|
373 |
} |
374 |
|
375 |
public void testAddMul() { |
376 |
String source = "precio + 10 * 2 + 20 + 30"; |
377 |
|
378 |
Code code = compileExpression(source); |
379 |
assertEquals("(((\"precio\" + (10 * 2)) + 20) + 30)", code.toString(formatter()));
|
380 |
} |
381 |
|
382 |
public void testAddMulPar() { |
383 |
String source = "(precio + 10) * 2 + 20 + 30"; |
384 |
|
385 |
Code code = compileExpression(source); |
386 |
assertEquals("((((\"precio\" + 10) * 2) + 20) + 30)", code.toString(formatter()));
|
387 |
} |
388 |
|
389 |
public void testAbs() { |
390 |
String source = "precio + abs(10)"; |
391 |
|
392 |
Code code = compileExpression(source); |
393 |
assertEquals("(\"precio\" + abs(10))", code.toString(formatter()));
|
394 |
} |
395 |
|
396 |
public void testAbs2() { |
397 |
String source = "precio + abs(-10)"; |
398 |
|
399 |
Code code = compileExpression(source); |
400 |
assertEquals("(\"precio\" + abs(-10))", code.toString(formatter()));
|
401 |
} |
402 |
|
403 |
public void testPI() { |
404 |
String source = "precio + PI()"; |
405 |
|
406 |
Code code = compileExpression(source); |
407 |
assertEquals("(\"precio\" + PI())", code.toString(formatter()));
|
408 |
} |
409 |
|
410 |
public void testCeil() { |
411 |
String source = "precio + CEIL(PI())"; |
412 |
|
413 |
Code code = compileExpression(source); |
414 |
assertEquals("(\"precio\" + CEIL(PI()))", code.toString(formatter()));
|
415 |
} |
416 |
|
417 |
public void testGetitem1() { |
418 |
String source = "LIST('uno','dos','tres')[1]" ; |
419 |
|
420 |
Code code = compileExpression(source); |
421 |
assertEquals("GETITEM(LIST('uno', 'dos', 'tres'), 1)", code.toString(formatter()));
|
422 |
} |
423 |
|
424 |
public void testConcat() { |
425 |
String source = "CONCAT(precio,' euros')"; |
426 |
|
427 |
Code code = compileExpression(source); |
428 |
assertEquals("CONCAT(\"precio\", ' euros')", code.toString(formatter()));
|
429 |
} |
430 |
|
431 |
public void test1() { |
432 |
String source = "NOMBRE03 = 'Torre d''En Besora (la)'"; |
433 |
|
434 |
Code code = compileExpression(source); |
435 |
assertEquals("(\"NOMBRE03\" = 'Torre d''En Besora (la)')", code.toString(formatter()));
|
436 |
} |
437 |
|
438 |
public void test2() { |
439 |
org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
440 |
compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(true);
|
441 |
|
442 |
String source = "[1990] = 0.168873933773767"; |
443 |
|
444 |
Code code = compiler.compileExpression(source); |
445 |
assertEquals("(\"1990\" = 0.168873933773767)", code.toString(formatter()));
|
446 |
} |
447 |
|
448 |
public void test2fields() { |
449 |
org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
450 |
compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(true);
|
451 |
|
452 |
String source = "[1990] = [precio]"; |
453 |
|
454 |
Code code = compiler.compileExpression(source); |
455 |
assertEquals("(\"1990\" = \"precio\")", code.toString(formatter()));
|
456 |
} |
457 |
|
458 |
|
459 |
public void testInvokeFunction1() { |
460 |
String source = "ST_Area(GEOMETRY)"; |
461 |
|
462 |
Code code = compileExpression(source); |
463 |
assertEquals("ST_Area(\"GEOMETRY\")", code.toString(formatter()));
|
464 |
} |
465 |
|
466 |
} |