1 10 11 package com.triactive.jdo.test; 12 13 import com.triactive.jdo.PersistenceManagerFactoryImpl; 14 import com.triactive.jdo.SchemaManager; 15 import com.triactive.jdo.SchemaManagerFactory; 16 import com.triactive.jdo.store.StoreManager; 17 import javax.jdo.PersistenceManagerFactory; 18 import javax.sql.DataSource ; 19 import org.apache.log4j.Category; 20 21 30 31 public abstract class PersistenceTestCase extends DatabaseTestCase 32 { 33 private static final Category LOG = Category.getInstance(PersistenceTestCase.class); 34 35 public static interface DataSourceFactory 36 { 37 DataSource makePooledDataSource(String dbDriver, String dbURL, String dbUser, String dbPassword) throws Exception ; 38 } 39 40 41 42 protected static PersistenceManagerFactory pmf; 43 44 45 protected static SchemaManager schemaMgr; 46 47 48 protected static String vendorID; 49 50 51 56 57 public PersistenceTestCase(String name) 58 { 59 super(name); 60 init(); 61 } 62 63 64 private synchronized void init() 65 { 66 if (pmf == null) 67 { 68 try 69 { 70 pmf = makePooledPMF("com.triactive.jdo.test.DBCPDataSourceFactory"); 71 } 72 catch (Exception e) 73 { 74 LOG.warn("Jakarta DBCP classes not available, connection pooling will not be used"); 75 pmf = makeNonpooledPMF(); 76 } 77 78 schemaMgr = SchemaManagerFactory.getSchemaManager(pmf.getPersistenceManager()); 79 vendorID = ((StoreManager)schemaMgr).getDatabaseAdapter().getVendorID(); 80 81 if ("sqlserver".equals(vendorID)) 82 TestObject.allowNegativeByteValues = false; 83 } 84 } 85 86 87 protected PersistenceManagerFactory makePooledPMF(String dsfClassName) throws Exception 88 { 89 DataSourceFactory dsf = (DataSourceFactory)Class.forName(dsfClassName).newInstance(); 90 DataSource ds = dsf.makePooledDataSource(dbDriver, dbURL, dbUser, dbPassword); 91 92 pmf = new PersistenceManagerFactoryImpl(); 93 pmf.setConnectionFactory(ds); 94 95 return pmf; 96 } 97 98 99 protected PersistenceManagerFactory makeNonpooledPMF() 100 { 101 pmf = new PersistenceManagerFactoryImpl(); 102 103 pmf.setConnectionDriverName(dbDriver); 104 pmf.setConnectionURL(dbURL); 105 pmf.setConnectionUserName(dbUser); 106 pmf.setConnectionPassword(dbPassword); 107 108 return pmf; 109 } 110 111 112 protected void addClassesToSchema(Class [] classes) 113 { 114 schemaMgr.addClasses(classes); 115 } 116 } 117 | Popular Tags |