svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / expressionevaluator / TestGrammarCompiler.java @ 46954
History | View | Annotate | Download (18.2 KB)
1 |
package org.gvsig.expressionevaluator; |
---|---|
2 |
|
3 |
import java.util.Objects; |
4 |
import junit.framework.TestCase; |
5 |
import org.gvsig.expressionevaluator.impl.expressionbuilder.formatters.DALFormatter; |
6 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER; |
7 |
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
8 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
9 |
|
10 |
/**
|
11 |
*
|
12 |
* @author jjdelcerro
|
13 |
*/
|
14 |
public class TestGrammarCompiler extends TestCase { |
15 |
|
16 |
public TestGrammarCompiler(String testName) { |
17 |
super(testName);
|
18 |
} |
19 |
|
20 |
@Override
|
21 |
protected void setUp() throws Exception { |
22 |
super.setUp();
|
23 |
new DefaultLibrariesInitializer().fullInitialize();
|
24 |
} |
25 |
|
26 |
@Override
|
27 |
protected void tearDown() throws Exception { |
28 |
super.tearDown();
|
29 |
} |
30 |
|
31 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
32 |
// public void testHello() {}
|
33 |
|
34 |
protected LexicalAnalyzer createLexicalAnalyzer() {
|
35 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
36 |
LexicalAnalyzer lexer = manager.createLexicalAnalyzer(); |
37 |
return lexer;
|
38 |
} |
39 |
|
40 |
protected org.gvsig.expressionevaluator.Compiler createCompiler() {
|
41 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
42 |
Compiler compiler = manager.createCompiler();
|
43 |
compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
44 |
return compiler;
|
45 |
} |
46 |
|
47 |
protected SymbolTable createSymbolTable() {
|
48 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
49 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
50 |
symbolTable.setVar("precio", 200); |
51 |
symbolTable.setVar("1990", 0.168873933773767); |
52 |
return symbolTable;
|
53 |
} |
54 |
|
55 |
protected Interpreter createInterpreter(SymbolTable symbolTable) {
|
56 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
57 |
Interpreter interpreter = manager.createInterpreter(); |
58 |
interpreter.setSymbolTable(symbolTable); |
59 |
return interpreter;
|
60 |
} |
61 |
|
62 |
private void link(Code code) { |
63 |
code.link(createSymbolTable()); |
64 |
} |
65 |
|
66 |
private void dump(String testname, Object expected, Object actual) { |
67 |
System.out.println("### ---------------------"); |
68 |
System.out.println("### "+testname); |
69 |
System.out.println("### expected: "+Objects.toString(expected)); |
70 |
System.out.println("### actual : "+Objects.toString(actual)); |
71 |
} |
72 |
|
73 |
private void checkEquals(String testname, Object expected, Object actual) { |
74 |
dump(testname,expected,actual); |
75 |
assertEquals(expected, actual); |
76 |
} |
77 |
|
78 |
public void testSelect() { |
79 |
StringBuilder source = new StringBuilder(); |
80 |
source.append("SELECT * FROM countries;");
|
81 |
|
82 |
Compiler compiler = createCompiler();
|
83 |
|
84 |
Code code = compiler.compileExpression(source.toString()); |
85 |
link(code); |
86 |
checkEquals("testSelect", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL)", code.toString()); |
87 |
} |
88 |
|
89 |
public void testSelectToValue() { |
90 |
StringBuilder source = new StringBuilder(); |
91 |
source.append("SELECT * FROM countries;");
|
92 |
|
93 |
Compiler compiler = createCompiler();
|
94 |
|
95 |
Code code = compiler.compileExpression(source.toString()); |
96 |
link(code); |
97 |
|
98 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
99 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
100 |
checkEquals("testSelectToValue", "( SELECT * FROM \"countries\" )", code.toValue(builder).toString()); |
101 |
} |
102 |
|
103 |
public void testSelectCount() { |
104 |
StringBuilder source = new StringBuilder(); |
105 |
source.append("SELECT COUNT(*) FROM countries;");
|
106 |
|
107 |
Compiler compiler = createCompiler();
|
108 |
|
109 |
Code code = compiler.compileExpression(source.toString()); |
110 |
link(code); |
111 |
checkEquals("testSelect", "SELECT_COUNT(\"countries\", NULL)", code.toString()); |
112 |
} |
113 |
|
114 |
public void testSelectAggregate() { |
115 |
StringBuilder source = new StringBuilder(); |
116 |
source.append("SELECT sum(\"TOTAL_MUERTOS\") FROM \"ARENA2_ACCIDENTES\";");
|
117 |
|
118 |
Compiler compiler = createCompiler();
|
119 |
|
120 |
Code code = compiler.compileExpression(source.toString()); |
121 |
link(code); |
122 |
checkEquals("testSelect", "SELECT_AGGREGATE(\"ARENA2_ACCIDENTES\", NULL, 'sum', 'TOTAL_MUERTOS')", code.toString()); |
123 |
} |
124 |
|
125 |
public void testSelectCountToValue() { |
126 |
StringBuilder source = new StringBuilder(); |
127 |
source.append("SELECT COUNT(*) FROM countries;");
|
128 |
|
129 |
Compiler compiler = createCompiler();
|
130 |
|
131 |
Code code = compiler.compileExpression(source.toString()); |
132 |
link(code); |
133 |
|
134 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
135 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
136 |
checkEquals("testSelectToValue", "( SELECT COUNT(*) FROM \"countries\" )", code.toValue(builder).toString()); |
137 |
} |
138 |
|
139 |
|
140 |
|
141 |
public void testSelect1() { |
142 |
StringBuilder source = new StringBuilder(); |
143 |
source.append("SELECT * FROM countries");
|
144 |
|
145 |
Compiler compiler = createCompiler();
|
146 |
|
147 |
Code code = compiler.compileExpression(source.toString()); |
148 |
link(code); |
149 |
checkEquals("testSelect1", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL)", code.toString()); |
150 |
} |
151 |
|
152 |
public void testSelect2() { |
153 |
StringBuilder source = new StringBuilder(); |
154 |
source.append("BEGIN ");
|
155 |
source.append(" SET X = 0; ");
|
156 |
source.append(" FOR row in (SELECT * FROM countries;) "); // Con ; |
157 |
source.append(" BEGIN ");
|
158 |
source.append(" IF row.LASTCENSUS > 0 THEN ");
|
159 |
source.append(" SET X = X + row.LASTCENSUS ");
|
160 |
source.append(" END IF ");
|
161 |
source.append(" END FOR ");
|
162 |
source.append("END");
|
163 |
|
164 |
Compiler compiler = createCompiler();
|
165 |
|
166 |
Code code = compiler.compileExpression(source.toString()); |
167 |
link(code); |
168 |
checkEquals("testSelect2", "BLOCK(LET('X', 0), FOREACH('row', SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL), IF((\"row\".\"LASTCENSUS\" > 0), LET('X', (\"X\" + \"row\".\"LASTCENSUS\")))))", code.toString()); |
169 |
} |
170 |
|
171 |
public void testSelect3() { |
172 |
StringBuilder source = new StringBuilder(); |
173 |
source.append("BEGIN ");
|
174 |
source.append(" SET X = 0; ");
|
175 |
source.append(" FOR row in (SELECT * FROM countries) "); // Sin ; |
176 |
source.append(" BEGIN ");
|
177 |
source.append(" IF row.LASTCENSUS > 0 THEN ");
|
178 |
source.append(" SET X = X + row.LASTCENSUS ");
|
179 |
source.append(" END IF ");
|
180 |
source.append(" END FOR ");
|
181 |
source.append("END");
|
182 |
|
183 |
Compiler compiler = createCompiler();
|
184 |
|
185 |
Code code = compiler.compileExpression(source.toString()); |
186 |
link(code); |
187 |
checkEquals("testSelect3", "BLOCK(LET('X', 0), FOREACH('row', SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL), IF((\"row\".\"LASTCENSUS\" > 0), LET('X', (\"X\" + \"row\".\"LASTCENSUS\")))))", code.toString()); |
188 |
} |
189 |
|
190 |
public void testSelectWhere() { |
191 |
StringBuilder source = new StringBuilder(); |
192 |
source.append("SELECT * FROM countries ");
|
193 |
source.append(" WHERE LASTCENSUS > 0; ");
|
194 |
|
195 |
Compiler compiler = createCompiler();
|
196 |
|
197 |
Code code = compiler.compileExpression(source.toString()); |
198 |
link(code); |
199 |
checkEquals("testSelectWhere", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL)", code.toString()); |
200 |
} |
201 |
|
202 |
public void testSelectCountWhere() { |
203 |
StringBuilder source = new StringBuilder(); |
204 |
source.append("SELECT COUNT(*) FROM countries ");
|
205 |
source.append(" WHERE LASTCENSUS > 0 ; ");
|
206 |
|
207 |
Compiler compiler = createCompiler();
|
208 |
|
209 |
Code code = compiler.compileExpression(source.toString()); |
210 |
link(code); |
211 |
checkEquals("testSelectCountWhere", "SELECT_COUNT(\"countries\", (\"LASTCENSUS\" > 0))", code.toString()); |
212 |
} |
213 |
|
214 |
public void testSelectCountWhere2() { |
215 |
StringBuilder source = new StringBuilder(); |
216 |
source.append("BEGIN");
|
217 |
source.append(" tableName := 'countries';");
|
218 |
source.append(" SELECT COUNT(*) FROM :tableName ");
|
219 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
220 |
source.append("END");
|
221 |
|
222 |
Compiler compiler = createCompiler();
|
223 |
|
224 |
Code code = compiler.compileExpression(source.toString()); |
225 |
link(code); |
226 |
checkEquals("testSelectCountWhere", "BLOCK(LET('tableName', 'countries'), SELECT_COUNT($HOSTEXPRESSION(\"tableName\"), (\"countries\".\"LASTCENSUS\" > 0)))", code.toString()); |
227 |
} |
228 |
|
229 |
public void testSelectWhere2() { |
230 |
StringBuilder source = new StringBuilder(); |
231 |
source.append("SELECT * FROM countries ");
|
232 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
233 |
|
234 |
Compiler compiler = createCompiler();
|
235 |
|
236 |
Code code = compiler.compileExpression(source.toString()); |
237 |
link(code); |
238 |
checkEquals("testSelectWhere2", "SELECT(TUPLE(), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL)", code.toString()); |
239 |
} |
240 |
|
241 |
public void testSelectWhere3() { |
242 |
StringBuilder source = new StringBuilder(); |
243 |
source.append("BEGIN");
|
244 |
source.append(" tableName := 'countries';");
|
245 |
source.append(" SELECT * FROM :tableName ");
|
246 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
247 |
source.append("END");
|
248 |
|
249 |
Compiler compiler = createCompiler();
|
250 |
|
251 |
Code code = compiler.compileExpression(source.toString()); |
252 |
link(code); |
253 |
checkEquals("testSelectWhere3", "BLOCK(LET('tableName', 'countries'), SELECT(TUPLE(), $HOSTEXPRESSION(\"tableName\"), (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL))", code.toString()); |
254 |
} |
255 |
|
256 |
public void testSelectWhereToValue3() { |
257 |
StringBuilder source = new StringBuilder(); |
258 |
source.append("BEGIN");
|
259 |
source.append(" SELECT * FROM countries ");
|
260 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
261 |
source.append("END");
|
262 |
|
263 |
Compiler compiler = createCompiler();
|
264 |
|
265 |
Code code = compiler.compileExpression(source.toString()); |
266 |
link(code); |
267 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
268 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
269 |
checkEquals("testSelectWhereToValue3", "BLOCK(( SELECT * FROM \"countries\" WHERE (\"countries\".LASTCENSUS > 0) ))", code.toValue(builder).toString()); |
270 |
} |
271 |
|
272 |
public void testSelectCountWhereToValue3() { |
273 |
StringBuilder source = new StringBuilder(); |
274 |
source.append("BEGIN");
|
275 |
source.append(" SELECT COUNT(*) FROM countries ");
|
276 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
277 |
source.append("END");
|
278 |
|
279 |
Compiler compiler = createCompiler();
|
280 |
|
281 |
Code code = compiler.compileExpression(source.toString()); |
282 |
link(code); |
283 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
284 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase());
|
285 |
checkEquals("testSelectCountWhereToValue3", "BLOCK(( SELECT COUNT(*) FROM \"countries\" WHERE (\"countries\".LASTCENSUS > 0) ))", code.toValue(builder).toString()); |
286 |
} |
287 |
|
288 |
public void testSelectOrder() { |
289 |
StringBuilder source = new StringBuilder(); |
290 |
source.append("SELECT * FROM countries ");
|
291 |
source.append(" ORDER BY CONTINENT ASC, LASTCENSUS DESC; ");
|
292 |
|
293 |
Compiler compiler = createCompiler();
|
294 |
|
295 |
Code code = compiler.compileExpression(source.toString()); |
296 |
link(code); |
297 |
checkEquals("testSelectOrder", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(\"CONTINENT\", \"LASTCENSUS\"), TUPLE(TRUE, FALSE), NULL)", code.toString()); |
298 |
} |
299 |
|
300 |
public void testSelectWhereOrder() { |
301 |
StringBuilder source = new StringBuilder(); |
302 |
source.append("SELECT * FROM countries ");
|
303 |
source.append(" WHERE LASTCENSUS > 0 ");
|
304 |
source.append(" ORDER BY ID ; ");
|
305 |
|
306 |
Compiler compiler = createCompiler();
|
307 |
|
308 |
Code code = compiler.compileExpression(source.toString()); |
309 |
link(code); |
310 |
checkEquals("testSelectWhereOrder", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(\"ID\"), TUPLE(TRUE), NULL)", code.toString()); |
311 |
} |
312 |
|
313 |
public void testSelectWhereOrderLimit() { |
314 |
StringBuilder source = new StringBuilder(); |
315 |
source.append("SELECT * FROM countries ");
|
316 |
source.append(" WHERE LASTCENSUS > 0 ");
|
317 |
source.append(" ORDER BY LASTCENSUS DESC");
|
318 |
source.append(" LIMIT 3 ;");
|
319 |
|
320 |
Compiler compiler = createCompiler();
|
321 |
|
322 |
Code code = compiler.compileExpression(source.toString()); |
323 |
link(code); |
324 |
checkEquals("testSelectWhereOrderLimit", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(\"LASTCENSUS\"), TUPLE(FALSE), 3)", code.toString()); |
325 |
} |
326 |
|
327 |
public void testSelectLimit() { |
328 |
StringBuilder source = new StringBuilder(); |
329 |
source.append("SELECT * FROM countries ");
|
330 |
source.append(" LIMIT 3; ");
|
331 |
|
332 |
Compiler compiler = createCompiler();
|
333 |
|
334 |
Code code = compiler.compileExpression(source.toString()); |
335 |
link(code); |
336 |
checkEquals("testSelectLimit", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), 3)", code.toString()); |
337 |
} |
338 |
|
339 |
public void testExists() { |
340 |
StringBuilder source = new StringBuilder(); |
341 |
source.append("EXISTS(NULL)");
|
342 |
|
343 |
Compiler compiler = createCompiler();
|
344 |
|
345 |
Code code = compiler.compileExpression(source.toString()); |
346 |
link(code); |
347 |
String id = "????????????????????????????????"; |
348 |
String s = code.toString();
|
349 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
350 |
checkEquals("testExists", "EXISTS(NULL, 'EXISTS????????????????????????????????')", s); |
351 |
} |
352 |
|
353 |
public void testExistsSelect1() { |
354 |
StringBuilder source = new StringBuilder(); |
355 |
source.append("EXISTS(");
|
356 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
357 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
358 |
source.append(")");
|
359 |
|
360 |
Compiler compiler = createCompiler();
|
361 |
|
362 |
Code code = compiler.compileExpression(source.toString()); |
363 |
link(code); |
364 |
String id = "????????????????????????????????"; |
365 |
String s = code.toString();
|
366 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
367 |
checkEquals("testExistsSelect1", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL), 'EXISTS????????????????????????????????')", s); |
368 |
} |
369 |
|
370 |
public void testExistsSelect2() { |
371 |
StringBuilder source = new StringBuilder(); |
372 |
source.append("EXISTS(");
|
373 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
374 |
source.append(" WHERE countries.LASTCENSUS > 0 ");
|
375 |
source.append(")");
|
376 |
|
377 |
Compiler compiler = createCompiler();
|
378 |
|
379 |
Code code = compiler.compileExpression(source.toString()); |
380 |
link(code); |
381 |
String id = "????????????????????????????????"; |
382 |
String s = code.toString();
|
383 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
384 |
checkEquals("testExistsSelect2", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL), 'EXISTS????????????????????????????????')", s); |
385 |
} |
386 |
|
387 |
public void testExistsSelectLimit1() { |
388 |
StringBuilder source = new StringBuilder(); |
389 |
source.append("EXISTS(");
|
390 |
source.append(" SELECT \"ISO_A2\" FROM countries ");
|
391 |
source.append(" WHERE countries.LASTCENSUS > 0 ");
|
392 |
source.append(" LIMIT 1; ");
|
393 |
source.append(")");
|
394 |
|
395 |
Compiler compiler = createCompiler();
|
396 |
|
397 |
Code code = compiler.compileExpression(source.toString()); |
398 |
link(code); |
399 |
String id = "????????????????????????????????"; |
400 |
String s = code.toString();
|
401 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
402 |
checkEquals("testExistsSelectLimit1", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), 1), 'EXISTS????????????????????????????????')", s); |
403 |
} |
404 |
|
405 |
public void testExistsSelectLimit2() { |
406 |
// Sobre la tabla de continentes, para obtener la lista de continentes
|
407 |
// que tienen paises sin censo.
|
408 |
// Filtramos continentes por un campo del pais.
|
409 |
StringBuilder source = new StringBuilder(); |
410 |
source.append("EXISTS(");
|
411 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
412 |
source.append(" WHERE ");
|
413 |
source.append(" continents.NAME = countries.CONTINENT AND ");
|
414 |
source.append(" countries.LASTCENSUS < 0 ");
|
415 |
source.append(" LIMIT 1; ");
|
416 |
source.append(")");
|
417 |
|
418 |
Compiler compiler = createCompiler();
|
419 |
|
420 |
Code code = compiler.compileExpression(source.toString()); |
421 |
link(code); |
422 |
String id = "????????????????????????????????"; |
423 |
String s = code.toString();
|
424 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
425 |
checkEquals("testExistsSelectLimit2", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)), TUPLE(), TUPLE(), 1), 'EXISTS????????????????????????????????')", s); |
426 |
} |
427 |
|
428 |
public void testExistsSelectLimit3() { |
429 |
// Sobre la tabla de continentes, para obtener la lista de continentes
|
430 |
// que tienen paises sin censo.
|
431 |
// Filtramos continentes por un campo del pais.
|
432 |
StringBuilder source = new StringBuilder(); |
433 |
source.append("EXISTS(");
|
434 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
435 |
source.append(" WHERE ");
|
436 |
source.append(" continents.NAME = countries.CONTINENT AND ");
|
437 |
source.append(" countries.LASTCENSUS < 0 ");
|
438 |
source.append(" LIMIT 1 ");
|
439 |
source.append(" ,");
|
440 |
source.append(" 'patata'");
|
441 |
source.append(")");
|
442 |
|
443 |
Compiler compiler = createCompiler();
|
444 |
|
445 |
Code code = compiler.compileExpression(source.toString()); |
446 |
link(code); |
447 |
checkEquals("testExistsSelectLimit3", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)), TUPLE(), TUPLE(), 1), 'patata')", code.toString()); |
448 |
} |
449 |
|
450 |
} |