KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > jorm > TestJormQueryRewriter


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2005 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.jorm;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinder;
28 import org.objectweb.jorm.naming.api.PName;
29 import org.objectweb.jorm.type.api.PTypeSpace;
30 import org.objectweb.medor.api.MedorException;
31 import org.objectweb.medor.expression.api.Expression;
32 import org.objectweb.medor.expression.lib.And;
33 import org.objectweb.medor.expression.lib.BasicOperand;
34 import org.objectweb.medor.expression.lib.BasicParameterOperand;
35 import org.objectweb.medor.expression.lib.Equal;
36 import org.objectweb.medor.filter.api.FieldOperand;
37 import org.objectweb.medor.filter.jorm.lib.CompositePName;
38 import org.objectweb.medor.filter.jorm.lib.EncodePName;
39 import org.objectweb.medor.filter.jorm.lib.SinglePName;
40 import org.objectweb.medor.filter.lib.Avg;
41 import org.objectweb.medor.filter.lib.BasicFieldOperand;
42 import org.objectweb.medor.filter.lib.Count;
43 import org.objectweb.medor.filter.lib.MemberOf;
44 import org.objectweb.medor.filter.lib.Sum;
45 import org.objectweb.medor.optim.api.QueryRewriter;
46 import org.objectweb.medor.optim.lib.IndexesGenerator;
47 import org.objectweb.medor.query.TestQueryTreeHelper;
48 import org.objectweb.medor.query.api.OrderField;
49 import org.objectweb.medor.query.api.QueryLeaf;
50 import org.objectweb.medor.query.api.QueryNode;
51 import org.objectweb.medor.query.api.QueryTree;
52 import org.objectweb.medor.query.api.QueryTreeField;
53 import org.objectweb.medor.query.lib.BasicOrderField;
54 import org.objectweb.medor.query.lib.Nest;
55 import org.objectweb.medor.query.lib.QueryTreePrinter;
56 import org.objectweb.medor.query.lib.SelectProject;
57 import org.objectweb.medor.query.rdb.api.QualifiedTable;
58 import org.objectweb.medor.query.rdb.api.RdbExpField;
59 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf;
60 import org.objectweb.medor.query.rdb.api.RdbQueryLeaf;
61 import org.objectweb.medor.query.rdb.lib.AggregateRdbQueryNode;
62 import org.objectweb.medor.query.rdb.lib.BasicQualifiedTable;
63 import org.objectweb.medor.query.rdb.lib.BasicRdbExpQueryLeaf;
64 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
65 import org.objectweb.util.monolog.api.BasicLevel;
66
67 /**
68  * @author S.Chassande-Barrioz
69  */

