KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > query > TestQueries


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S. Chassande-Barrioz
25  *
26  */

27
28 package org.objectweb.speedo.runtime.query;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.NoSuchElementException JavaDoc;
40
41 import javax.jdo.Extent;
42 import javax.jdo.JDOException;
43 import javax.jdo.PersistenceManager;
44 import javax.jdo.Query;
45
46 import junit.framework.Assert;
47
48 import org.objectweb.speedo.SpeedoTestHelper;
49 import org.objectweb.speedo.api.ExceptionHelper;
50 import org.objectweb.speedo.pobjects.basic.BasicA;
51 import org.objectweb.speedo.pobjects.basic.Product;
52 import org.objectweb.speedo.pobjects.collection.AMMB;
53 import org.objectweb.speedo.pobjects.collection.BMMB;
54 import org.objectweb.speedo.pobjects.collection.Group;
55 import org.objectweb.speedo.pobjects.collection.Ref2Ref2AMMB;
56 import org.objectweb.speedo.pobjects.collection.User;
57 import org.objectweb.speedo.pobjects.inheritance.query.GroupUser;
58 import org.objectweb.speedo.pobjects.inheritance.query.MailingList;
59 import org.objectweb.speedo.pobjects.inheritance.query.NewsGroup;
60 import org.objectweb.speedo.pobjects.inheritance.userCache.Litem;
61 import org.objectweb.speedo.pobjects.ref.Department;
62 import org.objectweb.speedo.pobjects.ref.Employee;
63 import org.objectweb.speedo.pobjects.ref.GeoRef;
64 import org.objectweb.speedo.pobjects.userid.AutoIncFieldId;
65 import org.objectweb.speedo.pobjects.userid.Ref2AutoIncFieldId;
66 import org.objectweb.util.monolog.api.BasicLevel;
67
68 /**
69  * This test tests the access to 2 fields from the Employee class (name
70  * and salary), Salary is a Float (with 2 parameters, a String and a Float
71  * object).
72  */

