commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r782124 - in /commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/event/ xdocs/
Date Fri, 05 Jun 2009 20:27:16 GMT
Author: oheger
Date: Fri Jun  5 20:27:16 2009
New Revision: 782124

URL: http://svn.apache.org/viewvc?rev=782124&view=rev
Log:
CONFIGURATION-385: DatabaseConfiguration now generates correct events for the clear() and
clearProperty() methods.
The handling of the in-process database used for the tests was refactored into a helper class.
A new unit test for the events generated by DatabaseConfiguration was added.

Added:
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java
  (with props)
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java
  (with props)
Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
    commons/proper/configuration/trunk/xdocs/changes.xml

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java?rev=782124&r1=782123&r2=782124&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DatabaseConfiguration.java
Fri Jun  5 20:27:16 2009
@@ -406,7 +406,7 @@
      *
      * @param key the key of the property to be removed
      */
-    public void clearProperty(String key)
+    protected void clearPropertyDirect(String key)
     {
         // build the query
         StringBuffer query = new StringBuffer("DELETE FROM " + table + " WHERE " + keyColumn
+ "=?");
@@ -452,6 +452,7 @@
      */
     public void clear()
     {
+        fireEvent(EVENT_CLEAR, null, null, true);
         // build the query
         StringBuffer query = new StringBuffer("DELETE FROM " + table);
         if (nameColumn != null)
@@ -484,6 +485,7 @@
             // clean up
             close(conn, pstmt);
         }
+        fireEvent(EVENT_CLEAR, null, null, false);
     }
 
     /**

Added: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java?rev=782124&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java
(added)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java
Fri Jun  5 20:27:16 2009
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration;
+
+import java.io.FileInputStream;
+
+import javax.sql.DataSource;
+
+import org.apache.commons.configuration.test.HsqlDB;
+import org.apache.commons.dbcp.BasicDataSource;
+import org.dbunit.database.DatabaseConnection;
+import org.dbunit.database.IDatabaseConnection;
+import org.dbunit.dataset.IDataSet;
+import org.dbunit.dataset.xml.XmlDataSet;
+import org.dbunit.operation.DatabaseOperation;
+
+/**
+ * A helper class for performing tests for {@link DatabaseConfiguration}. This
+ * class maintains an in-process database that stores configuration data and can
+ * be accessed from a {@link DatabaseConfiguration} instance. Constants for
+ * table and column names and database connection settings are provided, too.
+ *
+ * @version $Id$
+ */
+public class DatabaseConfigurationTestHelper
+{
+    /** Constant for the JDBC driver class. */
+    public final String DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
+
+    /** Constant for the connection URL. */
+    public final String DATABASE_URL = "jdbc:hsqldb:mem:testdb";
+
+    /** Constant for the DB user name. */
+    public final String DATABASE_USERNAME = "sa";
+
+    /** Constant for the DB password. */
+    public final String DATABASE_PASSWORD = "";
+
+    /** Constant for the configuration table. */
+    public static final String TABLE = "configuration";
+
+    /** Constant for the multi configuration table. */
+    public static final String TABLE_MULTI = "configurations";
+
+    /** Constant for the column with the keys. */
+    public static final String COL_KEY = "key";
+
+    /** Constant for the column with the values. */
+    public static final String COL_VALUE = "value";
+
+    /** Constant for the column with the configuration name. */
+    public static final String COL_NAME = "name";
+
+    /** Constant for the name of the test configuration. */
+    public static final String CONFIG_NAME = "test";
+
+    /** Stores the in-process database. */
+    private static HsqlDB hsqlDB = null;
+
+    /** The data source. */
+    private DataSource datasource;
+
+    /**
+     * Initializes this helper object. This method can be called from a
+     * <code>setUp()</code> method of a unit test class. It creates the database
+     * instance if necessary and populates it with test data.
+     *
+     * @throws Exception if an error occurs
+     */
+    public void setUp() throws Exception
+    {
+        if (hsqlDB == null)
+        {
+            hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER,
+                    "conf/testdb.script");
+        }
+
+        BasicDataSource datasource = new BasicDataSource();
+        datasource.setDriverClassName(DATABASE_DRIVER);
+        datasource.setUrl(DATABASE_URL);
+        datasource.setUsername(DATABASE_USERNAME);
+        datasource.setPassword(DATABASE_PASSWORD);
+
+        this.datasource = datasource;
+
+        // prepare the database
+        IDatabaseConnection connection = new DatabaseConnection(datasource
+                .getConnection());
+        IDataSet dataSet = new XmlDataSet(new FileInputStream(
+                "conf/dataset.xml"));
+
+        try
+        {
+            DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
+        }
+        finally
+        {
+            connection.close();
+        }
+    }
+
+    /**
+     * Frees the resources used by this helper class. This method can be called
+     * by a <code>tearDown()</code> method of a unit test class.
+     *
+     * @throws Exception if an error occurs
+     */
+    public void tearDown() throws Exception
+    {
+        datasource.getConnection().commit();
+        datasource.getConnection().close();
+    }
+
+    /**
+     * Creates a database configuration with default values.
+     *
+     * @return the configuration
+     */
+    public DatabaseConfiguration setUpConfig()
+    {
+        return new DatabaseConfiguration(datasource, TABLE, COL_KEY, COL_VALUE);
+    }
+
+    /**
+     * Creates a database configuration that supports multiple configurations in
+     * a table with default values.
+     *
+     * @return the configuration
+     */
+    public DatabaseConfiguration setUpMultiConfig()
+    {
+        return new DatabaseConfiguration(datasource, TABLE_MULTI, COL_NAME,
+                COL_KEY, COL_VALUE, CONFIG_NAME);
+    }
+
+    /**
+     * Returns the <code>DataSource</code> managed by this class.
+     *
+     * @return the <code>DataSource</code>
+     */
+    public DataSource getDatasource()
+    {
+        return datasource;
+    }
+}

Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/DatabaseConfigurationTestHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java?rev=782124&r1=782123&r2=782124&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
Fri Jun  5 20:27:16 2009
@@ -17,7 +17,6 @@
 
 package org.apache.commons.configuration;
 
