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 @ 46010
History | View | Annotate | Download (13.5 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 testSelectWhere2() { |
161 |
StringBuilder source = new StringBuilder(); |
162 |
source.append("SELECT * FROM countries ");
|
163 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
164 |
|
165 |
Compiler compiler = createCompiler();
|
166 |
|
167 |
Code code = compiler.compileExpression(source.toString()); |
168 |
link(code); |
169 |
checkEquals("testSelectWhere2", "SELECT(TUPLE(), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL)", code.toString()); |
170 |
} |
171 |
|
172 |
public void testSelectOrder() { |
173 |
StringBuilder source = new StringBuilder(); |
174 |
source.append("SELECT * FROM countries ");
|
175 |
source.append(" ORDER BY CONTINENT ASC, LASTCENSUS DESC; ");
|
176 |
|
177 |
Compiler compiler = createCompiler();
|
178 |
|
179 |
Code code = compiler.compileExpression(source.toString()); |
180 |
link(code); |
181 |
checkEquals("testSelectOrder", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(\"CONTINENT\", \"LASTCENSUS\"), TUPLE(TRUE, FALSE), NULL)", code.toString()); |
182 |
} |
183 |
|
184 |
public void testSelectWhereOrder() { |
185 |
StringBuilder source = new StringBuilder(); |
186 |
source.append("SELECT * FROM countries ");
|
187 |
source.append(" WHERE LASTCENSUS > 0 ");
|
188 |
source.append(" ORDER BY ID ; ");
|
189 |
|
190 |
Compiler compiler = createCompiler();
|
191 |
|
192 |
Code code = compiler.compileExpression(source.toString()); |
193 |
link(code); |
194 |
checkEquals("testSelectWhereOrder", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(\"ID\"), TUPLE(TRUE), NULL)", code.toString()); |
195 |
} |
196 |
|
197 |
public void testSelectWhereOrderLimit() { |
198 |
StringBuilder source = new StringBuilder(); |
199 |
source.append("SELECT * FROM countries ");
|
200 |
source.append(" WHERE LASTCENSUS > 0 ");
|
201 |
source.append(" ORDER BY LASTCENSUS DESC");
|
202 |
source.append(" LIMIT 3 ;");
|
203 |
|
204 |
Compiler compiler = createCompiler();
|
205 |
|
206 |
Code code = compiler.compileExpression(source.toString()); |
207 |
link(code); |
208 |
checkEquals("testSelectWhereOrderLimit", "SELECT(TUPLE(), \"countries\", (\"LASTCENSUS\" > 0), TUPLE(\"LASTCENSUS\"), TUPLE(FALSE), 3)", code.toString()); |
209 |
} |
210 |
|
211 |
public void testSelectLimit() { |
212 |
StringBuilder source = new StringBuilder(); |
213 |
source.append("SELECT * FROM countries ");
|
214 |
source.append(" LIMIT 3; ");
|
215 |
|
216 |
Compiler compiler = createCompiler();
|
217 |
|
218 |
Code code = compiler.compileExpression(source.toString()); |
219 |
link(code); |
220 |
checkEquals("testSelectLimit", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), 3)", code.toString()); |
221 |
} |
222 |
|
223 |
public void testExists() { |
224 |
StringBuilder source = new StringBuilder(); |
225 |
source.append("EXISTS(NULL)");
|
226 |
|
227 |
Compiler compiler = createCompiler();
|
228 |
|
229 |
Code code = compiler.compileExpression(source.toString()); |
230 |
link(code); |
231 |
String id = "????????????????????????????????"; |
232 |
String s = code.toString();
|
233 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
234 |
checkEquals("testExists", "EXISTS(NULL, 'EXISTS????????????????????????????????')", s); |
235 |
} |
236 |
|
237 |
public void testExistsSelect1() { |
238 |
StringBuilder source = new StringBuilder(); |
239 |
source.append("EXISTS(");
|
240 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
241 |
source.append(" WHERE countries.LASTCENSUS > 0 ; ");
|
242 |
source.append(")");
|
243 |
|
244 |
Compiler compiler = createCompiler();
|
245 |
|
246 |
Code code = compiler.compileExpression(source.toString()); |
247 |
link(code); |
248 |
String id = "????????????????????????????????"; |
249 |
String s = code.toString();
|
250 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
251 |
checkEquals("testExistsSelect1", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL), 'EXISTS????????????????????????????????')", s); |
252 |
} |
253 |
|
254 |
public void testExistsSelect2() { |
255 |
StringBuilder source = new StringBuilder(); |
256 |
source.append("EXISTS(");
|
257 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
258 |
source.append(" WHERE countries.LASTCENSUS > 0 ");
|
259 |
source.append(")");
|
260 |
|
261 |
Compiler compiler = createCompiler();
|
262 |
|
263 |
Code code = compiler.compileExpression(source.toString()); |
264 |
link(code); |
265 |
String id = "????????????????????????????????"; |
266 |
String s = code.toString();
|
267 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
268 |
checkEquals("testExistsSelect2", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL), 'EXISTS????????????????????????????????')", s); |
269 |
} |
270 |
|
271 |
public void testExistsSelectLimit1() { |
272 |
StringBuilder source = new StringBuilder(); |
273 |
source.append("EXISTS(");
|
274 |
source.append(" SELECT \"ISO_A2\" FROM countries ");
|
275 |
source.append(" WHERE countries.LASTCENSUS > 0 ");
|
276 |
source.append(" LIMIT 1; ");
|
277 |
source.append(")");
|
278 |
|
279 |
Compiler compiler = createCompiler();
|
280 |
|
281 |
Code code = compiler.compileExpression(source.toString()); |
282 |
link(code); |
283 |
String id = "????????????????????????????????"; |
284 |
String s = code.toString();
|
285 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
286 |
checkEquals("testExistsSelectLimit1", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), 1), 'EXISTS????????????????????????????????')", s); |
287 |
} |
288 |
|
289 |
public void testExistsSelectLimit2() { |
290 |
// Sobre la tabla de continentes, para obtener la lista de continentes
|
291 |
// que tienen paises sin censo.
|
292 |
// Filtramos continentes por un campo del pais.
|
293 |
StringBuilder source = new StringBuilder(); |
294 |
source.append("EXISTS(");
|
295 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
296 |
source.append(" WHERE ");
|
297 |
source.append(" continents.NAME = countries.CONTINENT AND ");
|
298 |
source.append(" countries.LASTCENSUS < 0 ");
|
299 |
source.append(" LIMIT 1; ");
|
300 |
source.append(")");
|
301 |
|
302 |
Compiler compiler = createCompiler();
|
303 |
|
304 |
Code code = compiler.compileExpression(source.toString()); |
305 |
link(code); |
306 |
String id = "????????????????????????????????"; |
307 |
String s = code.toString();
|
308 |
s = s.substring(0, s.length()-id.length()-2)+id+s.substring(s.length()-2); |
309 |
checkEquals("testExistsSelectLimit2", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)), TUPLE(), TUPLE(), 1), 'EXISTS????????????????????????????????')", s); |
310 |
} |
311 |
|
312 |
public void testExistsSelectLimit3() { |
313 |
// Sobre la tabla de continentes, para obtener la lista de continentes
|
314 |
// que tienen paises sin censo.
|
315 |
// Filtramos continentes por un campo del pais.
|
316 |
StringBuilder source = new StringBuilder(); |
317 |
source.append("EXISTS(");
|
318 |
source.append(" SELECT \"ISO_A2\" FROM countries");
|
319 |
source.append(" WHERE ");
|
320 |
source.append(" continents.NAME = countries.CONTINENT AND ");
|
321 |
source.append(" countries.LASTCENSUS < 0 ");
|
322 |
source.append(" LIMIT 1 ");
|
323 |
source.append(" ,");
|
324 |
source.append(" 'patata'");
|
325 |
source.append(")");
|
326 |
|
327 |
Compiler compiler = createCompiler();
|
328 |
|
329 |
Code code = compiler.compileExpression(source.toString()); |
330 |
link(code); |
331 |
checkEquals("testExistsSelectLimit3", "EXISTS(SELECT(TUPLE(\"ISO_A2\"), \"countries\", ((\"continents\".\"NAME\" = \"countries\".\"CONTINENT\") AND (\"countries\".\"LASTCENSUS\" < 0)), TUPLE(), TUPLE(), 1), 'patata')", code.toString()); |
332 |
} |
333 |
|
334 |
} |