You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by th...@apache.org on 2015/04/14 22:23:53 UTC

svn commit: r1673551 - in /commons/proper/dbutils/trunk/src: main/java/org/apache/commons/dbutils/BeanProcessor.java test/java/org/apache/commons/dbutils/BeanProcessorTest.java

Author: thecarlhall
Date: Tue Apr 14 20:23:53 2015
New Revision: 1673551

URL: http://svn.apache.org/r1673551
Log:
DBUTILS-89 Apply patch and adjust tests for populateBean method

Modified:
    commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BeanProcessor.java
    commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java

Modified: commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BeanProcessor.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BeanProcessor.java?rev=1673551&r1=1673550&r2=1673551&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BeanProcessor.java (original)
+++ commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BeanProcessor.java Tue Apr 14 20:23:53 2015
@@ -136,13 +136,8 @@ public class BeanProcessor {
      * @return the newly created bean
      */
     public <T> T toBean(ResultSet rs, Class<? extends T> type) throws SQLException {
-
-        PropertyDescriptor[] props = this.propertyDescriptors(type);
-
-        ResultSetMetaData rsmd = rs.getMetaData();
-        int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
-
-        return this.createBean(rs, type, props, columnToProperty);
+        T bean = this.newInstance(type);
+        return this.populateBean(rs, bean);
     }
 
     /**
@@ -207,10 +202,43 @@ public class BeanProcessor {
      * @throws SQLException if a database error occurs.
      */
     private <T> T createBean(ResultSet rs, Class<T> type,
-            PropertyDescriptor[] props, int[] columnToProperty)
-            throws SQLException {
+                             PropertyDescriptor[] props, int[] columnToProperty)
+    throws SQLException {
 
         T bean = this.newInstance(type);
+        return populateBean(rs, bean, props, columnToProperty);
+    }
+
+    /**
+     * Initializes the fields of the provided bean from the ResultSet.
+     * @param <T> The type of bean
+     * @param rs The result set.
+     * @param bean The bean to be populated.
+     * @return An initialized object.
+     * @throws SQLException if a database error occurs.
+     */
+    public <T> T populateBean(ResultSet rs, T bean) throws SQLException {
+        PropertyDescriptor[] props = this.propertyDescriptors(bean.getClass());
+        ResultSetMetaData rsmd = rs.getMetaData();
+        int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
+
+        return populateBean(rs, bean, props, columnToProperty);
+    }
+
+    /**
+     * This method populates a bean from the ResultSet based upon the underlying meta-data.
+     *
+     * @param <T> The type of bean
+     * @param rs The result set.
+     * @param bean The bean to be populated.
+     * @param props The property descriptors.
+     * @param columnToProperty The column indices in the result set.
+     * @return An initialized object.
+     * @throws SQLException if a database error occurs.
+     */
+    private <T> T populateBean(ResultSet rs, T bean,
+            PropertyDescriptor[] props, int[] columnToProperty)
+            throws SQLException {
 
         for (int i = 1; i < columnToProperty.length; i++) {
 

Modified: commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java?rev=1673551&r1=1673550&r2=1673551&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java (original)
+++ commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java Tue Apr 14 20:23:53 2015
@@ -27,7 +27,7 @@ public class BeanProcessorTest extends B
 
     private static final BeanProcessor beanProc = new BeanProcessor();
 
-    public void testProcess() throws SQLException {
+    public void testProcessWithToBean() throws SQLException {
         TestBean b = null;
         assertTrue(this.rs.next());
         b = beanProc.toBean(this.rs, TestBean.class);
@@ -39,6 +39,22 @@ public class BeanProcessorTest extends B
         assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0);
         assertEquals(b.getThree(), TestBean.Ordinal.SIX);
 
+        assertFalse(this.rs.next());
+    }
+
+    public void testProcessWithPopulateBean() throws SQLException {
+        TestBean b = new TestBean();
+
+        assertTrue(this.rs.next());
+        b = beanProc.populateBean(this.rs, b);
+        assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0);
+        assertEquals(b.getThree(), TestBean.Ordinal.THREE);
+
+        assertTrue(this.rs.next());
+        b = beanProc.populateBean(this.rs, b);
+        assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0);
+        assertEquals(b.getThree(), TestBean.Ordinal.SIX);
+
         assertFalse(this.rs.next());
     }