2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.configuration;
19
import java.io.FileInputStream;
20
import java.sql.Connection;
22
import javax.sql.DataSource;
24
import org.apache.commons.configuration.test.HsqlDB;
25
import org.apache.commons.dbcp.BasicDataSource;
26
import org.dbunit.database.DatabaseConnection;
27
import org.dbunit.database.IDatabaseConnection;
28
import org.dbunit.dataset.IDataSet;
29
import org.dbunit.dataset.xml.XmlDataSet;
30
import org.dbunit.operation.DatabaseOperation;
33
* A helper class for performing tests for {@link DatabaseConfiguration}. This
34
* class maintains an in-process database that stores configuration data and can
35
* be accessed from a {@link DatabaseConfiguration} instance. Constants for
36
* table and column names and database connection settings are provided, too.
38
* @version $Id: DatabaseConfigurationTestHelper.java 931307 2010-04-06 20:29:51Z oheger $
40
public class DatabaseConfigurationTestHelper
42
/** Constant for the JDBC driver class. */
43
public final String DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
45
/** Constant for the connection URL. */
46
public final String DATABASE_URL = "jdbc:hsqldb:mem:testdb";
48
/** Constant for the DB user name. */
49
public final String DATABASE_USERNAME = "sa";
51
/** Constant for the DB password. */
52
public final String DATABASE_PASSWORD = "";
54
/** Constant for the configuration table. */
55
public static final String TABLE = "configuration";
57
/** Constant for the multi configuration table. */
58
public static final String TABLE_MULTI = "configurations";
60
/** Constant for the column with the keys. */
61
public static final String COL_KEY = "key";
63
/** Constant for the column with the values. */
64
public static final String COL_VALUE = "value";
66
/** Constant for the column with the configuration name. */
67
public static final String COL_NAME = "name";
69
/** Constant for the name of the test configuration. */
70
public static final String CONFIG_NAME = "test";
72
/** Stores the in-process database. */
73
private HsqlDB hsqlDB;
75
/** The data source. */
76
private DataSource datasource;
79
* The auto-commit mode for the connections created by the managed data
82
private boolean autoCommit = true;
85
* Returns the auto-commit mode of the connections created by the managed
88
* @return the auto-commit mode
90
public boolean isAutoCommit()
96
* Sets the auto-commit mode of the connections created by the managed data
99
* @param autoCommit the auto-commit mode
101
public void setAutoCommit(boolean autoCommit)
103
this.autoCommit = autoCommit;
107
* Initializes this helper object. This method can be called from a
108
* <code>setUp()</code> method of a unit test class. It creates the database
109
* instance if necessary.
111
* @throws Exception if an error occurs
113
public void setUp() throws Exception
115
hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER, "conf/testdb.script");
119
* Frees the resources used by this helper class. This method can be called
120
* by a <code>tearDown()</code> method of a unit test class.
122
* @throws Exception if an error occurs
124
public void tearDown() throws Exception
126
if (datasource != null)
128
datasource.getConnection().close();
134
* Creates a database configuration with default values.
136
* @return the configuration
138
public DatabaseConfiguration setUpConfig()
140
return new DatabaseConfiguration(getDatasource(), TABLE, COL_KEY,
141
COL_VALUE, !isAutoCommit());
145
* Creates a database configuration that supports multiple configurations in
146
* a table with default values.
148
* @return the configuration
150
public DatabaseConfiguration setUpMultiConfig()
152
return setUpMultiConfig(CONFIG_NAME);
156
* Creates a database configuration that supports multiple configurations in
157
* a table and sets the specified configuration name.
159
* @param configName the name of the configuration
160
* @return the configuration
162
public DatabaseConfiguration setUpMultiConfig(String configName)
164
return new DatabaseConfiguration(getDatasource(), TABLE_MULTI,
165
COL_NAME, COL_KEY, COL_VALUE, configName, !isAutoCommit());
169
* Returns the <code>DataSource</code> managed by this class. The data
170
* source is created on first access.
172
* @return the <code>DataSource</code>
174
public DataSource getDatasource()
176
if (datasource == null)
180
datasource = setUpDataSource();
184
throw new ConfigurationRuntimeException(
185
"Could not create data source", ex);
192
* Creates the internal data source. This method also initializes the
195
* @return the data source
196
* @throws Exception if an error occurs
198
private DataSource setUpDataSource() throws Exception
200
BasicDataSource ds = new BasicDataSource();
201
ds.setDriverClassName(DATABASE_DRIVER);
202
ds.setUrl(DATABASE_URL);
203
ds.setUsername(DATABASE_USERNAME);
204
ds.setPassword(DATABASE_PASSWORD);
205
ds.setDefaultAutoCommit(isAutoCommit());
207
// prepare the database
208
Connection conn = ds.getConnection();
209
IDatabaseConnection connection = new DatabaseConnection(conn);
210
IDataSet dataSet = new XmlDataSet(new FileInputStream(
211
"conf/dataset.xml"));
215
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);