KickJava   Java API By Example, From Geeks To Geeks.

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


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.expression.lib.And;
31 import org.objectweb.medor.filter.lib.BasicFieldOperand;
32 import org.objectweb.medor.expression.lib.Equal;
33 import org.objectweb.medor.lib.Log;
34 import org.objectweb.medor.lib.TestMedorHelper;
35 import org.objectweb.medor.optim.lib.DropUselessNodeRule;
36 import org.objectweb.medor.query.TestQueryTreeHelper;
37 import org.objectweb.medor.query.api.QueryNode;
38 import org.objectweb.medor.query.api.QueryTree;
39 import org.objectweb.medor.query.api.QueryTreeField;
40 import org.objectweb.medor.query.lib.JoinProject;
41 import org.objectweb.medor.query.lib.Project;
42 import org.objectweb.medor.query.lib.QueryTreePrinter;
43 import org.objectweb.medor.query.rdb.api.RdbField;
44 import org.objectweb.medor.query.rdb.api.RdbStringQueryLeaf;
45 import org.objectweb.medor.query.rdb.lib.BasicRdbStringQueryLeaf;
46 import org.objectweb.medor.expression.api.Expression;
47 import org.objectweb.util.monolog.api.BasicLevel;
48
49 /**
50  * It tests the rule DropUselessNodeRule.
51  *
52  * @author S.Chassande-Barrioz
53  */

