KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseTestSuite.java,v 1.4 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.util.Enumeration JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestResult;
28 import junit.framework.TestSuite;
29
30 import org.opensubsystems.core.error.OSSException;
31 import org.opensubsystems.core.util.Config;
32
33 /**
34  * This test suite which should be used to group tests accessing
35  * database. It provides functionality necessary to run database tests,
36  * mainly it points all tests in the suite to the correct database.
37  *
38  * @version $Id: DatabaseTestSuite.java,v 1.4 2007/01/07 06:14:20 bastafidli Exp $
39  * @author Miro Halas
40  * @code.reviewer Miro Halas
41  * @code.reviewed Initial revision
42  */

43 public class DatabaseTestSuite extends TestSuite
44 {
45    /**
46     * Data source which will be used by all tests in this suite
47     */

48    protected String JavaDoc m_strDataSourceName = null;
49    
50    /**
51     * Static initializer
52     */

53    static
54    {
55       if (Config.getInstance().getPropertyFileName() == null)
56       {
57          Config.getInstance().setPropertyFileName(DatabaseTest.DEFAULT_PROPERTY_FILE);
58       }
59    }
60    
61    /**
62     * Run all tests from
63     *
64     * @param strName - name of the test suite
65     */

66    public DatabaseTestSuite(
67       String JavaDoc strName
68    )
69    {
70       super(strName);
71    }
72
73    /**
74     * Run all tests from
75     *
76     * @param strName - name of the test suite
77     * @param strDataSourceName - data source which will be used by all tests in this suite
78     * @param strDriverName - driver name which will be used by by all tests in this suite
79     * @param strUrl - url this test connects to the database
80     * @throws OSSException - an error has occured
81     */

82    public DatabaseTestSuite(
83       String JavaDoc strName,
84       String JavaDoc strDataSourceName,
85       String JavaDoc strDriverName,
86       String JavaDoc strUrl
87    ) throws OSSException
88    {
89       super(strName);
90
91       DatabaseTest.addDataSource(strDataSourceName, strDriverName, strUrl);
92       DatabaseTest.setDataSourceName(strDataSourceName);
93       m_strDataSourceName = strDataSourceName;
94    }
95    
96    /**
97     * Run all tests from
98     *
99     * @param strName - name of the test suite
100     * @param strDataSourceName - data source which will be used by all tests in this suite
101     * @param strDriverName - driver name which will be used by by all tests in this suite
102     * @param strUrl - url this test connects to the database
103     * @param strUser - user name to connects to the database
104     * @param strPassword - password to connects to the database
105     * @throws OSSException - an error has occured
106     */

107    public DatabaseTestSuite(
108       String JavaDoc strName,
109       String JavaDoc strDataSourceName,
110       String JavaDoc strDriverName,
111       String JavaDoc strUrl,
112       String JavaDoc strUser,
113       String JavaDoc strPassword
114    ) throws OSSException
115    {
116       super(strName);
117
118       DatabaseTest.addDataSource(strDataSourceName, strDriverName, strUrl,
119                                  strUser, strPassword);
120       m_strDataSourceName = strDataSourceName;
121    }
122
123    /**
124     * {@inheritDoc}
125     */

126    public void runTest(
127       Test testToRun,
128       TestResult result
129    )
130    {
131       try
132       {
133          setTestDataSource(testToRun);
134       }
135       catch (OSSException bfeExc)
136       {
137          // This method cannto throw exception so we need to convert it to
138
// uncheck exception
139
throw new RuntimeException JavaDoc(bfeExc);
140       }
141       
142       super.runTest(testToRun, result);
143    }
144
145    /**
146     * Set datasource for all database tests in specified test or test suite
147     *
148     * @param testToSet - test or test suite to set the data source for
149     * @throws OSSException - an error has occured
150     */

151    protected void setTestDataSource(
152       Test testToSet
153    ) throws OSSException
154    {
155       if (m_strDataSourceName != null)
156       {
157          if (testToSet instanceof DatabaseTest)
158          {
159             DatabaseTest.setDataSourceName(m_strDataSourceName);
160          }
161          else if (testToSet instanceof TestSuite)
162          {
163             // We need to set the datasource for every applicable test in the suite
164
Enumeration JavaDoc enumTests;
165             Test testTemp;
166             
167             for (enumTests = ((TestSuite)testToSet).tests(); enumTests.hasMoreElements();)
168             {
169                testTemp = (Test)enumTests.nextElement();
170                // Do this to limit recursion
171
if (testTemp instanceof DatabaseTest)
172                {
173                   DatabaseTest.setDataSourceName(m_strDataSourceName);
174                }
175                else
176                {
177                   setTestDataSource(testTemp);
178                }
179             }
180          }
181       }
182    }
183 }
184
Popular Tags