KickJava   Java API By Example, From Geeks To Geeks.

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


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, A. Lefebvre, S. Chassande-Barrioz
22  */

23
24 package org.objectweb.medor.query;
25
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.metainfo.api.GenClassRef;
28 import org.objectweb.medor.api.Field;
29 import org.objectweb.medor.api.MedorException;
30 import org.objectweb.medor.api.TupleStructure;
31 import org.objectweb.medor.filter.TestExpressionHelper;
32 import org.objectweb.medor.lib.TestMedorHelper;
33 import org.objectweb.medor.query.api.CalculatedField;
34 import org.objectweb.medor.query.api.PropagFromNestedField;
35 import org.objectweb.medor.query.api.PropagatedField;
36 import org.objectweb.medor.query.api.QueryNode;
37 import org.objectweb.medor.query.api.QueryTree;
38 import org.objectweb.medor.query.api.QueryTreeField;
39 import org.objectweb.medor.query.jorm.api.JormField;
40 import org.objectweb.medor.query.jorm.lib.ClassExtent;
41 import org.objectweb.medor.query.jorm.lib.GenClassExtent;
42 import org.objectweb.medor.query.jorm.lib.PNameField;
43 import org.objectweb.medor.query.lib.QueryTreePrinter;
44 import org.objectweb.medor.query.rdb.api.QualifiedTable;
45 import org.objectweb.medor.query.rdb.api.RdbExpField;
46 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
47 import org.objectweb.medor.query.rdb.api.RdbStringQueryLeaf;
48 import org.objectweb.medor.query.rdb.api.RdbField;
49 import org.objectweb.medor.expression.api.ExpressionException;
50 import org.objectweb.util.monolog.api.BasicLevel;
51 import org.objectweb.util.monolog.api.Logger;
52
53 import java.util.ArrayList JavaDoc;
54
55 public class TestQueryTreeHelper extends TestMedorHelper {
56
57     public TestQueryTreeHelper(String JavaDoc testName) {
58         super(testName);
59     }
60
61     public static void equals(String JavaDoc prefix, QueryTree qt1, QueryTree qt2,
62                               Logger log)
63         throws MedorException {
64         if (qt1 == null && qt2 == null)
65             return;
66         if (qt1 == null ^ qt2 == null)
67             fail(prefix + "one of the query tree instances is null: qt1=" + qt1
68                 + " / qt2=" + qt2);
69         if (qt1.getDistinct() != qt2.getDistinct())
70             fail(prefix + "Not same distinct qt1=" + qt1.getDistinct()
71                 + " /qt2=" + qt2.getDistinct());
72         if (qt1.getClass() != qt2.getClass())
73             fail(prefix + "Not same class qt1=" + qt1.getClass().getName()
74                 + " / qt2=" + qt2.getClass().getName());
75
76         log.log(BasicLevel.DEBUG, "QueryNode: name=" + qt1.getName() + " / class: " + qt1.getClass().getName());
77
78         equals(prefix, qt1.getTupleStructure(), qt2.getTupleStructure(), log);
79
80         if (qt1 instanceof QueryNode) {
81             QueryNode qn1 = (QueryNode) qt1;
82             QueryNode qn2 = (QueryNode) qt2;
83             TestExpressionHelper.equals(prefix,
84                 qn1.getQueryFilter(), qn2.getQueryFilter(), log);
85             assertEquals(prefix + "QueryNode(" + qt1.getName() + "): Not same type", qn1.getType(), qn2.getType());
86             assertEquals(prefix + "QueryNode(" + qt1.getName() + "): Number of child is different",
87                 qn1.getChildren().length, qn2.getChildren().length);
88             ArrayList JavaDoc child1 = QueryTreePrinter.getChildren(qn1);
89             ArrayList JavaDoc child2 = QueryTreePrinter.getChildren(qn2);
90             for (int i = 0; i < child1.size(); i++) {
91                 equals(prefix,
92                     (QueryTree) child1.get(i),
93                     (QueryTree) child2.get(i), log);
94             }
95         }
96         else if (qt1 instanceof ClassExtent) {
97             ClassExtent je1 = (ClassExtent) qt1;
98             ClassExtent je2 = (ClassExtent) qt2;
99             assertEquals(prefix + "ClassExtent(" + qt1.getName() + "): Not same meta object",
100                 ((Class JavaDoc) je1.getMetaObject()).getName(), ((Class JavaDoc) je2.getMetaObject()).getName());
101             if (je1.getDataStore() != null)
102                 assertEquals(prefix + "ClassExtent(" + qt1.getName() + "): Not same datstore",
103                     je1.getDataStore().getName(), je2.getDataStore().getName());
104         }
105         else if (qt1 instanceof GenClassExtent) {
106             GenClassExtent je1 = (GenClassExtent) qt1;
107             GenClassExtent je2 = (GenClassExtent) qt2;
108             assertEquals(prefix + "ClassExtent(" + qt1.getName() + "): Not same meta object",
109                 ((GenClassRef) je1.getMetaObject()).getName(), ((GenClassRef) je2.getMetaObject()).getName());
110             assertEquals(prefix + "ClassExtent(" + qt1.getName() + "): Not same mapper",
111                 je1.getPMapper(), je2.getPMapper());
112             assertEquals(prefix + "ClassExtent(" + qt1.getName() + "): Not same datstore",
113                 je1.getDataStore(), je2.getDataStore());
114         }
115         else if (qt1 instanceof RdbExpQueryLeaf) {
116             RdbExpQueryLeaf ql1 = (RdbExpQueryLeaf) qt1;
117             RdbExpQueryLeaf ql2 = (RdbExpQueryLeaf) qt2;
118             QualifiedTable[] t1 = ql1.getQualifiedTables();
119             QualifiedTable[] t2 = ql2.getQualifiedTables();
120             assertEquals(prefix + "RdbExpQueryLeaf(" + qt1.getName() + "): Number of QualifiedTable is different",
121                 t1.length, t2.length);
122             for (int i = 0; i < t1.length; i++) {
123                 assertEquals(prefix + "RdbExpQueryLeaf(" + qt1.getName() + "): Not same table name",
124                     t1[i].getTableName(), t2[i].getTableName());
125                 assertEquals(prefix + "RdbExpQueryLeaf(" + qt1.getName() + "): Not same alias name",
126                     t1[i].getAliasName(), t2[i].getAliasName());
127             }
128             try {
129                 log.log(BasicLevel.DEBUG, "SQL for leaf 2"
130                         + ql2.getSqlRequest(null, false, false));
131                 log.log(BasicLevel.DEBUG, "SQL for leaf 1"
132                         + ql1.getSqlRequest(null, false, false));
133                 assertEquals(prefix + "Not same SQL request",
134                         ql1.getSqlRequest(null, false, false),
135                         ql2.getSqlRequest(null, false, false));
136             }
137             catch (ExpressionException e) {
138                 throw new MedorException(e);
139             }
140         }
141         else if (qt1 instanceof RdbStringQueryLeaf) {
142             RdbStringQueryLeaf ql1 = (RdbStringQueryLeaf) qt1;
143             RdbStringQueryLeaf ql2 = (RdbStringQueryLeaf) qt2;
144             try {
145                 assertEquals(prefix + "RdbStringQueryLeaf(" + qt1.getName() + "): Not same request",
146                     ql1.getSqlRequest(null, false, false),
147                         ql2.getSqlRequest(null, false, false));
148             }
149             catch (ExpressionException e) {
150                 log.log(BasicLevel.ERROR,
151                     prefix + "RdbStringQueryLeaf(" + qt1.getName() + "): Impossible to fetch the sqlRequest: ",
152                     e);
153                 throw new MedorException(e);
154             }
155             catch (MedorException e) {
156                 log.log(BasicLevel.ERROR,
157                     prefix + "RdbStringQueryLeaf(" + qt1.getName() + "): Impossible to fetch the sqlRequest: ",
158                     e);
159                 throw e;
160             }
161         }
162     }
163
164     public static void equals(String JavaDoc prefix,
165                               TupleStructure ts1,
166                               TupleStructure ts2,
167                               Logger log)
168         throws MedorException {
169         Field[] f1s = ts1.getFields();
170         Field[] f2s = ts2.getFields();
171         assertEquals(prefix + "TupleStructure: not same size",
172             f1s.length, f2s.length);
173         for (int i = 0; i < f1s.length; i++) {
174             equals(prefix, f1s[i], f2s[i], log);
175         }
176     }
177
178     public static void equals(String JavaDoc prefix, Field f1, Field f2, Logger log)
179         throws MedorException {
180         if (f1 == null && f2 == null)
181             return;
182         if (f1 == null ^ f2 == null)
183             fail(prefix + "one of the fields is null: f1=" + f1
184                 + " / f2=" + f2);
185         if (f1.getClass() != f2.getClass())
186             fail(prefix + "Not same class f1=" + f1.getClass().getName()
187                 + " / f2=" + f2.getClass().getName());
188         log.log(BasicLevel.DEBUG, "Field class: " + f1.getClass().getName());
189
190         assertEquals(prefix + "Field: Name is different", f1.getName(), f2.getName());
191         log.log(BasicLevel.DEBUG, "Field name: " + f1.getName());
192
193         if (f1.getType() != null) {
194             log.log(BasicLevel.DEBUG, "Field type f1: " + f1.getType().getJavaName() + " / " + f1.getType().getJormName());
195             log.log(BasicLevel.DEBUG, "Field type f2: " + f2.getType().getJavaName() + " / " + f2.getType().getJormName());
196         assertTrue(prefix + "Field: (" + f1.getName() + ") Type is different (was " + f2.getType().getTypeCode() + ", expected " + f1.getType().getTypeCode() + ")",
197             f1.getType().isa(f2.getType()));
198         log.log(BasicLevel.DEBUG, "Field type: " + f1.getType());
199         }
200
201         if (f1 instanceof CalculatedField) {
202             TestExpressionHelper.equals(prefix,
203                 ((CalculatedField) f1).getExpression(),
204                 ((CalculatedField) f2).getExpression(),
205                 log);
206         }
207         else if (f1 instanceof PropagatedField) {
208             Field[] fs1 = ((PropagatedField) f1).getPreviousFields();
209             Field[] fs2 = ((PropagatedField) f2).getPreviousFields();
210             assertEquals(prefix +
211                 "PropagatedField: Number of previous field is different",
212                 fs1.length, fs2.length);
213             if (f1 instanceof PropagFromNestedField) {
214                 fs1 = ((PropagFromNestedField) f1).getPreviousFlatField();
215                 fs2 = ((PropagFromNestedField) f2).getPreviousFlatField();
216                 assertEquals(prefix +
217                     "PropagatedField: Number of previous field is different",
218                     fs1.length, fs2.length);
219             }
220         }
221         else if (f1 instanceof RdbExpField) {
222             RdbExpField ref1 = (RdbExpField) f1;
223             RdbExpField ref2 = (RdbExpField) f2;
224             assertEquals("RdbExpField: Not same column name",
225                 ref1.getColumnName(), ref2.getColumnName());
226             assertEquals("RdbExpField: Not same table name",
227                 ref1.getTable().getTableName(), ref2.getTable().getTableName());
228             assertEquals("RdbExpField: Not same table name",
229                 ref1.getTable().getAliasName(), ref2.getTable().getAliasName());
230         }
231         else if (f1 instanceof RdbField) {
232             RdbField ref1 = (RdbField) f1;
233             RdbField ref2 = (RdbField) f2;
234             assertEquals("RdbField: Not same column name",
235                 ref1.getColumnName(), ref2.getColumnName());
236         }
237         else if (f1 instanceof JormField) {
238             log.log(BasicLevel.DEBUG, "Compare JormField: " + f1.getName()
239                 + " qt=" + ((QueryTreeField) f1).getQueryTree().getName());
240             log.log(BasicLevel.DEBUG, "with JormField: " + f2.getName()
241                 + " qt=" + ((QueryTreeField) f2).getQueryTree().getName());
242             assertEquals(
243                 prefix + "JormField: not same TypedElement",
244                 ((JormField) f1).getTypedElement(),
245                 ((JormField) f2).getTypedElement());
246         }
247         else if (f1 instanceof PNameField) {
248             PNameField pnf1 = (PNameField) f1;
249             PNameField pnf2 = (PNameField) f2;
250             if (pnf1.isInGenClass())
251                 assertEquals(prefix + "PNameField: not same genclassRef",
252                     pnf1.getGenClassRef().getName(), pnf2.getGenClassRef().getName());
253             if (pnf1.getMetaObjectClass() != null)
254                 assertEquals(prefix + "PNameField: not same MetaObjectClass",
255                     pnf1.getMetaObjectClass().getName(), pnf2.getMetaObjectClass().getName());
256             if (pnf1.getReference() != null)
257                 assertEquals(prefix + "PNameField: not same reference",
258                     pnf1.getReference().getName(), pnf2.getReference().getName());
259             assertEquals(prefix + "PNameField: not same isClassPName",
260                 pnf1.isClassPName(), pnf2.isClassPName());
261             assertEquals(prefix + "PNameField: not same isInGenClass",
262                 pnf1.isInGenClass(), pnf2.isInGenClass());
263         }
264         else
265         //TODO handle BasicNestedField
266
log.log(BasicLevel.WARN,
267                 "Unknown field type: " + f1.getClass().getName());
268     }
269 }
270
Popular Tags