1 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 24 import javax.sql.DataSource ; 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 43 public class TestDatabaseConfiguration extends TestCase 44 { 45 public final String DATABASE_DRIVER = "org.hsqldb.jdbcDriver"; 46 public final String DATABASE_URL = "jdbc:hsqldb:target/test-classes/testdb"; 47 public final String DATABASE_USERNAME = "sa"; 48 public final String DATABASE_PASSWORD = ""; 49 50 private static HsqlDB hsqlDB = null; 51 52 private DataSource datasource; 53 54 protected void setUp() throws Exception 55 { 56 60 62 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 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 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 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 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 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 i = config1.getKeys(); 229 assertTrue(i.hasNext()); 230 Object 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 |