KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.jdo;
2
3 /* Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
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 {
31     /** Cheat and use our impl directly */
32     private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
33
34     public void testLoadExtent() throws Exception JavaDoc
35     {
36         Person article = new Person();
37         PersistenceManager pm = factory.getPersistenceManager();
38         Transaction tx = pm.currentTransaction();
39         tx.begin();
40         pm.makePersistent(article);
41         tx.commit();
42
43         tx.begin();
44         Extent extent = pm.getExtent(Person.class, true);
45         Iterator JavaDoc itty = extent.iterator();
46         assertTrue(itty.hasNext());
47         tx.commit();
48         pm.close();
49     }
50
51     public void testIteratorClosedWhenOutsideTx() throws Exception JavaDoc
52     {
53         Person article = new Person();
54         PersistenceManager pm = factory.getPersistenceManager();
55         Transaction tx = pm.currentTransaction();
56         tx.begin();
57         pm.makePersistent(article);
58         tx.commit();
59
60         tx.begin();
61         Extent extent = pm.getExtent(Person.class, true);
62         Iterator JavaDoc itty = extent.iterator();
63         tx.commit();
64         try
65         {
66             itty.next();
67             fail("Should have thrown exception");
68         }
69         catch (NoSuchElementException JavaDoc e)
70         {
71             assertTrue("Flow of control will pass through here", true);
72         }
73         pm.close();
74     }
75
76     public void testVerifyCannotQueryExtentWithoutSubclasses()
77     {
78         PersistenceManager pm = factory.getPersistenceManager();
79         pm.currentTransaction().begin();
80         try
81         {
82             pm.getExtent(Person.class, false);
83             fail("Not supposed to be supported!");
84         }
85         catch (UnsupportedOperationException JavaDoc e)
86         {
87             assertTrue("Flow goes through here", true);
88         }
89         pm.currentTransaction().commit();
90         pm.close();
91     }
92
93     public void testQueryClassCast()
94     {
95         PersistenceManager pm = factory.getPersistenceManager();
96         pm.currentTransaction().begin();
97         try
98         {
99             pm.newQuery(new Object JavaDoc());
100             fail("Should not be able to pass incorrect argument to newQuery(Object)");
101         }
102         catch (IllegalArgumentException JavaDoc e)
103         {
104             assertTrue("Flow goes through here", true);
105         }
106         pm.currentTransaction().commit();
107         pm.close();
108     }
109 }
110
Popular Tags