01: package org.apache.ojb.jdo;
02:
03: import java.util.Collection;
04:
05: import javax.jdo.PersistenceManager;
06: import javax.jdo.Query;
07:
08: import junit.framework.TestCase;
09:
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: public class TestQueries extends TestCase {
28: private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
29:
30: private PersistenceManager pm;
31:
32: public void setUp() {
33: this .pm = factory.getPersistenceManager();
34: }
35:
36: public void tearDown() {
37: if (!this .pm.isClosed())
38: this .pm.close();
39: }
40:
41: public void testEmptyFilter() {
42: Person p = new Person("George", "Harrison");
43: pm.currentTransaction().begin();
44: pm.makePersistent(p);
45: pm.currentTransaction().commit();
46:
47: pm.evictAll();
48:
49: pm.currentTransaction().begin();
50: Query q = pm.newQuery(Person.class);
51: Collection results = (Collection) q.execute();
52: assertTrue(results.size() > 0);
53: assertTrue(results.iterator().next() instanceof Person);
54: pm.currentTransaction().commit();
55: }
56:
57: public void testEmptyFilterSettingExtent() {
58: Person p = new Person("George", "Harrison");
59: pm.currentTransaction().begin();
60: pm.makePersistent(p);
61: pm.currentTransaction().commit();
62:
63: pm.evictAll();
64:
65: pm.currentTransaction().begin();
66: Query q = pm.newQuery();
67: q.setCandidates(pm.getExtent(Person.class, true));
68: Collection results = (Collection) q.execute();
69: assertTrue(results.size() > 0);
70: assertTrue(results.iterator().next() instanceof Person);
71: pm.currentTransaction().commit();
72: }
73:
74: public void testClonedQuery() {
75: Person p = new Person("George", "Harrison");
76: pm.currentTransaction().begin();
77: pm.makePersistent(p);
78: pm.currentTransaction().commit();
79:
80: pm.evictAll();
81:
82: pm.currentTransaction().begin();
83: Query q = pm.newQuery(Person.class);
84: q.compile();
85: Query q2 = pm.newQuery(q);
86: Collection r = (Collection) q2.execute();
87: assertTrue(r.size() > 0);
88: assertTrue(r.iterator().next() instanceof Person);
89: pm.currentTransaction().commit();
90: }
91: }
|