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 |
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 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 |
|
60 |
private void link(Code code) { |
61 |
code.link(createSymbolTable()); |
62 |
} |
63 |
|
64 |
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 |
public void testSelect() { |
77 |
StringBuilder source = new StringBuilder(); |
78 |
source.append("SELECT * FROM countries;");
|
79 |
|
80 |
Compiler compiler = createCompiler();
|
81 |
|
82 |
Code code = compiler.compileExpression(source.toString()); |
83 |
link(code); |
84 |
checkEquals("testSelect", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL)", code.toString()); |
85 |
} |
86 |
|
87 |
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 |
checkEquals("testSelect1", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL)", code.toString()); |
96 |
} |
97 |
|
98 |
public void testSelect2() { |
99 |
StringBuilder source = new StringBuilder(); |
100 |
source.append("BEGIN ");
|
101 |
source.append(" SET X = 0; ");
|
102 |
source.append(" FOR row in (SELECT * FROM countries;) "); // Con ; |
103 |
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 |
|
110 |
Compiler compiler = createCompiler();
|
111 |
|
112 |
Code code = compiler.compileExpression(source.toString()); |
113 |
link(code); |
114 |
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 |
} |
116 |
|
117 |
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 |
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 |
} |
135 |
|
136 |
public void testSelectWhere() { |
137 |
StringBuilder source = new StringBuilder(); |
138 |
source.append("SELECT * FROM countries ");
|
139 |
source.append(" WHERE LASTCENSUS > 0; ");
|
140 |
|
141 |
Compiler compiler = createCompiler();
|
142 |
|
143 |
Code code = compiler.compileExpression(source.toString()); |
144 |
link(code); |
145 |
checkEquals("testSelectWhere", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL)", code.toString()); |
146 |
} |
147 |
|
148 |
public void testSelectCountWhere() { |
149 |
StringBuilder source = new StringBuilder(); |
150 |
source.append("SELECT COUNT(*) FROM countries ");
|
151 |
source.append(" WHERE LASTCENSUS > 0 ; ");
|
152 |
|
153 |
Compiler compiler = createCompiler();
|
154 |
|
155 |
Code code = compiler.compileExpression(source.toString()); |
156 |
link(code); |
157 |
checkEquals("testSelectCountWhere", "SELECT_COUNT(\"countries\", (\"LASTCENSUS\" > 0))", code.toString()); |
158 |
} |
159 |
|
160 |
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 |
public void testSelectWhere2() { |
176 |
StringBuilder source = new StringBuilder(); |
177 |
source.append("SELECT * FROM countries ");
|
178 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
179 |
|
180 |
Compiler compiler = createCompiler();
|
181 |
|
182 |
Code code = compiler.compileExpression(source.toString()); |
183 |
link(code); |
184 |
checkEquals("testSelectWhere2", "SELECT(TUPLE(), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL)", code.toString()); |
185 |
} |
186 |
|
187 |
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 |
public void testSelectOrder() { |
203 |
StringBuilder source = new StringBuilder(); |
204 |
source.append("SELECT * FROM countries ");
|
205 |
source.append(" ORDER BY CONTINENT ASC, LASTCENSUS DESC; ");
|
206 |
|
207 |
Compiler compiler = createCompiler();
|
208 |
|
209 |
Code code = compiler.compileExpression(source.toString()); |
210 |
link(code); |
211 |
checkEquals("testSelectOrder", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(\"CONTINENT\", \"LASTCENSUS\"), TUPLE(TRUE, FALSE), NULL)", code.toString()); |
212 |
} |
213 |
|
214 |
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 |
|
220 |
Compiler compiler = createCompiler();
|
221 |
|
222 |
Code code = compiler.compileExpression(source.toString()); |
223 |
link(code); |
224 |
checkEquals("testSelectWhereOrder", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(\"ID\"), TUPLE(TRUE), NULL)", code.toString()); |
225 |
} |
226 |
|
227 |
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 |
|
234 |
Compiler compiler = createCompiler();
|
235 |
|
236 |
Code code = compiler.compileExpression(source.toString()); |
237 |
link(code); |
238 |
checkEquals("testSelectWhereOrderLimit", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(\"LASTCENSUS\"), TUPLE(FALSE), 3)", code.toString()); |
239 |
} |
240 |
|
241 |
public void testSelectLimit() { |
242 |
StringBuilder source = new StringBuilder(); |
243 |
source.append("SELECT * FROM countries ");
|
244 |
source.append(" LIMIT 3; ");
|
245 |
|
246 |
Compiler compiler = createCompiler();
|
247 |
|
248 |
Code code = compiler.compileExpression(source.toString()); |
249 |
link(code); |
250 |
checkEquals("testSelectLimit", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), 3)", code.toString()); |
251 |
} |
252 |
|
253 |
public void testExists() { |
254 |
StringBuilder source = new StringBuilder(); |
255 |
source.append("EXISTS(NULL)");
|
256 |
|
257 |
Compiler compiler = createCompiler();
|
258 |
|
259 |
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 |
checkEquals("testExists", "EXISTS(NULL, 'EXISTS????????????????????????????????')", s); |
265 |
} |
266 |
|
267 |
public void testExistsSelect1() { |
268 |
StringBuilder source = new StringBuilder(); |
269 |
source.append("EXISTS(");
|
270 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
271 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
272 |
source.append(")");
|
273 |
|
274 |
Compiler compiler = createCompiler();
|
275 |
|
276 |
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 |
checkEquals("testExistsSelect1", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL), 'EXISTS????????????????????????????????')", s); |
282 |
} |
283 |
|
284 |
public void testExistsSelect2() { |
285 |
StringBuilder source = new StringBuilder(); |
286 |
source.append("EXISTS(");
|
287 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
288 |
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 |
checkEquals("testExistsSelect2", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL), 'EXISTS????????????????????????????????')", s); |
299 |
} |
300 |
|
301 |
public void testExistsSelectLimit1() { |
302 |
StringBuilder source = new StringBuilder(); |
303 |
source.append("EXISTS(");
|
304 |
source.append(" SELECT \"ISO_A2\" FROM countries ");
|
305 |
source.append(" WHERE countries.LASTCENSUS > 0 ");
|
306 |
source.append(" LIMIT 1; ");
|
307 |
source.append(")");
|
308 |
|
309 |
Compiler compiler = createCompiler();
|
310 |
|
311 |
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 |
checkEquals("testExistsSelectLimit1", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), 1), 'EXISTS????????????????????????????????')", s); |
317 |
} |
318 |
|
319 |
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 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
326 |
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 |
|
332 |
Compiler compiler = createCompiler();
|
333 |
|
334 |
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 |
checkEquals("testExistsSelectLimit2", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)), TUPLE(), TUPLE(), 1), 'EXISTS????????????????????????????????')", s); |
340 |
} |
341 |
|
342 |
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 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
349 |
source.append(" WHERE ");
|
350 |
source.append(" continents.NAME = countries.CONTINENT AND ");
|
351 |
source.append(" countries.LASTCENSUS < 0 ");
|
352 |
source.append(" LIMIT 1 ");
|
353 |
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 |
checkEquals("testExistsSelectLimit3", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)), TUPLE(), TUPLE(), 1), 'patata')", code.toString()); |
362 |
} |
363 |
|
364 |
} |