KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package org.objectweb.medor.optim;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import junit.textui.TestRunner;
29 import org.objectweb.medor.datasource.api.DataStore;
30 import org.objectweb.medor.datasource.lib.BasicDataStore;
31 import org.objectweb.medor.filter.api.FieldOperand;
32 import org.objectweb.medor.expression.lib.And;
33 import org.objectweb.medor.filter.lib.BasicFieldOperand;
34 import org.objectweb.medor.expression.lib.Equal;
35 import org.objectweb.medor.expression.lib.Greater;
36 import org.objectweb.medor.expression.lib.Lower;
37 import org.objectweb.medor.expression.lib.BasicOperand;
38 import org.objectweb.medor.expression.api.Expression;
39 import org.objectweb.medor.filter.lib.MemberOf;
40 import org.objectweb.medor.lib.Log;
41 import org.objectweb.medor.lib.TestMedorHelper;
42 import org.objectweb.medor.optim.api.RewriteRule;
43 import org.objectweb.medor.optim.rdb.GroupSameDBRule;
44 import org.objectweb.medor.query.api.QueryTreeField;
45 import org.objectweb.medor.query.lib.JoinProject;
46 import org.objectweb.medor.query.lib.QueryTreePrinter;
47 import org.objectweb.medor.query.rdb.api.QualifiedTable;
48 import org.objectweb.medor.query.rdb.api.RdbExpField;
49 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
50 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable;
51 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf;
52 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
53 import org.objectweb.util.monolog.api.BasicLevel;
54
55 import java.util.ArrayList JavaDoc;
56
57 /**
58  * It tests the rule DropUselessNodeRule.
59  *
60  * @author S.Chassande-Barrioz
61  */

62 public class TestGroupSameDBRule extends TestMedorHelper {
63
64     /**
65      * main method to launch the tests manually
66      */

67     public static void main(String JavaDoc[] args) {
68         TestRunner.run(suite());
69     }
70
71     /**
72      * This method creates a TestSuite object with the current tests
73      */

74     public static Test suite() {
75         return new TestSuite(TestGroupSameDBRule.class);
76     }
77
78     public TestGroupSameDBRule() {
79         super("TestGroupSameDBRule", Log.MEDOR_PREFIX + ".optim.groupsamedb");
80     }
81
82     public TestGroupSameDBRule(String JavaDoc testName) {
83         super(testName, Log.MEDOR_PREFIX + ".optim.groupsamedb");
84     }
85
86     protected void setUp() {
87     }
88
89     protected void tearDown() {
90     }
91
92     public void testA() {
93         try {
94             logger.log(BasicLevel.DEBUG, "Entering testA");
95             //create qualified tables
96
QualifiedTable t1 = new BasicQualifiedTable("t1", null);
97             QualifiedTable t2 = new BasicQualifiedTable("t2", "al2");
98             QualifiedTable[] tables1 = {t1, t2};
99             // Create a DataStore
100
DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "RDB");
101             //create a new leaf
102
RdbExpQueryLeaf ql1 =
103                 new BasicRdbExpQueryLeaf(ds, tables1, "" + "leaf1");
104             //add fields
105
RdbExpField f1 =
106                 ql1.addRdbField("f1", PTypeSpaceMedor.INT, "col1", t1);
107             RdbExpField f2 =
108                 ql1.addRdbField("f2", PTypeSpaceMedor.STRING, "col2", t2);
109             //create a filter to be added to the query leaf
110
FieldOperand fieldOp1 = new BasicFieldOperand(f1);
111             FieldOperand fieldOp2 = new BasicFieldOperand(f2);
112             BasicOperand valueL = new BasicOperand(1013);
113             BasicOperand valueR = new BasicOperand("coucou");
114             Expression left = new Lower(fieldOp1, valueL);
115             Expression right = new Greater(fieldOp2, valueR);
116             Expression filter1 = new And(left, right);
117             //assigne the filter to the query leaf
118
ql1.setQueryFilter(filter1);
119             // Create a second RdbExpQueryLeaf
120
QualifiedTable[] tables2 = {t2, t1};
121             RdbExpQueryLeaf ql2 =
122                 new BasicRdbExpQueryLeaf(ds, tables2, "leaf2");
123             RdbExpField f3 =
124                 ql2.addRdbField("f3", PTypeSpaceMedor.STRING, "col3", t2);
125             RdbExpField f4 =
126                 ql2.addRdbField("f4", PTypeSpaceMedor.SHORT, "col4", t1);
127             //create a filter to be added to the query leaf
128
FieldOperand fieldOp3 = new BasicFieldOperand(f3);
129             BasicOperand valueR1 = new BasicOperand("ayemayema");
130             Expression filter2 = new Equal(fieldOp3, valueR1);
131             ql2.setQueryFilter(filter2);
132             // Create a JOIN PROJECT Operation
133
JoinProject jp = new JoinProject("join");
134             jp.addPropagatedField("f5", PTypeSpaceMedor.INT,
135                 new QueryTreeField[]{f1});
136             jp.addPropagatedField("f6", PTypeSpaceMedor.STRING,
137                 new QueryTreeField[]{f2, f3});
138             jp.addPropagatedField("f7", PTypeSpaceMedor.SHORT,
139                 new QueryTreeField[]{f4});
140             // Create a QueryFilter
141
FieldOperand fieldOp4 = new BasicFieldOperand(f2);
142             FieldOperand fieldOp5 = new BasicFieldOperand(f3);
143             Expression filter3 = new Equal(fieldOp4, fieldOp5);
144             jp.setQueryFilter(filter3);
145             logger.log(BasicLevel.DEBUG, "Original tree:");
146             QueryTreePrinter.printQueryTree(jp, logger);
147             logger.log(BasicLevel.DEBUG,
148                     ql1.getSqlRequest(null, false, false));
149             logger.log(BasicLevel.DEBUG,
150                     ql2.getSqlRequest(null, false, false));
151
152             RewriteRule gqr = new GroupSameDBRule();
153             RdbExpQueryLeaf rewrittenQuery =
154                 (RdbExpQueryLeaf) (gqr.rewrite(jp));
155             logger.log(BasicLevel.DEBUG, "Rewritten tree:");
156             QueryTreePrinter.printQueryTree(rewrittenQuery, logger);
157
158             logger.log(BasicLevel.DEBUG,
159                     rewrittenQuery.getSqlRequest(null, false, false));
160         }
161         catch (Exception JavaDoc e) {
162             e.printStackTrace();
163             fail("fail to create the request" + e.getMessage());
164         }
165     }
166
167     /**
168      * testB tests a join containing a MemberOf operator
169      */

