KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestDatabaseConfiguration


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 JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.sql.DataSource JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.apache.commons.configuration.test.HsqlDB;
29 import org.apache.commons.dbcp.BasicDataSource;
30 import org.dbunit.database.DatabaseConnection;
31 import org.dbunit.database.IDatabaseConnection;
32 import org.dbunit.dataset.IDataSet;
33 import org.dbunit.dataset.xml.XmlDataSet;
34 import org.dbunit.operation.DatabaseOperation;
35
36 /**
37  * Test for database stored configurations. Note, when running this Unit
38  * Test in Eclipse it sometimes takes a couple tries. Otherwise you may get
39  * database is already in use by another process errors.
40  *
41  * @version $Revision: 156639 $, $Date: 2005-03-09 13:50:07 +0100 (Mi, 09 Mrz 2005) $
42  */

43 public class TestDatabaseConfiguration extends TestCase
44 {
45     public final String JavaDoc DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
46     public final String JavaDoc DATABASE_URL = "jdbc:hsqldb:target/test-classes/testdb";
47     public final String JavaDoc DATABASE_USERNAME = "sa";
48     public final String JavaDoc DATABASE_PASSWORD = "";
49
50     private static HsqlDB hsqlDB = null;
51
52     private DataSource JavaDoc datasource;
53
54     protected void setUp() throws Exception JavaDoc
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(DATABASE_USERNAME);
73         datasource.setPassword(DATABASE_PASSWORD);
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 JavaDoc("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 JavaDoc{
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         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
124
125         assertEquals("property1", "value1", config.getProperty("key1"));
126         assertEquals("property2", "value2", config.getProperty("key2"));
127         assertEquals("unknown property", null, config.getProperty("key3"));
128     }
129
130     public void testGetPropertyDirectMultiple()
131     {
132         Configuration 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         Configuration 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         Configuration 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 testClearSingle()
156     {
157         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
158         config.clear();
159
160         assertTrue("configuration is not cleared", config.isEmpty());
161     }
162
163     public void testClearMultiple()
164     {
165         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
166         config.clear();
167
168         assertTrue("configuration is not cleared", config.isEmpty());
169     }
170
171     public void testGetKeysSingle()
172     {
173         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
174         Iterator JavaDoc it = config.getKeys();
175
176         assertEquals("1st key", "key1", it.next());
177         assertEquals("2nd key", "key2", it.next());
178     }
179
180     public void testGetKeysMultiple()
181     {
182         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
183         Iterator JavaDoc it = config.getKeys();
184
185         assertEquals("1st key", "key1", it.next());
186         assertEquals("2nd key", "key2", it.next());
187     }
188
189     public void testContainsKeySingle()
190     {
191         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
192         assertTrue("missing key1", config.containsKey("key1"));
193         assertTrue("missing key2", config.containsKey("key2"));
194     }
195
196     public void testContainsKeyMultiple()
197     {
198         Configuration config = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
199         assertTrue("missing key1", config.containsKey("key1"));
200         assertTrue("missing key2", config.containsKey("key2"));
201     }
202
203     public void testIsEmptySingle()
204     {
205         Configuration config1 = new DatabaseConfiguration(datasource, "configuration", "key", "value");
206         assertFalse("The configuration is empty", config1.isEmpty());
207     }
208
209     public void testIsEmptyMultiple()
210     {
211         Configuration config1 = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "test");
212         assertFalse("The configuration named 'test' is empty", config1.isEmpty());
213
214         Configuration config2 = new DatabaseConfiguration(datasource, "configurations", "name", "key", "value", "testIsEmpty");
215         assertTrue("The configuration named 'testIsEmpty' is not empty", config2.isEmpty());
216     }
217     
218     public void testGetList()
219     {
220         Configuration config1 = new DatabaseConfiguration(datasource, "configurationList", "key", "value");
221         List JavaDoc list = config1.getList("key3");
222         assertEquals(3,list.size());
223     }
224     
225     public void testGetKeys()
226     {
227         Configuration config1 = new DatabaseConfiguration(datasource, "configurationList", "key", "value");
228         Iterator JavaDoc i = config1.getKeys();
229         assertTrue(i.hasNext());
230         Object JavaDoc key = i.next();
231         assertEquals("key3",key.toString());
232         assertFalse(i.hasNext());
233     }
234
235     public void testClearSubset()
236     {
237         Configuration config = new DatabaseConfiguration(datasource, "configuration", "key", "value");
238
239         Configuration subset = config.subset("key1");
240         subset.clear();
241
242         assertTrue("the subset is not empty", subset.isEmpty());
243         assertFalse("the parent configuration is empty", config.isEmpty());
244     }
245
246 }
247
Popular Tags