~ubuntu-branches/ubuntu/saucy/commons-configuration/saucy

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-07-01 16:29:44 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130701162944-98waq5gogha5gpn1
Tags: 1.9-1
* New upstream release
* debian/control:
  - Updated Standards-Version to 3.9.4 (no changes)
  - Use canonical URLs for the Vcs-* fields
  - Added new build dependencies (libjavacc-maven-plugin-java, junit4)
  - Upgraded the dependency on the Servlet API (2.5 -> 3.0)
  - Removed the dependency on the Activation Framework (glassfish-activation)
  - Replaced the dependency on glassfish-mail with libgnumail-java
  - Removed the unused dependencies:
    liblog4j1.2-java-doc, libmaven-assembly-plugin-java
  - Replaced the dependency on libcommons-jexl-java by libcommons-jexl2-java
* debian/watch: Changed to point the official Apache distribution server
* Removed the obsolete file debian/ant.properties
* Installed the upstream changelog in the binary packages
* Added the report plugins to maven.ignoreRules
* Added the classpath attribute to the jar manifest

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
package org.apache.commons.configuration;
18
 
 
19
 
import java.io.FileInputStream;
20
 
import java.sql.Connection;
21
 
 
22
 
import javax.sql.DataSource;
23
 
 
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;
31
 
 
32
 
/**
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.
37
 
 *
38
 
 * @version $Id: DatabaseConfigurationTestHelper.java 931307 2010-04-06 20:29:51Z oheger $
39
 
 */
40
 
public class DatabaseConfigurationTestHelper
41
 
{
42
 
    /** Constant for the JDBC driver class. */
43
 
    public final String DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
44
 
 
45
 
    /** Constant for the connection URL. */
46
 
    public final String DATABASE_URL = "jdbc:hsqldb:mem:testdb";
47
 
 
48
 
    /** Constant for the DB user name. */
49
 
    public final String DATABASE_USERNAME = "sa";
50
 
 
51
 
    /** Constant for the DB password. */
52
 
    public final String DATABASE_PASSWORD = "";
53
 
 
54
 
    /** Constant for the configuration table. */
55
 
    public static final String TABLE = "configuration";
56
 
 
57
 
    /** Constant for the multi configuration table. */
58
 
    public static final String TABLE_MULTI = "configurations";
59
 
 
60
 
    /** Constant for the column with the keys. */
61
 
    public static final String COL_KEY = "key";
62
 
 
63
 
    /** Constant for the column with the values. */
64
 
    public static final String COL_VALUE = "value";
65
 
 
66
 
    /** Constant for the column with the configuration name. */
67
 
    public static final String COL_NAME = "name";
68
 
 
69
 
    /** Constant for the name of the test configuration. */
70
 
    public static final String CONFIG_NAME = "test";
71
 
 
72
 
    /** Stores the in-process database. */
73
 
    private HsqlDB hsqlDB;
74
 
 
75
 
    /** The data source. */
76
 
    private DataSource datasource;
77
 
 
78
 
    /**
79
 
     * The auto-commit mode for the connections created by the managed data
80
 
     * source.
81
 
     */
82
 
    private boolean autoCommit = true;
83
 
 
84
 
    /**
85
 
     * Returns the auto-commit mode of the connections created by the managed
86
 
     * data source.
87
 
     *
88
 
     * @return the auto-commit mode
89
 
     */
90
 
    public boolean isAutoCommit()
91
 
    {
92
 
        return autoCommit;
93
 
    }
94
 
 
95
 
    /**
96
 
     * Sets the auto-commit mode of the connections created by the managed data
97
 
     * source.
98
 
     *
99
 
     * @param autoCommit the auto-commit mode
100
 
     */
101
 
    public void setAutoCommit(boolean autoCommit)
102
 
    {
103
 
        this.autoCommit = autoCommit;
104
 
    }
105
 
 
106
 
    /**
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.
110
 
     *
111
 
     * @throws Exception if an error occurs
112
 
     */
113
 
    public void setUp() throws Exception
114
 
    {
115
 
        hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER, "conf/testdb.script");
116
 
    }
117
 
 
118
 
    /**
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.
121
 
     *
122
 
     * @throws Exception if an error occurs
123
 
     */
124
 
    public void tearDown() throws Exception
125
 
    {
126
 
        if (datasource != null)
127
 
        {
128
 
            datasource.getConnection().close();
129
 
        }
130
 
        hsqlDB.close();
131
 
    }
132
 
 
133
 
    /**
134
 
     * Creates a database configuration with default values.
135
 
     *
136
 
     * @return the configuration
137
 
     */
138
 
    public DatabaseConfiguration setUpConfig()
139
 
    {
140
 
        return new DatabaseConfiguration(getDatasource(), TABLE, COL_KEY,
141
 
                COL_VALUE, !isAutoCommit());
142
 
    }
143
 
 
144
 
    /**
145
 
     * Creates a database configuration that supports multiple configurations in
146
 
     * a table with default values.
147
 
     *
148
 
     * @return the configuration
149
 
     */
150
 
    public DatabaseConfiguration setUpMultiConfig()
151
 
    {
152
 
        return setUpMultiConfig(CONFIG_NAME);
153
 
    }
154
 
 
155
 
    /**
156
 
     * Creates a database configuration that supports multiple configurations in
157
 
     * a table and sets the specified configuration name.
158
 
     *
159
 
     * @param configName the name of the configuration
160
 
     * @return the configuration
161
 
     */
162
 
    public DatabaseConfiguration setUpMultiConfig(String configName)
163
 
    {
164
 
        return new DatabaseConfiguration(getDatasource(), TABLE_MULTI,
165
 
                COL_NAME, COL_KEY, COL_VALUE, configName, !isAutoCommit());
166
 
    }
167
 
 
168
 
    /**
169
 
     * Returns the <code>DataSource</code> managed by this class. The data
170
 
     * source is created on first access.
171
 
     *
172
 
     * @return the <code>DataSource</code>
173
 
     */
174
 
    public DataSource getDatasource()
175
 
    {
176
 
        if (datasource == null)
177
 
        {
178
 
            try
179
 
            {
180
 
                datasource = setUpDataSource();
181
 
            }
182
 
            catch (Exception ex)
183
 
            {
184
 
                throw new ConfigurationRuntimeException(
185
 
                        "Could not create data source", ex);
186
 
            }
187
 
        }
188
 
        return datasource;
189
 
    }
190
 
 
191
 
    /**
192
 
     * Creates the internal data source. This method also initializes the
193
 
     * database.
194
 
     *
195
 
     * @return the data source
196
 
     * @throws Exception if an error occurs
197
 
     */
198
 
    private DataSource setUpDataSource() throws Exception
199
 
    {
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());
206
 
 
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"));
212
 
 
213
 
        try
214
 
        {
215
 
            DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
216
 
        }
217
 
        finally
218
 
        {
219
 
            if (!isAutoCommit())
220
 
            {
221
 
                conn.commit();
222
 
            }
223
 
            connection.close();
224
 
        }
225
 
 
226
 
        return ds;
227
 
    }
228
 
}