001: package org.apache.ojb.junit;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.PersistenceBroker;
019: import org.apache.ojb.broker.PersistenceBrokerFactory;
020: import org.apache.ojb.broker.PersistenceBrokerException;
021: import org.apache.ojb.broker.platforms.Platform;
022:
023: /**
024: * A base class for PB-api based test cases.
025: * NOTE: The PB instance is declared <tt>public</tt> (no getter/setter) for easy use.
026: *
027: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
028: * @version $Id: PBTestCase.java,v 1.3.2.4 2005/12/21 22:31:29 tomdz Exp $
029: */
030: public class PBTestCase extends OJBTestCase {
031: public PersistenceBroker broker;
032: private String platformClass;
033:
034: public PBTestCase() {
035: }
036:
037: public PBTestCase(String name) {
038: super (name);
039: }
040:
041: public void setUp() throws Exception {
042: Platform platform;
043:
044: super .setUp();
045: assertNotNull(broker = PersistenceBrokerFactory
046: .defaultPersistenceBroker());
047: assertNotNull(platform = broker.serviceConnectionManager()
048: .getSupportedPlatform());
049: platformClass = platform.getClass().getName();
050: }
051:
052: public void tearDown() throws Exception {
053: if (broker != null) {
054: try {
055: broker.close();
056: } catch (Exception ignore) {
057: }
058: }
059: super .tearDown();
060: }
061:
062: /**
063: * Returns the platform implementation class name of the currently
064: * used broker.
065: * @return platform implementation class name
066: */
067: public String getPlatformClass() {
068: return platformClass;
069: }
070:
071: /**
072: * Persists an object with PB-API in a method-local transaction.
073: * @param obj the object to persist
074: * @throws org.apache.ojb.broker.TransactionInProgressException
075: * if external transaction in progress
076: * @throws org.apache.ojb.broker.PersistenceBrokerException
077: * on persistence error
078: */
079: public void pbPersist(Object obj) {
080: try {
081: broker.beginTransaction();
082: broker.store(obj);
083: broker.commitTransaction();
084: } catch (PersistenceBrokerException pbe) {
085: throw pbe;
086: } catch (ClassCastException cce) {
087: System.err.println("Error in JDBC-driver while storing: "
088: + obj);
089: throw cce;
090: } finally {
091: if (broker.isInTransaction()) {
092: try {
093: broker.abortTransaction();
094: } catch (Throwable ignore) {
095: //ignore
096: }
097: }
098: }
099: }
100:
101: }
|