KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > junit > PBTestCase


1 package org.apache.ojb.junit;
2
3 /* Copyright 2002-2005 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 org.apache.ojb.broker.PersistenceBroker;
19 import org.apache.ojb.broker.PersistenceBrokerFactory;
20 import org.apache.ojb.broker.PersistenceBrokerException;
21 import org.apache.ojb.broker.platforms.Platform;
22
23 /**
24  * A base class for PB-api based test cases.
25  * NOTE: The PB instance is declared <tt>public</tt> (no getter/setter) for easy use.
26  *
27  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
28  * @version $Id: PBTestCase.java,v 1.3.2.4 2005/12/21 22:31:29 tomdz Exp $
29  */

30 public class PBTestCase extends OJBTestCase
31 {
32     public PersistenceBroker broker;
33     private String JavaDoc platformClass;
34
35     public PBTestCase()
36     {
37     }
38
39     public PBTestCase(String JavaDoc name)
40     {
41         super(name);
42     }
43
44     public void setUp() throws Exception JavaDoc
45     {
46         Platform platform;
47
48         super.setUp();
49         assertNotNull(broker = PersistenceBrokerFactory.defaultPersistenceBroker());
50         assertNotNull(platform = broker.serviceConnectionManager().getSupportedPlatform());
51         platformClass = platform.getClass().getName();
52     }
53
54     public void tearDown() throws Exception JavaDoc
55     {
56         if(broker != null)
57         {
58             try
59             {
60                 broker.close();
61             }
62             catch(Exception JavaDoc ignore)
63             {
64             }
65         }
66         super.tearDown();
67     }
68
69     /**
70      * Returns the platform implementation class name of the currently
71      * used broker.
72      * @return platform implementation class name
73      */

74     public String JavaDoc getPlatformClass()
75     {
76         return platformClass;
77     }
78
79     /**
80      * Persists an object with PB-API in a method-local transaction.
81      * @param obj the object to persist
82      * @throws org.apache.ojb.broker.TransactionInProgressException
83      * if external transaction in progress
84      * @throws org.apache.ojb.broker.PersistenceBrokerException
85      * on persistence error
86      */

87     public void pbPersist(Object JavaDoc obj)
88     {
89         try
90         {
91             broker.beginTransaction();
92             broker.store(obj);
93             broker.commitTransaction();
94         }
95         catch (PersistenceBrokerException pbe)
96         {
97             throw pbe;
98         }
99         catch (ClassCastException JavaDoc cce)
100         {
101             System.err.println("Error in JDBC-driver while storing: " + obj);
102             throw cce;
103         }
104         finally
105         {
106             if (broker.isInTransaction())
107             {
108                 try
109                 {
110                     broker.abortTransaction();
111                 }
112                 catch (Throwable JavaDoc ignore)
113                 {
114                     //ignore
115
}
116             }
117         }
118     }
119
120 }
121
Popular Tags