01: package org.apache.ojb.jdo;
02:
03: /* Copyright 2004 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import java.util.Iterator;
19: import java.util.NoSuchElementException;
20:
21: import javax.jdo.Extent;
22: import javax.jdo.PersistenceManager;
23: import javax.jdo.Transaction;
24:
25: import junit.framework.TestCase;
26:
27: import org.apache.ojb.otm.Person;
28:
29: public class TestPersistenceManager extends TestCase {
30: /** Cheat and use our impl directly */
31: private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
32:
33: public void testLoadExtent() throws Exception {
34: Person article = new Person();
35: PersistenceManager pm = factory.getPersistenceManager();
36: Transaction tx = pm.currentTransaction();
37: tx.begin();
38: pm.makePersistent(article);
39: tx.commit();
40:
41: tx.begin();
42: Extent extent = pm.getExtent(Person.class, true);
43: Iterator itty = extent.iterator();
44: assertTrue(itty.hasNext());
45: tx.commit();
46: pm.close();
47: }
48:
49: public void testIteratorClosedWhenOutsideTx() throws Exception {
50: Person article = new Person();
51: PersistenceManager pm = factory.getPersistenceManager();
52: Transaction tx = pm.currentTransaction();
53: tx.begin();
54: pm.makePersistent(article);
55: tx.commit();
56:
57: tx.begin();
58: Extent extent = pm.getExtent(Person.class, true);
59: Iterator itty = extent.iterator();
60: tx.commit();
61: try {
62: itty.next();
63: fail("Should have thrown exception");
64: } catch (NoSuchElementException e) {
65: assertTrue("Flow of control will pass through here", true);
66: }
67: pm.close();
68: }
69:
70: public void testVerifyCannotQueryExtentWithoutSubclasses() {
71: PersistenceManager pm = factory.getPersistenceManager();
72: pm.currentTransaction().begin();
73: try {
74: pm.getExtent(Person.class, false);
75: fail("Not supposed to be supported!");
76: } catch (UnsupportedOperationException e) {
77: assertTrue("Flow goes through here", true);
78: }
79: pm.currentTransaction().commit();
80: pm.close();
81: }
82:
83: public void testQueryClassCast() {
84: PersistenceManager pm = factory.getPersistenceManager();
85: pm.currentTransaction().begin();
86: try {
87: pm.newQuery(new Object());
88: fail("Should not be able to pass incorrect argument to newQuery(Object)");
89: } catch (IllegalArgumentException e) {
90: assertTrue("Flow goes through here", true);
91: }
92: pm.currentTransaction().commit();
93: pm.close();
94: }
95: }
|