KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > parser > string > TestExpression


1 package org.objectweb.medor.expression.parser.string;
2
3 import org.objectweb.jorm.type.api.PTypeSpace;
4 import org.objectweb.medor.expression.TestExpressionHelper;
5 import org.objectweb.medor.expression.api.Expression;
6 import org.objectweb.medor.expression.api.MalformedExpressionException;
7 import org.objectweb.medor.expression.lib.And;
8 import org.objectweb.medor.expression.lib.BasicOperand;
9 import org.objectweb.medor.expression.lib.BasicParameterOperand;
10 import org.objectweb.medor.expression.lib.DivideBy;
11 import org.objectweb.medor.expression.lib.Equal;
12 import org.objectweb.medor.expression.lib.Greater;
13 import org.objectweb.medor.expression.lib.GreaterEqual;
14 import org.objectweb.medor.expression.lib.Lower;
15 import org.objectweb.medor.expression.lib.LowerEqual;
16 import org.objectweb.medor.expression.lib.Minus;
17 import org.objectweb.medor.expression.lib.Mod;
18 import org.objectweb.medor.expression.lib.Mult;
19 import org.objectweb.medor.expression.lib.NotEqual;
20 import org.objectweb.medor.expression.lib.Or;
21 import org.objectweb.medor.expression.lib.Plus;
22 import org.objectweb.medor.expression.lib.ConditionalAnd;
23 import org.objectweb.medor.expression.lib.Concat;
24 import org.objectweb.medor.expression.lib.TypeConverter;
25 import org.objectweb.medor.expression.parser.api.ParameterTypeProvider;
26 import org.objectweb.medor.expression.parser.lib.MapParameterTypeProvider;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * @author P. Dechamboux
35  */