54 public class TestDropUselessNodeRule extends TestMedorHelper {
55
56     /**
57      * main method to launch the tests manually
58      */

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

66     public static Test suite() {
67         return new TestSuite(TestDropUselessNodeRule.class);
68     }
69
70     public TestDropUselessNodeRule() {
71         super("TestDropUselessNodeRule", Log.MEDOR_PREFIX + ".optim.dropuselessnode");
72     }
73
74     public TestDropUselessNodeRule(String JavaDoc testName) {
75         super(testName, Log.MEDOR_PREFIX + ".optim.dropuselessnode");
76     }
77
78
79     protected void setUp() {
80     }
81
82     protected void tearDown() {
83     }
84
85     public void testA() {
86         QueryTree myQuery = null;
87         try {
88             RdbStringQueryLeaf ql = new BasicRdbStringQueryLeaf(null, "Select", "");
89             RdbField f1 = ql.addRdbField("f1", PTypeSpace.BYTE, "col1");
90             RdbField f2 = ql.addRdbField("f2", PTypeSpace.DATE, "col2");
91             QueryNode q1 = new Project(new String JavaDoc[]{"f2"}, ql, "");
92             myQuery = new Project(new String JavaDoc[]{"f2"}, q1, "");
93         }
94         catch (MedorException e) {
95             e.printStackTrace();
96             fail("fail to create the request" + e.getMessage());
97         }
98             logger.log(BasicLevel.DEBUG, "Original tree:");
99             QueryTreePrinter.printQueryTree(myQuery, logger);
100
101         QueryTree result = null;
102         try {
103             result = new DropUselessNodeRule().rewrite(myQuery);
104         }
105         catch (MedorException e) {
106             e.printStackTrace();
107             fail("fail to rewrite the request" + e.getMessage());
108         }
109             logger.log(BasicLevel.DEBUG, "Rewritten tree:");
110             QueryTreePrinter.printQueryTree(result, logger);
111         try {
112             RdbStringQueryLeaf ql = new BasicRdbStringQueryLeaf(null, "Select", "");
113             RdbField f1 = ql.addRdbField("f1", PTypeSpace.BYTE, "col1");
114             RdbField f2 = ql.addRdbField("f2", PTypeSpace.DATE, "col2");
115             TestQueryTreeHelper.equals("", ql, result, logger);
116         }
117         catch (MedorException e) {
118             e.printStackTrace();
119             fail("fail to create the expected request" + e.getMessage());
120         }
121     }
122
123     public void testB() {
124         QueryTree myQuery = null;
125         try {
126             RdbStringQueryLeaf ql1 =
127                 new BasicRdbStringQueryLeaf(null, "Select", "ql1");
128             QueryTreeField f1 = ql1.addRdbField("f1", PTypeSpace.LONG, "col1");
129             QueryTreeField f2 = ql1.addRdbField("f2", PTypeSpace.CHAR, "col2");
130             QueryNode q1 = new Project("q1");
131             f1 = q1.addPropagatedField(
132                 f1.getName(), f1.getType(), new QueryTreeField[]{f1});
133             f2 = q1.addPropagatedField(
134                 f2.getName(), f2.getType(), new QueryTreeField[]{f2});
135
136             RdbStringQueryLeaf ql2 =
137                 new BasicRdbStringQueryLeaf(null, "Select", "ql2");
138             QueryTreeField a = ql2.addRdbField("a", PTypeSpace.LONG, "colA");
139             QueryTreeField b = ql2.addRdbField("b", PTypeSpace.CHAR, "colB");
140             QueryNode q2 = new Project("q2");
141             a = q2.addPropagatedField(
142                 a.getName(), a.getType(), new QueryTreeField[]{a});
143             b = q2.addPropagatedField(
144                 b.getName(), b.getType(), new QueryTreeField[]{b});
145
146             Expression e = new org.objectweb.medor.expression.lib.And(
147                 new Equal(new BasicFieldOperand(f1), new BasicFieldOperand(a)),
148                 new Equal(new BasicFieldOperand(f2), new BasicFieldOperand(b)));
149             QueryNode q3 = new JoinProject(e, "q3");
150             q3.addPropagatedField(
151                 a.getName(), a.getType(), new QueryTreeField[]{a});
152             q3.addPropagatedField(
153                 b.getName(), b.getType(), new QueryTreeField[]{b});
154             myQuery = q3;
155
156         }
157         catch (MedorException e) {
158             e.printStackTrace();
159             fail("fail to create the request" + e.getMessage());
160         }
161             logger.log(BasicLevel.DEBUG, "Original tree:");
162             QueryTreePrinter.printQueryTree(myQuery, logger);
163
164         QueryTree result = null;
165         try {
166             result = new DropUselessNodeRule().rewrite(myQuery);
167         }
168         catch (MedorException e) {
169             e.printStackTrace();
170             fail("fail to rewrite the request" + e.getMessage());
171         }
172             logger.log(BasicLevel.DEBUG, "Rewritten tree:");
173             QueryTreePrinter.printQueryTree(result, logger);
174         try {
175             RdbStringQueryLeaf ql1 =
176                 new BasicRdbStringQueryLeaf(null, "Select", "ql1");
177             QueryTreeField f1 = ql1.addRdbField("f1", PTypeSpace.LONG, "col1");
178             QueryTreeField f2 = ql1.addRdbField("f2", PTypeSpace.CHAR, "col2");
179
180             RdbStringQueryLeaf ql2 =
181                 new BasicRdbStringQueryLeaf(null, "Select", "ql2");
182             QueryTreeField a = ql2.addRdbField("a", PTypeSpace.LONG, "colA");
183             QueryTreeField b = ql2.addRdbField("b", PTypeSpace.CHAR, "colB");
184             Expression e = new And(
185                 new Equal(new BasicFieldOperand(f1), new BasicFieldOperand(a)),
186                 new Equal(new BasicFieldOperand(f2), new BasicFieldOperand(b)));
187             QueryNode q3 = new JoinProject(e, "q3");
188             q3.addPropagatedField(
189                 "q2." + a.getName(), a.getType(), new QueryTreeField[]{a});
190             q3.addPropagatedField(
191                 "q2." + b.getName(), b.getType(), new QueryTreeField[]{b});
192                 logger.log(BasicLevel.DEBUG, "Expected tree:");
193                 QueryTreePrinter.printQueryTree(q3, logger);
194             TestQueryTreeHelper.equals("", q3, result, logger);
195         }
196         catch (MedorException e) {
197             e.printStackTrace();
198             fail("fail to create expected the request" + e.getMessage());
199         }
200
201     }
202
203 }
204
Popular Tags