You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/04/09 22:00:42 UTC
svn commit: r1466200 - in /commons/proper/configuration/trunk/src:
changes/changes.xml
main/java/org/apache/commons/configuration/DatabaseConfiguration.java
test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java
Author: oheger
Date: Tue Apr 9 20:00:42 2013
New Revision: 1466200
URL: http://svn.apache.org/r1466200
Log:
[CONFIGURATION-533] DatabaseConfiguration now can deal with CLOB values.
If a property value read from the database is of type java.sql.Clob,
it is automatically converted to a String value.
Modified:
commons/proper/configuration/trunk/src/changes/changes.xml
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java
Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1466200&r1=1466199&r2=1466200&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Tue Apr 9 20:00:42 2013
@@ -51,6 +51,10 @@
The includesAllowed property of PropertyConfiguration is now independent
from the existence of a base path.
</action>
+ <action dev="oheger" type="add" issue="CONFIGURATION-533">
+ DatabaseConfiguration now automatically converts CLOBs to strings if
+ they appear in property values.
+ </action>
<action dev="oheger" type="update" issue="CONFIGURATION-527" due-to="Matthias Richter">
AbstractConfiguration.clearPropertyDirect() is now abstract.
</action>
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java?rev=1466200&r1=1466199&r2=1466200&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java Tue Apr 9 20:00:42 2013
@@ -17,6 +17,7 @@
package org.apache.commons.configuration;
+import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -29,6 +30,7 @@ import java.util.List;
import javax.sql.DataSource;
+import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.LogFactory;
/**
@@ -321,7 +323,7 @@ public class DatabaseConfiguration exten
List<Object> results = new ArrayList<Object>();
while (rs.next())
{
- Object value = rs.getObject(valueColumn);
+ Object value = extractPropertyValue(rs);
if (isDelimiterParsingDisabled())
{
results.add(value);
@@ -624,6 +626,41 @@ public class DatabaseConfiguration exten
}
/**
+ * Extracts the value of a property from the given result set. The passed in
+ * {@code ResultSet} was created by a SELECT statement on the underlying
+ * database table. This implementation reads the value of the column
+ * determined by the {@code valueColumn} property. Normally the contained
+ * value is directly returned. However, if it is of type {@code CLOB}, text
+ * is extracted as string.
+ *
+ * @param rs the current {@code ResultSet}
+ * @return the value of the property column
+ * @throws SQLException if an error occurs
+ */
+ protected Object extractPropertyValue(ResultSet rs) throws SQLException
+ {
+ Object value = rs.getObject(valueColumn);
+ if (value instanceof Clob)
+ {
+ value = convertClob((Clob) value);
+ }
+ return value;
+ }
+
+ /**
+ * Converts a CLOB to a string.
+ *
+ * @param clob the CLOB to be converted
+ * @return the extracted string value
+ * @throws SQLException if an error occurs
+ */
+ private static Object convertClob(Clob clob) throws SQLException
+ {
+ int len = (int) clob.length();
+ return (len > 0) ? clob.getSubString(1, len) : StringUtils.EMPTY;
+ }
+
+ /**
* An internally used helper class for simplifying database access through
* plain JDBC. This class provides a simple framework for creating and
* executing a JDBC statement. It especially takes care of proper handling
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java?rev=1466200&r1=1466199&r2=1466200&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java Tue Apr 9 20:00:42 2013
@@ -21,6 +21,8 @@ import static org.junit.Assert.assertEqu
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.sql.Clob;
+import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
@@ -503,6 +505,49 @@ public class TestDatabaseConfiguration
}
/**
+ * Tests whether a CLOB as a property value is handled correctly.
+ */
+ @Test
+ public void testExtractPropertyValueCLOB() throws ConfigurationException,
+ SQLException
+ {
+ ResultSet rs = EasyMock.createMock(ResultSet.class);
+ Clob clob = EasyMock.createMock(Clob.class);
+ final String content = "This is the content of the test CLOB!";
+ EasyMock.expect(rs.getObject(DatabaseConfigurationTestHelper.COL_VALUE))
+ .andReturn(clob);
+ EasyMock.expect(clob.length())
+ .andReturn(Long.valueOf(content.length()));
+ EasyMock.expect(clob.getSubString(1, content.length())).andReturn(
+ content);
+ EasyMock.replay(rs, clob);
+ DatabaseConfiguration config = helper.setUpConfig();
+ assertEquals("Wrong extracted value", content,
+ config.extractPropertyValue(rs));
+ EasyMock.verify(rs, clob);
+ }
+
+ /**
+ * Tests whether an empty CLOB is correctly handled by
+ * extractPropertyValue().
+ */
+ @Test
+ public void testExtractPropertyValueCLOBEmpty()
+ throws ConfigurationException, SQLException
+ {
+ ResultSet rs = EasyMock.createMock(ResultSet.class);
+ Clob clob = EasyMock.createMock(Clob.class);
+ EasyMock.expect(rs.getObject(DatabaseConfigurationTestHelper.COL_VALUE))
+ .andReturn(clob);
+ EasyMock.expect(clob.length()).andReturn(0L);
+ EasyMock.replay(rs, clob);
+ DatabaseConfiguration config = helper.setUpConfig();
+ assertEquals("Wrong extracted value", "",
+ config.extractPropertyValue(rs));
+ EasyMock.verify(rs, clob);
+ }
+
+ /**
* A specialized database configuration implementation that can be
* configured to throw an exception when obtaining a connection. This way
* database exceptions can be simulated.