001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: PersistenceTestCase.java,v 1.9 2003/10/20 05:59:29 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import com.triactive.jdo.PersistenceManagerFactoryImpl;
014: import com.triactive.jdo.SchemaManager;
015: import com.triactive.jdo.SchemaManagerFactory;
016: import com.triactive.jdo.store.StoreManager;
017: import javax.jdo.PersistenceManagerFactory;
018: import javax.sql.DataSource;
019: import org.apache.log4j.Category;
020:
021: /**
022: * Abstract base class for all JDO unit tests needing access to a persistence
023: * manager factory. Uses database connection parameters acquired from system
024: * properties.
025: *
026: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
027: * @author <a href="mailto:cwalk@triactive.com">Christopher Walk</a>
028: * @version $Revision: 1.9 $
029: */
030:
031: public abstract class PersistenceTestCase extends DatabaseTestCase {
032: private static final Category LOG = Category
033: .getInstance(PersistenceTestCase.class);
034:
035: public static interface DataSourceFactory {
036: DataSource makePooledDataSource(String dbDriver, String dbURL,
037: String dbUser, String dbPassword) throws Exception;
038: }
039:
040: /** The PersistenceManagerFactory to use for all tests. */
041: protected static PersistenceManagerFactory pmf;
042:
043: /** The SchemaManager to use for all test. */
044: protected static SchemaManager schemaMgr;
045:
046: /** The unique string identifying the DBMS. */
047: protected static String vendorID;
048:
049: /**
050: * Used by the JUnit framework to construct tests.
051: *
052: * @param name Name of the <tt>TestCase</tt>.
053: */
054:
055: public PersistenceTestCase(String name) {
056: super (name);
057: init();
058: }
059:
060: private synchronized void init() {
061: if (pmf == null) {
062: try {
063: pmf = makePooledPMF("com.triactive.jdo.test.DBCPDataSourceFactory");
064: } catch (Exception e) {
065: LOG
066: .warn("Jakarta DBCP classes not available, connection pooling will not be used");
067: pmf = makeNonpooledPMF();
068: }
069:
070: schemaMgr = SchemaManagerFactory.getSchemaManager(pmf
071: .getPersistenceManager());
072: vendorID = ((StoreManager) schemaMgr).getDatabaseAdapter()
073: .getVendorID();
074:
075: if ("sqlserver".equals(vendorID))
076: TestObject.allowNegativeByteValues = false;
077: }
078: }
079:
080: protected PersistenceManagerFactory makePooledPMF(
081: String dsfClassName) throws Exception {
082: DataSourceFactory dsf = (DataSourceFactory) Class.forName(
083: dsfClassName).newInstance();
084: DataSource ds = dsf.makePooledDataSource(dbDriver, dbURL,
085: dbUser, dbPassword);
086:
087: pmf = new PersistenceManagerFactoryImpl();
088: pmf.setConnectionFactory(ds);
089:
090: return pmf;
091: }
092:
093: protected PersistenceManagerFactory makeNonpooledPMF() {
094: pmf = new PersistenceManagerFactoryImpl();
095:
096: pmf.setConnectionDriverName(dbDriver);
097: pmf.setConnectionURL(dbURL);
098: pmf.setConnectionUserName(dbUser);
099: pmf.setConnectionPassword(dbPassword);
100:
101: return pmf;
102: }
103:
104: protected void addClassesToSchema(Class[] classes) {
105: schemaMgr.addClasses(classes);
106: }
107: }
|