KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > PersistenceTestCase


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: PersistenceTestCase.java,v 1.9 2003/10/20 05:59:29 jackknifebarber Exp $
9  */

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 JavaDoc;
19 import org.apache.log4j.Category;
20
21 /**
22  * Abstract base class for all JDO unit tests needing access to a persistence
23  * manager factory. Uses database connection parameters acquired from system
24  * properties.
25  *
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  * @author <a HREF="mailto:cwalk@triactive.com">Christopher Walk</a>
28  * @version $Revision: 1.9 $
29  */

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 JavaDoc makePooledDataSource(String JavaDoc dbDriver, String JavaDoc dbURL, String JavaDoc dbUser, String JavaDoc dbPassword) throws Exception JavaDoc;
38     }
39
40
41     /** The PersistenceManagerFactory to use for all tests. */
42     protected static PersistenceManagerFactory pmf;
43
44     /** The SchemaManager to use for all test. */
45     protected static SchemaManager schemaMgr;
46
47     /** The unique string identifying the DBMS. */
48     protected static String JavaDoc vendorID;
49
50
51     /**
52      * Used by the JUnit framework to construct tests.
53      *
54      * @param name Name of the <tt>TestCase</tt>.
55      */

56
57     public PersistenceTestCase(String JavaDoc 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 JavaDoc 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 JavaDoc dsfClassName) throws Exception JavaDoc
88     {
89         DataSourceFactory dsf = (DataSourceFactory)Class.forName(dsfClassName).newInstance();
90         DataSource JavaDoc 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 JavaDoc[] classes)
113     {
114         schemaMgr.addClasses(classes);
115     }
116 }
117
Popular Tags