Author: ebourg Date: Mon Apr 16 04:54:11 2007 New Revision: 529210 URL: http://svn.apache.org/viewvc?view=rev&rev=529210 Log: Improved the javadoc for DatabaseConfiguration Made getConnection() package private since it's only used for testing
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java?view=diff&rev=529210&r1=529209&r2=529210 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java Mon Apr 16 04:54:11 2007 @@ -33,11 +33,51 @@ import org.apache.commons.logging.LogFactory; /** - * Configuration stored in a database. + * Configuration stored in a database. The properties are retrieved from a + * table containing at least one column for the keys, and one column for the + * values. It's possible to store several configurations in the same table by + * adding a column containing the name of the configuration. The name of the + * table and the columns is specified in the constructor. + * + * <h4>Example 1 - One configuration per table</h4> + * + * <pre> + * CREATE TABLE myconfig ( + * `key` VARCHAR NOT NULL PRIMARY KEY, + * `value` VARCHAR + * ); + * + * INSERT INTO myconfig (key, value) VALUES ('foo', 'bar'); + * + * + * Configuration config = new DatabaseConfiguration(datasource, "myconfig", "key", "value"); + * String value = config.getString("foo"); + * </pre> + * + * <h4>Example 2 - Multiple configurations per table</h4> + * + * <pre> + * CREATE TABLE myconfigs ( + * `name` VARCHAR NOT NULL, + * `key` VARCHAR NOT NULL, + * `value` VARCHAR, + * CONSTRAINT sys_pk_myconfigs PRIMARY KEY (`name`, `key`) + * ); + * + * INSERT INTO myconfigs (name, key, value) VALUES ('config1', 'key1', 'value1'); + * INSERT INTO myconfigs (name, key, value) VALUES ('config2', 'key2', 'value2'); + * + * + * Configuration config1 = new DatabaseConfiguration(datasource, "myconfigs", "name", "key", "value", "config1"); + * String value1 = conf.getString("key1"); + * + * Configuration config2 = new DatabaseConfiguration(datasource, "myconfigs", "name", "key", "value", "config2"); + * String value2 = conf.getString("key2"); + * </pre> * * @since 1.0 * - * @author Emmanuel Bourg + * @author <a href="mailto:[EMAIL PROTECTED]">Emmanuel Bourg</a> * @version $Revision$, $Date$ */ public class DatabaseConfiguration extends AbstractConfiguration @@ -521,7 +561,7 @@ * @throws SQLException if an error occurs * @since 1.4 */ - protected Connection getConnection() throws SQLException + Connection getConnection() throws SQLException { return getDatasource().getConnection(); } Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java?view=diff&rev=529210&r1=529209&r2=529210 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java Mon Apr 16 04:54:11 2007 @@ -133,8 +133,7 @@ */ private PotentialErrorDatabaseConfiguration setUpConfig() { - return new PotentialErrorDatabaseConfiguration(datasource, TABLE, - COL_KEY, COL_VALUE); + return new PotentialErrorDatabaseConfiguration(datasource, TABLE, COL_KEY, COL_VALUE); } /** @@ -145,8 +144,7 @@ */ private DatabaseConfiguration setUpMultiConfig() { - return new DatabaseConfiguration(datasource, TABLE_MULTI, COL_NAME, - COL_KEY, COL_VALUE, CONFIG_NAME); + return new DatabaseConfiguration(datasource, TABLE_MULTI, COL_NAME, COL_KEY, COL_VALUE, CONFIG_NAME); } /** @@ -157,8 +155,7 @@ private void setUpErrorListener(PotentialErrorDatabaseConfiguration config) { // remove log listener to avoid exception longs - config.removeErrorListener((ConfigurationErrorListener) config - .getErrorListeners().iterator().next()); + config.removeErrorListener((ConfigurationErrorListener) config.getErrorListeners().iterator().next()); listener = new TestErrorListener(); config.addErrorListener(listener); config.failOnConnect = true; @@ -189,10 +186,8 @@ { assertEquals("Wrong number of errors", 1, listener.errorCount); assertEquals("Wrong event type", type, listener.event.getType()); - assertTrue("Wrong event source", - listener.event.getSource() instanceof DatabaseConfiguration); - assertTrue("Wrong exception", - listener.event.getCause() instanceof SQLException); + assertTrue("Wrong event source", listener.event.getSource() instanceof DatabaseConfiguration); + assertTrue("Wrong exception", listener.event.getCause() instanceof SQLException); assertTrue("Wrong property key", (key == null) ? listener.event .getPropertyName() == null : key.equals(listener.event .getPropertyName())); @@ -357,10 +352,8 @@ */ public void testLogErrorListener() { - DatabaseConfiguration config = new DatabaseConfiguration(datasource, - TABLE, COL_KEY, COL_VALUE); - assertEquals("No error listener registered", 1, config - .getErrorListeners().size()); + DatabaseConfiguration config = new DatabaseConfiguration(datasource, TABLE, COL_KEY, COL_VALUE); + assertEquals("No error listener registered", 1, config.getErrorListeners().size()); } /** @@ -369,8 +362,7 @@ public void testGetPropertyError() { setUpErrorConfig().getProperty("key1"); - checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key1", - null); + checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key1", null); } /** @@ -379,8 +371,7 @@ public void testAddPropertyError() { setUpErrorConfig().addProperty("key1", "value"); - checkErrorListener(AbstractConfiguration.EVENT_ADD_PROPERTY, "key1", - "value"); + checkErrorListener(AbstractConfiguration.EVENT_ADD_PROPERTY, "key1", "value"); } /** @@ -388,10 +379,8 @@ */ public void testIsEmptyError() { - assertTrue("Wrong return value for failure", setUpErrorConfig() - .isEmpty()); - checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null, - null); + assertTrue("Wrong return value for failure", setUpErrorConfig().isEmpty()); + checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null, null); } /** @@ -399,10 +388,8 @@ */ public void testContainsKeyError() { - assertFalse("Wrong return value for failure", setUpErrorConfig() - .containsKey("key1")); - checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key1", - null); + assertFalse("Wrong return value for failure", setUpErrorConfig().containsKey("key1")); + checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key1", null); } /** @@ -411,8 +398,7 @@ public void testClearPropertyError() { setUpErrorConfig().clearProperty("key1"); - checkErrorListener(AbstractConfiguration.EVENT_CLEAR_PROPERTY, "key1", - null); + checkErrorListener(AbstractConfiguration.EVENT_CLEAR_PROPERTY, "key1", null); } /** @@ -430,8 +416,7 @@ public void testGetKeysError() { Iterator it = setUpErrorConfig().getKeys(); - checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null, - null); + checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null, null); assertFalse("Iteration is not empty", it.hasNext()); } @@ -458,8 +443,7 @@ DatabaseConfiguration config = setUpConfig(); config.setListDelimiter(';'); config.setDelimiterParsingDisabled(true); - assertEquals("Wrong value of property", "a;b;c", config - .getString("keyMulti")); + assertEquals("Wrong value of property", "a;b;c", config.getString("keyMulti")); } /** @@ -481,8 +465,7 @@ * configured to throw an exception when obtaining a connection. This way * database exceptions can be simulated. */ - static class PotentialErrorDatabaseConfiguration extends - DatabaseConfiguration + static class PotentialErrorDatabaseConfiguration extends DatabaseConfiguration { /** A flag whether a getConnection() call should fail. */ boolean failOnConnect; --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]