KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > TestPushSelectionRule


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, S. Chassande-Barrioz, A. Lefebvre
22  */

23 package org.objectweb.medor.optim;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28 import org.objectweb.jorm.type.api.PTypeSpace;
29 import org.objectweb.medor.api.MedorException;
30 import org.objectweb.medor.filter.TestExpressionHelper;
31 import org.objectweb.medor.expression.lib.And;
32 import org.objectweb.medor.filter.lib.BasicFieldOperand;
33 import org.objectweb.medor.expression.lib.Equal;
34 import org.objectweb.medor.filter.lib.ExpressionPrinter;
35 import org.objectweb.medor.expression.lib.Greater;
36 import org.objectweb.medor.expression.lib.LowerEqual;
37 import org.objectweb.medor.expression.lib.Or;
38 import org.objectweb.medor.lib.Log;
39 import org.objectweb.medor.lib.TestMedorHelper;
40 import org.objectweb.medor.optim.lib.PushSelectionRule;
41 import org.objectweb.medor.query.api.QueryNode;
42 import org.objectweb.medor.query.api.QueryTreeField;
43 import org.objectweb.medor.query.lib.JoinProject;
44 import org.objectweb.medor.query.rdb.api.QualifiedTable;
45 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable;
46 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf;
47 import org.objectweb.medor.expression.lib.BasicOperand;
48 import org.objectweb.medor.expression.api.Expression;
49 import org.objectweb.util.monolog.api.BasicLevel;
50
51 /**
52  * This JUnit test permits to check the PushSelectionRule class.
53  *
54  * @author S.Chassande-Barrioz
55  */