170     public void testMemberOf1() {
171         try {
172             logger.log(BasicLevel.DEBUG, "Entering testMemberOf1");
173             //create qualified tables
174
QualifiedTable t1 = new BasicQualifiedTable("t1", null);
175             QualifiedTable t2 = new BasicQualifiedTable("t2", null);
176             QualifiedTable[] tables1 = {t1};
177             // Create a DataStore
178
DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "RDB");
179             //create a new leaf
180
RdbExpQueryLeaf ql1 =
181                 new BasicRdbExpQueryLeaf(ds, tables1, "leaf1");
182             //add fields
183
RdbExpField f1 =
184                 ql1.addRdbField("f1", PTypeSpaceMedor.INT, "col1", t1);
185             RdbExpField f2 =
186                 ql1.addRdbField("f2", PTypeSpaceMedor.STRING, "col2", t1);
187             // Create a second RdbExpQueryLeaf
188
QualifiedTable[] tables2 = {t2};
189             RdbExpQueryLeaf ql2 =
190                 new BasicRdbExpQueryLeaf(ds, tables2, "leaf2");
191             RdbExpField f3 =
192                 ql2.addRdbField("f3", PTypeSpaceMedor.STRING, "col3", t2);
193             // Create a JOIN PROJECT Operation
194
JoinProject jp = new JoinProject("join");
195             jp.addPropagatedField("f5", PTypeSpaceMedor.INT,
196                 new QueryTreeField[]{f1});
197             // Create a QueryFilter
198
FieldOperand fieldOp4 = new BasicFieldOperand(f2);
199             FieldOperand fieldOp5 = new BasicFieldOperand(f3);
200             ArrayList JavaDoc list1 = new ArrayList JavaDoc(1);
201             ArrayList JavaDoc list2 = new ArrayList JavaDoc(1);
202             list1.add(fieldOp4);
203             list2.add(fieldOp5);
204             Expression filter3 = new MemberOf(list1, list2);
205             jp.setQueryFilter(filter3);
206             logger.log(BasicLevel.DEBUG, "Original tree:");
207             QueryTreePrinter.printQueryTree(jp, logger);
208             logger.log(BasicLevel.DEBUG, ql1.getSqlRequest(null, false, false));
209             logger.log(BasicLevel.DEBUG, ql2.getSqlRequest(null, false, false));
210             RewriteRule gqr = new GroupSameDBRule();
211             RdbExpQueryLeaf rewrittenQuery =
212                 (RdbExpQueryLeaf) (gqr.rewrite(jp));
213             logger.log(BasicLevel.DEBUG, "Rewritten tree:");
214             QueryTreePrinter.printQueryTree(rewrittenQuery, logger);
215
216             logger.log(BasicLevel.DEBUG, rewrittenQuery.getSqlRequest(null,
217                     false, false));
218         }
219         catch (Exception JavaDoc e) {
220             e.printStackTrace();
221             fail("fail to create the request" + e.getMessage());
222         }
223     }
224
225     public void testMemberOf2() {
226         try {
227             logger.log(BasicLevel.DEBUG, "Entering testMemberOf2");
228             //create qualified tables
229
QualifiedTable t1 = new BasicQualifiedTable("t1", null);
230             QualifiedTable t2 = new BasicQualifiedTable("t2", null);
231             QualifiedTable t3 = new BasicQualifiedTable("t3", null);
232             QualifiedTable[] tables1 = {t1};
233             // Create a DataStore
234
DataStore ds = new BasicDataStore(DataStore.JDBC_STORE, "RDB");
235             //create a new leaf
236
RdbExpQueryLeaf ql1 =
237                 new BasicRdbExpQueryLeaf(ds, tables1, "leaf1");
238             //add fields
239
RdbExpField f11 =
240                 ql1.addRdbField("f11", PTypeSpaceMedor.INT, "col1", t1);
241             RdbExpField f12 =
242                 ql1.addRdbField("f12", PTypeSpaceMedor.STRING, "col2", t1);
243             RdbExpField f13 =
244                 ql1.addRdbField("f13", PTypeSpaceMedor.STRING, "col3", t1);
245             // Create a second RdbExpQueryLeaf
246
QualifiedTable[] tables2 = {t2};
247             RdbExpQueryLeaf ql2 =
248                 new BasicRdbExpQueryLeaf(ds, tables2, "leaf2");
249             RdbExpField f23 =
250                 ql2.addRdbField("f23", PTypeSpaceMedor.STRING, "col3", t2);
251             // Create a third RdbExpQueryLeaf
252
QualifiedTable[] tables3 = {t3};
253             RdbExpQueryLeaf ql3 =
254                 new BasicRdbExpQueryLeaf(ds, tables3, "leaf3");
255             RdbExpField f31 =
256                 ql3.addRdbField("f31", PTypeSpaceMedor.STRING, "col3", t3);
257             // Create a JOIN PROJECT Operation
258
JoinProject jp = new JoinProject("join");
259             jp.addPropagatedField("f5", PTypeSpaceMedor.INT,
260                 new QueryTreeField[]{f11});
261             // Create a QueryFilter
262
FieldOperand fieldOp1 = new BasicFieldOperand(f12);
263             FieldOperand fieldOp2 = new BasicFieldOperand(f23);
264             ArrayList JavaDoc list1 = new ArrayList JavaDoc(1);
265             ArrayList JavaDoc list2 = new ArrayList JavaDoc(1);
266             list1.add(fieldOp1);
267             list2.add(fieldOp2);
268             Expression filter1 = new MemberOf(list1, list2);
269             // Create a QueryFilter
270
FieldOperand fieldOp3 = new BasicFieldOperand(f13);
271             FieldOperand fieldOp4 = new BasicFieldOperand(f31);
272             ArrayList JavaDoc list3 = new ArrayList JavaDoc(1);
273             ArrayList JavaDoc list4 = new ArrayList JavaDoc(1);
274             list3.add(fieldOp3);
275             list4.add(fieldOp4);
276             Expression filter2 = new MemberOf(list3, list4);
277             jp.setQueryFilter(new And(filter1, filter2));
278             logger.log(BasicLevel.DEBUG, "Original tree:");
279             QueryTreePrinter.printQueryTree(jp, logger);
280             logger.log(BasicLevel.DEBUG, ql1.getSqlRequest(null, false, false));
281             logger.log(BasicLevel.DEBUG, ql2.getSqlRequest(null, false, false));
282             logger.log(BasicLevel.DEBUG, ql3.getSqlRequest(null, false, false));
283             RewriteRule gqr = new GroupSameDBRule();
284             RdbExpQueryLeaf rewrittenQuery =
285                 (RdbExpQueryLeaf) (gqr.rewrite(jp));
286             logger.log(BasicLevel.DEBUG, "Rewritten tree:");
287             QueryTreePrinter.printQueryTree(rewrittenQuery, logger);
288             logger.log(BasicLevel.DEBUG, rewrittenQuery.getSqlRequest(null,
289                     false, false));
290         }
291         catch (Exception JavaDoc e) {
292             e.printStackTrace();
293             fail("fail to create the request" + e.getMessage());
294         }
295     }
296 }
297
Popular Tags