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