36 public class TestExpression extends TestExpressionHelper {
37     public TestExpression(String JavaDoc s) {
38         super(s);
39     }
40
41     public void testConstStringExtract() {
42         try {
43             ExpressionParser expr = new ExpressionParser();
44             String JavaDoc result;
45             ArrayList JavaDoc stringConst = new ArrayList JavaDoc();
46             result = expr.extractStringConst("a == 5", stringConst);
47             assertEquals("Unexpected string constant extracted", 0, stringConst.size());
48             assertEquals("Unexpected result", "a==5", result);
49             stringConst.clear();
50             result = expr.extractStringConst("a == \"\"", stringConst);
51             assertEquals("Bad string constant extracted", "", stringConst.get(0));
52             assertEquals("Unexpected result", "a==$", result);
53             stringConst.clear();
54             result = expr.extractStringConst("a == \"toto\"", stringConst);
55             assertEquals("Bad string constant extracted", "toto", stringConst.get(0));
56             assertEquals("Unexpected result", "a==$", result);
57             stringConst.clear();
58             result = expr.extractStringConst("\"toto\" == a", stringConst);
59             assertEquals("Bad string constant extracted", "toto", stringConst.get(0));
60             assertEquals("Unexpected result", "$==a", result);
61             stringConst.clear();
62             result = expr.extractStringConst("a == \"toto\"", stringConst);
63             assertEquals("Bad string constant extracted", "toto", stringConst.get(0));
64             assertEquals("Unexpected result", "a==$", result);
65             stringConst.clear();
66             result = expr.extractStringConst("\"titi\" == \"toto\"", stringConst);
67             assertEquals("Bad string constant extracted",
68                     Arrays.asList(new String JavaDoc[]{"titi", "toto"}), stringConst);
69             assertEquals("Unexpected result", "$==$", result);
70             stringConst.clear();
71             result = expr.extractStringConst(" a == \"titi\" + \"toto\" ", stringConst);
72             assertEquals("Bad string constant extracted",
73                     Arrays.asList(new String JavaDoc[]{"titi", "toto"}), stringConst);
74             assertEquals("Unexpected result", "a==$+$", result);
75             stringConst.clear();
76             result = expr.extractStringConst("(a == \"toto\")", stringConst);
77             assertEquals("Bad string constant extracted", "toto", stringConst.get(0));
78             assertEquals("Unexpected result", "(a==$)", result);
79         } catch (MalformedExpressionException e) {
80             fail(e.getMessage());
81         }
82     }
83
84     public void testAtomicOperand() {
85         Map JavaDoc ptmap = new HashMap JavaDoc();
86         ptmap.put("a", PTypeSpace.INT);
87         ptmap.put("azerty", PTypeSpace.INT);
88         ParameterTypeProvider ptp = new MapParameterTypeProvider(ptmap);
89
90         try {
91             ExpressionParser parser = new ExpressionParser();
92             Expression expr;
93             expr = parser.parse("1", null);
94             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand((byte) 1), expr, logger);
95             expr = parser.parse("12", null);
96             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand((byte) 12), expr, logger);
97             expr = parser.parse("\"azerty\"", null);
98             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand("azerty"), expr, logger);
99             expr = parser.parse("\"\"", null);
100             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand(""), expr, logger);
101             expr = parser.parse("\"a\"", null);
102             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand("a"), expr, logger);
103             expr = parser.parse("a", ptp);
104             TestExpressionHelper.equals("Wrong canonical expr", new BasicParameterOperand(PTypeSpace.INT, "a"), expr, logger);
105             expr = parser.parse("azerty", ptp);
106             TestExpressionHelper.equals("Wrong canonical expr", new BasicParameterOperand(PTypeSpace.INT, "azerty"), expr, logger);
107             expr = parser.parse("(1)", null);
108             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand((byte) 1), expr, logger);
109             expr = parser.parse("(12)", null);
110             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand((byte) 12), expr, logger);
111             expr = parser.parse("(\"azerty\")", null);
112             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand("azerty"), expr, logger);
113             expr = parser.parse("(\"\")", null);
114             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand(""), expr, logger);
115             expr = parser.parse("(\"a\")", null);
116             TestExpressionHelper.equals("Wrong canonical expr", new BasicOperand("a"), expr, logger);
117             expr = parser.parse("(a)", ptp);
118             TestExpressionHelper.equals("Wrong canonical expr", new BasicParameterOperand(PTypeSpace.INT, "a"), expr, logger);
119             expr = parser.parse("(azerty)", ptp);
120             TestExpressionHelper.equals("Wrong canonical expr", new BasicParameterOperand(PTypeSpace.INT, "azerty"), expr, logger);
121             expr = parser.parse("((azerty))", ptp);
122             TestExpressionHelper.equals("Wrong canonical expr", new BasicParameterOperand(PTypeSpace.INT, "azerty"), expr, logger);
123         } catch (Exception JavaDoc e) {
124             e.printStackTrace();
125             fail(e.getMessage());
126         }
127     }
128
129     public void testArithmExpr() {
130         try {
131             ExpressionParser parser = new ExpressionParser();
132             Expression expr;
133             expr = parser.parse("1 + 2", null);
134             TestExpressionHelper.equals("Wrong canonical expr",
135                     new Plus(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
136                     expr, logger);
137             expr = parser.parse("1 - 2", null);
138             TestExpressionHelper.equals("Wrong canonical expr",
139                     new Minus(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
140                     expr, logger);
141             expr = parser.parse("1 * 2", null);
142             TestExpressionHelper.equals("Wrong canonical expr",
143                     new Mult(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
144                     expr, logger);
145             expr = parser.parse("1 / 2", null);
146             TestExpressionHelper.equals("Wrong canonical expr",
147                     new DivideBy(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
148                     expr, logger);
149             expr = parser.parse("1 % 2", null);
150             TestExpressionHelper.equals("Wrong canonical expr",
151                     new Mod(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
152                     expr, logger);
153             expr = parser.parse("1 & 2", null);
154             TestExpressionHelper.equals("Wrong canonical expr",
155                     new And(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
156                     expr, logger);
157             expr = parser.parse("1 | 2", null);
158             TestExpressionHelper.equals("Wrong canonical expr",
159                     new Or(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
160                     expr, logger);
161             expr = parser.parse("1 + 2 + 3", null);
162             TestExpressionHelper.equals("Wrong canonical expr",
163                     new Plus(new BasicOperand((byte) 1),
164                             new Plus(new BasicOperand((byte) 2),
165                                     new BasicOperand((byte) 3))),
166                     expr, logger);
167             expr = parser.parse("1 + 2 - 3", null);
168             TestExpressionHelper.equals("Wrong canonical expr",
169                     new Plus(new BasicOperand((byte) 1),
170                             new Minus(new BasicOperand((byte) 2),
171                                     new BasicOperand((byte) 3))),
172                     expr, logger);
173             expr = parser.parse("1 * 2 / 3", null);
174             TestExpressionHelper.equals("Wrong canonical expr",
175                     new Mult(new BasicOperand((byte) 1),
176                             new DivideBy(new BasicOperand((byte) 2),
177                                     new BasicOperand((byte) 3))),
178                     expr, logger);
179             expr = parser.parse("1 + 2 * 3", null);
180             TestExpressionHelper.equals("Wrong canonical expr",
181                     new Plus(new BasicOperand((byte) 1),
182                             new Mult(new BasicOperand((byte) 2),
183                                     new BasicOperand((byte) 3))),
184                     expr, logger);
185             expr = parser.parse("1 * 2 + 3", null);
186             TestExpressionHelper.equals("Wrong canonical expr",
187                     new Plus(new Mult(new BasicOperand((byte) 1),
188                                 new BasicOperand((byte) 2)),
189                             new BasicOperand((byte) 3)),
190                     expr, logger);
191             expr = parser.parse("(1 + 2)", null);
192             TestExpressionHelper.equals("Wrong canonical expr",
193                     new Plus(new BasicOperand((byte) 1), new BasicOperand((byte) 2)),
194                     expr, logger);
195             expr = parser.parse("(1 + 2) + 3", null);
196             TestExpressionHelper.equals("Wrong canonical expr",
197                     new Plus(new Plus(new BasicOperand((byte) 1),
198                                 new BasicOperand((byte) 2)),
199                             new BasicOperand((byte) 3)),
200                     expr, logger);
201             expr = parser.parse("1 + (2 + 3)", null);
202             TestExpressionHelper.equals("Wrong canonical expr",
203                     new Plus(new BasicOperand((byte) 1),
204                             new Plus(new BasicOperand((byte) 2),
205                                     new BasicOperand((byte) 3))),
206                     expr, logger);
207             expr = parser.parse("(1 + 2) * (3 + 4)", null);
208             TestExpressionHelper.equals("Wrong canonical expr",
209                     new Mult(new Plus(new BasicOperand((byte) 1),
210                                 new BasicOperand((byte) 2)),
211                             new Plus(new BasicOperand((byte) 3),
212                                     new BasicOperand((byte) 4))),
213                     expr, logger);
214             expr = parser.parse("(1 + 2) * 3 + 4", null);
215             TestExpressionHelper.equals("Wrong canonical expr",
216                     new Plus(new Mult(
217                                 new Plus(new BasicOperand((byte) 1),
218                                     new BasicOperand((byte) 2)),
219                                 new BasicOperand((byte) 3)),
220                             new BasicOperand((byte) 4)),
221                     expr, logger);
222
223             expr = parser.parse("(\"toto\" + 2)", null);
224             TestExpressionHelper.equals("Wrong canonical expr",
225                 new Concat(new BasicOperand("toto"),
226                     new TypeConverter(
227                         new BasicOperand((byte) 2), PTypeSpace.STRING)),
228                 expr, logger);
229             expr = parser.parse("(\"toto\" + \"titi\")", null);
230             TestExpressionHelper.equals("Wrong canonical expr",
231                 new Concat(new BasicOperand("toto"), new BasicOperand("titi")),
232                 expr, logger);
233             expr = parser.parse("((\"toto\" + 2) + 3)", null);
234             TestExpressionHelper.equals("Wrong canonical expr",
235                 new Concat(
236                     new Concat(new BasicOperand("toto"),
237                         new TypeConverter(
238                             new BasicOperand((byte) 2), PTypeSpace.STRING)),
239                     new TypeConverter(
240                             new BasicOperand((byte) 3), PTypeSpace.STRING)),
241                 expr, logger);
242         } catch (Exception JavaDoc e) {
243             e.printStackTrace();
244             fail(e.getMessage());
245         }
246     }
247
248     public void testCompExpr() {
249         try {
250             ExpressionParser parser = new ExpressionParser();
251             Expression expr;
252             Map JavaDoc ptmap = new HashMap JavaDoc();
253             ptmap.put("a", PTypeSpace.INT);
254             ptmap.put("b", PTypeSpace.INT);
255             ptmap.put("c", PTypeSpace.STRING);
256             ParameterTypeProvider ptprov =
257                     new MapParameterTypeProvider(ptmap);
258             expr = parser.parse("c == \"toto\"", ptprov);
259             TestExpressionHelper.equals("Wrong canonical expr",
260                     new Equal(new BasicParameterOperand(PTypeSpace.STRING, "c"),
261                         new BasicOperand("toto")), expr, logger);
262             expr = parser.parse("a == 2", ptprov);
263             TestExpressionHelper.equals("Wrong canonical expr",
264                     new Equal(new BasicParameterOperand(PTypeSpace.INT, "a"),
265                         new BasicOperand((byte) 2)), expr, logger);
266             expr = parser.parse("a != 2", ptprov);
267             TestExpressionHelper.equals("Wrong canonical expr",
268                     new NotEqual(new BasicParameterOperand(PTypeSpace.INT, "a"),
269                         new BasicOperand((byte) 2)), expr, logger);
270             expr = parser.parse("a < 2", ptprov);
271             TestExpressionHelper.equals("Wrong canonical expr",
272                     new Lower(new BasicParameterOperand(PTypeSpace.INT, "a"),
273                         new BasicOperand((byte) 2)), expr, logger);
274             expr = parser.parse("a <= 2", ptprov);
275             TestExpressionHelper.equals("Wrong canonical expr",
276                     new LowerEqual(new BasicParameterOperand(PTypeSpace.INT, "a"),
277                         new BasicOperand((byte) 2)), expr, logger);
278             expr = parser.parse("a > 2", ptprov);
279             TestExpressionHelper.equals("Wrong canonical expr",
280                     new Greater(new BasicParameterOperand(PTypeSpace.INT, "a"),
281                         new BasicOperand((byte) 2)), expr, logger);
282             expr = parser.parse("a >= 2", ptprov);
283             TestExpressionHelper.equals("Wrong canonical expr",
284                     new GreaterEqual(new BasicParameterOperand(PTypeSpace.INT, "a"),
285                         new BasicOperand((byte) 2)), expr, logger);
286             expr = parser.parse("a + 1 == b - 2", ptprov);
287             TestExpressionHelper.equals("Wrong canonical expr",
288                     new Equal(
289                             new Plus(new BasicParameterOperand(PTypeSpace.INT, "a"),
290                                     new BasicOperand((byte) 1)),
291                             new Minus(new BasicParameterOperand(PTypeSpace.INT, "b"),
292                                     new BasicOperand((byte) 2))
293                     ),
294                     expr, logger);
295             expr = parser.parse("(a + 1 == b - 2) && a - 4 != b * 2", ptprov);
296             TestExpressionHelper.equals("Wrong canonical expr",
297                     new ConditionalAnd(
298                         new Equal(
299                                 new Plus(new BasicParameterOperand(PTypeSpace.INT, "a"),
300                                         new BasicOperand((byte) 1)),
301                                 new Minus(new BasicParameterOperand(PTypeSpace.INT, "b"),
302                                         new BasicOperand((byte) 2))
303                         ),
304                         new NotEqual(
305                                 new Minus(new BasicParameterOperand(PTypeSpace.INT, "a"),
306                                         new BasicOperand((byte) 4)),
307                                 new Mult(new BasicParameterOperand(PTypeSpace.INT, "b"),
308                                         new BasicOperand((byte) 2))
309                         )),
310                     expr, logger);
311         } catch (Exception JavaDoc e) {
312             e.printStackTrace();
313             fail(e.getMessage());
314         }
315     }
316 }
317
Popular Tags