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 / TestCodeToValue.java @ 47077
History | View | Annotate | Download (7.23 KB)
1 | 44006 | jjdelcerro | package org.gvsig.expresionevaluator.impl; |
---|---|---|---|
2 | 43128 | jjdelcerro | |
3 | 44006 | jjdelcerro | import junit.framework.TestCase; |
4 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
5 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
6 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
7 | import org.gvsig.expressionevaluator.LexicalAnalyzer; |
||
8 | 44006 | jjdelcerro | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
9 | 43128 | jjdelcerro | |
10 | 44198 | jjdelcerro | public class TestCodeToValue extends TestCase { |
11 | |||
12 | public TestCodeToValue(String testName) { |
||
13 | 44006 | jjdelcerro | super(testName);
|
14 | } |
||
15 | |||
16 | 43128 | jjdelcerro | @Override
|
17 | 44006 | jjdelcerro | protected void setUp() throws Exception { |
18 | super.setUp();
|
||
19 | new DefaultLibrariesInitializer().fullInitialize();
|
||
20 | } |
||
21 | 43128 | jjdelcerro | |
22 | 44006 | jjdelcerro | @Override
|
23 | protected void tearDown() throws Exception { |
||
24 | super.tearDown();
|
||
25 | 43128 | jjdelcerro | } |
26 | 44006 | jjdelcerro | |
27 | // TODO add test methods here. The name must begin with 'test'. For example:
|
||
28 | // public void testHello() {}
|
||
29 | 43128 | jjdelcerro | |
30 | 44198 | jjdelcerro | protected LexicalAnalyzer createLexicalAnalyzer() {
|
31 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
32 | LexicalAnalyzer lexer = manager.createLexicalAnalyzer(); |
||
33 | return lexer;
|
||
34 | 43128 | jjdelcerro | } |
35 | 44198 | jjdelcerro | |
36 | protected org.gvsig.expressionevaluator.Compiler createCompiler() {
|
||
37 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
38 | org.gvsig.expressionevaluator.Compiler compiler = manager.createCompiler(); |
||
39 | compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
||
40 | return compiler;
|
||
41 | } |
||
42 | 43128 | jjdelcerro | |
43 | 44198 | jjdelcerro | public Code compileExpression(String source) { |
44 | org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
||
45 | Code code = compiler.compileExpression(source); |
||
46 | return code;
|
||
47 | 43128 | jjdelcerro | } |
48 | |||
49 | 44198 | jjdelcerro | public void testIdentifier1() { |
50 | String source = "precio"; |
||
51 | |||
52 | Code code = compileExpression(source); |
||
53 | assertEquals("\"precio\"", code.toValue().toString());
|
||
54 | } |
||
55 | |||
56 | public void testIdentifier2() { |
||
57 | org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
||
58 | compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(true);
|
||
59 | |||
60 | String source = "[precio]"; |
||
61 | |||
62 | Code code = compiler.compileExpression(source); |
||
63 | assertEquals("\"precio\"", code.toValue().toString());
|
||
64 | } |
||
65 | |||
66 | public void testIdentifier3() { |
||
67 | String source = "\"precio\""; |
||
68 | |||
69 | Code code = compileExpression(source); |
||
70 | assertEquals("\"precio\"", code.toValue().toString());
|
||
71 | } |
||
72 | |||
73 | |||
74 | public void testTrue() { |
||
75 | String source = "true"; |
||
76 | |||
77 | Code code = compileExpression(source); |
||
78 | assertEquals("TRUE", code.toValue().toString());
|
||
79 | } |
||
80 | |||
81 | public void testFalse() { |
||
82 | String source = "false"; |
||
83 | |||
84 | Code code = compileExpression(source); |
||
85 | assertEquals("FALSE", code.toValue().toString());
|
||
86 | } |
||
87 | |||
88 | public void testNull() { |
||
89 | String source = "null"; |
||
90 | |||
91 | Code code = compileExpression(source); |
||
92 | assertEquals("NULL", code.toValue().toString());
|
||
93 | } |
||
94 | |||
95 | public void testNotTrue() { |
||
96 | String source = "not true"; |
||
97 | |||
98 | Code code = compileExpression(source); |
||
99 | assertEquals("NOT(TRUE)", code.toValue().toString());
|
||
100 | } |
||
101 | |||
102 | public void testInteger() { |
||
103 | String source = "23"; |
||
104 | |||
105 | Code code = compileExpression(source); |
||
106 | assertEquals("23", code.toValue().toString());
|
||
107 | } |
||
108 | |||
109 | public void operator(String operatorName) { |
||
110 | String source = "precio " + operatorName + " 23"; |
||
111 | |||
112 | Code code = compileExpression(source); |
||
113 | assertEquals("(\"precio\" "+ operatorName + " 23)", code.toValue().toString()); |
||
114 | } |
||
115 | |||
116 | public void testOperators() { |
||
117 | |||
118 | operator("=");
|
||
119 | operator("<>");
|
||
120 | operator(">");
|
||
121 | operator(">=");
|
||
122 | operator("<");
|
||
123 | operator("<=");
|
||
124 | operator("LIKE");
|
||
125 | operator("ILIKE");
|
||
126 | operator("||");
|
||
127 | operator("+");
|
||
128 | operator("-");
|
||
129 | operator("*");
|
||
130 | operator("OR");
|
||
131 | operator("AND");
|
||
132 | 44885 | jjdelcerro | // operator("%");
|
133 | 46498 | jjdelcerro | // operator("IS");
|
134 | 44198 | jjdelcerro | |
135 | operator("~");
|
||
136 | } |
||
137 | |||
138 | // public void testILike() {
|
||
139 | // String source = "precio ILike 23";
|
||
140 | //
|
||
141 | // Code code = compileExpression(source);
|
||
142 | // assertEquals("LOWER(\"precio\") LIKE LOWER(23)", code.toValue().toString());
|
||
143 | // }
|
||
144 | |||
145 | public void testAddMul() { |
||
146 | String source = "precio + 10 * 2 + 20 + 30"; |
||
147 | |||
148 | Code code = compileExpression(source); |
||
149 | assertEquals("(((\"precio\" + (10 * 2)) + 20) + 30)", code.toValue().toString());
|
||
150 | } |
||
151 | |||
152 | public void testAddMulPar() { |
||
153 | String source = "(precio + 10) * 2 + 20 + 30"; |
||
154 | |||
155 | Code code = compileExpression(source); |
||
156 | assertEquals("((((\"precio\" + 10) * 2) + 20) + 30)", code.toValue().toString());
|
||
157 | } |
||
158 | |||
159 | public void testAbs() { |
||
160 | String source = "precio + abs(10)"; |
||
161 | |||
162 | Code code = compileExpression(source); |
||
163 | assertEquals("(\"precio\" + abs(10))", code.toValue().toString());
|
||
164 | } |
||
165 | |||
166 | public void testAbs2() { |
||
167 | String source = "precio + abs(-10)"; |
||
168 | |||
169 | Code code = compileExpression(source); |
||
170 | 44211 | jjdelcerro | assertEquals("(\"precio\" + abs(-10))", code.toValue().toString());
|
171 | 44198 | jjdelcerro | } |
172 | |||
173 | public void testPI() { |
||
174 | String source = "precio + PI()"; |
||
175 | |||
176 | Code code = compileExpression(source); |
||
177 | assertEquals("(\"precio\" + PI())", code.toValue().toString());
|
||
178 | } |
||
179 | |||
180 | public void testCeil() { |
||
181 | String source = "precio + CEIL(PI())"; |
||
182 | |||
183 | Code code = compileExpression(source); |
||
184 | assertEquals("(\"precio\" + CEIL(PI()))", code.toValue().toString());
|
||
185 | } |
||
186 | |||
187 | public void testGetitem1() { |
||
188 | String source = "LIST('uno','dos','tres')[1]" ; |
||
189 | |||
190 | Code code = compileExpression(source); |
||
191 | 45011 | jjdelcerro | assertEquals("LIST('uno', 'dos', 'tres')[1]", code.toValue().toString());
|
192 | 44198 | jjdelcerro | } |
193 | |||
194 | public void testConcat() { |
||
195 | String source = "CONCAT(precio,' euros')"; |
||
196 | |||
197 | Code code = compileExpression(source); |
||
198 | assertEquals("CONCAT(\"precio\", ' euros')", code.toValue().toString());
|
||
199 | } |
||
200 | |||
201 | public void test1() { |
||
202 | String source = "NOMBRE03 = 'Torre d''En Besora (la)'"; |
||
203 | |||
204 | Code code = compileExpression(source); |
||
205 | assertEquals("(\"NOMBRE03\" = 'Torre d''En Besora (la)')", code.toValue().toString());
|
||
206 | } |
||
207 | |||
208 | 43128 | jjdelcerro | public void test2() { |
209 | 44198 | jjdelcerro | org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
210 | compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(true);
|
||
211 | |||
212 | String source = "[1990] = 0.168873933773767"; |
||
213 | |||
214 | Code code = compiler.compileExpression(source); |
||
215 | assertEquals("(\"1990\" = 0.168873933773767)", code.toValue().toString());
|
||
216 | 43128 | jjdelcerro | } |
217 | 44198 | jjdelcerro | |
218 | public void test2fields() { |
||
219 | org.gvsig.expressionevaluator.Compiler compiler = createCompiler(); |
||
220 | compiler.getLexicalAnalyzer().setUseBracketsForIdentifiers(true);
|
||
221 | |||
222 | String source = "[1990] = [precio]"; |
||
223 | |||
224 | Code code = compiler.compileExpression(source); |
||
225 | assertEquals("(\"1990\" = \"precio\")", code.toValue().toString());
|
||
226 | } |
||
227 | 43128 | jjdelcerro | |
228 | 44198 | jjdelcerro | |
229 | public void testInvokeFunction1() { |
||
230 | String source = "ST_Area(GEOMETRY)"; |
||
231 | |||
232 | Code code = compileExpression(source); |
||
233 | assertEquals("ST_Area(\"GEOMETRY\")", code.toValue().toString());
|
||
234 | } |
||
235 | 43128 | jjdelcerro | |
236 | } |