KickJava   Java API By Example, From Geeks To Geeks.

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


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.medor.api.Field;
29 import org.objectweb.medor.api.MedorException;
30 import org.objectweb.medor.lib.Log;
31 import org.objectweb.medor.lib.TestMedorHelper;
32 import org.objectweb.medor.optim.lib.DropUnusedProjFieldsRule;
33 import org.objectweb.medor.query.api.PropagatedField;
34 import org.objectweb.medor.query.api.QueryNode;
35 import org.objectweb.medor.query.api.QueryTree;
36 import org.objectweb.medor.query.api.QueryTreeField;
37 import org.objectweb.medor.query.lib.Project;
38 import org.objectweb.medor.query.lib.QueryTreePrinter;
39 import org.objectweb.medor.query.rdb.api.RdbField;
40 import org.objectweb.medor.query.rdb.api.RdbStringQueryLeaf;
41 import org.objectweb.medor.query.rdb.lib.BasicRdbStringQueryLeaf;
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 public class TestDropUnusedProjFieldRule extends TestMedorHelper {
45
46     /**
47      * main method to launch the tests manually
48      */

49     public static void main(String JavaDoc[] args) {
50         TestRunner.run(suite());
51     }
52
53     /**
54      * This method creates a TestSuite object with the current tests
55      */

56     public static Test suite() {
57         return new TestSuite(TestDropUnusedProjFieldRule.class);
58     }
59
60     public TestDropUnusedProjFieldRule() {
61         super("TestDropUnusedProjFieldRule", Log.MEDOR_PREFIX + ".optim.dropunusedprojfield");
62     }
63
64     public TestDropUnusedProjFieldRule(String JavaDoc testName) {
65         super(testName, Log.MEDOR_PREFIX + ".optim.dropunusedprojfield");
66     }
67
68
69     protected void setUp() {
70     }
71
72     protected void tearDown() {
73     }
74
75     public void testSimple() {
76         QueryTree myRequest = null;
77         try {
78             RdbStringQueryLeaf ql = new BasicRdbStringQueryLeaf(null, "Select", "");
79             RdbField f1 = ql.addRdbField("f1", null, "col1");
80             RdbField f2 = ql.addRdbField("f2", null, "col2");
81             QueryNode q1 = new Project(new String JavaDoc[]{"f1", "f2"}, ql, "");
82             myRequest = new Project(new String JavaDoc[]{"f2"}, q1, "");
83         } catch (MedorException e) {
84             e.printStackTrace();
85             fail("fail to create the request" + e.getMessage());
86         }
87         logger.log(BasicLevel.DEBUG, "Original tree:");
88         logger.log(BasicLevel.DEBUG, "==============");
89         QueryTreePrinter.printQueryTree(myRequest, logger);
90
91         QueryTree result = null;
92         try {
93             result = new DropUnusedProjFieldsRule().rewrite(myRequest);
94         }
95         catch (MedorException e) {
96             e.printStackTrace();
97             fail("fail to rewrite the request" + e.getMessage());
98         }
99         logger.log(BasicLevel.DEBUG, "Rewritten tree:");
100         logger.log(BasicLevel.DEBUG, "===============");
101         QueryTreePrinter.printQueryTree(result, logger);
102
103         assertTrue("The root is not a QueryNode", result instanceof QueryNode);
104         QueryNode qn = (QueryNode) result;
105         QueryTree[] children = qn.getChildren();
106         assertNotNull("root.children array is null", children);
107         assertEquals("Bad root.children array size", 1, children.length);
108
109         QueryTreeField f = null;
110         Field[] previous = null;
111
112         // Check the root node
113
try {
114             f = (QueryTreeField) qn.getTupleStructure().getField("f1");
115             fail("the root.f1 field exists");
116         }
117         catch (MedorException e) {
118         }
119         try {
120             f = (QueryTreeField) qn.getTupleStructure().getField("f2");
121         }
122         catch (MedorException e) {
123             fail("no field root.f2");
124         }
125         assertNotNull("no field root.f2", f);
126         assertTrue("root.f2 is not a PropagatedField", f instanceof PropagatedField);
127         previous = ((PropagatedField) f).getPreviousFields();
128         assertNotNull("Previous fields of root.f2 is null", previous);
129         assertEquals("Bad number of previous field in root.f2",
130             1, previous.length);
131         assertTrue("Previous fields of root.f2 is not a QueryTreeField",
132             previous[0] instanceof QueryTreeField);
133         f = (QueryTreeField) previous[0];
134         assertEquals("root.children array or QueryTreeField is wrong",
135             children[0], f.getQueryTree());
136
137         // check the middle node
138
assertTrue("The middle node is not a QueryNode", children[0] instanceof QueryNode);
139         qn = (QueryNode) children[0];
140         children = qn.getChildren();
141         assertNotNull("middle.children array is null", children);
142         assertEquals("Bad middle.children array size", 1, children.length);
143
144         try {
145             f = (QueryTreeField) qn.getTupleStructure().getField("f1");
146             fail("The middle.f1 field exists");
147         }
148         catch (MedorException e) {
149         }
150         try {
151             f = (QueryTreeField) qn.getTupleStructure().getField("f2");
152         }
153         catch (MedorException e) {
154             fail("no field root.f2");
155         }
156         assertNotNull("No field middle.f2", f);
157         assertTrue("middle.f2 is not a PropagatedField", f instanceof PropagatedField);
158         previous = ((PropagatedField) f).getPreviousFields();
159         assertNotNull("Previous fields of middle.f2 is null", previous);
160         assertEquals("Bad number of previous field in middle.f2",
161             1, previous.length);
162         assertTrue("Previous fields of middle.f2 is not a QueryTreeField",
163             previous[0] instanceof QueryTreeField);
164         f = (QueryTreeField) previous[0];
165         assertEquals("middle.children array or QueryTreeField is wrong",
166             children[0], f.getQueryTree());
167
168         // check the leaf node
169
assertTrue("The middle node is not a RdbStringQueryLeaf",
170             children[0] instanceof RdbStringQueryLeaf);
171         RdbStringQueryLeaf leaf = (RdbStringQueryLeaf) children[0];
172         try {
173             f = (QueryTreeField) leaf.getTupleStructure().getField("f1");
174         }
175         catch (MedorException e) {
176             fail("No field leaf.f1");
177         }
178         assertNotNull("No field leaf.f1", f);
179         try {
180             f = (QueryTreeField) leaf.getTupleStructure().getField("f2");
181         }
182         catch (MedorException e) {
183             fail("No field leaf.f2");
184         }
185         assertNotNull("No field leaf.f2", f);
186     }
187
188 }
189
Popular Tags