-import java.io.FileInputStream;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Iterator;
@@ -28,13 +27,6 @@
 import junit.framework.TestCase;
 
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
-import org.apache.commons.configuration.test.HsqlDB;
-import org.apache.commons.dbcp.BasicDataSource;
-import org.dbunit.database.DatabaseConnection;
-import org.dbunit.database.IDatabaseConnection;
-import org.dbunit.dataset.IDataSet;
-import org.dbunit.dataset.xml.XmlDataSet;
-import org.dbunit.operation.DatabaseOperation;
 
 /**
  * Test for database stored configurations.  Note, when running this Unit
@@ -45,36 +37,12 @@
  */
 public class TestDatabaseConfiguration extends TestCase
 {
-    public final String DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
-    public final String DATABASE_URL = "jdbc:hsqldb:target/test-classes/testdb";
-    public final String DATABASE_USERNAME = "sa";
-    public final String DATABASE_PASSWORD = "";
-
-    /** Constant for the configuration table.*/
-    private static final String TABLE = "configuration";
-
-    /** Constant for the multi configuration table.*/
-    private static final String TABLE_MULTI = "configurations";
-
-    /** Constant for the column with the keys.*/
-    private static final String COL_KEY = "key";
-
-    /** Constant for the column with the values.*/
-    private static final String COL_VALUE = "value";
-
-    /** Constant for the column with the configuration name.*/
-    private static final String COL_NAME = "name";
-
-    /** Constant for the name of the test configuration.*/
-    private static final String CONFIG_NAME = "test";
-
-    private static HsqlDB hsqlDB = null;
-
-    private DataSource datasource;
-
     /** An error listener for testing whether internal errors occurred.*/
     private ConfigurationErrorListenerImpl listener;
 
+    /** The test helper. */
+    private DatabaseConfigurationTestHelper helper;
+
     protected void setUp() throws Exception
     {
         /*
@@ -85,43 +53,19 @@
 
         // set up the datasource
 
-        if (hsqlDB == null)
-        {
-            hsqlDB = new HsqlDB(DATABASE_URL, DATABASE_DRIVER, "conf/testdb.script");
-        }
-
-        BasicDataSource datasource = new BasicDataSource();
-        datasource.setDriverClassName(DATABASE_DRIVER);
-        datasource.setUrl(DATABASE_URL);
-        datasource.setUsername(DATABASE_USERNAME);
-        datasource.setPassword(DATABASE_PASSWORD);
-
-        this.datasource = datasource;
-
-
-        // prepare the database
-        IDatabaseConnection connection = new DatabaseConnection(datasource.getConnection());
-        IDataSet dataSet = new XmlDataSet(new FileInputStream("conf/dataset.xml"));
-
-        try
-        {
-            DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
-        }
-        finally
-        {
-            connection.close();
-        }
+        helper = new DatabaseConfigurationTestHelper();
+        helper.setUp();
     }
 
-    protected void tearDown() throws Exception{
-        datasource.getConnection().commit();
-        datasource.getConnection().close();
-
+    protected void tearDown() throws Exception
+    {
         // if an error listener is defined, we check whether an error occurred
         if(listener != null)
         {
             assertEquals("An internal error occurred", 0, listener.getErrorCount());
         }
+        helper.tearDown();
+
         super.tearDown();
     }
 
@@ -132,7 +76,10 @@
      */
     private PotentialErrorDatabaseConfiguration setUpConfig()
     {
-        return new PotentialErrorDatabaseConfiguration(datasource, TABLE, COL_KEY, COL_VALUE);
+        return new PotentialErrorDatabaseConfiguration(helper.getDatasource(),
+                DatabaseConfigurationTestHelper.TABLE,
+                DatabaseConfigurationTestHelper.COL_KEY,
+                DatabaseConfigurationTestHelper.COL_VALUE);
     }
 
     /**
@@ -143,7 +90,7 @@
      */
     private DatabaseConfiguration setUpMultiConfig()
     {
-        return new DatabaseConfiguration(datasource, TABLE_MULTI, COL_NAME, COL_KEY, COL_VALUE,
CONFIG_NAME);
+        return helper.setUpMultiConfig();
     }
 
     /**
@@ -309,20 +256,20 @@
         Configuration config1 = setUpMultiConfig();
         assertFalse("The configuration named 'test' is empty", config1.isEmpty());
 
-        Configuration config2 = new DatabaseConfiguration(datasource, TABLE_MULTI, COL_NAME,
COL_KEY, COL_VALUE, "testIsEmpty");
+        Configuration config2 = new DatabaseConfiguration(helper.getDatasource(), DatabaseConfigurationTestHelper.TABLE_MULTI,
DatabaseConfigurationTestHelper.COL_NAME, DatabaseConfigurationTestHelper.COL_KEY, DatabaseConfigurationTestHelper.COL_VALUE,
"testIsEmpty");
         assertTrue("The configuration named 'testIsEmpty' is not empty", config2.isEmpty());
     }
 
     public void testGetList()
     {
-        Configuration config1 = new DatabaseConfiguration(datasource, "configurationList",
COL_KEY, COL_VALUE);
+        Configuration config1 = new DatabaseConfiguration(helper.getDatasource(), "configurationList",
DatabaseConfigurationTestHelper.COL_KEY, DatabaseConfigurationTestHelper.COL_VALUE);
         List list = config1.getList("key3");
         assertEquals(3,list.size());
     }
 
     public void testGetKeys()
     {
-        Configuration config1 = new DatabaseConfiguration(datasource, "configurationList",
COL_KEY, COL_VALUE);
+        Configuration config1 = new DatabaseConfiguration(helper.getDatasource(), "configurationList",
DatabaseConfigurationTestHelper.COL_KEY, DatabaseConfigurationTestHelper.COL_VALUE);
         Iterator i = config1.getKeys();
         assertTrue(i.hasNext());
         Object key = i.next();
@@ -347,7 +294,7 @@
      */
     public void testLogErrorListener()
     {
-        DatabaseConfiguration config = new DatabaseConfiguration(datasource, TABLE, COL_KEY,
COL_VALUE);
+        DatabaseConfiguration config = new DatabaseConfiguration(helper.getDatasource(),
DatabaseConfigurationTestHelper.TABLE, DatabaseConfigurationTestHelper.COL_KEY, DatabaseConfigurationTestHelper.COL_VALUE);
         assertEquals("No error listener registered", 1, config.getErrorListeners().size());
     }
 

Added: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java?rev=782124&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java
(added)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java
Fri Jun  5 20:27:16 2009
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.event;
+
+import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.DatabaseConfigurationTestHelper;
+
+/**
+ * A test class for the events generated by DatabaseConfiguration.
+ *
+ * @author hacker
+ * @version $Id$
+ */
+public class TestDatabaseConfigurationEvents extends
+        AbstractTestConfigurationEvents
+{
+    /** The test helper. */
+    private DatabaseConfigurationTestHelper helper;
+
+    protected void setUp() throws Exception
+    {
+        helper = new DatabaseConfigurationTestHelper();
+        helper.setUp();
+
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception
+    {
+        helper.tearDown();
+        super.tearDown();
+    }
+
+    protected AbstractConfiguration createConfiguration()
+    {
+        return helper.setUpConfig();
+    }
+}

Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestDatabaseConfigurationEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=782124&r1=782123&r2=782124&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Fri Jun  5 20:27:16 2009
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.7" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-385">
+        DatabaseConfiguration now generates correct events for the clear() and
+        clearProperty() methods.
+      </action>
       <action dev="rgoers" type="add" issue="CONFIGURATION-380">
         Add ExprLookup to allow expressions to be evaluated in configurations. When 
         used, this requires that Apache Commons Jexl be added as a dependency to



Mime
View raw message