KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > DatabaseTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseTest.java,v 1.11 2007/01/07 06:14:20 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db;
23
24 import java.sql.Connection JavaDoc;
25
26 import javax.transaction.Status JavaDoc;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.opensubsystems.core.error.OSSException;
32 import org.opensubsystems.core.util.Config;
33
34 /**
35  * Base class for all tests that access the database. Test cases derived from
36  * this class should use DatabaseTestSetup adapter and follow comments in this
37  * class to properly initialize and shutdown database.
38  *
39  * @version $Id: DatabaseTest.java,v 1.11 2007/01/07 06:14:20 bastafidli Exp $
40  * @author Miro Halas
41  * @code.reviewer Miro Halas
42  * @code.reviewed 1.5 2004/12/18 06:18:22 bastafidli
43  */

44 public abstract class DatabaseTest extends TestCase
45 {
46    // Constants ////////////////////////////////////////////////////////////////
47

48    /**
49     * Default name for database user. Make this name different from the name
50     * used by the real code so that tests execute in tables owned by different
51     * user.
52     */

53    protected static final String JavaDoc DEFAULT_DB_USER = "bastatest";
54
55    /**
56     * Default password for database user.
57     */

58    protected static final String JavaDoc DEFAULT_DB_PASSWORD = "fidli";
59
60    /**
61     * Default property file used to run tests.
62     */

63    protected static final String JavaDoc DEFAULT_PROPERTY_FILE = "osstest.properties";
64
65    // Attributes ///////////////////////////////////////////////////////////////
66

67    /**
68     * Database transaction which can be used by test. This transaction object
69     * is instantiated before every test is started and checked if the transaction
70     * was propertly finished after the test is done.
71     */

72    protected UserTransaction JavaDoc m_transaction;
73    
74    /**
75     * Connection to the database which can be used by test. This connection object
76     * is instantiated before every test is started and returned after the test is
77     * done. This connection may be used to quickly access the database in the test
78     * without worrying about its creation and destruction.
79     */

80    protected Connection JavaDoc m_connection;
81
82    /**
83     * How many connections were requested at the beginning of the test from
84     * the pool. This should must the number of requested connection after the
85     * test.
86     */

87    protected int m_iRequestedConnectionCount;
88
89    // Constructors /////////////////////////////////////////////////////////////
90

91    /**
92     * Static initializer
93     */

94    static
95    {
96       if (Config.getInstance().getPropertyFileName() == null)
97       {
98          Config.getInstance().setPropertyFileName(DEFAULT_PROPERTY_FILE);
99       }
100    }
101
102    /**
103     * Create new DatabaseTest.
104     *
105     * @param strTestName - name of the test
106     */

107    public DatabaseTest(
108       String JavaDoc strTestName
109    )
110    {
111       super(strTestName);
112     
113       m_transaction = null;
114       m_connection = null;
115    }
116    
117    /**
118     * Create data source to be managed by the transaction manager.
119     * Each test must specify the name of the data source. If it doesn't exist,
120     * it will be created.
121     *
122     * @param strDataSourceName - data source which will be used by this test
123     * @param strDriverName - driver name which will be used by this test
124     * @param strUrl - url this test connects to the database \
125     * @throws OSSException - an error has occured
126     */

127    public static synchronized void addDataSource(
128       String JavaDoc strDataSourceName,
129       String JavaDoc strDriverName,
130       String JavaDoc strUrl
131    ) throws OSSException
132    {
133       DatabaseConnectionFactoryImpl.getInstance().addDataSource(strDataSourceName,
134                                                                 strDriverName,
135                                                                 strUrl,
136                                                                 DEFAULT_DB_USER,
137                                                                 DEFAULT_DB_PASSWORD);
138    }
139    
140    /**
141     * Create data source to be managed by the transaction manager.
142     * Each test must specify the name of the data source. If it doesn't exist,
143     * it will be created.
144     *
145     * @param strDataSourceName - data source which will be used by this test
146     * @param strDriverName - driver name which will be used by this test
147     * @param strUrl - url this test connects to the database
148     * @param strUser - user name to connects to the database
149     * @param strPassword - password to connects to the database
150     * @throws OSSException - an error has occured
151     */

152    public static synchronized void addDataSource(
153       String JavaDoc strDataSourceName,
154       String JavaDoc strDriverName,
155       String JavaDoc strUrl,
156       String JavaDoc strUser,
157       String JavaDoc strPassword
158    ) throws OSSException
159    {
160       DatabaseConnectionFactoryImpl.getInstance().addDataSource(strDataSourceName,
161                                                                 strDriverName,
162                                                                 strUrl,
163                                                                 strUser,
164                                                                 strPassword);
165    }
166
167    /**
168     * Set the data source to be used by this database test. THis method must
169     * be called before any test can be run.
170     *
171     * @param strDataSourceName - data source which will be used by this test
172     * @throws OSSException - an error has occured
173     */

174    public static void setDataSourceName(
175       String JavaDoc strDataSourceName
176    ) throws OSSException
177    {
178       DatabaseConnectionFactoryImpl.getInstance().setDefaultDataSourceName(
179          strDataSourceName);
180    }
181
182    /**
183     * Get the name of the data source used by this test.
184     *
185     * @return String - data source name
186     * @throws OSSException - an error has occured
187     */

188    public static String JavaDoc getDataSourceName(
189    ) throws OSSException
190    {
191       return DatabaseConnectionFactoryImpl.getInstance().getDefaultDataSourceName();
192    }
193
194    /**
195     * Set up environment for the test case.
196     *
197     * @throws Exception - an error has occured while setting up test
198     */

199    protected void setUp(
200    ) throws Exception JavaDoc
201    {
202       super.setUp();
203
204       // Get connection after the transaction so that we do not have to worry
205
// about returning it in case it fails
206
m_transaction = DatabaseTransactionFactoryImpl.getInstance().requestTransaction();
207       // Request autocommit false since we might be modifying database
208
m_connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false);
209       
210       // Remember how many connections should be out of the pool after the test
211
m_iRequestedConnectionCount
212          = DatabaseConnectionFactoryImpl.getInstance().getTotalRequestedConnectionCount();
213    }
214
215    /**
216     * Restore original environment after the test case.
217     *
218     * @throws Exception - an error has occured while tearing down test
219     */

220    protected void tearDown(
221    ) throws Exception JavaDoc
222    {
223       try
224       {
225          // Remember how many connections should be out of the pool after the test
226
assertEquals("Somebody forgot to return connection to the pool.",
227             m_iRequestedConnectionCount,
228             DatabaseConnectionFactoryImpl.getInstance().getTotalRequestedConnectionCount());
229
230          int iStatus;
231          
232          iStatus = m_transaction.getStatus();
233          
234          if ((iStatus != Status.STATUS_NO_TRANSACTION)
235              && (iStatus != Status.STATUS_COMMITTED)
236              && (iStatus != Status.STATUS_ROLLEDBACK))
237          {
238             fail("Transaction wasn't commited or rollbacked. Status is " + iStatus);
239          }
240       }
241       finally
242       {
243          if (m_connection != null)
244          {
245             if (!m_connection.isClosed())
246             {
247                DatabaseConnectionFactoryImpl.getInstance().returnConnection(m_connection);
248             }
249          }
250       }
251
252       super.tearDown();
253    }
254 }
255
Popular Tags