70 public abstract class TestJormQueryRewriter extends TestJormPDHelper {
71
72     public TestJormQueryRewriter(String JavaDoc testName, String JavaDoc loggerName) {
73         super(testName, loggerName);
74     }
75
76     public abstract QueryLeaf getQueryLeafA(Example e) throws MedorException;
77
78     public abstract QueryLeaf getQueryLeafAgg(Example e) throws MedorException;
79
80     public abstract QueryLeaf getQueryLeafAggParam(Example e) throws MedorException;
81
82     public abstract QueryLeaf getQueryLeafAggDirect(Example e)
83             throws MedorException;
84
85     public abstract QueryLeaf getQueryLeafAggWithWhere(Example e)
86             throws MedorException;
87
88     public abstract QueryLeaf getQueryLeafOrderByA(Example e)
89             throws MedorException;
90
91     public abstract QueryLeaf getQueryLeafMemberOfBD(Example eB, Example eD)
92             throws Exception JavaDoc;
93
94     public abstract QueryLeaf getQueryLeafMemberOfBC(Example eB, Example eC)
95             throws Exception JavaDoc;
96
97     public abstract QueryLeaf getQueryLeafMOfParamBD(Example eB, Example eD)
98             throws Exception JavaDoc;
99
100     public abstract QueryLeaf getQueryLeafAnimal(Example e)
101             throws MedorException;
102
103     public abstract QueryLeaf getQueryLeafLongLong(Example e)
104             throws MedorException;
105
106
107     public void test1() {
108         try {
109             Example e = getExtentOfA();
110             Expression exp = new Equal(
111                     new BasicFieldOperand(e.extent.getField(e.clazz.getFQName() + ".field1")),
112                     new BasicParameterOperand(PTypeSpace.STRING, "?1"));
113             QueryNode qn = new SelectProject(exp, "");
114             QueryTreeField qtf = (QueryTreeField)
115                     e.extent.getField(e.extent.getPNameFieldName());
116             qn.addPropagatedField(
117                     qtf.getName(),
118                     qtf.getType(),
119                     new QueryTreeField[]{qtf});
120             QueryRewriter jqr = this.createQueryRewriter();
121
122             logger.log(BasicLevel.DEBUG, "test1: QueryTree before transformation");
123             QueryTreePrinter.printQueryTree(qn, logger);
124             //assign mapper + project names to each extent
125
e.extent.setPMapper(mapper, PROJECT_NAME);
126             e.extent.setProjectName("test");
127             QueryTree result = jqr.transform(qn);
128             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
129
130             QueryTreePrinter.printQueryTree(result, logger);
131             QueryLeaf leaf = getQueryLeafA(e);
132             QueryNode expected = new SelectProject("");
133             expected.addCalculatedField(
134                     e.clazz.getFQName() + ".object",
135                     e.extent.getField(e.extent.getPNameFieldName()).getType(),
136                     new SinglePName(
137                             new BasicFieldOperand(
138                                     leaf.getTupleStructure().getField(e.clazz.getFQName()
139                     + "." + "id")),
140                             new BasicParameterOperand(
141                                     PTypeSpaceMedor.NAMING_CONTEXT,
142                                     "A"),
143                             e.extent.getField(e.clazz.getFQName() + ".object").getType()
144                     )
145             );
146             logger.log(BasicLevel.DEBUG, "test1: Expected QueryTree");
147             logger.log(BasicLevel.DEBUG, "=========================================");
148             QueryTreePrinter.printQueryTree(expected, logger);
149             TestQueryTreeHelper.equals("", expected, result, logger);
150         } catch (MedorException e1) {
151             e1.printStackTrace();
152             fail(e1.getMessage());
153         }
154     }
155
156     public void test1distinct() {
157         try {
158             Example e = getExtentOfA();
159             Expression exp = new Equal(
160                     new BasicFieldOperand(e.extent.getField(e.clazz.getFQName() + ".field1")),
161                     new BasicParameterOperand(PTypeSpace.STRING, "?1"));
162             QueryNode qn = new SelectProject(exp, "");
163             qn.setDistinct(true);
164             QueryTreeField qtf = (QueryTreeField)
165                     e.extent.getField(e.extent.getPNameFieldName());
166             qn.addPropagatedField(
167                     qtf.getName(),
168                     qtf.getType(),
169                     new QueryTreeField[]{qtf});
170             QueryRewriter jqr = this.createQueryRewriter();
171
172             logger.log(BasicLevel.DEBUG, "test1distinct: QueryTree before transformation");
173             QueryTreePrinter.printQueryTree(qn, logger);
174             //assign mapper + project names to each extent
175
e.extent.setPMapper(mapper, PROJECT_NAME);
176             e.extent.setProjectName("test");
177             QueryTree result = jqr.transform(qn);
178             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
179
180             QueryTreePrinter.printQueryTree(result, logger);
181             QueryLeaf leaf = getQueryLeafA(e);
182             QueryNode expected = new SelectProject("");
183             expected.setDistinct(true);
184             leaf.setDistinct(true);
185             expected.addCalculatedField(
186                     e.clazz.getFQName() + ".object",
187                     e.extent.getField(e.extent.getPNameFieldName()).getType(),
188                     new SinglePName(
189                             new BasicFieldOperand(
190                                     leaf.getTupleStructure().getField(e.clazz.getFQName()
191                     + "." + "id")),
192                             new BasicParameterOperand(
193                                     PTypeSpaceMedor.NAMING_CONTEXT,
194                                     "A"),
195                             e.extent.getField(e.clazz.getFQName() + ".object").getType()
196                     )
197             );
198             logger.log(BasicLevel.DEBUG, "test1distinct: Expected QueryTree");
199             logger.log(BasicLevel.DEBUG, "=========================================");
200             QueryTreePrinter.printQueryTree(expected, logger);
201             TestQueryTreeHelper.equals("", expected, result, logger);
202         } catch (MedorException e1) {
203             e1.printStackTrace();
204             fail(e1.getMessage());
205         }
206     }
207
208     public void testOrderBy() {
209         try {
210             Example e = getExtentOfA();
211             Expression exp = new Equal(
212                     new BasicFieldOperand(e.extent.getField(e.clazz.getFQName() + ".field1")),
213                     new BasicParameterOperand(PTypeSpace.STRING, "?1"));
214             QueryNode qn = new SelectProject(exp, "");
215             QueryTreeField qtf = (QueryTreeField)
216                     e.extent.getField(e.extent.getPNameFieldName());
217             qn.addPropagatedField(
218                     qtf.getName(),
219                     qtf.getType(),
220                     new QueryTreeField[]{qtf});
221             QueryTreeField field1 =
222                     (QueryTreeField)e.extent.getField(e.clazz.getFQName() + ".field1");
223             QueryTreeField field2 =
224                     (QueryTreeField)e.extent.getField(e.clazz.getFQName() + ".field2");
225             // Add an OrderBy Field
226
OrderField of1 = new BasicOrderField(field1, true);
227             OrderField of2 = new BasicOrderField(field2);
228             qn.setOrderBy(new OrderField[] {of1, of2});
229             QueryRewriter jqr = this.createQueryRewriter();
230
231             logger.log(BasicLevel.DEBUG, "test1distinct: QueryTree before transformation");
232             QueryTreePrinter.printQueryTree(qn, logger);
233             //assign mapper + project names to each extent
234
e.extent.setPMapper(mapper, PROJECT_NAME);
235             e.extent.setProjectName("test");
236             QueryTree result = jqr.transform(qn);
237             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
238
239             QueryTreePrinter.printQueryTree(result, logger);
240             QueryLeaf leaf = getQueryLeafOrderByA(e);
241             QueryNode expected = new SelectProject("");
242             expected.addCalculatedField(
243                     e.clazz.getFQName() + ".object",
244                     e.extent.getField(e.extent.getPNameFieldName()).getType(),
245                     new SinglePName(
246                             new BasicFieldOperand(
247                                     leaf.getTupleStructure().getField(e.clazz.getFQName()
248                     + "." + "id")),
249                             new BasicParameterOperand(
250                                     PTypeSpaceMedor.NAMING_CONTEXT,
251                                     "A"),
252                             e.extent.getField(e.clazz.getFQName() + ".object").getType()
253                     )
254             );
255             logger.log(BasicLevel.DEBUG, "test1distinct: Expected QueryTree");
256             logger.log(BasicLevel.DEBUG, "=========================================");
257             QueryTreePrinter.printQueryTree(expected, logger);
258             TestQueryTreeHelper.equals("", expected, result, logger);
259         } catch (MedorException e1) {
260             e1.printStackTrace();
261             fail(e1.getMessage());
262         }
263     }
264
265     //TODO corrected expected query tree
266
public void _testOrderByWithNode() {
267         try {
268             Example eA = getExtentOfA();
269             Example eB = getExtentOfB();
270             Example eC = getExtentOfC();
271             Expression exp2 = new Equal(
272                     new BasicFieldOperand(eB.extent.getField(eB.clazz.getFQName() + ".y")),
273                     new BasicFieldOperand(eC.extent.getField(eC.clazz.getFQName() + ".x")));
274             QueryNode qn2 = new SelectProject(exp2, "");
275             QueryTreeField qtf2 = (QueryTreeField)
276                     eB.extent.getField(eB.extent.getPNameFieldName());
277             QueryTreeField fqn2 = qn2.addPropagatedField(
278                     qtf2.getName(),
279                     qtf2.getType(),
280                     new QueryTreeField[]{qtf2});
281             Expression exp = new Equal(
282                     new BasicFieldOperand(eA.extent.getField(eA.clazz.getFQName() + ".field1")),
283                     new BasicParameterOperand(PTypeSpace.STRING, "?1"));
284             QueryNode qn = new SelectProject(exp, "");
285             QueryTreeField qtf = (QueryTreeField)
286                     eA.extent.getField(eA.extent.getPNameFieldName());
287             qn.addPropagatedField(
288                     qtf.getName(),
289                     qtf.getType(),
290                     new QueryTreeField[]{qtf});
291             qn.addPropagatedField(
292                     fqn2.getName(),
293                     fqn2.getType(),
294                     new QueryTreeField[]{fqn2});
295             QueryTreeField field1 =
296                     (QueryTreeField)eA.extent.getField(eA.clazz.getFQName() + ".field1");
297             QueryTreeField field2 =
298                     (QueryTreeField)eA.extent.getField(eA.clazz.getFQName() + ".field2");
299             // Add an OrderBy Field
300
OrderField of1 = new BasicOrderField(field1, true);
301             OrderField of2 = new BasicOrderField(field2);
302             qn.setOrderBy(new OrderField[] {of1, of2});
303             QueryRewriter jqr = this.createQueryRewriter();
304
305             logger.log(BasicLevel.DEBUG, "test1distinct: QueryTree before transformation");
306             QueryTreePrinter.printQueryTree(qn, logger);
307             //assign mapper + project names to each extent
308
eA.extent.setPMapper(mapper, PROJECT_NAME);
309             eA.extent.setProjectName("test");
310             QueryTree result = jqr.transform(qn);
311             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
312
313             QueryTreePrinter.printQueryTree(result, logger);
314             QueryLeaf leaf = getQueryLeafOrderByA(eA);
315             QueryNode expected = new SelectProject("");
316             expected.addCalculatedField(
317                     eA.clazz.getFQName() + ".object",
318                     eA.extent.getField(eA.extent.getPNameFieldName()).getType(),
319                     new SinglePName(
320                             new BasicFieldOperand(
321                                     leaf.getTupleStructure().getField(eA.clazz.getFQName()
322                     + "." + "id")),
323                             new BasicParameterOperand(
324                                     PTypeSpaceMedor.NAMING_CONTEXT,
325                                     "A"),
326                             eA.extent.getField(eA.clazz.getFQName() + ".object").getType()
327                     )
328             );
329
330             logger.log(BasicLevel.DEBUG, "test1distinct: Expected QueryTree");
331             logger.log(BasicLevel.DEBUG, "=========================================");
332             QueryTreePrinter.printQueryTree(expected, logger);
333             TestQueryTreeHelper.equals("", expected, result, logger);
334         } catch (MedorException e1) {
335             e1.printStackTrace();
336             fail(e1.getMessage());
337         }
338     }
339     public void testMemberOfsingleBC() throws Exception JavaDoc {
340         try {
341             Example eB = getExtentOfB(new String JavaDoc[] {"y"});
342             Example eC = getExtentOfC();
343             ArrayList JavaDoc al1 = new ArrayList JavaDoc(1);
344             al1.add(new BasicFieldOperand(eB.extent.getField(eB.clazz.getFQName() + ".y")));
345             ArrayList JavaDoc al2 = new ArrayList JavaDoc(1);
346             al2.add(new BasicFieldOperand(eC.extent.getField(eC.clazz.getFQName() + ".x")));
347             Expression exp1 = new MemberOf(al1, al2);
348             Expression exp = new And(exp1,
349                     new Equal(new BasicFieldOperand(eC.extent.getField(eC.clazz.getFQName() + ".x")),
350                             new BasicOperand((long) 2)));
351             QueryNode qn = new SelectProject(exp, "");
352             QueryTreeField qtf = (QueryTreeField)
353                     eB.extent.getField(eB.extent.getPNameFieldName());
354             qn.addPropagatedField(
355                     qtf.getName(),
356                     qtf.getType(),
357                     new QueryTreeField[]{qtf});
358             QueryRewriter jqr = this.createQueryRewriter();
359
360             logger.log(BasicLevel.DEBUG, "testMemberOfSingleBC: QueryTree before transformation");
361             QueryTreePrinter.printQueryTree(qn, logger);
362             //assign mapper + project names to each extent
363
eB.extent.setPMapper(mapper, PROJECT_NAME);
364             eB.extent.setProjectName(PROJECT_NAME);
365             eC.extent.setPMapper(mapper, PROJECT_NAME);
366             eC.extent.setProjectName(PROJECT_NAME);
367             QueryTree result = jqr.transform(qn);
368             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
369             QueryTreePrinter.printQueryTree(result, logger);
370             QueryLeaf leaf = getQueryLeafMemberOfBC(eB, eC);
371             QueryNode expected = new SelectProject("");
372             expected.addCalculatedField(
373                     eB.clazz.getFQName() + ".object",
374                     eB.extent.getField(eB.extent.getPNameFieldName()).getType(),
375                     new CompositePName(
376                             new FieldOperand[]{
377                                 new BasicFieldOperand(
378                                         leaf.getTupleStructure().getField(eB.clazz.getFQName()
379                                         + "." + "c2")),
380                                 new BasicFieldOperand(
381                                         leaf.getTupleStructure().getField(eB.clazz.getFQName()
382                                         + "." + "c1"))
383                             },
384                             new String JavaDoc[]{"c2","c1"},
385                             null,
386                             eB.extent.getField(eB.clazz.getFQName() + ".object").getType()
387                     )
388             );
389             logger.log(BasicLevel.DEBUG, "testMemberOfSingleBC: Expected QueryTree");
390             logger.log(BasicLevel.DEBUG, "=========================================");
391             QueryTreePrinter.printQueryTree(expected, logger);
392             TestQueryTreeHelper.equals("", expected, result, logger);
393         } catch (MedorException e1) {
394             e1.printStackTrace();
395             fail(e1.getMessage());
396         }
397     }
398
399     public void testIsNullOnPName() throws Exception JavaDoc {
400         try {
401             Example eLL = getExtentOfLongLong();
402             eLL.extent.setPMapper(mapper, PROJECT_NAME);
403             eLL.extent.setProjectName(PROJECT_NAME);
404             QueryTreeField qtf = (QueryTreeField)
405                     eLL.extent.getField(eLL.extent.getPNameFieldName());
406             FieldOperand bPName = new BasicFieldOperand(qtf);
407
408             PolymorphIdBinder prnc = new PolymorphIdBinder();
409             PName pnamenull = prnc.getNull();
410
411             Expression exp = new Equal(bPName,
412                     new BasicOperand(pnamenull, PTypeSpaceMedor.PNAME));
413             QueryNode qn = new SelectProject(exp, "");
414             qn.addPropagatedField(
415                     qtf.getName(),
416                     qtf.getType(),
417                     new QueryTreeField[]{qtf});
418             QueryRewriter jqr = this.createQueryRewriter();
419
420             logger.log(BasicLevel.DEBUG, "testIsNullOnPName: QueryTree before transformation");
421             QueryTreePrinter.printQueryTree(qn, logger);
422             //assign mapper + project names to each extent
423
eLL.extent.setPMapper(mapper, PROJECT_NAME);
424             eLL.extent.setProjectName("test");
425             QueryTree result = jqr.transform(qn);
426             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
427             QueryTreePrinter.printQueryTree(result, logger);
428
429             //building the expected result
430
QueryNode expected = new SelectProject("");
431             QueryLeaf leaf = getQueryLeafLongLong(eLL);
432
433             expected.addCalculatedField(
434                     eLL.clazz.getFQName() + ".object",
435                     eLL.extent.getField(eLL.extent.getPNameFieldName()).getType(),
436                     new CompositePName(
437                             new FieldOperand[]{
438                                 new BasicFieldOperand(
439                                         leaf.getTupleStructure().getField(eLL.clazz.getFQName()
440                                         + "." + "c2")),
441                                 new BasicFieldOperand(
442                                         leaf.getTupleStructure().getField(eLL.clazz.getFQName()
443                                         + "." + "c1"))
444                             },
445                             new String JavaDoc[]{"c2","c1"},
446                             null,
447                             eLL.extent.getField(eLL.clazz.getFQName() + ".object").getType()
448                     )
449             );
450
451             logger.log(BasicLevel.DEBUG, "testIsNullOnPName: Expected QueryTree");
452             logger.log(BasicLevel.DEBUG, "=========================================");
453             QueryTreePrinter.printQueryTree(expected, logger);
454             TestQueryTreeHelper.equals("", expected, result, logger);
455         } catch (MedorException e1) {
456             e1.printStackTrace();
457             fail(e1.getMessage());
458         }
459     }
460
461     public void testMemberOfmultiBD() throws Exception JavaDoc {
462         try {
463             Example eB = getExtentOfB();
464             Example eD = getExtentOfD();
465             ArrayList JavaDoc al1 = new ArrayList JavaDoc(1);
466             al1.add(new BasicFieldOperand(eB.extent.getField(eB.clazz.getFQName() + ".object")));
467             ArrayList JavaDoc al2 = new ArrayList JavaDoc(1);
468             al2.add(new BasicFieldOperand(eD.extent.getField(eD.clazz.getFQName() + ".object")));
469             Expression exp = new MemberOf(al1, al2);
470             QueryNode qn = new SelectProject(exp, "");
471             QueryTreeField qtf = (QueryTreeField)
472                     eB.extent.getField(eB.extent.getPNameFieldName());
473             qn.addPropagatedField(
474                     qtf.getName(),
475                     qtf.getType(),
476                     new QueryTreeField[]{qtf});
477             QueryRewriter jqr = this.createQueryRewriter();
478
479             logger.log(BasicLevel.DEBUG, "testMemberOf: QueryTree before transformation");
480             QueryTreePrinter.printQueryTree(qn, logger);
481             //assign mapper + project names to each extent
482
eB.extent.setPMapper(mapper, PROJECT_NAME);
483             eB.extent.setProjectName("test");
484             eD.extent.setPMapper(mapper, PROJECT_NAME);
485             eD.extent.setProjectName("test");
486             QueryTree result = jqr.transform(qn);
487             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
488             QueryTreePrinter.printQueryTree(result, logger);
489
490             //building the expected result
491
QueryLeaf leaf = getQueryLeafMemberOfBD(eB, eD);
492             QueryNode expected = new SelectProject("");
493             expected.addCalculatedField(
494                     eB.clazz.getFQName() + ".object",
495                     eB.extent.getField(eB.extent.getPNameFieldName()).getType(),
496                     new CompositePName(
497                             new FieldOperand[]{
498                                 new BasicFieldOperand(
499                                         leaf.getTupleStructure().getField(eB.clazz.getFQName()
500                                         + "." + "c2")),
501                                 new BasicFieldOperand(
502                                         leaf.getTupleStructure().getField(eB.clazz.getFQName()
503                                         + "." + "c1"))
504                             },
505                             new String JavaDoc[]{"c2","c1"},
506                             null,
507                             eB.extent.getField(eB.clazz.getFQName() + ".object").getType()
508                     )
509             );
510             logger.log(BasicLevel.DEBUG, "testMemberOf: Expected QueryTree");
511             logger.log(BasicLevel.DEBUG, "=========================================");
512             QueryTreePrinter.printQueryTree(expected, logger);
513             TestQueryTreeHelper.equals("", expected, result, logger);
514         } catch (MedorException e1) {
515             e1.printStackTrace();
516             fail(e1.getMessage());
517         }
518     }
519
520     /**
521      * TODO Handle MemberOf(param) in JormFlatten2Rdb for this test to pass
522      */

523     public void _testMemberOfParamBD() throws Exception JavaDoc {
524         try {
525             logger.log(BasicLevel.DEBUG, "Entering testMemberOfParamBD");
526
527             Example eB = getExtentOfB(new String JavaDoc[] {"y"});
528             Example eD = getExtentOfD();
529             ArrayList JavaDoc al1 = new ArrayList JavaDoc(1);
530             BasicOperand value = new BasicOperand(1013);
531             al1.add(value);
532             ArrayList JavaDoc al2 = new ArrayList JavaDoc(1);
533             al2.add(new BasicFieldOperand(eB.extent.getField(eB.clazz.getFQName() + ".y")));
534             Expression exp = new MemberOf(al1, al2);
535             QueryNode qn = new SelectProject(exp, "");
536             QueryTreeField qtf = (QueryTreeField)
537                     eD.extent.getField(eD.extent.getPNameFieldName());
538             qn.addPropagatedField(
539                     qtf.getName(),
540                     qtf.getType(),
541                     new QueryTreeField[]{qtf});
542             QueryRewriter jqr = this.createQueryRewriter();
543
544             logger.log(BasicLevel.DEBUG, "testMemberOfParamBD: QueryTree before transformation");
545             QueryTreePrinter.printQueryTree(qn, logger);
546             //assign mapper + project names to each extent
547
eB.extent.setPMapper(mapper, PROJECT_NAME);
548             eB.extent.setProjectName("test");
549             eD.extent.setPMapper(mapper, PROJECT_NAME);
550             eD.extent.setProjectName("test");
551             QueryTree result = jqr.transform(qn);
552             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
553             QueryTreePrinter.printQueryTree(result, logger);
554
555             //building the expected result
556
QueryLeaf leaf = getQueryLeafMOfParamBD(eB, eD);
557             QueryNode expected = new SelectProject("");
558             expected.addCalculatedField(
559                     eD.clazz.getFQName() + ".object",
560                     eD.extent.getField(eD.extent.getPNameFieldName()).getType(),
561                     new CompositePName(
562                             new FieldOperand[]{
563                                 new BasicFieldOperand(
564                                         leaf.getTupleStructure().getField(eD.clazz.getFQName()
565                                         + "." + "b")),
566                                 new BasicFieldOperand(
567                                         leaf.getTupleStructure().getField(eD.clazz.getFQName()
568                                         + "." + "a"))
569                             },
570                             new String JavaDoc[]{"b","a"},
571                             null,
572                             eD.extent.getField(eD.clazz.getFQName() + ".object").getType()
573                     )
574             );
575             logger.log(BasicLevel.DEBUG, "testMemberOfParamBD: Expected QueryTree");
576             logger.log(BasicLevel.DEBUG, "=========================================");
577             QueryTreePrinter.printQueryTree(expected, logger);
578             TestQueryTreeHelper.equals("", expected, result, logger);
579         } catch (MedorException e1) {
580             e1.printStackTrace();
581             fail(e1.getMessage());
582         }
583     }
584
585     //TODO replace tables with JoinTables for the expected result
586
public void _testAtoB() {
587         try {
588             Example a = getExtentOfAcolocB();
589             Example b1 = getExtentOfBcolocC();
590             Example b2 = getExtentOfBcolocC();
591             Expression exp1 = new Equal(
592                     new BasicFieldOperand(a.extent.getField(a.clazz.getFQName() + ".b1")),
593                     new BasicFieldOperand(b1.extent.getField(b1.extent.getPNameFieldName()))
594             );
595             Expression exp2 = new Equal(
596                     new BasicFieldOperand(a.extent.getField(a.clazz.getFQName() + ".b2")),
597                     new BasicFieldOperand(b2.extent.getField(b2.extent.getPNameFieldName()))
598             );
599             Expression exp3 = new And(exp1, exp2);
600             Expression exp4 = new Equal(
601                     new BasicFieldOperand(b1.extent.getField(b1.clazz.getFQName() + ".f1")),
602                     new BasicFieldOperand(b2.extent.getField(b2.clazz.getFQName() + ".f2"))
603             );
604             Expression exp = new And(exp3, exp4);
605             QueryNode qn = new SelectProject(exp, "");
606
607             QueryTreeField f = (QueryTreeField) a.extent.getField(a.extent.getPNameFieldName());
608             qn.addPropagatedField(f.getName(), f.getType(), new QueryTreeField[]{f});
609
610             QueryRewriter jqr = this.createQueryRewriter();
611             /*
612             logger.log(BasicLevel.DEBUG, "testAtoB: QueryTree before transformation");
613             logger.log(BasicLevel.DEBUG, "=========================================");
614             QueryTreePrinter.printQueryTree(qn, logger);
615             */

616             //assign mapper + project names to each extent
617
a.extent.setPMapper(mapper, getMapperName());
618             a.extent.setProjectName("test");
619             b1.extent.setPMapper(mapper, getMapperName());
620             b1.extent.setProjectName("test");
621             b2.extent.setPMapper(mapper, getMapperName());
622             b2.extent.setProjectName("test");
623             QueryTree result = jqr.transform(qn);
624             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
625             QueryTreePrinter.printQueryTree(result, logger);
626             logger.log(BasicLevel.DEBUG, "=========================================");
627
628             //TODO replace expected below with JoinedTables
629
Example ea = getExtentOfAcolocB();
630             QualifiedTable tableA = new BasicQualifiedTable("T_A", "T_A_0");
631             String JavaDoc nameA = "org.objectweb.medor.optim.jorm.rdb.AcolocB";
632             String JavaDoc prefixA = nameA + ".";
633             QualifiedTable tableB1 = new BasicQualifiedTable("T_B", "T_B_1_b1_join");
634             QualifiedTable tableB2 = new BasicQualifiedTable("T_B", "T_B_2_b2_join");
635             String JavaDoc nameB = "org.objectweb.medor.optim.jorm.rdb.BcolocC";
636             String JavaDoc prefixB = nameB + ".";
637             RdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(null,
638                     new QualifiedTable[]{tableA, tableB1, tableB2}, "");
639             RdbExpField idA = leaf.addRdbField(
640                     prefixA + "a_name", PTypeSpace.LONG, "A_PK", tableA);
641             RdbExpField b1ToA = leaf.addRdbField(
642                     "BtoA_1_FK", PTypeSpace.LONG, "BtoA_1_FK", tableB1);
643             RdbExpField f1 = leaf.addRdbField(
644                     prefixB + "f1", PTypeSpace.STRING, "col_f1", tableB1);
645             leaf.removeRdbField(f1);
646             RdbExpField b2ToA = leaf.addRdbField(
647                     "BtoA_2_FK", PTypeSpace.LONG, "BtoA_2_FK", tableB2);
648             RdbExpField f2 = leaf.addRdbField(
649                     prefixB + "f2", PTypeSpace.STRING, "col_f2", tableB2);
650             leaf.removeRdbField(f2);
651             Expression eq3 = new Equal(
652                     new BasicFieldOperand(f1), new BasicFieldOperand(f2)
653             );
654             Expression eq1 = new Equal(
655                     new BasicFieldOperand(idA), new BasicFieldOperand(b1ToA)
656             );
657             Expression eq2 = new Equal(
658                     new BasicFieldOperand(idA), new BasicFieldOperand(b2ToA)
659             );
660             Expression expRdb = new And(new And(eq1, eq2), eq3);
661             leaf.setQueryFilter(expRdb);
662             QueryNode expected = new SelectProject("");
663             SinglePName spn = new SinglePName(
664                     new BasicFieldOperand(idA),
665                     new BasicParameterOperand(
666                             PTypeSpaceMedor.NAMING_CONTEXT, prefixA + "object"),
667                     ea.extent.getField(prefixA + "object").getType());
668             expected.addCalculatedField(prefixA + "object", spn.getType(), spn);
669
670             logger.log(BasicLevel.DEBUG, "testAtoB: Expected QueryTree");
671             logger.log(BasicLevel.DEBUG, "=========================================");
672
673             QueryTreePrinter.printQueryTree(expected, logger);
674             TestQueryTreeHelper.equals("", expected, result, logger);
675             logger.log(BasicLevel.DEBUG, "End of testAtoB");
676
677         } catch (MedorException e1) {
678             e1.printStackTrace();
679             fail(e1.getMessage());
680         }
681     }
682
683     public void _testAtoBtoC() {
684         try {
685             Example a = getExtentOfAcolocB();
686             Example b = getExtentOfBcolocC();
687             Example c = getExtentOfCcoloc();
688             Expression exp1 = new Equal(
689                     new BasicFieldOperand(a.extent.getField(a.clazz.getFQName() + ".b1")),
690                     new BasicFieldOperand(b.extent.getField(b.extent.getPNameFieldName()))
691             );
692             Expression exp2 = new Equal(
693                     new BasicFieldOperand(b.extent.getField(b.clazz.getFQName() + ".c")),
694                     new BasicFieldOperand(c.extent.getField(c.extent.getPNameFieldName()))
695             );
696             Expression exp = new And(exp1, exp2);
697             QueryNode qn = new SelectProject(exp, "");
698             QueryTreeField f = (QueryTreeField) a.extent.getField(a.extent.getPNameFieldName());
699             QueryTreeField fc = (QueryTreeField) c.extent.getField(c.clazz.getFQName() + ".fc");
700             qn.addPropagatedField(f.getName(), f.getType(), new QueryTreeField[]{f});
701             qn.addPropagatedField(fc.getName(), fc.getType(), new QueryTreeField[]{fc});
702
703             // Add an OrderBy Field
704
OrderField of1 = new BasicOrderField((QueryTreeField)a.extent.getField(a.clazz.getFQName() + ".b1"), true);
705             qn.setOrderBy(new OrderField[] {of1});
706
707             QueryRewriter jqr = this.createQueryRewriter();
708             logger.log(BasicLevel.DEBUG, "testBtoC: QueryTree before transformation");
709             QueryTreePrinter.printQueryTree(qn, logger);
710             //assign mapper + project names to each extent
711
a.extent.setPMapper(mapper, getMapperName());
712             a.extent.setProjectName("test");
713             b.extent.setPMapper(mapper, getMapperName());
714             b.extent.setProjectName("test");
715             c.extent.setPMapper(mapper, getMapperName());
716             c.extent.setProjectName("test");
717             QueryTree result = jqr.transform(qn);
718             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
719             QueryTreePrinter.printQueryTree(result, logger);
720
721             //TODO replace expected below with JoinedTables
722
Example ea = getExtentOfAcolocB();
723             QualifiedTable tableA = new BasicQualifiedTable("T_A", "T_A_0");
724             String JavaDoc nameA = "org.objectweb.medor.optim.jorm.rdb.AcolocB";
725             String JavaDoc prefixA = nameA + ".";
726             QualifiedTable tableB = new BasicQualifiedTable("T_B", "T_B_1_b1_join");
727             QualifiedTable tableC = new BasicQualifiedTable("T_C", "T_C_4_c_join");
728             String JavaDoc nameB = "org.objectweb.medor.optim.jorm.rdb.BcolocC";
729             String JavaDoc prefixB = nameB + ".";
730             String JavaDoc nameC = "org.objectweb.medor.optim.jorm.rdb.Ccoloc";
731             String JavaDoc prefixC = nameC + ".";
732             RdbExpQueryLeaf leaf = new BasicRdbExpQueryLeaf(null,
733                     new QualifiedTable[]{tableA, tableC, tableB}, "");
734             QueryTreeField idA = leaf.addRdbField(
735                     prefixA + "a_name", PTypeSpace.LONG, "A_PK", tableA);
736             QueryTreeField b1ToA = leaf.addRdbField(
737                     "BtoA_1_FK", PTypeSpace.LONG, "BtoA_1_FK", tableB);
738             QueryTreeField cToB = leaf.addRdbField(
739                     "CtoB_FK", PTypeSpace.STRING, "CtoB_FK", tableC);
740             QueryTreeField idB = leaf.addRdbField(
741                     prefixB + "b_name", PTypeSpace.STRING, "B_PK", tableB);
742             QueryTreeField fcField = leaf.addRdbField(
743                     prefixC + "fc", PTypeSpace.STRING, "FC_COL", tableC);
744             Expression eq1 = new Equal(
745                     new BasicFieldOperand(idA), new BasicFieldOperand(b1ToA)
746             );
747             Expression eq2 = new Equal(
748                     new BasicFieldOperand(idB), new BasicFieldOperand(cToB)
749             );
750             Expression expRdb = new And(eq1, eq2);
751             leaf.setQueryFilter(expRdb);
752             QueryNode expected = new SelectProject("");
753             SinglePName spn = new SinglePName(
754                     new BasicFieldOperand(idA),
755                     new BasicParameterOperand(
756                             PTypeSpaceMedor.NAMING_CONTEXT, prefixA + "object"),
757                     ea.extent.getField(prefixA + "object").getType());
758             expected.addCalculatedField(prefixA + "object", spn.getType(), spn);
759             expected.addPropagatedField(prefixC + "fc", fcField.getType(), new QueryTreeField[]{fcField});
760
761             logger.log(BasicLevel.DEBUG, "testAtoB: Expected QueryTree");
762             logger.log(BasicLevel.DEBUG, "=========================================");
763
764             QueryTreePrinter.printQueryTree(expected, logger);
765             TestQueryTreeHelper.equals("", expected, result, logger);
766             logger.log(BasicLevel.DEBUG, "End of testAtoB");
767
768
769         } catch (MedorException e1) {
770             e1.printStackTrace();
771             fail(e1.getMessage());
772         }
773     }
774     
775     public void testAtoBtoCOrderBy() {
776         try {
777             Example a = getExtentOfAcolocB();
778             Example b = getExtentOfBcolocC();
779             Example c = getExtentOfCcoloc();
780
781             Expression exp2 = new Equal(
782                     new BasicFieldOperand(b.extent.getField(b.clazz.getFQName() + ".c")),
783                     new BasicFieldOperand(c.extent.getField(c.extent.getPNameFieldName()))
784             );
785             QueryNode qn2 = new SelectProject(exp2, "");
786             QueryTreeField bf1 = (QueryTreeField)b.extent.getField(b.clazz.getFQName() + ".f1");
787             QueryTreeField bf1OnQn2 =
788                     qn2.addPropagatedField(bf1.getName(), bf1.getType(), new QueryTreeField[]{bf1});
789
790             Expression exp1 = new Equal(
791                     new BasicFieldOperand(a.extent.getField(a.clazz.getFQName() + ".b1")),
792                     new BasicFieldOperand(bf1OnQn2)
793             );
794             QueryNode qn1 = new SelectProject(exp1, "");
795             QueryTreeField f = (QueryTreeField) a.extent.getField(a.extent.getPNameFieldName());
796             qn1.addPropagatedField(f.getName(), f.getType(), new QueryTreeField[]{f});
797             QueryTreeField bcOnQn1 = qn1.addPropagatedField(bf1OnQn2.getName(), bf1OnQn2.getType(), new QueryTreeField[]{bf1OnQn2});
798
799
800             //Add an additional level
801
QueryNode qn = new SelectProject(exp1, "");
802             QueryTreeField ff = (QueryTreeField)qn1.getTupleStructure().getField(a.extent.getPNameFieldName());
803             qn.addPropagatedField(ff.getName(), ff.getType(), new QueryTreeField[]{ff});
804
805             // Add an OrderBy Field
806
OrderField of1 = new BasicOrderField(bcOnQn1, true);
807             qn.setOrderBy(new OrderField[] {of1});
808
809             QueryRewriter jqr = this.createQueryRewriter();
810             logger.log(BasicLevel.DEBUG, "testBtoCOrderBy: QueryTree before transformation");
811             QueryTreePrinter.printQueryTree(qn, logger);
812             //assign mapper + project names to each extent
813
a.extent.setPMapper(mapper, PROJECT_NAME);
814             a.extent.setProjectName("test");
815             b.extent.setPMapper(mapper, PROJECT_NAME);
816             b.extent.setProjectName("test");
817             c.extent.setPMapper(mapper, PROJECT_NAME);
818             c.extent.setProjectName("test");
819             QueryTree result = jqr.transform(qn);
820             logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
821             QueryTreePrinter.printQueryTree(result, logger);
822
823             assertEquals("Bad sql request",
824                 "SELECT T_A_0.A_PK FROM T_A as T_A_0 left outer join T_B as T_B_1_b1_join on T_A_0.A_PK = T_B_1_b1_join.BtoA_1_FK, T_B as T_B_3 WHERE (((T_B_1_b1_join.B_PK singlePName ?) = T_B_3.col_f1) AND ((T_B_1_b1_join.B_PK singlePName ?) = T_B_3.col_f1)) ORDER BY T_B_3.col_f1 DESC",
825                 ((RdbExpQueryLeaf)
826                     ((QueryNode)result).getChildren()[0]).getSqlRequest(null,
827                             false, false));
828             logger.log(BasicLevel.DEBUG, "End of testAtoBtoCOrderBy");
829
830
831         } catch (Exception JavaDoc e1) {
832             e1.printStackTrace();
833             fail(e1.getMessage());
834         }
835     }
836
837     public void testCountAll() {
838         try {
839             Example e = getExtentOfAgg();
840             QueryNode qn = new SelectProject("");
841             QueryTreeField qtfpn = (QueryTreeField)
842                     e.extent.getField(e.extent.getPNameFieldName());
843             QueryTreeField fpn = qn.addPropagatedField(
844                     qtfpn.getName(),
845                     qtfpn.getType(),
846                     new QueryTreeField[]{qtfpn});
847
848             QueryTreeField qtfy = (QueryTreeField)
849             e.extent.getField(e.clazz.getFQName() + ".y");
850             QueryTreeField fy = qn.addPropagatedField(
851                     qtfy.getName(),
852                     qtfy.getType(),
853                     new QueryTreeField[]{qtfy});
854
855             Nest nest = new Nest(
856                     new QueryTreeField[] {fpn},
857                     "grouped",
858                     //new QueryTreeField[] {},
859
new QueryTreeField[] {fy},
860                     "nestNode");
861             //count with distinct
862
nest.addCalculatedField("count",
863                     PTypeSpaceMedor.INT,
864                     new Count(
865                             new BasicFieldOperand(fpn), true
866                     ));
867
868             QueryRewriter jqr = this.createQueryRewriter();
869
870             logger.log(BasicLevel.DEBUG, "testCountAll: QueryTree before transformation");
871             QueryTreePrinter.printQueryTree(nest, logger);
872             //assign mapper + project names to each extent
873
e.extent.setPMapper(mapper, PROJECT_NAME);
874             e.extent.setProjectName("test");
875             logger.log(BasicLevel.DEBUG, "testCountAll: Performing transformation");
876             QueryTree result = jqr.transform(nest);
877             //QueryTree result = jqr.transform(qn);
878
logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
879
880             QueryTreePrinter.printQueryTree(result, logger);
881             QueryLeaf leaf = getQueryLeafAgg(e);
882             QueryTreeField expC2 =
883                     (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".c2");
884             QueryTreeField expC1 =
885                     (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".c1");
886             QueryTreeField expY =
887                 (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".y");
888             leaf.setDistinct(true);
889             ((RdbQueryLeaf)leaf).setIsSubquery(true);
890             QueryNode expected = new AggregateRdbQueryNode(
891                     new QueryTreeField[]{expC1, expC2},
892                             "grouped",
893                             new QueryTreeField[] {expY},
894                             (BasicRdbExpQueryLeaf) leaf,
895                             null, "nestNode");
896             Count theCount = new Count(
897                             new BasicFieldOperand(expC2), true
898                     );
899             theCount.setCountAll();
900             expected.addCalculatedField("count",
901                     PTypeSpaceMedor.INT,
902                     theCount
903                     );
904
905             logger.log(BasicLevel.DEBUG, "testCountAll: Expected QueryTree");
906             logger.log(BasicLevel.DEBUG, "=========================================");
907             QueryTreePrinter.printQueryTree(expected, logger);
908             TestQueryTreeHelper.equals("", expected, result, logger);
909         } catch (MedorException e1) {
910             e1.printStackTrace();
911             fail(e1.getMessage());
912         }
913     }
914     
915     public void testCountWithParam() {
916         try {
917             Example e = getExtentOfA();
918             Expression exp = new Equal(
919                     new BasicFieldOperand(e.extent.getField(e.clazz.getFQName() + ".object")),
920                     new BasicParameterOperand(PTypeSpace.STRING, "?1"));
921
922             QueryNode qn = new SelectProject(exp, "");
923             QueryTreeField qtfpn = (QueryTreeField)
924                     e.extent.getField(e.extent.getPNameFieldName());
925             QueryTreeField fpn = qn.addPropagatedField(
926                     qtfpn.getName(),
927                     qtfpn.getType(),
928                     new QueryTreeField[]{qtfpn});
929
930             Nest nest = new Nest(
931                     new QueryTreeField[] {fpn},
932                     "grouped",
933                     new QueryTreeField[] {},
934                     "nestNode");
935             //count with distinct
936
nest.addCalculatedField("count",
937                     PTypeSpaceMedor.INT,
938                     new Count(
939                             new BasicFieldOperand(fpn), false
940                     ));
941
942             QueryRewriter jqr = this.createQueryRewriter();
943
944             logger.log(BasicLevel.DEBUG, "testCountAll: QueryTree before transformation");
945             QueryTreePrinter.printQueryTree(nest, logger);
946             //assign mapper + project names to each extent
947
e.extent.setPMapper(mapper, PROJECT_NAME);
948             e.extent.setProjectName("test");
949             QueryTree result = jqr.transform(nest);
950             //QueryTree result = jqr.transform(qn);
951
logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
952
953             QueryTreePrinter.printQueryTree(result, logger);
954
955             QueryLeaf leaf = getQueryLeafAggParam(e);
956             QueryTreeField qtfAid = (QueryTreeField)
957                     e.extent.getField(e.extent.getPNameFieldName());
958
959             QueryTreeField expId =
960                     (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".id");
961             ((RdbQueryLeaf)leaf).setIsSubquery(true);
962
963             ((BasicRdbExpQueryLeaf)leaf).setQueryFilter(
964                     new Equal(
965                             new BasicFieldOperand(leaf.getTupleStructure().getField(e.clazz.getFQName() + ".id")),
966                             new EncodePName(PTypeSpace.LONG, "?1")
967                     )
968             );
969             QueryNode expected = new AggregateRdbQueryNode(
970                     new QueryTreeField[]{qtfAid},
971                             "grouped",
972                             new QueryTreeField[] {},
973                             (BasicRdbExpQueryLeaf) leaf,
974                             null, "nestNode");
975
976             expected.addCalculatedField("count",
977                     PTypeSpaceMedor.INT,
978                     new Count(
979                             new BasicFieldOperand(expId), false
980                     )
981                     );
982
983             logger.log(BasicLevel.DEBUG, "testCountAll: Expected QueryTree");
984             logger.log(BasicLevel.DEBUG, "=========================================");
985             QueryTreePrinter.printQueryTree(expected, logger);
986             TestQueryTreeHelper.equals("", expected, result, logger);
987         } catch (MedorException e1) {
988             e1.printStackTrace();
989             fail(e1.getMessage());
990         }
991     }
992
993     public void testAvgDirect() {
994         try {
995             Example e = getExtentOfAgg();
996
997             QueryTreeField qtf1 = (QueryTreeField)
998                     e.extent.getField(e.clazz.getFQName() + ".c1");
999
1000            QueryTreeField qtfy = (QueryTreeField)
1001                e.extent.getField(e.clazz.getFQName() + ".y");
1002
1003            Nest nest = new Nest(
1004                    new QueryTreeField[] {qtf1},
1005                    "grouped",
1006                    new QueryTreeField[] {qtfy},
1007                    "nestNode");
1008            //count with distinct
1009
nest.addCalculatedField("avg",
1010                    PTypeSpaceMedor.INT,
1011                    new Avg(
1012                            new BasicFieldOperand(qtf1), true
1013                    ));
1014
1015            nest.addPropagatedField(qtfy.getName(), qtfy.getType(), new QueryTreeField[]{qtfy});
1016            
1017            QueryRewriter jqr = this.createQueryRewriter();
1018
1019            logger.log(BasicLevel.DEBUG, "testAvgDirect: QueryTree before transformation");
1020            QueryTreePrinter.printQueryTree(nest, logger);
1021            //assign mapper + project names to each extent
1022
e.extent.setPMapper(mapper, PROJECT_NAME);
1023            e.extent.setProjectName("test");
1024            QueryTree result = jqr.transform(nest);
1025            //QueryTree result = jqr.transform(qn);
1026
logger.log(BasicLevel.DEBUG, "testAvgDirect: QueryTree rewritten");
1027
1028            QueryTreePrinter.printQueryTree(result, logger);
1029            QueryLeaf leaf = getQueryLeafAggDirect(e);
1030            QueryTreeField expC1 =
1031                    (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".c1");
1032            QueryTreeField expY =
1033                (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".y");
1034            //((RdbQueryLeaf)leaf).setIsSubquery(false);
1035
QueryNode expected = new AggregateRdbQueryNode(
1036                    new QueryTreeField[]{expC1},
1037                            "grouped",
1038                            new QueryTreeField[] {expY},
1039                            (BasicRdbExpQueryLeaf) leaf,
1040                            null, "nestNode");
1041            Avg theAvg = new Avg(
1042                            new BasicFieldOperand(expC1), true
1043                    );
1044            expected.addCalculatedField("avg",
1045                    PTypeSpaceMedor.INT,
1046                    theAvg
1047                    );
1048            expected.addPropagatedField(expY.getName(), expY.getType(), new QueryTreeField[]{expY});
1049
1050
1051            logger.log(BasicLevel.DEBUG, "testAvgDirect: Expected QueryTree");
1052            logger.log(BasicLevel.DEBUG, "=========================================");
1053            QueryTreePrinter.printQueryTree(expected, logger);
1054            TestQueryTreeHelper.equals("", expected, result, logger);
1055        } catch (MedorException e1) {
1056            e1.printStackTrace();
1057            fail(e1.getMessage());
1058        }
1059    }
1060    
1061    public void testAvgSum() {
1062        try {
1063            Example e = getExtentOfAgg();
1064
1065            Expression exp = new Equal(
1066                    new BasicFieldOperand(e.extent.getField(e.clazz.getFQName() + ".c2")),
1067                    new BasicParameterOperand(PTypeSpace.STRING, "?1"));
1068            QueryNode qn = new SelectProject(exp, "");
1069
1070            QueryTreeField qtf1 = (QueryTreeField)
1071                    e.extent.getField(e.clazz.getFQName() + ".c1");
1072            QueryTreeField fc1 = qn.addPropagatedField(
1073                    qtf1.getName(),
1074                    qtf1.getType(),
1075                    new QueryTreeField[]{qtf1});
1076            QueryTreeField qtfy = (QueryTreeField)
1077                    e.extent.getField(e.clazz.getFQName() + ".y");
1078            QueryTreeField fcy = qn.addPropagatedField(
1079                    qtfy.getName(),
1080                    qtfy.getType(),
1081                    new QueryTreeField[]{qtfy});
1082
1083            QueryTreeField qtfpn = (QueryTreeField)
1084                    e.extent.getField(e.extent.getPNameFieldName());
1085
1086
1087            Nest nest = new Nest(
1088                    new QueryTreeField[] {fc1, fcy},
1089                    "grouped",
1090                    new QueryTreeField[] {},
1091                    "nestNode");
1092            nest.addCalculatedField("avg",
1093                    PTypeSpaceMedor.FLOAT,
1094                    new Avg(
1095                            new BasicFieldOperand(fc1), true
1096                    ));
1097            nest.addCalculatedField("sum",
1098                    PTypeSpaceMedor.FLOAT,
1099                    new Sum(
1100                            new BasicFieldOperand(fcy)
1101                    ));
1102
1103            QueryRewriter jqr = this.createQueryRewriter();
1104
1105            logger.log(BasicLevel.DEBUG, "testAvgSum: QueryTree before transformation");
1106            QueryTreePrinter.printQueryTree(nest, logger);
1107            //assign mapper + project names to each extent
1108
e.extent.setPMapper(mapper, PROJECT_NAME);
1109            e.extent.setProjectName("test");
1110            QueryTree result = jqr.transform(nest);
1111            //QueryTree result = jqr.transform(qn);
1112
logger.log(BasicLevel.DEBUG, "testAvgSum: QueryTree rewritten");
1113
1114            QueryTreePrinter.printQueryTree(result, logger);
1115
1116            //testing indexes generation
1117
IndexesGenerator ig = new IndexesGenerator();
1118            ig.transform(result);
1119
1120            QueryLeaf leaf = getQueryLeafAggWithWhere(e);
1121
1122            QueryTreeField expC1 =
1123                    (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".c1");
1124            QueryTreeField expy =
1125                    (QueryTreeField) leaf.getTupleStructure().getField(e.clazz.getFQName() + ".y");
1126            QueryNode expected = new AggregateRdbQueryNode(
1127                    new QueryTreeField[]{expC1, expy},
1128                            "grouped",
1129                            new QueryTreeField[] {},
1130                            (BasicRdbExpQueryLeaf) leaf,
1131                            null, "nestNode");
1132            expected.addCalculatedField("avg",
1133                    PTypeSpaceMedor.FLOAT,
1134                    new Avg(
1135                            new BasicFieldOperand(expC1), true
1136                    ));
1137            expected.addCalculatedField("sum",
1138                    PTypeSpaceMedor.FLOAT,
1139                    new Sum(
1140                            new BasicFieldOperand(expy)
1141                    ));
1142
1143            logger.log(BasicLevel.DEBUG, "testAvgSum: Expected QueryTree");
1144            logger.log(BasicLevel.DEBUG, "=========================================");
1145            QueryTreePrinter.printQueryTree(expected, logger);
1146            TestQueryTreeHelper.equals("", expected, result, logger);
1147        } catch (MedorException e1) {
1148            e1.printStackTrace();
1149            fail(e1.getMessage());
1150        }
1151    }
1152
1153    public void _testInheritance() {
1154        try {
1155            Example e = getExtentOfAnimal();
1156            e.extent.setWithSubClasses(true);
1157            Expression exp = new Equal(
1158                    new BasicFieldOperand(e.extent.getField(e.clazz.getFQName() + ".species")),
1159                    new BasicOperand("marsupial"));
1160            QueryNode qn = new SelectProject(exp, "");
1161            QueryTreeField qtf = (QueryTreeField)
1162                    e.extent.getField(e.extent.getPNameFieldName());
1163            qn.addPropagatedField(
1164                    qtf.getName(),
1165                    qtf.getType(),
1166                    new QueryTreeField[]{qtf});
1167            QueryRewriter jqr = this.createQueryRewriter();
1168
1169            logger.log(BasicLevel.DEBUG, "testInheritance: QueryTree before transformation");
1170            QueryTreePrinter.printQueryTree(qn, logger);
1171            //assign mapper + project names to each extent
1172
e.extent.setPMapper(mapper, PROJECT_NAME);
1173            e.extent.setProjectName("test");
1174            QueryTree result = jqr.transform(qn);
1175            logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
1176
1177            QueryTreePrinter.printQueryTree(result, logger);
1178            logger.log(BasicLevel.DEBUG, "End of QueryTree rewritten");
1179            QueryLeaf leaf = getQueryLeafAnimal(e);
1180            QueryNode expected = new SelectProject("");
1181            expected.addCalculatedField(
1182                    e.clazz.getFQName() + ".object",
1183                    e.extent.getField(e.extent.getPNameFieldName()).getType(),
1184                    new SinglePName(
1185                            new BasicFieldOperand(
1186                                    leaf.getTupleStructure().getField(e.clazz.getFQName()
1187                    + "." + "animal_id")),
1188                            new BasicParameterOperand(
1189                                    PTypeSpaceMedor.NAMING_CONTEXT,
1190                                    "A"),
1191                            e.extent.getField(e.clazz.getFQName() + ".object").getType()
1192                    )
1193            );
1194
1195            logger.log(BasicLevel.DEBUG, "testInheritance: Expected QueryTree");
1196            logger.log(BasicLevel.DEBUG, "=========================================");
1197            QueryTreePrinter.printQueryTree(expected, logger);
1198            TestQueryTreeHelper.equals("", expected, result, logger);
1199        } catch (MedorException e1) {
1200            e1.printStackTrace();
1201            fail(e1.getMessage());
1202        }
1203
1204    }
1205
1206    public void _testInheritanceNF() {
1207        try {
1208            Example e = getExtentOfAnimalNF();
1209            e.extent.setWithSubClasses(true);
1210            Expression exp = new Equal(
1211                    new BasicFieldOperand(e.extent.getField(e.clazz.getFQName() + ".species")),
1212                    new BasicOperand("marsupial"));
1213            QueryNode qn = new SelectProject(exp, "");
1214            QueryTreeField qtf = (QueryTreeField)
1215                    e.extent.getField(e.extent.getPNameFieldName());
1216            qn.addPropagatedField(
1217                    qtf.getName(),
1218                    qtf.getType(),
1219                    new QueryTreeField[]{qtf});
1220            QueryRewriter jqr = this.createQueryRewriter();
1221
1222            logger.log(BasicLevel.DEBUG, "testInheritance: QueryTree before transformation");
1223            QueryTreePrinter.printQueryTree(qn, logger);
1224            //assign mapper + project names to each extent
1225
e.extent.setPMapper(mapper, PROJECT_NAME);
1226            e.extent.setProjectName("test");
1227            QueryTree result = jqr.transform(qn);
1228            logger.log(BasicLevel.DEBUG, "QueryTree rewritten");
1229
1230            QueryTreePrinter.printQueryTree(result, logger);
1231            logger.log(BasicLevel.DEBUG, "End of QueryTree rewritten");
1232            QueryLeaf leaf = getQueryLeafAnimal(e);
1233            QueryNode expected = new SelectProject("");
1234            //TODO use composite pname instead
1235
expected.addCalculatedField(
1236                    e.clazz.getFQName() + ".object",
1237                    e.extent.getField(e.extent.getPNameFieldName()).getType(),
1238                    new SinglePName(
1239                            new BasicFieldOperand(
1240                                    leaf.getTupleStructure().getField(e.clazz.getFQName()
1241                    + "." + "animal_id")),
1242                            new BasicParameterOperand(
1243                                    PTypeSpaceMedor.NAMING_CONTEXT,
1244                                    "A"),
1245                            e.extent.getField(e.clazz.getFQName() + ".object").getType()
1246                    )
1247            );
1248
1249            logger.log(BasicLevel.DEBUG, "testInheritance: Expected QueryTree");
1250            logger.log(BasicLevel.DEBUG, "=========================================");
1251            QueryTreePrinter.printQueryTree(expected, logger);
1252            TestQueryTreeHelper.equals("", expected, result, logger);
1253        } catch (MedorException e1) {
1254            e1.printStackTrace();
1255            fail(e1.getMessage());
1256        }
1257
1258    }
1259
1260}
1261
Popular Tags