    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
    18  package org.apache.commons.configuration;
    19
    20  import java.io.FileInputStream;
    21  import java.sql.SQLException;
    22  import java.util.Iterator;
    23  import java.util.List;
    24
    25  import javax.sql.DataSource;
    26
    27  import junit.framework.TestCase;
    28
    29  import org.apache.commons.configuration.test.HsqlDB;
    30  import org.apache.commons.dbcp.BasicDataSource;
    31  import org.dbunit.database.DatabaseConnection;
    32  import org.dbunit.database.IDatabaseConnection;
    33  import org.dbunit.dataset.IDataSet;
    34  import org.dbunit.dataset.xml.XmlDataSet;
    35  import org.dbunit.operation.DatabaseOperation;
    36
    37  /***
    38   * Test for database stored configurations.  Note, when running this Unit
    39   * Test in Eclipse it sometimes takes a couple tries. Otherwise you may get
    40   * database is already in use by another process errors.
    41   *
    42   * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
    43   */
    44  public class TestDatabaseConfiguration extends TestCase
    45  {
    46      public final String DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
    47      public final String DATABASE_URL = "jdbc:hsqldb:target/test-classes/testdb";
    48      public final String DATABASE_USERNAME = "sa";
    49      public final String DATABASE_PASSWORD = "";
    50
    51      private static HsqlDB hsqlDB = null;
    52
    53      private DataSource datasource;
    54
    55      protected void setUp() throws Exception
    56      {
    57          /*
    58           * Thread.sleep may or may not help with the database is already in
    59           * use exception.
    60           */
    61          //Thread.sleep(1000);
    62
    63          // set up the datasource
    64
    65          if (hsqlDB == null)
    66          {
    67              hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER, "conf/testdb.script");
    68          }
    69
    70          BasicDataSource datasource = new BasicDataSource();
    71          datasource.setDriverClassName(DATABASE_DRIVER);
    72          datasource.setUrl(DATABASE_URL);
    73          datasource.setUsername(DATABASE_USERNAME);
    74          datasource.setPassword(DATABASE_PASSWORD);
    75
    76          this.datasource = datasource;
    77
    78
    79          // prepare the database
    80          IDatabaseConnection connection = new DatabaseConnection(datasource.getConnection());
    81          IDataSet dataSet = new XmlDataSet(new FileInputStream("conf/dataset.xml"));
    82
    83          try
    84          {
    85              DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    86          }
    87          finally
    88          {
    89              connection.close();
    90          }
    91      }
    92
    93      protected void tearDown() throws SQLException{
    94          datasource.getConnection().commit();
    95          datasource.getConnection().close();
    96      }
    97
    98      public void testAddPropertyDirectSingle()
    99      {
    100         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    101         config.addPropertyDirect("key", "value");
    102
    103         assertTrue("missing property", config.containsKey("key"));
    104     }
    105
    106     public void testAddPropertyDirectMultiple()
    107     {
    108         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    109         config.addPropertyDirect("key", "value");
    110
    111         assertTrue("missing property", config.containsKey("key"));
    112     }
    113
    114     public void testAddNonStringProperty()
    115     {
    116         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    117         config.addPropertyDirect("boolean", Boolean.TRUE);
    118
    119         assertTrue("missing property", config.containsKey("boolean"));
    120     }
    121
    122     public void testGetPropertyDirectSingle()
    123     {
    124         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    125
    126         assertEquals("property1", "value1", config.getProperty("key1"));
    127         assertEquals("property2", "value2", config.getProperty("key2"));
    128         assertEquals("unknown property", null, config.getProperty("key3"));
    129     }
    130
    131     public void testGetPropertyDirectMultiple()
    132     {
    133         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    134
    135         assertEquals("property1", "value1", config.getProperty("key1"));
    136         assertEquals("property2", "value2", config.getProperty("key2"));
    137         assertEquals("unknown property", null, config.getProperty("key3"));
    138     }
    139
    140     public void testClearPropertySingle()
    141     {
    142         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    143         config.clearProperty("key");
    144
    145         assertFalse("property not cleared", config.containsKey("key"));
    146     }
    147
    148     public void testClearPropertyMultiple()
    149     {
    150         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    151         config.clearProperty("key");
    152
    153         assertFalse("property not cleared", config.containsKey("key"));
    154     }
    155
    156     public void testClearSingle()
    157     {
    158         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    159         config.clear();
    160
    161         assertTrue("configuration is not cleared", config.isEmpty());
    162     }
    163
    164     public void testClearMultiple()
    165     {
    166         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    167         config.clear();
    168
    169         assertTrue("configuration is not cleared", config.isEmpty());
    170     }
    171
    172     public void testGetKeysSingle()
    173     {
    174         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    175         Iterator it = config.getKeys();
    176
    177         assertEquals("1st key", "key1", it.next());
    178         assertEquals("2nd key", "key2", it.next());
    179     }
    180
    181     public void testGetKeysMultiple()
    182     {
    183         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    184         Iterator it = config.getKeys();
    185
    186         assertEquals("1st key", "key1", it.next());
    187         assertEquals("2nd key", "key2", it.next());
    188     }
    189
    190     public void testContainsKeySingle()
    191     {
    192         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    193         assertTrue("missing key1", config.containsKey("key1"));
    194         assertTrue("missing key2", config.containsKey("key2"));
    195     }
    196
    197     public void testContainsKeyMultiple()
    198     {
    199         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    200         assertTrue("missing key1", config.containsKey("key1"));
    201         assertTrue("missing key2", config.containsKey("key2"));
    202     }
    203
    204     public void testIsEmptySingle()
    205     {
    206         Configuration config1 = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    207         assertFalse("The configuration is empty", config1.isEmpty());
    208     }
    209
    210     public void testIsEmptyMultiple()
    211     {
    212         Configuration config1 = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    213         assertFalse("The configuration named 'test' is empty", config1.isEmpty());
    214
    215         Configuration config2 = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "testIsEmpty");
    216         assertTrue("The configuration named 'testIsEmpty' is not empty", config2.isEmpty());
    217     }
    218
    219     public void testGetList()
    220     {
    221         Configuration config1 = new DatabaseConfiguration(datasource, "configurationList", "key", "value");
    222         List list = config1.getList("key3");
    223         assertEquals(3,list.size());
    224     }
    225
    226     public void testGetKeys()
    227     {
    228         Configuration config1 = new DatabaseConfiguration(datasource, "configurationList", "key", "value");
    229         Iterator i = config1.getKeys();
    230         assertTrue(i.hasNext());
    231         Object key = i.next();
    232         assertEquals("key3",key.toString());
    233         assertFalse(i.hasNext());
    234     }
    235
    236     public void testClearSubset()
    237     {
    238         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    239
    240         Configuration subset = config.subset("key1");
    241         subset.clear();
    242
    243         assertTrue("the subset is not empty", subset.isEmpty());
    244         assertFalse("the parent configuration is empty", config.isEmpty());
    245     }
    246
    247 }