    1   /*
    2    * Copyright 2001-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License")
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.configuration;
    18
    19  import java.io.FileInputStream;
    20  import java.sql.SQLException;
    21  import java.util.Iterator;
    22  import java.util.List;
    23  import java.util.Vector;
    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
    32  import org.dbunit.database.DatabaseConnection;
    33  import org.dbunit.database.IDatabaseConnection;
    34  import org.dbunit.dataset.IDataSet;
    35  import org.dbunit.dataset.xml.XmlDataSet;
    36  import org.dbunit.operation.DatabaseOperation;
    37
    38  /***
    39   * Test for database stored configurations.  Note, when running this Unit
    40   * Test in Eclipse it sometimes takes a couple tries.  Otherwise you ma get
    41   * database is already in use by another process errors.
    42   *
    43   * @version $Revision: 1.9 $, $Date: 2004/10/11 09:17:09 $
    44   */
    45  public class TestDatabaseConfiguration extends TestCase
    46  {
    47      public final String DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
    48      public final String DATABASE_URL = "jdbc:hsqldb:target/test-classes/testdb";
    49
    50      private static HsqlDB hsqlDB = null;
    51
    52      private DataSource datasource;
    53
    54      protected void setUp() throws Exception
    55      {
    56      	/*
    57      	 * Thread.sleep may or may not help with the database is already in
    58      	 * use exception.
    59      	 */
    60      	//Thread.sleep(1000);
    61
    62          // set up the datasource
    63
    64          if (hsqlDB == null)
    65          {
    66              hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER, "conf/testdb.script");
    67          }
    68
    69          BasicDataSource datasource = new BasicDataSource();
    70          datasource.setDriverClassName(DATABASE_DRIVER);
    71          datasource.setUrl(DATABASE_URL);
    72          datasource.setUsername("sa");
    73          datasource.setPassword("");
    74
    75          this.datasource = datasource;
    76
    77
    78          // prepare the database
    79          IDatabaseConnection connection = new DatabaseConnection(datasource.getConnection());
    80          IDataSet dataSet = new XmlDataSet(new FileInputStream("conf/dataset.xml"));
    81
    82          try
    83          {
    84              DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    85          }
    86          finally
    87          {
    88              connection.close();
    89          }
    90      }
    91
    92      protected void tearDown() throws SQLException{
    93          datasource.getConnection().commit();
    94          datasource.getConnection().close();
    95      }
    96
    97      public void testAddPropertyDirectSingle()
    98      {
    99          DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    100         config.addPropertyDirect("key", "value");
    101
    102         assertTrue("missing property", config.containsKey("key"));
    103     }
    104
    105     public void testAddPropertyDirectMultiple()
    106     {
    107         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    108         config.addPropertyDirect("key", "value");
    109
    110         assertTrue("missing property", config.containsKey("key"));
    111     }
    112
    113     public void testAddNonStringProperty()
    114     {
    115         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    116         config.addPropertyDirect("boolean", Boolean.TRUE);
    117
    118         assertTrue("missing property", config.containsKey("boolean"));
    119     }
    120
    121     public void testGetPropertyDirectSingle()
    122     {
    123         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    124
    125         assertEquals("property1", "value1", config.getPropertyDirect("key1"));
    126         assertEquals("property2", "value2", config.getPropertyDirect("key2"));
    127         assertEquals("unknown property", null, config.getPropertyDirect("key3"));
    128     }
    129
    130     public void testGetPropertyDirectMultiple()
    131     {
    132         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    133
    134         assertEquals("property1", "value1", config.getProperty("key1"));
    135         assertEquals("property2", "value2", config.getProperty("key2"));
    136         assertEquals("unknown property", null, config.getProperty("key3"));
    137     }
    138
    139     public void testClearPropertySingle()
    140     {
    141         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    142         config.clearProperty("key");
    143
    144         assertFalse("property not cleared", config.containsKey("key"));
    145     }
    146
    147     public void testClearPropertyMultiple()
    148     {
    149         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    150         config.clearProperty("key");
    151
    152         assertFalse("property not cleared", config.containsKey("key"));
    153     }
    154
    155     public void testGetKeysSingle()
    156     {
    157         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    158         Iterator it = config.getKeys();
    159
    160         assertEquals("1st key", "key1", it.next());
    161         assertEquals("2nd key", "key2", it.next());
    162     }
    163
    164     public void testGetKeysMultiple()
    165     {
    166         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    167         Iterator it = config.getKeys();
    168
    169         assertEquals("1st key", "key1", it.next());
    170         assertEquals("2nd key", "key2", it.next());
    171     }
    172
    173     public void testContainsKeySingle()
    174     {
    175         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    176         assertTrue("missing key1", config.containsKey("key1"));
    177         assertTrue("missing key2", config.containsKey("key2"));
    178     }
    179
    180     public void testContainsKeyMultiple()
    181     {
    182         DatabaseConfiguration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    183         assertTrue("missing key1", config.containsKey("key1"));
    184         assertTrue("missing key2", config.containsKey("key2"));
    185     }
    186
    187     public void testIsEmptySingle()
    188     {
    189         DatabaseConfiguration config1 = new DatabaseConfiguration(datasource, "configuration", "key", "value");
    190         assertFalse("The configuration is empty", config1.isEmpty());
    191     }
    192
    193     public void testIsEmptyMultiple()
    194     {
    195         DatabaseConfiguration config1 = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
    196         assertFalse("The configuration named 'test' is empty", config1.isEmpty());
    197
    198         DatabaseConfiguration config2 = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "testIsEmpty");
    199         assertTrue("The configuration named 'testIsEmpty' is not empty", config2.isEmpty());
    200     }
    201
    202     public void testGetList()
    203     {
    204         DatabaseConfiguration config1 = new DatabaseConfiguration(datasource, "configurationList", "key", "value");
    205         List list = config1.getList("key3");
    206         assertEquals(3,list.size());
    207     }
    208
    209     public void testGetVector()
    210     {
    211         DatabaseConfiguration config1 = new DatabaseConfiguration(datasource, "configurationList", "key", "value");
    212         Vector vector = config1.getVector("key3");
    213         assertEquals(3,vector.size());
    214     }
    215
    216     public void testGetKeys()
    217     {
    218         DatabaseConfiguration config1 = new DatabaseConfiguration(datasource, "configurationList", "key", "value");
    219         Iterator i = config1.getKeys();
    220         assertTrue(i.hasNext());
    221         Object key = i.next();
    222         assertEquals("key3",key.toString());
    223         assertFalse(i.hasNext());
    224     }
    225
    226 }