KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
19 import org.apache.ojb.otm.Person;
20
21 import javax.jdo.JDOUserException;
22 import javax.jdo.PersistenceManager;
23 import javax.jdo.Query;
24 import javax.jdo.Transaction;
25 import java.util.Collection JavaDoc;
26
27 public class TestTransactions extends TestCase
28 {
29     /** Cheat and use our impl directly */
30     private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
31
32
33     public void testReBeginTx() throws Exception JavaDoc
34     {
35         Person person = new Person();
36         person.setFirstname("Brian");
37         person.setLastname("McCallister");
38         PersistenceManager pm = factory.getPersistenceManager();
39         Transaction tx = pm.currentTransaction();
40         tx.begin();
41         pm.makePersistent(person);
42         tx.commit();
43
44         tx.begin();
45         Query q = pm.newQuery(Person.class);
46         Collection JavaDoc persons = (Collection JavaDoc) q.execute();
47         tx.commit();
48         assertTrue(persons.size() > 0);
49         pm.close();
50     }
51
52     public void testExceptionOnReBegin()
53     {
54         PersistenceManager pm = factory.getPersistenceManager();
55         Transaction tx = pm.currentTransaction();
56         tx.begin();
57         try
58         {
59             tx.begin();
60             fail("Should have thrown an exception");
61         }
62         catch (JDOUserException e)
63         {
64             assertTrue("Flow passes through here", true);
65         }
66         pm.close();
67     }
68 }
69
Popular Tags