56 public class TestPushSelectionRule extends TestMedorHelper {
57
58     /**
59      * main method to launch the tests manually
60      */

61     public static void main(String JavaDoc[] args) {
62         TestRunner.run(suite());
63     }
64
65     /**
66      * This method creates a TestSuite object with the current tests
67      */

68     public static Test suite() {
69         return new TestSuite(TestPushSelectionRule.class);
70     }
71
72     public TestPushSelectionRule() {
73         super("TestPushSelectionRule", Log.MEDOR_PREFIX + ".optim.pushselection");
74     }
75
76     public TestPushSelectionRule(String JavaDoc testName) {
77         super(testName, Log.MEDOR_PREFIX + ".optim.pushselection");
78     }
79
80
81     protected void setUp() {
82     }
83
84     protected void tearDown() {
85     }
86
87     public void testComplex() {
88         QueryTreeField fa1 = null,
89             fa2 = null,
90             fb1 = null,
91             fb2 = null,
92             fc1 = null,
93             fc2 = null,
94             fd1 = null,
95             fe1 = null,
96             fe2 = null;
97         QueryNode qn1 = null, root = null;
98         BasicRdbExpQueryLeaf ql1 = null, ql2 = null, ql3 = null;
99         try {
100             QualifiedTable t1 = new BasicQualifiedTable("Table_1", "t1");
101             QualifiedTable t2 = new BasicQualifiedTable("Table_2", "t2");
102             QualifiedTable t3 = new BasicQualifiedTable("Table_3", "t3");
103             ql1 = new BasicRdbExpQueryLeaf(null, new QualifiedTable[] {t1}, "");
104             logger.log(BasicLevel.DEBUG, "ql1="+ql1);
105             fa1 = ql1.addRdbField("a1", PTypeSpace.LONG, "cola", t1);
106
107             ql2 = new BasicRdbExpQueryLeaf(null, new QualifiedTable[] {t2}, "");
108             logger.log(BasicLevel.DEBUG, "ql2="+ql2);
109             fb1 = ql2.addRdbField("b1", PTypeSpace.LONG, "colb", t2);
110             fc1 = ql2.addRdbField("c1", PTypeSpace.LONG, "colc", t2);
111             fe1 = ql2.addRdbField("e1", PTypeSpace.LONG, "cole", t2);
112
113             ql3 = new BasicRdbExpQueryLeaf(null, new QualifiedTable[] {t3}, "");
114             logger.log(BasicLevel.DEBUG, "ql3="+ql3);
115             fd1 = ql3.addRdbField("d1", PTypeSpace.LONG, "cold", t3);
116
117             qn1 = new JoinProject("");
118             logger.log(BasicLevel.DEBUG, "qn1="+qn1);
119             fa2 = qn1.addPropagatedField(
120                 "a2", PTypeSpace.LONG, new QueryTreeField[]{fa1});
121             fb2 = qn1.addPropagatedField(
122                 "b2", PTypeSpace.LONG, new QueryTreeField[]{fb1});
123             fc2 = qn1.addPropagatedField(
124                 "c2", PTypeSpace.LONG, new QueryTreeField[]{fc1});
125             fe2 = qn1.addPropagatedField(
126                 "e2", PTypeSpace.LONG, new QueryTreeField[]{fe1});
127
128             root = new JoinProject("");
129             root.addPropagatedField("a3", PTypeSpace.LONG, new QueryTreeField[]{fa2});
130             root.addPropagatedField("b3", PTypeSpace.LONG, new QueryTreeField[]{fb2});
131             root.addPropagatedField("c3", PTypeSpace.LONG, new QueryTreeField[]{fc2});
132             root.addPropagatedField("d2", PTypeSpace.LONG, new QueryTreeField[]{fd1});
133             root.addPropagatedField("e3", PTypeSpace.LONG, new QueryTreeField[]{fe2});
134         }
135         catch (MedorException ex) {
136             ex.printStackTrace();
137             fail("Impossible to build the tree: " + ex.getMessage());
138         }
139         PushSelectionRule psr = new PushSelectionRule();
140         PushSelectionRule.SameQT sqt = null;
141         try {
142             //((d2<=3) | (b2>2 & b2=c2)) & ((e2=3 | a2>b2) & a2=12)
143
Expression e = null, e2 = null, e3 = null;
144             e = new BasicFieldOperand(fd1);
145             sqt = psr.isSameQT(e);
146             assertTrue("verify isSame d1", sqt.isSame);
147             assertEquals("verify qt d1", ql3, sqt.qt);
148
149             e2 = new BasicOperand((long) 3);
150             sqt = psr.isSameQT(e2);
151             assertTrue("verify isSame 3", sqt.isSame);
152             assertNull("verify qt 3", sqt.qt);
153
154             e = new LowerEqual(e, e2);
155             sqt = psr.isSameQT(e);
156             assertTrue("verify isSame d1<=3", sqt.isSame);
157             assertEquals("verify qt d1<=3", ql3, sqt.qt);
158
159             e2 = new Greater(new BasicFieldOperand(fb2), new BasicOperand((long) 2));
160             sqt = psr.isSameQT(e2);
161             assertTrue("verify isSame b2>2", sqt.isSame);
162             assertEquals("verify qt b2>2", qn1, sqt.qt);
163             e3 = new Equal(new BasicFieldOperand(fb2), new BasicFieldOperand(fc2));
164             sqt = psr.isSameQT(e3);
165             assertTrue("verify isSame b2==c2", sqt.isSame);
166             assertEquals("verify qt b2==c2", qn1, sqt.qt);
167             e2 = new And(e2, e3);
168             sqt = psr.isSameQT(e2);
169             assertTrue("verify isSame b2>2 & b2=c2", sqt.isSame);
170             assertEquals("verify qt b2>2 & b2=c2", qn1, sqt.qt);
171             e = new Or(e, e2);
172             sqt = psr.isSameQT(e);
173             assertTrue("verify isSame (d1<=3) | (b2>2 & b2=c2)", !sqt.isSame);
174
175             e2 = new Equal(new BasicFieldOperand(fe2), new BasicOperand((long) 3));
176             sqt = psr.isSameQT(e2);
177             assertTrue("verify isSame e2=3", sqt.isSame);
178             assertEquals("verify qt e2=3", qn1, sqt.qt);
179
180             e3 = new Greater(new BasicFieldOperand(fa2), new BasicFieldOperand(fb2));
181             sqt = psr.isSameQT(e3);
182             assertTrue("verify isSame a2>b2", sqt.isSame);
183             assertEquals("verify qt a2>b2", qn1, sqt.qt);
184
185             e2 = new Or(e2, e3);
186             sqt = psr.isSameQT(e2);
187             assertTrue("verify isSame e2=3 | a2>b2", sqt.isSame);
188             assertEquals("verify qt e2=3 | a2>b2", qn1, sqt.qt);
189
190             e3 = new Equal(new BasicFieldOperand(fa2), new BasicOperand((long) 12));
191             sqt = psr.isSameQT(e3);
192             assertTrue("verify isSame a2=12", sqt.isSame);
193             assertEquals("verify qt a2=12", qn1, sqt.qt);
194
195             e2 = new And(e2, e3);
196             sqt = psr.isSameQT(e2);
197             assertTrue("verify isSame (e2=3 | a2>b2) & a2=12", sqt.isSame);
198             assertEquals("verify qt (e2=3 | a2>b2) & a2=12", qn1, sqt.qt);
199
200             e = new And(e, e2);
201             root.setQueryFilter(e);
202             sqt = psr.isSameQT(e);
203             assertTrue(
204                 "verify isSame ((d1<=3) | (b2>2 & b2=c2)) & ((e2=3 | a2>b2) & a2=12)",
205                 !sqt.isSame);
206
207             // b1>3
208
e = new Greater(new BasicFieldOperand(fb1), new BasicOperand((long) 3));
209             qn1.setQueryFilter(e);
210             sqt = psr.isSameQT(e);
211             assertTrue("verify isSame b>3", sqt.isSame);
212             assertEquals("verify qt b1>3", ql2, sqt.qt);
213
214             logger.log(BasicLevel.DEBUG, "root.filter=" + e2Str(root.getQueryFilter()));
215             logger.log(BasicLevel.DEBUG, "qn1.filter=" + e2Str(qn1.getQueryFilter()));
216             logger.log(BasicLevel.DEBUG, "ql1.filter=" + e2Str(ql1.getQueryFilter()));
217             logger.log(BasicLevel.DEBUG, "ql2.filter=" + e2Str(ql2.getQueryFilter()));
218             logger.log(BasicLevel.DEBUG, "ql3.filter=" + e2Str(ql3.getQueryFilter()));
219             psr.rewrite(root);
220             logger.log(BasicLevel.DEBUG, "root.filter=" + e2Str(root.getQueryFilter()));
221             logger.log(BasicLevel.DEBUG, "qn1.filter=" + e2Str(qn1.getQueryFilter()));
222             logger.log(BasicLevel.DEBUG, "ql1.filter=" + e2Str(ql1.getQueryFilter()));
223             logger.log(BasicLevel.DEBUG, "ql2.filter=" + e2Str(ql2.getQueryFilter()));
224             logger.log(BasicLevel.DEBUG, "ql3.filter=" + e2Str(ql3.getQueryFilter()));
225
226             //Check root filter: d1<=3 | b2>2 & b2==c2
227
//========================================
228
Expression compare= null;
229             e2 = new LowerEqual(
230                 new BasicFieldOperand(fd1), new BasicOperand((long) 3));
231             e3 = new And(
232                 new Greater(new BasicFieldOperand(fb2), new BasicOperand((long) 2)),
233                 new Equal(new BasicFieldOperand(fb2), new BasicFieldOperand(fc2))
234                 );
235             compare = new Or(e2, e3);
236
237             try {
238                 TestExpressionHelper.equals("root.filter checking :",
239                     root.getQueryFilter(), compare, logger);
240             }
241             catch (MedorException ex) {
242                 fail("root.filter checking fail: " + ex.getMessage());
243             }
244
245             // Check middle.filter: e1=3 | a1>b1
246
//==================================
247
compare = new Or(
248                 new Equal(new BasicFieldOperand(fe1), new BasicOperand((long) 3)),
249                 new Greater(new BasicFieldOperand(fa1), new BasicFieldOperand(fb1)));
250
251             try {
252                 TestExpressionHelper.equals("qn1.filter checking :",
253                     qn1.getQueryFilter(), compare, logger);
254             }
255             catch (MedorException ex) {
256                 fail("qn1.filter checking fail: " + ex.getMessage());
257             }
258
259             // Check ql1.filter: a1 = 12
260
//==========================
261
compare = new Equal(
262                 new BasicFieldOperand(fa1),
263                 new BasicOperand((long) 12));
264             try {
265                 TestExpressionHelper.equals("ql1.filter checking :",
266                     ql1.getQueryFilter(), compare, logger);
267             }
268             catch (MedorException ex) {
269                 fail("ql1.filter checking fail: " + ex.getMessage());
270             }
271
272             // Check ql2.filter b > 3
273
//=======================
274
compare = new Greater(
275                 new BasicFieldOperand(fb1),
276                 new BasicOperand((long) 3));
277             try {
278                 TestExpressionHelper.equals("ql2.filter checking :",
279                     ql2.getQueryFilter(), compare, logger);
280             }
281             catch (MedorException ex) {
282                 fail("ql2.filter checking fail: " + ex.getMessage());
283             }
284
285             // Check ql3.filter: nothing
286
//=======================
287
try {
288                 TestExpressionHelper.equals(
289                     "ql3.filter checking :", ql3.getQueryFilter(), null,
290                     logger);
291             }
292             catch (MedorException ex) {
293                 fail("ql3.filter checking fail: " + ex.getMessage());
294             }
295         }
296         catch (MedorException ex) {
297             ex.printStackTrace();
298             fail("Impossible to do isSameQT: " + ex.getMessage());
299         }
300     }
301
302     private static final String JavaDoc e2Str(Expression e) {
303         return ExpressionPrinter.e2str(e);
304     }
305 }
306
Popular Tags