73 public class TestQueries extends SpeedoTestHelper {
74
75     public TestQueries(String JavaDoc name) {
76         super(name);
77     }
78
79     protected String JavaDoc getLoggerName() {
80         return LOG_NAME + ".rt.query.TestQueries";
81     }
82
83     public void test0Parameter() {
84         logger.log(BasicLevel.DEBUG, "test0Parameter");
85         PersistenceManager pm = pmf.getPersistenceManager();
86         try {
87             Class JavaDoc empClass = Employee.class;
88             Query query = pm.newQuery(empClass);
89             Collection JavaDoc col = (Collection JavaDoc) query.execute();
90             Iterator JavaDoc it = col.iterator();
91             while(it.hasNext()) {
92                 Employee e = (Employee) it.next();
93                 Assert.assertNotNull("null element in the query result", e);
94                 Assert.assertNotNull("null name of a element in the query result", e.getName());
95             }
96             query.close(col);
97         } catch (JDOException e) {
98             Exception JavaDoc ie = ExceptionHelper.getNested(e);
99             logger.log(BasicLevel.ERROR, "", ie);
100             fail(ie.getMessage());
101         } finally {
102             pm.close();
103         }
104     }
105
106     public void test10rder() {
107         logger.log(BasicLevel.DEBUG, "test10rder");
108         PersistenceManager pm = pmf.getPersistenceManager();
109         try {
110             Class JavaDoc empClass = Employee.class;
111             Query query = pm.newQuery(empClass);
112             query.setOrdering("name ascending");
113             Collection JavaDoc col = (Collection JavaDoc) query.execute();
114             Iterator JavaDoc it = col.iterator();
115             int i=0;
116             while(it.hasNext()) {
117                 assertTrue("More result than expected ", i<POBuilder.nameOrder.length);
118                 Employee e = (Employee) it.next();
119                 Assert.assertNotNull("null element in the query result", e);
120                 try {
121                     Assert.assertEquals("Bad order (" + i + ")",
122                         POBuilder.names[POBuilder.nameOrder[i]],e.getName());
123                 } catch (Throwable JavaDoc e1) {
124                     e1.printStackTrace();
125                 }
126                 i++;
127             }
128             assertEquals("Not enough result", POBuilder.nameOrder.length, i);
129             query.close(col);
130         } catch (JDOException e) {
131             Exception JavaDoc ie = ExceptionHelper.getNested(e);
132             logger.log(BasicLevel.ERROR, "", ie);
133             fail(ie.getMessage());
134         } finally {
135             pm.close();
136         }
137     }
138
139     public void test20rder() {
140         logger.log(BasicLevel.DEBUG, "test20rder");
141         PersistenceManager pm = pmf.getPersistenceManager();
142         Class JavaDoc empClass = Employee.class;
143         Query query = pm.newQuery(empClass);
144         Collection JavaDoc col = null;
145         try {
146             query.setOrdering("salary descending, name ascending");
147             col = (Collection JavaDoc) query.execute();
148             Iterator JavaDoc it = col.iterator();
149             int i=0;
150             while(it.hasNext()) {
151                 assertTrue("More result than expected ", i<POBuilder.salariesOrder.length);
152                 Assert.assertTrue("No enough values in the result", it.hasNext());
153                 Employee e = (Employee) it.next();
154                 Assert.assertNotNull("null element in the query result", e);
155                 try {
156                     Assert.assertEquals("Bad order (" + i + ")",
157                         POBuilder.names[POBuilder.salariesOrder[i]],e.getName());
158                 } catch (Throwable JavaDoc e1) {
159                     e1.printStackTrace();
160                 }
161                 i++;
162             }
163             assertEquals("Not enough result", POBuilder.salariesOrder.length, i);
164         } catch (JDOException e) {
165             Exception JavaDoc ie = ExceptionHelper.getNested(e);
166             logger.log(BasicLevel.ERROR, "", ie);
167             fail(ie.getMessage());
168         } finally {
169             try {
170                 query.closeAll();
171             } finally {
172                 pm.close();
173             }
174         }
175     }
176
177     public void test1Parameter() {
178         logger.log(BasicLevel.DEBUG, "test1Parameter");
179         PersistenceManager pm = pmf.getPersistenceManager();
180         try {
181             Class JavaDoc empClass = Employee.class;
182             Query query = pm.newQuery(empClass);
183             query.declareParameters("String aName");
184             query.setFilter("(name == aName)");
185             Collection JavaDoc col = (Collection JavaDoc) query.execute(POBuilder.names[1]);
186             Iterator JavaDoc iter = col.iterator();
187             Assert.assertTrue("The query result is empty", iter.hasNext());
188             Employee e = (Employee) iter.next();
189             Assert.assertNotNull("Null collection element", e);
190             Assert.assertEquals("Bad employee name", POBuilder.names[1], e.getName());
191             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
192             Assert.assertEquals("Bad query result size", 1, col.size());
193             query.close(col);
194         } catch (JDOException e) {
195             Exception JavaDoc ie = ExceptionHelper.getNested(e);
196             logger.log(BasicLevel.ERROR, "", ie);
197             fail(ie.getMessage());
198         } finally {
199             pm.close();
200         }
201     }
202
203     public void test1ParameterBis() {
204         logger.log(BasicLevel.DEBUG, "test1ParameterBis");
205         PersistenceManager pm = pmf.getPersistenceManager();
206         try {
207             Class JavaDoc empClass = Employee.class;
208             Query query = pm.newQuery(empClass);
209             query.declareParameters("float sal");
210             query.setFilter("(salary > sal)");
211             Collection JavaDoc col = (Collection JavaDoc) query.execute(new Float JavaDoc(POBuilder.salaries[2]));
212             Iterator JavaDoc iter = col.iterator();
213             HashSet JavaDoc s = new HashSet JavaDoc();
214             while(iter.hasNext()) {
215                 Employee e = (Employee) iter.next();
216                 s.add(e.getName());
217             }
218             query.close(col);
219             assertSameCollection("Bad query result",
220                 Arrays.asList(new String JavaDoc[]{POBuilder.names[0],POBuilder.names[1],POBuilder.names[3]}),
221                 s);
222         } catch (JDOException e) {
223             Exception JavaDoc ie = ExceptionHelper.getNested(e);
224             logger.log(BasicLevel.ERROR, "", ie);
225             fail(ie.getMessage());
226         } finally {
227             pm.close();
228         }
229     }
230
231     public void testFieldParameter() {
232         logger.log(BasicLevel.DEBUG, "testFieldParameter");
233         PersistenceManager pm = pmf.getPersistenceManager();
234         try {
235             Class JavaDoc empClass = Employee.class;
236             Query query = pm.newQuery(empClass);
237
238             String JavaDoc filterFieldParameter = "((salary == mysalary) && (name == myname))";
239             String JavaDoc param = "String myname, Float mysalary";
240
241             query.declareParameters(param);
242             query.setFilter(filterFieldParameter);
243
244             Collection JavaDoc col = (Collection JavaDoc)
245                     query.execute(POBuilder.names[1], new Float JavaDoc(3000.0));
246             Iterator JavaDoc iter = col.iterator();
247             Assert.assertTrue("The query result is empty", iter.hasNext());
248             Employee e = (Employee) iter.next();
249             Assert.assertNotNull("Null collection element", e);
250             Assert.assertEquals("Bad employee name", POBuilder.names[1], e.getName());
251             Assert.assertEquals("Bad employee salary", new Float JavaDoc(3000.0), new Float JavaDoc(e.getSalary()));
252             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
253             Assert.assertEquals("Bad query result size", 1, col.size());
254             query.close(col);
255         } catch (JDOException e) {
256             Exception JavaDoc ie = ExceptionHelper.getNested(e);
257             logger.log(BasicLevel.ERROR, "", ie);
258             fail(ie.getMessage());
259         } finally {
260             pm.close();
261         }
262     }
263
264     public void testMatches() {
265         logger.log(BasicLevel.DEBUG, "testMatches");
266         PersistenceManager pm = pmf.getPersistenceManager();
267         try {
268             Query query = pm.newQuery(Employee.class);
269             String JavaDoc filterFieldParameter = "((name.toUpperCase().matches(\"%IE\")) && (name.length()>2))";
270             query.setFilter(filterFieldParameter);
271             ArrayList JavaDoc col = new ArrayList JavaDoc((Collection JavaDoc) query.execute());
272             query.closeAll();
273             for(int i=0; i<col.size(); i++) {
274                 col.add(i, ((Employee) col.remove(i)).getName());
275             }
276             assertSameCollection("Bad result of the testMatches method",
277                 Arrays.asList(new String JavaDoc[]{ POBuilder.names[2], POBuilder.names[3]}),
278                 col);
279         } catch (JDOException e) {
280             Exception JavaDoc ie = ExceptionHelper.getNested(e);
281             logger.log(BasicLevel.ERROR, "", ie);
282             fail(ie.getMessage());
283         } finally {
284             pm.close();
285         }
286     }
287
288     public void testStartsWith() {
289         logger.log(BasicLevel.DEBUG, "testStartsWith");
290         PersistenceManager pm = pmf.getPersistenceManager();
291         try {
292             Query query = pm.newQuery(Employee.class);
293             String JavaDoc filterFieldParameter = "(name.toUpperCase().startsWith(\"JOH\"))";
294             query.setFilter(filterFieldParameter);
295             ArrayList JavaDoc col = new ArrayList JavaDoc((Collection JavaDoc) query.execute());
296             query.closeAll();
297             for(int i=0; i<col.size(); i++) {
298                 col.add(i, ((Employee) col.remove(i)).getName());
299             }
300             assertSameCollection("Bad result of the testStartsWith method",
301                 Arrays.asList(new String JavaDoc[]{ POBuilder.names[0]}),
302                 col);
303         } catch (JDOException e) {
304             Exception JavaDoc ie = ExceptionHelper.getNested(e);
305             logger.log(BasicLevel.ERROR, "", ie);
306             fail(ie.getMessage());
307         } finally {
308             pm.close();
309         }
310     }
311     
312     public void _testSubString() {
313 //TODO: support Substring operator in parsing
314
logger.log(BasicLevel.DEBUG, "testMatches");
315         PersistenceManager pm = pmf.getPersistenceManager();
316         try {
317             Query query = pm.newQuery(Employee.class);
318             String JavaDoc filterFieldParameter = "(name.substring(0 2).matches(\"%ie\"))";
319             query.setFilter(filterFieldParameter);
320             ArrayList JavaDoc col = new ArrayList JavaDoc((Collection JavaDoc) query.execute());
321             query.closeAll();
322             for(int i=0; i<col.size(); i++) {
323                 col.add(i, ((Employee) col.remove(i)).getName());
324             }
325             assertSameCollection("Bad result of the testMatches method",
326                 Arrays.asList(new String JavaDoc[]{ POBuilder.names[2], POBuilder.names[3]}),
327                 col);
328         } catch (JDOException e) {
329             Exception JavaDoc ie = ExceptionHelper.getNested(e);
330             logger.log(BasicLevel.ERROR, "", ie);
331             fail(ie.getMessage());
332         } finally {
333             pm.close();
334         }
335     }
336
337     public void testUpper() {
338         logger.log(BasicLevel.DEBUG, "testMatches");
339         PersistenceManager pm = pmf.getPersistenceManager();
340         try {
341             Query query = pm.newQuery(Employee.class);
342             String JavaDoc filterFieldParameter = "(name.toUpperCase().matches(\"%IE\"))";
343             query.setFilter(filterFieldParameter);
344             ArrayList JavaDoc col = new ArrayList JavaDoc((Collection JavaDoc) query.execute());
345             query.closeAll();
346             for(int i=0; i<col.size(); i++) {
347                 col.add(i, ((Employee) col.remove(i)).getName());
348             }
349             assertSameCollection("Bad result of the testMatches method",
350                 Arrays.asList(new String JavaDoc[]{ POBuilder.names[2], POBuilder.names[3]}),
351                 col);
352         } catch (JDOException e) {
353             Exception JavaDoc ie = ExceptionHelper.getNested(e);
354             logger.log(BasicLevel.ERROR, "", ie);
355             fail(ie.getMessage());
356         } finally {
357             pm.close();
358         }
359     }
360
361     public void test2Parameters() {
362         logger.log(BasicLevel.DEBUG, "test2Parameters");
363         PersistenceManager pm = pmf.getPersistenceManager();
364         try {
365             Class JavaDoc empClass = Employee.class;
366             Query query = pm.newQuery(empClass);
367             query.declareParameters("String aName, String anotherName");
368             query.setFilter("((name == aName) || (name == anotherName))");
369             Collection JavaDoc col = (Collection JavaDoc)
370                     query.execute(POBuilder.names[1], POBuilder.names[2]);
371             Iterator JavaDoc iter = col.iterator();
372             Assert.assertTrue("The query result is empty", iter.hasNext());
373             Employee e1 = (Employee) iter.next();
374             Assert.assertNotNull("Null collection element 1", e1);
375             Assert.assertTrue("Only one element in the query result", iter.hasNext());
376             Employee e2 = (Employee) iter.next();
377             Assert.assertNotNull("Null collection element 2", e2);
378             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
379             Assert.assertEquals("Bad query result size", 2, col.size());
380             query.close(col);
381
382             if (POBuilder.names[1].equals(e1.getName())) {
383                 Assert.assertEquals("Bad employee name", POBuilder.names[2], e2.getName());
384             } else if (POBuilder.names[1].equals(e2.getName())) {
385                 Assert.assertEquals("Bad employee name", POBuilder.names[2], e1.getName());
386             } else {
387                 fail("Bad employee name");
388             }
389         } catch (JDOException e) {
390             Exception JavaDoc ie = ExceptionHelper.getNested(e);
391             logger.log(BasicLevel.ERROR, "", ie);
392             fail(ie.getMessage());
393         } finally {
394             pm.close();
395         }
396     }
397
398     public void test3Parameters() {
399         logger.log(BasicLevel.DEBUG, "test3Parameters");
400         PersistenceManager pm = pmf.getPersistenceManager();
401         try {
402             Class JavaDoc empClass = Employee.class;
403             Query query = pm.newQuery(empClass);
404             String JavaDoc filter = "(name == aName) | (name == anotherName) | (name == thirdName)";
405             String JavaDoc param = "String aName, String anotherName, String thirdName";
406             query.declareParameters(param);
407             query.setFilter(filter);
408             Collection JavaDoc col = (Collection JavaDoc) query.execute(POBuilder.names[1],
409                                                         POBuilder.names[2],
410                                                         POBuilder.names[3]);
411             Iterator JavaDoc iter = col.iterator();
412             Collection JavaDoc expectedNames = new ArrayList JavaDoc(3);
413             Collection JavaDoc foundNames = new ArrayList JavaDoc(3);
414             expectedNames.add(POBuilder.names[1]);
415             expectedNames.add(POBuilder.names[2]);
416             expectedNames.add(POBuilder.names[3]);
417             Assert.assertTrue("The query result is empty", iter.hasNext());
418             Employee e = (Employee) iter.next();
419             Assert.assertNotNull("Null collection element 1", e);
420             foundNames.add(e.getName());
421             Assert.assertTrue("Only one element in the query result", iter.hasNext());
422             e = (Employee) iter.next();
423             Assert.assertNotNull("Null collection element 2", e);
424             foundNames.add(e.getName());
425             e = (Employee) iter.next();
426             Assert.assertNotNull("Null collection element 3", e);
427             foundNames.add(e.getName());
428             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
429             Assert.assertEquals("Bad query result size", expectedNames.size(), col.size());
430             query.close(col);
431
432             Assert.assertTrue("Bad Employee names, expected:"
433                     + expectedNames + " / found: " + foundNames,
434                     expectedNames.containsAll(foundNames)
435                     && foundNames.containsAll(expectedNames));
436         } catch (JDOException e) {
437             Exception JavaDoc ie = ExceptionHelper.getNested(e);
438             logger.log(BasicLevel.ERROR, "", ie);
439             fail(ie.getMessage());
440         } finally {
441             pm.close();
442         }
443     }
444
445     public void testMapParameters() {
446         logger.log(BasicLevel.DEBUG, "testMapParameters");
447         PersistenceManager pm = pmf.getPersistenceManager();
448         try {
449             Class JavaDoc empClass = Employee.class;
450             Query query = pm.newQuery(empClass);
451
452             String JavaDoc filter = "((name == aName) || (name == anotherName))";
453             String JavaDoc param = "String aName, String anotherName";
454
455             query.declareParameters(param);
456             query.setFilter(filter);
457
458             Map JavaDoc map = new HashMap JavaDoc();
459             map.put("aName", POBuilder.names[1]);
460             map.put("anotherName", POBuilder.names[2]);
461
462             Collection JavaDoc col = (Collection JavaDoc) query.executeWithMap(map);
463             Iterator JavaDoc iter = col.iterator();
464             Collection JavaDoc foundNames = new ArrayList JavaDoc(2);
465             Collection JavaDoc expectedNames = map.values();
466
467             Assert.assertTrue("The query result is empty", iter.hasNext());
468             Employee e = (Employee) iter.next();
469             Assert.assertNotNull("Null collection element 1", e);
470             foundNames.add(e.getName());
471             Assert.assertTrue("Only one element in the query result", iter.hasNext());
472             e = (Employee) iter.next();
473             Assert.assertNotNull("Null collection element 2", e);
474             foundNames.add(e.getName());
475             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
476             Assert.assertEquals("Bad query result size", expectedNames.size(), col.size());
477             query.close(col);
478
479             Assert.assertTrue("Bad Employee names, expected:"
480                     + expectedNames + " / found: " + foundNames,
481                     expectedNames.containsAll(foundNames)
482                     && foundNames.containsAll(expectedNames));
483         } catch (JDOException e) {
484             Exception JavaDoc ie = ExceptionHelper.getNested(e);
485             logger.log(BasicLevel.ERROR, "", ie);
486             fail(ie.getMessage());
487         } finally {
488             pm.close();
489         }
490     }
491
492     public void testArrayParameters() {
493         logger.log(BasicLevel.DEBUG, "testArrayParameters");
494         PersistenceManager pm = pmf.getPersistenceManager();
495         try {
496             Class JavaDoc empClass = Employee.class;
497             Query query = pm.newQuery(empClass);
498
499             String JavaDoc filter = "((name == aName) | (name == anotherName))";
500             String JavaDoc param = "String aName, String anotherName";
501
502             query.declareParameters(param);
503             query.setFilter(filter);
504
505             Collection JavaDoc expectedNames = new ArrayList JavaDoc(2);
506             expectedNames.add(POBuilder.names[1]);
507             expectedNames.add(POBuilder.names[2]);
508             Collection JavaDoc col = (Collection JavaDoc) query.executeWithArray(expectedNames.toArray());
509             Iterator JavaDoc iter = col.iterator();
510
511             Collection JavaDoc foundNames = new ArrayList JavaDoc(2);
512             Assert.assertTrue("The query result is empty", iter.hasNext());
513             Employee e = (Employee) iter.next();
514             Assert.assertNotNull("Null collection element 1", e);
515             foundNames.add(e.getName());
516             Assert.assertTrue("Only one element in the query result", iter.hasNext());
517             e = (Employee) iter.next();
518             Assert.assertNotNull("Null collection element 2", e);
519             foundNames.add(e.getName());
520             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
521             Assert.assertEquals("Bad query result size", expectedNames.size(), col.size());
522             query.close(col);
523
524             Assert.assertTrue("Bad Employee names, expected:"
525                     + expectedNames + " / found: " + foundNames,
526                     expectedNames.containsAll(foundNames)
527                     && foundNames.containsAll(expectedNames));
528         } catch (JDOException e) {
529             Exception JavaDoc ie = ExceptionHelper.getNested(e);
530             logger.log(BasicLevel.ERROR, "", ie);
531             fail(ie.getMessage());
532         } finally {
533             pm.close();
534         }
535     }
536
537     public void testFieldRef() {
538         logger.log(BasicLevel.DEBUG, "testFieldRef");
539         PersistenceManager pm = pmf.getPersistenceManager();
540         try {
541             Class JavaDoc empClass = Employee.class;
542             Query query = pm.newQuery(empClass);
543             String JavaDoc filterFieldRef = "((dept.name == depName) && (name == \""
544                 + POBuilder.names[0] + "\"))";
545             String JavaDoc param = "String depName";
546
547             query.declareParameters(param);
548             query.setFilter(filterFieldRef);
549
550             Collection JavaDoc col = (Collection JavaDoc) query.execute("RD");
551             Iterator JavaDoc iter = col.iterator();
552             Assert.assertTrue("The query result is empty", iter.hasNext());
553             Employee e = (Employee) iter.next();
554             Assert.assertNotNull("Null collection element", e);
555             Assert.assertEquals("Bad employee name", POBuilder.names[0], e.getName());
556             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
557             Assert.assertEquals("Bad query result size", 1, col.size());
558
559             iter = col.iterator();
560             Assert.assertTrue("The query result is empty", iter.hasNext());
561             e = (Employee) iter.next();
562             Assert.assertNotNull("Null collection element", e);
563             Assert.assertEquals("Bad employee name", POBuilder.names[0], e.getName());
564             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
565
566             query.close(col);
567         } catch (Exception JavaDoc e) {
568             Exception JavaDoc ie = ExceptionHelper.getNested(e);
569             logger.log(BasicLevel.ERROR, "", ie);
570             fail(ie.getMessage());
571         } finally {
572             pm.close();
573         }
574     }
575
576     public void testFieldRefNull() {
577         logger.log(BasicLevel.DEBUG, "testFieldRef");
578         PersistenceManager pm = pmf.getPersistenceManager();
579         final int nbEmp = 5;
580         Object JavaDoc[] oid = new Object JavaDoc[nbEmp];
581         for(int i=0; i<nbEmp; i++) {
582             Employee e = new Employee("testFieldRefNull" + i, null);
583             pm.makePersistent(e);
584             oid[i] = pm.getObjectId(e);
585         }
586         pm.close();
587         try {
588             pm = pmf.getPersistenceManager();
589             Query query = pm.newQuery(Employee.class);
590             query.declareParameters("Department d");
591             query.setFilter("(dept == d)");
592             Collection JavaDoc col = (Collection JavaDoc) query.execute(null);
593             int size = col.size();
594             query.close(col);
595             assertEquals("Bad number of employee without department", nbEmp, size);
596         } catch (Exception JavaDoc e) {
597             Exception JavaDoc ie = ExceptionHelper.getNested(e);
598             logger.log(BasicLevel.ERROR, "", ie);
599             fail(ie.getMessage());
600         } finally {
601             pm.currentTransaction().begin();
602             for(int i=0; i<nbEmp; i++) {
603                 Employee e = (Employee) pm.getObjectById(oid[i], false);
604                 pm.deletePersistent(e);
605             }
606             pm.currentTransaction().commit();
607             pm.close();
608         }
609     }
610
611     public void testThis() {
612         logger.log(BasicLevel.DEBUG, "testThis");
613         PersistenceManager pm = pmf.getPersistenceManager();
614         try {
615             Class JavaDoc empClass = Employee.class;
616             Query query = pm.newQuery(empClass);
617
618             String JavaDoc filter = "(this.name == aName)";
619             String JavaDoc param = "String aName";
620
621             query.declareParameters(param);
622             query.setFilter(filter);
623
624             Collection JavaDoc col = (Collection JavaDoc) query.execute(POBuilder.names[1]);
625             Iterator JavaDoc iter = col.iterator();
626             Assert.assertTrue("The query result is empty", iter.hasNext());
627             Employee e = (Employee) iter.next();
628             Assert.assertNotNull("Null collection element", e);
629             Assert.assertEquals("Bad employee name", POBuilder.names[1], e.getName());
630             Assert.assertTrue("More than one element in the query result", !iter.hasNext());
631             Assert.assertEquals("Bad query result size", 1, col.size());
632             query.close(col);
633         } catch (Exception JavaDoc e) {
634             Exception JavaDoc ie = ExceptionHelper.getNested(e);
635             logger.log(BasicLevel.ERROR, "", ie);
636             fail(ie.getMessage());
637         } finally {
638             pm.close();
639         }
640     }
641
642     public void testParamRef() {
643         logger.log(BasicLevel.DEBUG, "testParamRef");
644         PersistenceManager pm = pmf.getPersistenceManager();
645         try {
646             Query query = pm.newQuery(Department.class);
647             String JavaDoc filter = "(this.name == \"" + POBuilder.depName + "\")";
648             query.setFilter(filter);
649             Collection JavaDoc col = (Collection JavaDoc) query.execute();
650             Iterator JavaDoc iter = col.iterator();
651             Assert.assertTrue("", iter.hasNext());
652             Department d = (Department) iter.next();
653             query.closeAll();
654
655             query = pm.newQuery(Employee.class);
656             filter = "(this.dept == d)";
657             String JavaDoc param = "Department d";
658             query.declareParameters(param);
659             query.setFilter(filter);
660             col = (Collection JavaDoc) query.execute(d);
661             iter = col.iterator();
662             Assert.assertTrue("The query result is empty", iter.hasNext());
663             Assert.assertEquals("Bad size", POBuilder.names.length, col.size());
664             query.closeAll();
665         } catch (Exception JavaDoc e) {
666             Exception JavaDoc ie = ExceptionHelper.getNested(e);
667             logger.log(BasicLevel.ERROR, "", ie);
668             fail(ie.getMessage());
669         } finally {
670             pm.close();
671         }
672     }
673
674     public void testVarContains() {
675         logger.log(BasicLevel.DEBUG, "testVarContains");
676         PersistenceManager pm = pmf.getPersistenceManager();
677         try {
678             Query query = pm.newQuery(Department.class);
679             query.setFilter("(depts.contains(name))");
680             query.declareParameters("Collection depts");
681             Collection JavaDoc col = (Collection JavaDoc) query.execute(Arrays.asList(
682                     new String JavaDoc[]{POBuilder.depName}));
683             Assert.assertEquals("Bad size", 1, col.size());
684             query.closeAll();
685
686             query = pm.newQuery(Department.class);
687             query.setFilter("(depts.contains(name))");
688             query.declareParameters("Collection depts");
689             col = (Collection JavaDoc) query.execute(Arrays.asList(
690                     new String JavaDoc[]{"toto", "titi"}));
691             Assert.assertEquals("Bad size", 0, col.size());
692             query.closeAll();
693         } catch (Exception JavaDoc e) {
694             Exception JavaDoc ie = ExceptionHelper.getNested(e);
695             logger.log(BasicLevel.ERROR, "", ie);
696             fail(ie.getMessage());
697         } finally {
698             pm.close();
699         }
700     }
701
702
703     public void testBsContainsParam(long ida,long idb, boolean exist) {
704         logger.log(BasicLevel.DEBUG, "testBsContainsParam_" + ida + '_' + idb + '_' + exist);
705         PersistenceManager pm = pmf.getPersistenceManager();
706         try {
707             Query query = pm.newQuery(BMMB.class);
708             query.setFilter("(idb == param)");
709             query.declareParameters("long param");
710             Collection JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(idb));
711             Iterator JavaDoc iter = col.iterator();
712             if (!iter.hasNext()) {
713                 fail("Test not initialized: impossible to load BMMB(" + idb
714                     + "): not found");
715             }
716             BMMB bmmb = (BMMB) iter.next();
717             query.close(col);
718
719 // loggerFactory.getLogger("org.objectweb.speedo.rt.query").setIntLevel(BasicLevel.DEBUG);
720
// loggerFactory.getLogger("org.objectweb.medor").setIntLevel(BasicLevel.DEBUG);
721
query = pm.newQuery(AMMB.class);
722
723             String JavaDoc filter = "((ida == a) && bs.contains(b))";
724             query.declareParameters("BMMB b, long a");
725             query.setFilter(filter);
726             col = (Collection JavaDoc) query.execute(bmmb, new Long JavaDoc(ida));
727             iter = col.iterator();
728             Assert.assertEquals("The BMMB(" + idb + ") was " + (exist ? "not " : "")
729                 + "found in the collection AMMB(" + ida + ").bs",
730                 exist, iter.hasNext());
731             if (exist) {
732                 Assert.assertEquals("Not the same value", ida, ((AMMB) iter.next()).getIda());
733             }
734             query.close(col);
735         } catch (Exception JavaDoc e) {
736             Exception JavaDoc ie = ExceptionHelper.getNested(e);
737             logger.log(BasicLevel.ERROR, "", ie);
738             fail(ie.getMessage());
739         } finally {
740             pm.close();
741         }
742     }
743
744     public void testBsContains2Param_0_3() {
745         testBsContains2Param(0,3);
746     }
747     public void testBsContains2Param(long ida,long idb) {
748         logger.log(BasicLevel.DEBUG, "testBsContainsParam_" + ida + '_' + idb);
749         PersistenceManager pm = pmf.getPersistenceManager();
750         try {
751             Query query = pm.newQuery(BMMB.class);
752             query.setFilter("((idb == param1) || (idb == param2))");
753             query.declareParameters("long param1, long param2");
754             Collection JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(idb), new Long JavaDoc(ida));
755             Iterator JavaDoc iter = col.iterator();
756             if (!iter.hasNext()) {
757                 fail("Test not initialized: impossible to load BMMB(" + idb
758                         + "): not found");
759             }
760             BMMB bmmb1 = (BMMB) iter.next();
761             BMMB bmmb2 = (BMMB) iter.next();
762             query.close(col);
763
764 // loggerFactory.getLogger("org.objectweb.speedo.rt.query").setIntLevel(BasicLevel.DEBUG);
765
// loggerFactory.getLogger("org.objectweb.medor").setIntLevel(BasicLevel.DEBUG);
766
query = pm.newQuery(AMMB.class);
767
768             String JavaDoc filter = "((bs.contains(b1)) || (bs.contains(b2)))";
769             query.declareParameters("BMMB b1, BMMB b2");
770             query.setFilter(filter);
771             col = (Collection JavaDoc) query.execute(bmmb1, bmmb2);
772             iter = col.iterator();
773             Assert.assertEquals("The BMMB(" + idb + ") was not found in the collection AMMB.bs",
774                     true, iter.hasNext());
775             iter.next();
776             Assert.assertEquals("The BMMB(" + ida + ") was not found in the collection AMMB.bs",
777                     true, iter.hasNext());
778             query.close(col);
779         } catch (Exception JavaDoc e) {
780             Exception JavaDoc ie = ExceptionHelper.getNested(e);
781             logger.log(BasicLevel.ERROR, "", ie);
782             fail(ie.getMessage());
783         } finally {
784             pm.close();
785         }
786     }
787     
788     public void testBsContainsParamA0B0() {
789         testBsContainsParam(0, 0, true);
790     }
791
792     public void testBsContainsParamA0B2() {
793         testBsContainsParam(0, 2, false);
794     }
795
796     public void testBsContainsVar(long id, Collection JavaDoc aNames) {
797         String JavaDoc testName = "testBsContainsVar_" + id + '_' + aNames;
798         logger.log(BasicLevel.DEBUG, testName);
799         PersistenceManager pm = pmf.getPersistenceManager();
800         try {
801             Query query = pm.newQuery(AMMB.class);
802
803             String JavaDoc filter = "(bs.contains(b) && (b.name==name))";
804             query.declareParameters("String name");
805             query.declareVariables("BMMB b");
806             query.setFilter(filter);
807             Collection JavaDoc col = (Collection JavaDoc) query.execute("B" + id);
808             Collection JavaDoc res = new ArrayList JavaDoc();
809             Iterator JavaDoc iter = col.iterator();
810             while(iter.hasNext()) {
811                  res.add(((AMMB) iter.next()).getName());
812             }
813             query.close(col);
814             assertSameCollection(testName + ":", aNames, res);
815         } catch (Exception JavaDoc e) {
816             Exception JavaDoc ie = ExceptionHelper.getNested(e);
817             logger.log(BasicLevel.ERROR, "", ie);
818             fail(ie.getMessage());
819         } finally {
820             pm.close();
821         }
822     }
823
824     public void testBsContainsVarB3EmptyShortPath() {
825         testBsContainsVar(3, Collections.EMPTY_LIST);
826     }
827
828     public void testBsContainsVarB0ShortPath() {
829         testBsContainsVar(0, Arrays.asList(new String JavaDoc[]{"A0", "A1"}));
830     }
831
832     public void testBsContainsVarB1ShortPath() {
833         testBsContainsVar(1, Arrays.asList(new String JavaDoc[]{"A0", "A1", "A2"}));
834     }
835
836     public void testBsContainsVarLongPath(long id, Collection JavaDoc ids) {
837         String JavaDoc testName = "testBsContainsVarLongPath_" + id + '_' + ids;
838         logger.log(BasicLevel.DEBUG, testName);
839         //NavigatorNodeFactory.logger = logger;
840
PersistenceManager pm = pmf.getPersistenceManager();
841         try {
842             Query query = pm.newQuery(Ref2Ref2AMMB.class);
843
844             String JavaDoc filter = "(ref.ref.bs.contains(b) && (b.name==name))";
845             query.declareParameters("String name");
846             query.declareVariables("BMMB b");
847             query.setFilter(filter);
848             Collection JavaDoc col = (Collection JavaDoc) query.execute("B" + id);
849             Collection JavaDoc res = new ArrayList JavaDoc();
850             Iterator JavaDoc iter = col.iterator();
851             while(iter.hasNext()) {
852                  res.add(new Long JavaDoc(((Ref2Ref2AMMB) iter.next()).getId()));
853             }
854             query.close(col);
855             assertSameCollection(testName + ":", ids, res);
856         } catch (Exception JavaDoc e) {
857             Exception JavaDoc ie = ExceptionHelper.getNested(e);
858             logger.log(BasicLevel.ERROR, "", ie);
859             fail(ie.getMessage());
860         } finally {
861             pm.close();
862         }
863     }
864
865     public void testBsContainsVarB3EmptyLongPath() {
866         testBsContainsVarLongPath(3, Collections.EMPTY_LIST);
867     }
868
869     public void testBsContainsVarB0LongPath() {
870         testBsContainsVarLongPath(0, Arrays.asList(new Long JavaDoc[]{new Long JavaDoc(0), new Long JavaDoc(100)}));
871     }
872
873     public void testBsContainsVarB1LongPath() {
874         testBsContainsVarLongPath(1, Arrays.asList(new Long JavaDoc[]{new Long JavaDoc(0), new Long JavaDoc(100), new Long JavaDoc(200)}));
875     }
876
877     public void testBsIsEmpty(long ida, boolean isEmpty) {
878         logger.log(BasicLevel.DEBUG, "testBsIsEmpty_" + ida + '_' + isEmpty);
879         //fail("The isEmpty operator is not support in Medor");
880
PersistenceManager pm = pmf.getPersistenceManager();
881         try {
882             Query query = pm.newQuery(AMMB.class);
883             String JavaDoc filter = "((ida == a) && bs.isEmpty())";
884             query.declareParameters("long a");
885             query.setFilter(filter);
886             Collection JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(ida));
887             Iterator JavaDoc iter = col.iterator();
888             Assert.assertEquals("The AMMB(" + ida + ").bs is " + (isEmpty
889                     ? "not " : "") + "empty.", isEmpty, iter.hasNext());
890             query.close(col);
891         } catch (Exception JavaDoc e) {
892             Exception JavaDoc ie = ExceptionHelper.getNested(e);
893             logger.log(BasicLevel.ERROR, "", ie);
894             fail(ie.getMessage());
895         } finally {
896             pm.close();
897         }
898     }
899
900     public void testBsIsEmptyA0() {
901         testBsIsEmpty(0, false);
902     }
903
904     public void testBsIsEmptyA3() {
905         testBsIsEmpty(3, true);
906     }
907
908     public void testBsIsEmptyWithNot(long ida, boolean hasResult) {
909         logger.log(BasicLevel.DEBUG, "testBsIsEmptyWithNot_" + ida + '_' + hasResult);
910         //fail("The isEmpty operator is not support in Medor");
911
PersistenceManager pm = pmf.getPersistenceManager();
912         try {
913             Query query = pm.newQuery(AMMB.class);
914             String JavaDoc filter = "(!(ida == a) && bs.isEmpty())";
915             query.declareParameters("long a");
916             query.setFilter(filter);
917             Collection JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(ida));
918             Iterator JavaDoc iter = col.iterator();
919             Assert.assertEquals("It " + (hasResult
920                     ? " does not exist"
921                     : "exists ") + " ida != "
922                     + ida + " with empty collection.",
923                     hasResult, iter.hasNext());
924             query.close(col);
925         } catch (Exception JavaDoc e) {
926             Exception JavaDoc ie = ExceptionHelper.getNested(e);
927             logger.log(BasicLevel.ERROR, "", ie);
928             fail(ie.getMessage());
929         } finally {
930             pm.close();
931         }
932     }
933
934     public void testBsIsEmptyA0WithNot() {
935         testBsIsEmptyWithNot(0, true);
936     }
937
938     public void testBsIsEmptyA3WithNot() {
939         testBsIsEmptyWithNot(3, false);
940     }
941
942     public void testBsIsNotEmpty(long ida, boolean isNotEmpty, boolean globalNot) {
943         logger.log(BasicLevel.DEBUG, "testBsIsNotEmpty_" + ida + "_ise=" + isNotEmpty + "_gn=" + globalNot);
944         //fail("The isEmpty operator is not support in Medor");
945
PersistenceManager pm = pmf.getPersistenceManager();
946         try {
947             Query query = pm.newQuery(AMMB.class);
948             String JavaDoc filter = (globalNot
949                     ? "(!((ida != a) || bs.isEmpty()))"
950                     : "((ida == a) && !(bs.isEmpty()))");
951             query.declareParameters("long a");
952             query.setFilter(filter);
953             Collection JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(ida));
954             Iterator JavaDoc iter = col.iterator();
955             Assert.assertEquals("The AMMB(" + ida + ").bs is " + (isNotEmpty
956                     ? "" : "not ") + "empty.", isNotEmpty, iter.hasNext());
957             query.close(col);
958         } catch (Exception JavaDoc e) {
959             Exception JavaDoc ie = ExceptionHelper.getNested(e);
960             logger.log(BasicLevel.ERROR, "", ie);
961             fail(ie.getMessage());
962         } finally {
963             pm.close();
964         }
965     }
966
967     public void testBsIsNotEmptyA0Global() {
968         testBsIsNotEmpty(0, true, true);
969     }
970
971     public void testBsIsNotEmptyA0Local() {
972         testBsIsNotEmpty(0, true, false);
973     }
974
975     public void testBsIsNotEmptyA3Global() {
976         testBsIsNotEmpty(3, false, true);
977     }
978     public void testBsIsNotEmptyA3Local() {
979         testBsIsNotEmpty(3, false, false);
980     }
981
982     public void testNot(long ida, Collection JavaDoc idsa) {
983         logger.log(BasicLevel.DEBUG, "testNot_" + ida + '_' + idsa);
984         PersistenceManager pm = pmf.getPersistenceManager();
985         try {
986             Query query = pm.newQuery(AMMB.class);
987             String JavaDoc filter = "(!(ida == a))";
988             query.declareParameters("long a");
989             query.setFilter(filter);
990             Collection JavaDoc col = (Collection JavaDoc) query.execute(new Long JavaDoc(ida));
991             ArrayList JavaDoc res = new ArrayList JavaDoc(idsa.size());
992             Iterator JavaDoc iter = col.iterator();
993             while(iter.hasNext()) {
994                 res.add(new Long JavaDoc(((AMMB) iter.next()).getIda()));
995             }
996             query.close(col);
997             this.assertSameCollection("", idsa, res);
998         } catch (Exception JavaDoc e) {
999             Exception JavaDoc ie = ExceptionHelper.getNested(e);
1000            logger.log(BasicLevel.ERROR, "", ie);
1001            fail(ie.getMessage());
1002        } finally {
1003            pm.close();
1004        }
1005    }
1006
1007    public void testNotA0() {
1008        testNot(0, Arrays.asList(new Long JavaDoc[]{new Long JavaDoc(1), new Long JavaDoc(2), new Long JavaDoc(3)}));
1009    }
1010
1011    public void testExtent(Class JavaDoc clazz, boolean withSubclass, Collection JavaDoc ids) {
1012        logger.log(BasicLevel.DEBUG, "testExtent_" + clazz.getName() + "_sc=" + withSubclass);
1013        PersistenceManager pm = pmf.getPersistenceManager();
1014        try {
1015            Extent e = pm.getExtent(clazz, withSubclass);
1016            Assert.assertEquals("Bad candidate class on the extent",
1017                    clazz, e.getCandidateClass());
1018            Assert.assertEquals("Bad sub class value on the extent",
1019                    withSubclass, e.hasSubclasses());
1020            Iterator JavaDoc it = e.iterator();
1021            ArrayList JavaDoc found = new ArrayList JavaDoc(ids.size());
1022            while(it.hasNext()) {
1023                Object JavaDoc o = it.next();
1024                if (o == null) {
1025                    fail("Null object returned by the extent iterator of the class "
1026                            + clazz.getName());
1027                } else if (o instanceof AMMB) {
1028                    AMMB a = (AMMB) o;
1029                    found.add(new Long JavaDoc(a.getIda()));
1030                } else if (o instanceof BMMB) {
1031                    BMMB b = (BMMB) o;
1032                    found.add(new Long JavaDoc(b.getIdb()));
1033                } else {
1034                    fail("the test does not manage the class " + o.getClass().getName());
1035                }
1036            }
1037            try {
1038                it.remove();
1039                fail("the remove operation does not throw an exception");
1040            } catch (UnsupportedOperationException JavaDoc e1) {
1041            }
1042            assertSameCollection("Bad extent of the class " + clazz.getName(), ids, found);
1043            e.close(it);
1044            try {
1045                it.hasNext();
1046                fail("the iterator does not throw an exception on the use of " +
1047                        "the 'hasNext' method whereas it has been closed");
1048            } catch (NoSuchElementException JavaDoc e1) {
1049            }
1050            try {
1051                it.next();
1052                fail("the iterator does not throw an exception on the use of " +
1053                        "the 'next' method whereas it has been closed");
1054            } catch (NoSuchElementException JavaDoc e1) {
1055            }
1056            e = pm.getExtent(clazz, withSubclass);
1057            Iterator JavaDoc[] its = new Iterator JavaDoc[5];
1058            for(int i=0; i<its.length; i++) {
1059                its[i] = e.iterator();
1060            }
1061            e.closeAll();
1062            for(int i=0; i<its.length; i++) {
1063                try {
1064                    its[i].next();
1065                    fail("the iterator " + i +" does not throw an exception on "
1066                            + "the use of the 'next' method whereas all"
1067                            + " iterator have been closed");
1068                } catch (NoSuchElementException JavaDoc e1) {
1069                }
1070            }
1071            it = e.iterator();
1072            for(int i=0; i<ids.size(); i++) {
1073                try {
1074                    it.next();
1075                } catch (NoSuchElementException JavaDoc e1) {
1076                    Assert.assertEquals("Bad size: ", ids.size(), i);
1077                }
1078            }
1079            e.close(it);
1080        } catch (Exception JavaDoc e) {
1081            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1082            logger.log(BasicLevel.ERROR, "", ie);
1083            fail(ie.getMessage());
1084        } finally {
1085            pm.close();
1086        }
1087    }
1088
1089    public void testExtentAMMBfalse() {
1090        ArrayList JavaDoc al = new ArrayList JavaDoc(POBuilder.NB_XMMB);
1091        for(int i=0; i<POBuilder.NB_XMMB; i++) {
1092            al.add(new Long JavaDoc(i));
1093        }
1094        testExtent(AMMB.class, false, al);
1095    }
1096
1097    public void testExtentBMMBfalse() {
1098        ArrayList JavaDoc al = new ArrayList JavaDoc(POBuilder.NB_XMMB);
1099        for(int i=0; i<POBuilder.NB_XMMB; i++) {
1100            al.add(new Long JavaDoc(i));
1101        }
1102        testExtent(BMMB.class, false, al);
1103    }
1104
1105    public void testQueryBasedOnExtent() {
1106        Class JavaDoc clazz = AMMB.class;
1107        boolean withSubclass = false;
1108        logger.log(BasicLevel.DEBUG, "testExtent_" + clazz.getName() + "_sc=" + withSubclass);
1109        PersistenceManager pm = pmf.getPersistenceManager();
1110        try {
1111            Extent e = pm.getExtent(clazz, false);
1112            Query q = pm.newQuery(e);
1113            Collection JavaDoc c = (Collection JavaDoc) q.execute();
1114            Assert.assertEquals("bad size", POBuilder.NB_XMMB, c.size());
1115            q.close(c);
1116        } catch (Exception JavaDoc e) {
1117            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1118            logger.log(BasicLevel.ERROR, "", ie);
1119            fail(ie.getMessage());
1120        } finally {
1121            pm.close();
1122        }
1123    }
1124
1125    public void testSequenceIdNavigateToPrimitive() {
1126        logger.log(BasicLevel.DEBUG, "testSequenceIdNavigateToPrimitive");
1127        PersistenceManager pm = pmf.getPersistenceManager();
1128        try {
1129            Query query = pm.newQuery(Ref2AutoIncFieldId.class);
1130            query.setFilter("(simpleRef.f1 == \"toto\")");
1131            Collection JavaDoc col = (Collection JavaDoc) query.execute();
1132            Iterator JavaDoc iter = col.iterator();
1133            Assert.assertTrue("The query result is empty", !iter.hasNext());
1134            query.close(col);
1135        } catch (Exception JavaDoc e) {
1136            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1137            logger.log(BasicLevel.ERROR, "", ie);
1138            fail(ie.getMessage());
1139        } finally {
1140            pm.close();
1141        }
1142    }
1143
1144    public void testSequenceIdNavigateToPrimitive2() {
1145        logger.log(BasicLevel.DEBUG, "testSequenceIdNavigateToPrimitive");
1146        PersistenceManager pm = pmf.getPersistenceManager();
1147        try {
1148            Query query = pm.newQuery(AutoIncFieldId.class);
1149            query.setFilter("(independantRef.f1 == \"toto\")");
1150            query.setOrdering("independantRef.f1 ascending");
1151            Collection JavaDoc col = (Collection JavaDoc) query.execute();
1152            Iterator JavaDoc iter = col.iterator();
1153            Assert.assertTrue("The query result is empty", !iter.hasNext());
1154            query.close(col);
1155        } catch (Exception JavaDoc e) {
1156            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1157            logger.log(BasicLevel.ERROR, "", ie);
1158            fail(ie.getMessage());
1159        } finally {
1160            pm.close();
1161        }
1162    }
1163
1164    public void testNotIgnoreCache() {
1165        logger.log(BasicLevel.DEBUG, "testSequenceIdNavigateToPrimitive");
1166        PersistenceManager pm = pmf.getPersistenceManager();
1167        BasicA ba = new BasicA();
1168        ba.writeF1("testNotIgnoreCache1");
1169        ba.writeF2(1);
1170        pm.makePersistent(ba);
1171        pm.close();
1172
1173        pm = pmf.getPersistenceManager();
1174        ba.writeF1("testNotIgnoreCache2");
1175        pm.setIgnoreCache(false);
1176        Assert.assertTrue("Bad ignore cache value", !pm.getIgnoreCache());
1177        Query query = pm.newQuery(BasicA.class);
1178        query.setFilter("(f1 == \"testNotIgnoreCache2\")");
1179        Collection JavaDoc col = (Collection JavaDoc) query.execute();
1180        assertEquals("The cache is ignored", 1, col.size());
1181        query.close(col);
1182        pm.close();
1183
1184        pm = pmf.getPersistenceManager();
1185        pm.currentTransaction().begin();
1186        pm.deletePersistent(ba);
1187        pm.currentTransaction().commit();
1188        pm.close();
1189    }
1190    
1191    public void testAvgSingle() {
1192        testAggregateSingle("AVG(salary)", new Float JavaDoc(3250), false);
1193        testAggregateSingle("avg(salary)", new Float JavaDoc(3250), false);
1194        testAggregateSingle("AVG(salary)", new Float JavaDoc(3250), true);
1195        testAggregateSingle("avg(salary)", new Float JavaDoc(3250), true);
1196    }
1197    public void testSumSingle() {
1198        testAggregateSingle("SUM(salary)", new Float JavaDoc(13000), false);
1199        testAggregateSingle("sum(salary)", new Float JavaDoc(13000), false);
1200        testAggregateSingle("SUM(salary)", new Float JavaDoc(13000), true);
1201        testAggregateSingle("sum(salary)", new Float JavaDoc(13000), true);
1202    }
1203    public void testMaxSingle() {
1204        testAggregateSingle("MAX(salary)", new Float JavaDoc(4000), false);
1205        testAggregateSingle("max(salary)", new Float JavaDoc(4000), false);
1206        testAggregateSingle("MAX(salary)", new Float JavaDoc(4000), true);
1207        testAggregateSingle("max(salary)", new Float JavaDoc(4000), true);
1208    }
1209    public void testMinSingle() {
1210        testAggregateSingle("MIN(salary)", new Float JavaDoc(2999), false);
1211        testAggregateSingle("min(salary)", new Float JavaDoc(2999), false);
1212        testAggregateSingle("MIN(salary)", new Float JavaDoc(2999), true);
1213        testAggregateSingle("min(salary)", new Float JavaDoc(2999), true);
1214    }
1215    public void testCountSingle() {
1216        testAggregateSingle("COUNT(salary)", new Long JavaDoc(4), false);
1217        testAggregateSingle("count(salary)", new Long JavaDoc(4), false);
1218        testAggregateSingle("count(this)", new Long JavaDoc(4), false);
1219        testAggregateSingle("count(*)", new Long JavaDoc(4), false);
1220        testAggregateSingle("COUNT(salary)", new Long JavaDoc(4), true);
1221        testAggregateSingle("count(salary)", new Long JavaDoc(4), true);
1222        testAggregateSingle("count(this)", new Long JavaDoc(4), true);
1223        testAggregateSingle("count(*)", new Long JavaDoc(4), true);
1224    }
1225    
1226    public void testAggregateSingle(String JavaDoc select, Object JavaDoc result, boolean unique) {
1227        logger.log(BasicLevel.DEBUG, "testAggregateSingle(" + select + ", " + result + ")");
1228        PersistenceManager pm = pmf.getPersistenceManager();
1229        try {
1230            Query query = pm.newQuery(Employee.class);
1231            query.setResult(select);
1232            query.setUnique(unique);
1233            Object JavaDoc res = query.execute();
1234            if (unique) {
1235                assertEquals("Bad result", result, res);
1236            } else {
1237                Iterator JavaDoc it = ((List JavaDoc) res).iterator();
1238                assertTrue("Not result!", it.hasNext());
1239                Object JavaDoc o = it.next();
1240                assertNotNull("Result is null", o);
1241                assertEquals("Bad result type: ", o.getClass(), result.getClass());
1242                assertEquals("Bad result value", o, result);
1243                assertTrue("More than one result!", !it.hasNext());
1244            }
1245            query.closeAll();
1246        } catch (JDOException e) {
1247            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1248            logger.log(BasicLevel.ERROR, "", ie);
1249            fail(ie.getMessage());
1250        } finally {
1251            pm.close();
1252        }
1253    }
1254
1255    public void testSingleField() {
1256        logger.log(BasicLevel.DEBUG, "testSingleField");
1257        PersistenceManager pm = pmf.getPersistenceManager();
1258        try {
1259            Query query = pm.newQuery(Employee.class);
1260            query.setResult("name");
1261            Collection JavaDoc col = (Collection JavaDoc) query.execute();
1262            assertSameCollection("Bad name collection",
1263                    Arrays.asList(POBuilder.names),col);
1264            query.close(col);
1265        } catch (JDOException e) {
1266            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1267            logger.log(BasicLevel.ERROR, "", ie);
1268            fail(ie.getMessage());
1269        } finally {
1270            pm.close();
1271        }
1272    }
1273    public void testSingleFieldRef() {
1274        logger.log(BasicLevel.DEBUG, "testSingleFieldRef");
1275        PersistenceManager pm = pmf.getPersistenceManager();
1276        try {
1277            Query query = pm.newQuery(Employee.class);
1278            query.setResult("dept");
1279            List JavaDoc list = (List JavaDoc) query.execute();
1280            assertEquals("bad collection size", 4, list.size());
1281            assertEquals("Bad dept", POBuilder.depName,
1282                    ((Department) list.get(0)).getName());
1283            assertEquals("0!=1", list.get(0), list.get(1));
1284            assertEquals("1!=2", list.get(1), list.get(2));
1285            assertEquals("2!=3", list.get(2), list.get(3));
1286            query.close(list);
1287        } catch (JDOException e) {
1288            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1289            logger.log(BasicLevel.ERROR, "", ie);
1290            fail(ie.getMessage());
1291        } finally {
1292            pm.close();
1293        }
1294    }
1295    public void testDistinctSingleFieldRef() {
1296        logger.log(BasicLevel.DEBUG, "testDistinctSingleFieldRef");
1297        PersistenceManager pm = pmf.getPersistenceManager();
1298        try {
1299            Query query = pm.newQuery(Employee.class);
1300            query.setResult("distinct dept");
1301            Collection JavaDoc col = (Collection JavaDoc) query.execute();
1302            List JavaDoc list = (List JavaDoc) query.execute();
1303            assertEquals("bad collection size", 1, list.size());
1304            assertEquals("Bad dept", POBuilder.depName,
1305                    ((Department) list.get(0)).getName());
1306            query.close(col);
1307        } catch (JDOException e) {
1308            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1309            logger.log(BasicLevel.ERROR, "", ie);
1310            fail(ie.getMessage());
1311        } finally {
1312            pm.close();
1313        }
1314    }
1315    
1316    public static class StringFloat {
1317        public String JavaDoc name;
1318        public Float JavaDoc value;
1319        public StringFloat(String JavaDoc n, Float JavaDoc v) {
1320            name = n;
1321            value = v;
1322        }
1323    }
1324    
1325    public void testCompositeResultWithUserClass() {
1326        testCompositeResult(true);
1327    }
1328    public void testCompositeResultWithoutUserClass() {
1329        testCompositeResult(false);
1330    }
1331    public void testCompositeResult(boolean withUserClassResult) {
1332        logger.log(BasicLevel.DEBUG, "testUserClassResult");
1333        PersistenceManager pm = pmf.getPersistenceManager();
1334        try {
1335            Query query = pm.newQuery(Employee.class);
1336            query.setResult("distinct dept.name, salary");
1337            if (withUserClassResult) {
1338                query.setResultClass(StringFloat.class);
1339            }
1340            Collection JavaDoc col = (Collection JavaDoc) query.execute();
1341            Iterator JavaDoc it = col.iterator();
1342            
1343            query.closeAll();
1344        } catch (JDOException e) {
1345            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1346            logger.log(BasicLevel.ERROR, "", ie);
1347            fail(ie.getMessage());
1348        } finally {
1349            pm.close();
1350        }
1351    }
1352
1353    public void testCompositeResultAgregWithUserClass() {
1354        testCompositeResultAgreg(true);
1355    }
1356    public void testCompositeResultAgregWithoutUserClass() {
1357        testCompositeResultAgreg(false);
1358    }
1359    
1360    public void testCompositeResultAgreg(boolean withUserClassResult) {
1361        logger.log(BasicLevel.DEBUG, "testUserClassResult");
1362        PersistenceManager pm = pmf.getPersistenceManager();
1363        try {
1364            Query query = pm.newQuery(Employee.class);
1365            query.setResult("dept.name, avg(salary)");
1366            if (withUserClassResult) {
1367                query.setResultClass(StringFloat.class);
1368            }
1369            query.setGrouping("dept.name");
1370            Collection JavaDoc col = (Collection JavaDoc) query.execute();
1371            Iterator JavaDoc it = col.iterator();
1372            
1373            query.closeAll();
1374        } catch (JDOException e) {
1375            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1376            logger.log(BasicLevel.ERROR, "", ie);
1377            fail(ie.getMessage());
1378        } finally {
1379            pm.close();
1380        }
1381    }
1382    
1383    public void _testCompositeWithRefResultAgregWithUserClass() {
1384        testCompositeWithRefResultAgreg(true);
1385    }
1386    public void testCompositeWithRefResultAgregWithoutUserClass() {
1387        testCompositeWithRefResultAgreg(false);
1388    }
1389    
1390    public void testCompositeWithRefResultAgreg(boolean withUserClassResult) {
1391        logger.log(BasicLevel.DEBUG, "testUserClassResult");
1392        PersistenceManager pm = pmf.getPersistenceManager();
1393        try {
1394            Query query = pm.newQuery(Employee.class);
1395            query.setResult("avg(salary)");
1396            if (withUserClassResult) {
1397                query.setResultClass(StringFloat.class);
1398            }
1399            query.setGrouping("dept");
1400            Collection JavaDoc col = (Collection JavaDoc) query.execute();
1401            Iterator JavaDoc it = col.iterator();
1402            
1403            query.closeAll();
1404        } catch (JDOException e) {
1405            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1406            logger.log(BasicLevel.ERROR, "", ie);
1407            fail(ie.getMessage());
1408        } finally {
1409            pm.close();
1410        }
1411    }
1412    
1413    public void testUserCacheSingle() {
1414        logger.log(BasicLevel.DEBUG, "testUserCacheSingle");
1415        PersistenceManager pm = pmf.getPersistenceManager();
1416        pm.currentTransaction().begin();
1417        final String JavaDoc n = "testUserCacheSingle_";
1418        for(int i=0; i<10; i++) {
1419            Product p = new Product(i);
1420            p.setProductName(n + i);
1421            pm.makePersistent(p);
1422        }
1423        pm.currentTransaction().commit();
1424        try {
1425            pm.currentTransaction().begin();
1426            Query query = pm.newQuery(Product.class);
1427            query.setFilter("productName == p");
1428            query.declareParameters("String p");
1429            query.setUnique(true);
1430            final String JavaDoc id = n + 3;
1431            Product p = (Product) query.execute(id); //no query
1432
assertNotNull("Null value for " + id, p);
1433            p.setProductName(id + "bis");
1434
1435            //Check that the old value is unbound
1436
p = (Product) query.execute(id); //query
1437
assertNull("Old value not unbound from the user cache, " + id, p);
1438
1439            p = (Product) query.execute(id + "bis");//query because the cache
1440
// does not contains the old value neither the new value
1441
assertNotNull("Null value for " + id + "bis", p);
1442
1443            p = (Product) query.execute(n + 10); //query because the object does not exist
1444
assertNull("Non null value for " + n + 10, p);
1445            pm.currentTransaction().commit();
1446            //At this time, the user cache has been updated with the modified Product
1447

1448            pm.currentTransaction().begin();
1449            p = (Product) query.execute(id + "bis"); //no query
1450
assertNotNull("Null value for " + id + "bis", p);
1451            pm.currentTransaction().commit();
1452
1453            pm.evictAll();
1454
1455            pm.currentTransaction().begin();
1456            p = (Product) query.execute(id + "bis"); //query
1457
assertNotNull("Null value for " + id + "bis", p);
1458            pm.currentTransaction().commit();
1459
1460            pm.currentTransaction().begin();
1461            Extent e = pm.getExtent(Product.class);
1462            for (Iterator JavaDoc iter = e.iterator(); iter.hasNext();) {
1463                pm.deletePersistent(iter.next());
1464            }
1465            pm.currentTransaction().commit();
1466        } catch (JDOException e) {
1467            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1468            logger.log(BasicLevel.ERROR, "", ie);
1469            fail(ie.getMessage());
1470        } finally {
1471            pm.close();
1472        }
1473        
1474    }
1475    
1476    public void testUserCacheInheritance() {
1477        logger.log(BasicLevel.DEBUG, "testUserCacheInheritance");
1478        PersistenceManager pm = pmf.getPersistenceManager();
1479        pm.currentTransaction().begin();
1480        final String JavaDoc n = "testUserCacheInheritance_";
1481        for(int i=0; i<10; i++) {
1482            org.objectweb.speedo.pobjects.inheritance.userCache.GeoRef geoRef =
1483                new org.objectweb.speedo.pobjects.inheritance.userCache.GeoRef(n + i);
1484            pm.makePersistent(geoRef);
1485        }
1486        pm.currentTransaction().commit();
1487        try {
1488            pm.currentTransaction().begin();
1489            Extent e = pm.getExtent(Litem.class, true);//with subclasses
1490
Query query = pm.newQuery(e);
1491            query.setFilter("name == id");
1492            query.declareParameters("String id");
1493            query.setUnique(true);
1494            final String JavaDoc id = n + 3;
1495            Litem litem = (Litem) query.execute(id);//no query
1496
assertNotNull("Null value for " + id, litem);
1497            litem.setName(id + "bis");
1498
1499            //Check that the old value is unbound
1500
litem = (Litem) query.execute(id);//query
1501
assertNull("Old value not unbound from the user cache, " + id, litem);
1502
1503            litem = (Litem) query.execute(id + "bis");//query
1504
assertNotNull("Null value for " + id + "bis", litem);
1505
1506            litem = (Litem) query.execute(n + 10);//query
1507
assertNull("Non null value for " + n + 10, litem);
1508            pm.currentTransaction().commit();
1509            
1510            pm.evictAll();
1511
1512            pm.currentTransaction().begin();
1513            litem = (Litem) query.execute(id + "bis"); //query
1514
assertNotNull("Null value for " + id + "bis", litem);
1515            pm.currentTransaction().commit();
1516
1517            pm.currentTransaction().begin();
1518            String JavaDoc myId = n+1;
1519            litem = (Litem) query.execute(myId);//no query
1520
assertNotNull("Null value for " + myId, litem);
1521            litem = (Litem) query.execute(myId);//no query
1522
assertNotNull("Null value for " + myId, litem);
1523            pm.currentTransaction().commit();
1524            
1525            pm.currentTransaction().begin();
1526            e = pm.getExtent(Litem.class, true);
1527            for (Iterator JavaDoc iter = e.iterator(); iter.hasNext();) {
1528                pm.deletePersistent(iter.next());
1529            }
1530            pm.currentTransaction().commit();
1531        } catch (JDOException e) {
1532            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1533            logger.log(BasicLevel.ERROR, "", ie);
1534            fail(ie.getMessage());
1535        } finally {
1536            pm.close();
1537        }
1538    }
1539    
1540    public void testNotContains() {
1541        logger.log(BasicLevel.DEBUG, "testUserCacheSingle");
1542        PersistenceManager pm = pmf.getPersistenceManager();
1543        Query q = pm.newQuery(Group.class);
1544        q.declareParameters("String p1");
1545        q.declareVariables("User u");
1546        q.setFilter("!(this.users.contains(u)) && (u.name == p1)");
1547        Collection JavaDoc c = (Collection JavaDoc) q.execute("user_g0_u0");
1548        Collection JavaDoc founds = new ArrayList JavaDoc(c.size());
1549        for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
1550            Group g = (Group) iter.next();
1551            founds.add(g.getName());
1552        }
1553        q.closeAll();
1554        pm.close();
1555        assertSameCollection("Bad group found",
1556                Arrays.asList(new String JavaDoc[]{"group_1", "group_2"}),
1557                founds);
1558    }
1559    
1560    public final static int NB_GROUP = 2;
1561    public final static int NB_USER_PER_GROUP = 2;
1562    
1563    protected GroupUser commonUser = new GroupUser("user_0and2");
1564    
1565    public void notContainsInheritanceABuilder() {
1566        PersistenceManager pm = pmf.getPersistenceManager();
1567        pm.currentTransaction().begin();
1568        //add newsgroup_0 -> [user_0and2]
1569
NewsGroup ng0 = new NewsGroup("newsgroup_0");
1570        Collection JavaDoc users = ng0.getUsers();
1571        users.add(commonUser);
1572        pm.makePersistent(ng0);
1573        //add newsgroup_1 -> [user_1]
1574
NewsGroup ng1 = new NewsGroup("newsgroup_1");
1575        users = ng1.getUsers();
1576        users.add(new GroupUser("user_1"));
1577        pm.makePersistent(ng1);
1578        pm.currentTransaction().commit();
1579        pm.close();
1580    }
1581    
1582    //not contains user_Oand2 --> newsgroup_1
1583
public void testNotContainsInheritanceA1() {
1584        notContainsInheritanceABuilder();
1585        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceA1");
1586        try {
1587            notContainsInheritance("user_0and2", new String JavaDoc[]{"newsgroup_1"});
1588        } finally {
1589            removingOfPersistentObject();
1590        }
1591    }
1592    
1593    //not contains user_1 --> newsgroup_0
1594
public void testNotContainsInheritanceA2() {
1595        notContainsInheritanceABuilder();
1596        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceA2");
1597        try {
1598            notContainsInheritance("user_1", new String JavaDoc[]{"newsgroup_0"});
1599        } finally {
1600            removingOfPersistentObject();
1601        }
1602    }
1603    
1604    //not contains dummy --> all
1605
public void testNotContainsInheritanceA3() {
1606        notContainsInheritanceABuilder();
1607        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceA3");
1608        try {
1609            notContainsInheritance("dummy", new String JavaDoc[]{"newsgroup_0", "newsgroup_1"});
1610        } finally {
1611            removingOfPersistentObject();
1612        }
1613    }
1614    
1615    public void notContainsInheritanceBBuilder() {
1616        notContainsInheritanceABuilder();
1617        PersistenceManager pm = pmf.getPersistenceManager();
1618        //add newsgroup_2 -> [user_0and2]
1619
NewsGroup ng = new NewsGroup("newsgroup_2");
1620        Collection JavaDoc users = ng.getUsers();
1621        users.add(commonUser);
1622        pm.makePersistent(ng);
1623        pm.close();
1624    }
1625    
1626    //not contains user_Oand2 --> newsgroup_1
1627
public void testNotContainsInheritanceB1() {
1628        notContainsInheritanceBBuilder();
1629        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceB1");
1630        try {
1631        notContainsInheritance("user_0and2", new String JavaDoc[]{"newsgroup_1"});
1632        } finally {
1633            removingOfPersistentObject();
1634        }
1635    }
1636    
1637    //not contains user_1 --> newsgroup_0, newsgroup_2
1638
public void testNotContainsInheritanceB2() {
1639        notContainsInheritanceBBuilder();
1640        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceB2");
1641        try {
1642            notContainsInheritance("user_1", new String JavaDoc[]{"newsgroup_0", "newsgroup_2"});
1643        } finally {
1644            removingOfPersistentObject();
1645        }
1646    }
1647    
1648    //not contains dummy --> all
1649
public void testNotContainsInheritanceB3() {
1650        notContainsInheritanceBBuilder();
1651        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceB3");
1652        try {
1653            notContainsInheritance("dummy", new String JavaDoc[]{"newsgroup_0", "newsgroup_1", "newsgroup_2"});
1654        } finally {
1655            removingOfPersistentObject();
1656        }
1657    }
1658    
1659    
1660    public void notContainsInheritanceCBuilder() {
1661        notContainsInheritanceBBuilder();
1662        PersistenceManager pm = pmf.getPersistenceManager();
1663        //add newsgroup_3 -> [empty]
1664
NewsGroup ng = new NewsGroup("newsgroup_3");
1665        pm.makePersistent(ng);
1666        pm.close();
1667    }
1668    
1669    //not contains user_Oand2 --> newsgroup_1 and newsgroup_3
1670
public void testNotContainsInheritanceC1() {
1671        notContainsInheritanceCBuilder();
1672        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceC1");
1673        try {
1674            notContainsInheritance("user_0and2", new String JavaDoc[]{"newsgroup_1", "newsgroup_3"});
1675        } finally {
1676            removingOfPersistentObject();
1677        }
1678    }
1679    
1680    //not contains user_1 --> newsgroup_0, newsgroup_2, newsgroup_3
1681
public void testNotContainsInheritanceC2() {
1682        notContainsInheritanceCBuilder();
1683        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceC2");
1684        try {
1685            notContainsInheritance("user_1", new String JavaDoc[]{"newsgroup_0", "newsgroup_2", "newsgroup_3"});
1686        } finally {
1687            removingOfPersistentObject();
1688        }
1689    }
1690    
1691    //not contains dummy --> all
1692
public void testNotContainsInheritanceC3() {
1693        notContainsInheritanceCBuilder();
1694        logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceC3");
1695        try {
1696            notContainsInheritance("dummy", new String JavaDoc[]{"newsgroup_0", "newsgroup_1", "newsgroup_2", "newsgroup_3"});
1697        } finally {
1698            removingOfPersistentObject();
1699        }
1700    }
1701    
1702    private void notContainsInheritance(String JavaDoc parameter, String JavaDoc[] resultExpected) {
1703        PersistenceManager pm = pmf.getPersistenceManager();
1704        Query q = pm.newQuery(NewsGroup.class);
1705        q.declareParameters("String p1");
1706        q.declareVariables("GroupUser u");
1707        q.setFilter("!(this.users.contains(u)) && (u.name == p1)");
1708        Collection JavaDoc c = (Collection JavaDoc) q.execute(parameter);
1709        Collection JavaDoc founds = new ArrayList JavaDoc(c.size());
1710        for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
1711            NewsGroup ng = (NewsGroup) iter.next();
1712            founds.add(ng.getName());
1713        }
1714        q.closeAll();
1715        pm.close();
1716        assertSameCollection("Bad group found",
1717                Arrays.asList(resultExpected),
1718                founds);
1719    }
1720    
1721    public void removingOfPersistentObject() {
1722        PersistenceManager pm = pmf.getPersistenceManager();
1723        try {
1724            Class JavaDoc[] cs = new Class JavaDoc[]{NewsGroup.class,
1725                                     GroupUser.class};
1726            pm.currentTransaction().begin();
1727            for(int i=0; i<cs.length; i++) {
1728                Query query = pm.newQuery(cs[i]);
1729                Collection JavaDoc col = (Collection JavaDoc) query.execute();
1730                Iterator JavaDoc it = col.iterator();
1731                while(it.hasNext()) {
1732                    Object JavaDoc o = it.next();
1733                    Assert.assertNotNull("null object in the query result"
1734                        + cs[i].getName(), o);
1735                    pm.deletePersistent(o);
1736
1737                }
1738                query.close(col);
1739            }
1740            pm.currentTransaction().commit();
1741        } catch (JDOException e) {
1742            Exception JavaDoc ie = ExceptionHelper.getNested(e);
1743            logger.log(BasicLevel.ERROR, "", ie);
1744            fail(ie.getMessage());
1745        } finally {
1746            pm.close();
1747        }
1748    }
1749    
1750    
1751    
1752    public void testNotContainsComposite() {
1753        logger.log(BasicLevel.DEBUG, "testNotContainsComposite");
1754        PersistenceManager pm = pmf.getPersistenceManager();
1755        Query q = pm.newQuery(MailingList.class);
1756        q.declareParameters("String p1, String p2");
1757        q.declareVariables("GroupModerator gm");
1758        q.setFilter("!(this.moderators.contains(gm)) && (gm.firstName == p1) && (gm.lastName == p2)");
1759        Collection JavaDoc c = (Collection JavaDoc) q.execute("moderator_ml0", "mod0");
1760        Collection JavaDoc founds = new ArrayList JavaDoc(c.size());
1761        for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
1762            MailingList ml = (MailingList) iter.next();
1763            founds.add(ml.getName());
1764        }
1765        q.closeAll();
1766        pm.close();
1767        assertSameCollection("Bad group found",
1768                Arrays.asList(new String JavaDoc[]{"mailinglist_1", "mailinglist_2"}),
1769                founds);
1770    }
1771    
1772    public void testTwoSameContains() {
1773        logger.log(BasicLevel.DEBUG, "testUserCacheSingle");
1774        PersistenceManager pm = pmf.getPersistenceManager();
1775        Query q = pm.newQuery(Group.class);
1776        q.declareParameters("String p1, String p2");
1777        q.declareVariables("User u1 ; User u2");
1778        q.setFilter("((this.users.contains(u1)) && (u1.name == p1)) && ((this.users.contains(u2)) && (u2.name == p2))");
1779        Collection JavaDoc c = (Collection JavaDoc) q.execute("user_g0_u0", "user_g0_u1");
1780        Collection JavaDoc founds = new ArrayList JavaDoc(c.size());
1781        for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
1782            Group g = (Group) iter.next();
1783            founds.add(g.getName());
1784        }
1785        q.closeAll();
1786        pm.close();
1787        assertSameCollection("Bad group found",
1788                Arrays.asList(new String JavaDoc[]{"group_0"}),
1789                founds);
1790    }
1791    
1792    public void testAutoRef() {
1793        String JavaDoc nameToFind = "g1";
1794        logger.log(BasicLevel.DEBUG, "testAutoRef");
1795        PersistenceManager pm = pmf.getPersistenceManager();
1796        String JavaDoc filter = "(previousRef != null) && (previousRef != this) && (previousRef.name == referenceName)";
1797        Query q = pm.newQuery(GeoRef.class, filter);
1798        q.declareParameters("String referenceName");
1799        Collection JavaDoc results = (Collection JavaDoc) q.execute(nameToFind);
1800        assertTrue("Result is empty", !results.isEmpty());
1801        Iterator JavaDoc it = results.iterator();
1802        while (it.hasNext()) {
1803            GeoRef g = (GeoRef) it.next();
1804            assertTrue("Name of previous ref is not correct", nameToFind.equals(g.getPreviousRef().getName()));
1805
1806        }
1807        q.closeAll();
1808        pm.close();
1809    }
1810        
1811    public void testSetRange() {
1812        logger.log(BasicLevel.DEBUG, "testUserCacheSingle");
1813        PersistenceManager pm = pmf.getPersistenceManager();
1814        Query q = pm.newQuery(User.class);
1815        q.setOrdering("name ascending");
1816        q.setRange(0, 5);
1817        Collection JavaDoc c = (Collection JavaDoc) q.execute();
1818        int s = c.size();
1819        q.closeAll();
1820        pm.close();
1821
1822        assertEquals("Bad result size", 5, s);
1823
1824        pm = pmf.getPersistenceManager();
1825        q = pm.newQuery(User.class);
1826        q.setOrdering("name ascending");
1827        q.setRange(2, 5);
1828        c = (Collection JavaDoc) q.execute();
1829        s = c.size();
1830        q.closeAll();
1831        pm.close();
1832        
1833        assertEquals("Bad result size", 3, s);
1834
1835        pm = pmf.getPersistenceManager();
1836        q = pm.newQuery(User.class);
1837        q.setOrdering("name ascending");
1838        c = (Collection JavaDoc) q.execute();
1839        s = c.size();
1840        q.closeAll();
1841        pm.close();
1842        
1843        assertEquals("Bad result size", 9, s);
1844        
1845        pm = pmf.getPersistenceManager();
1846        q = pm.newQuery(Group.class);
1847        Query q2 = pm.newQuery(Group.class);
1848        Collection JavaDoc usernames = Arrays.asList(new String JavaDoc[]{
1849                "user_g0_u0","user_g0_u1","user_g1_u0"});
1850        String JavaDoc var = "User u";
1851        q.declareVariables(var);
1852        q2.declareVariables(var);
1853        String JavaDoc param = "Collection usernames";
1854        q.declareParameters(param);
1855        q2.declareParameters(param);
1856        String JavaDoc filter = "(users.contains(u)) && usernames.contains(u.name)";
1857        q.setFilter(filter);
1858        q2.setFilter(filter);
1859        q.setRange(0, 5);
1860        q2.setRange(0, 5);
1861        
1862        q2.setResult("count(*)");
1863        q2.setUnique(true);
1864        
1865        int count = ((Long JavaDoc) q2.execute(usernames)).intValue();
1866        c = (Collection JavaDoc) q.execute(usernames);
1867        s = c.size();
1868        q.closeAll();
1869        q2.closeAll();
1870        pm.close();
1871        
1872        assertEquals("Bad count result", 3, count);
1873        assertEquals("Bad result size", 3, s);
1874    }
1875    
1876    public void testNullEqualityWithPrimitive() {
1877        PersistenceManager pm = pmf.getPersistenceManager();
1878        Query q = pm.newQuery(Group.class);
1879        q.setFilter("name != null");
1880        Collection JavaDoc c = (Collection JavaDoc) q.execute();
1881        int s = c.size();
1882        q.closeAll();
1883        pm.close();
1884        assertEquals("Bad result size", 3, s);
1885    }
1886    
1887}
1888
Popular Tags