commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r514234 - in /jakarta/commons/proper/configuration/trunk: ./ conf/ src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ xdocs/
Date Sat, 03 Mar 2007 20:18:15 GMT
Author: oheger
Date: Sat Mar  3 12:18:14 2007
New Revision: 514234

URL: http://svn.apache.org/viewvc?view=rev&rev=514234
Log:
CONFIGURATION-255: Updated handling of list delimiter parsing in DatabaseConfiguration

Modified:
    jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/configuration/trunk/conf/dataset.xml
    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
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt?view=diff&rev=514234&r1=514233&r2=514234
==============================================================================
--- jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/configuration/trunk/RELEASE-NOTES.txt Sat Mar  3 12:18:14 2007
@@ -90,6 +90,9 @@
   configuration. So when the clone was modified and then saved the content of
   the original configuration was written. This has now been fixed.
 
+* [CONFIGURATION-255]
+  DatabaseConfiguration now handles list delimiters in property values
+  correctly.
 
 IMPROVEMENTS IN 1.4
 ===================

Modified: jakarta/commons/proper/configuration/trunk/conf/dataset.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/conf/dataset.xml?view=diff&rev=514234&r1=514233&r2=514234
==============================================================================
--- jakarta/commons/proper/configuration/trunk/conf/dataset.xml (original)
+++ jakarta/commons/proper/configuration/trunk/conf/dataset.xml Sat Mar  3 12:18:14 2007
@@ -14,7 +14,11 @@
         <row>
             <value>key2</value>
             <value>value2</value>
-        </row>		
+        </row>
+        <row>
+            <value>keyMulti</value>
+            <value>a;b;c</value>
+        </row>
     </table>
 
     <table name="configurations">

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=514234&r1=514233&r2=514234
==============================================================================
--- 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
Sat Mar  3 12:18:14 2007
@@ -29,6 +29,7 @@
 
 import javax.sql.DataSource;
 
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.logging.LogFactory;
 
 /**
@@ -110,7 +111,9 @@
         Object result = null;
 
         // build the query
-        StringBuffer query = new StringBuffer("SELECT * FROM " + table + " WHERE " + keyColumn
+ "=?");
+        StringBuffer query = new StringBuffer("SELECT * FROM ");
+        query.append(table).append(" WHERE ");
+        query.append(keyColumn).append("=?");
         if (nameColumn != null)
         {
             query.append(" AND " + nameColumn + "=?");
@@ -133,22 +136,25 @@
 
             ResultSet rs = pstmt.executeQuery();
 
-            if (rs.next())
+            List results = new ArrayList();
+            while (rs.next())
             {
-                result = rs.getObject(valueColumn);
+                Object val = rs.getObject(valueColumn);
+                if (isDelimiterParsingDisabled())
+                {
+                    results.add(val);
+                }
+                else
+                {
+                    // Split value if it containts the list delimiter
+                    CollectionUtils.addAll(results, PropertyConverter
+                            .toIterator(val, getListDelimiter()));
+                }
             }
 
-            // build a list if there is more than one row in the resultset
-            if (rs.next())
+            if (!results.isEmpty())
             {
-                List results = new ArrayList();
-                results.add(result);
-                results.add(rs.getObject(valueColumn));
-                while (rs.next())
-                {
-                    results.add(rs.getObject(valueColumn));
-                }
-                result = results;
+                result = (results.size() > 1) ? results : results.get(0);
             }
         }
         catch (SQLException e)
@@ -213,6 +219,36 @@
         {
             // clean up
             closeQuietly(conn, pstmt);
+        }
+    }
+
+    /**
+     * Adds a property to this configuration. This implementation will
+     * temporarily disable list delimiter parsing, so that even if the value
+     * contains the list delimiter, only a single record will be written into
+     * the managed table. The implementation of <code>getProperty()</code>
+     * will take care about delimiters. So list delimiters are fully supported
+     * by <code>DatabaseConfiguration</code>, but internally treated a bit
+     * differently.
+     *
+     * @param key the key of the new property
+     * @param value the value to be added
+     */
+    public void addProperty(String key, Object value)
+    {
+        boolean parsingFlag = isDelimiterParsingDisabled();
+        try
+        {
+            if (value instanceof String)
+            {
+                // temporarily disable delimiter parsing
+                setDelimiterParsingDisabled(true);
+            }
+            super.addProperty(key, value);
+        }
+        finally
+        {
+            setDelimiterParsingDisabled(parsingFlag);
         }
     }
 

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=514234&r1=514233&r2=514234
==============================================================================
--- 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
Sat Mar  3 12:18:14 2007
@@ -436,6 +436,47 @@
     }
 
     /**
+     * Tests obtaining a property as list whose value contains the list
+     * delimiter. Multiple values should be returned.
+     */
+    public void testGetListWithDelimiter()
+    {
+        DatabaseConfiguration config = setUpConfig();
+        config.setListDelimiter(';');
+        List values = config.getList("keyMulti");
+        assertEquals("Wrong number of list elements", 3, values.size());
+        assertEquals("Wrong list element 0", "a", values.get(0));
+        assertEquals("Wrong list element 2", "c", values.get(2));
+    }
+
+    /**
+     * Tests obtaining a property whose value contains the list delimiter when
+     * delimiter parsing is disabled.
+     */
+    public void testGetListWithDelimiterParsingDisabled()
+    {
+        DatabaseConfiguration config = setUpConfig();
+        config.setListDelimiter(';');
+        config.setDelimiterParsingDisabled(true);
+        assertEquals("Wrong value of property", "a;b;c", config
+                .getString("keyMulti"));
+    }
+
+    /**
+     * Tests adding a property containing the list delimiter. When this property
+     * is queried multiple values should be returned.
+     */
+    public void testAddWithDelimiter()
+    {
+        DatabaseConfiguration config = setUpConfig();
+        config.setListDelimiter(';');
+        config.addProperty("keyList", "1;2;3");
+        String[] values = config.getStringArray("keyList");
+        assertEquals("Wrong number of property values", 3, values.length);
+        assertEquals("Wrong value at index 1", "2", values[1]);
+    }
+
+    /**
      * A specialized database configuration implementation that can be
      * configured to throw an exception when obtaining a connection. This way
      * database exceptions can be simulated.

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=514234&r1=514233&r2=514234
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Mar  3 12:18:14 2007
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.4-SNAPSHOT" date="in SVN">
+      <action dev="oheger" type="update" issue="CONFIGURATION-255">
+        DatabaseConfiguration now handles list delimiters in property values
+        correctly.
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-254" due-to="Carsten Kaiser">
         After cloning a XMLConfiguration there was still a connection to the
         original configuration. So when the clone was modified and then saved



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message