KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > TestNest


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.query;
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.Avg;
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.lib.Nest;
48 import org.objectweb.medor.query.rdb.api.QualifiedTable;
49 import org.objectweb.medor.query.rdb.api.RdbExpField;
50 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
51 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable;
52 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf;
53 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
54 import org.objectweb.util.monolog.api.BasicLevel;
55
56 /**
57  * It tests the rule DropUselessNodeRule.
58  *
59  * @author S.Chassande-Barrioz
60  */

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

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

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