KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdo > TestQueries


1 package org.apache.ojb.jdo;
2
3 import java.util.Collection JavaDoc;
4
5 import javax.jdo.PersistenceManager;
6 import javax.jdo.Query;
7
8 import junit.framework.TestCase;
9
10 import org.apache.ojb.otm.Person;
11
12 /* Copyright 2004 The Apache Software Foundation
13  *
14  * Licensed under the Apache License, Version 2.0 (the "License");
15  * you may not use this file except in compliance with the License.
16  * You may obtain a copy of the License at
17  *
18  * http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */

26
27
28 public class TestQueries extends TestCase
29 {
30     private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
31
32     private PersistenceManager pm;
33
34     public void setUp()
35     {
36         this.pm = factory.getPersistenceManager();
37     }
38
39     public void tearDown()
40     {
41         if (!this.pm.isClosed()) this.pm.close();
42     }
43
44     public void testEmptyFilter()
45     {
46         Person p = new Person("George", "Harrison");
47         pm.currentTransaction().begin();
48         pm.makePersistent(p);
49         pm.currentTransaction().commit();
50
51         pm.evictAll();
52
53         pm.currentTransaction().begin();
54         Query q = pm.newQuery(Person.class);
55         Collection JavaDoc results = (Collection JavaDoc) q.execute();
56         assertTrue(results.size() > 0);
57         assertTrue(results.iterator().next() instanceof Person);
58         pm.currentTransaction().commit();
59     }
60
61     public void testEmptyFilterSettingExtent()
62     {
63         Person p = new Person("George", "Harrison");
64         pm.currentTransaction().begin();
65         pm.makePersistent(p);
66         pm.currentTransaction().commit();
67
68         pm.evictAll();
69
70         pm.currentTransaction().begin();
71         Query q = pm.newQuery();
72         q.setCandidates(pm.getExtent(Person.class, true));
73         Collection JavaDoc results = (Collection JavaDoc) q.execute();
74         assertTrue(results.size() > 0);
75         assertTrue(results.iterator().next() instanceof Person);
76         pm.currentTransaction().commit();
77     }
78
79     public void testClonedQuery()
80     {
81         Person p = new Person("George", "Harrison");
82         pm.currentTransaction().begin();
83         pm.makePersistent(p);
84         pm.currentTransaction().commit();
85
86         pm.evictAll();
87
88         pm.currentTransaction().begin();
89         Query q = pm.newQuery(Person.class);
90         q.compile();
91         Query q2 = pm.newQuery(q);
92         Collection JavaDoc r = (Collection JavaDoc) q2.execute();
93         assertTrue(r.size() > 0);
94         assertTrue(r.iterator().next() instanceof Person);
95         pm.currentTransaction().commit();
96     }
97 }
98
Popular Tags