DBUTILS-89 Apply patch and adjust tests for populateBean method git-svn-id: https://svn.apache.org/repos/asf/commons/proper/dbutils/trunk@1673551 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/commons-dbutils/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-dbutils/commit/504838bd Tree: http://git-wip-us.apache.org/repos/asf/commons-dbutils/tree/504838bd Diff: http://git-wip-us.apache.org/repos/asf/commons-dbutils/diff/504838bd Branch: refs/heads/master Commit: 504838bd5208b9482fe5c953630749b3f02fac81 Parents: f8e826e Author: Carl Franklin Hall <[email protected]> Authored: Tue Apr 14 20:23:53 2015 +0000 Committer: Carl Franklin Hall <[email protected]> Committed: Tue Apr 14 20:23:53 2015 +0000 ---------------------------------------------------------------------- .../apache/commons/dbutils/BeanProcessor.java | 46 ++++++++++++++++---- .../commons/dbutils/BeanProcessorTest.java | 18 +++++++- 2 files changed, 54 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/504838bd/src/main/java/org/apache/commons/dbutils/BeanProcessor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils/BeanProcessor.java b/src/main/java/org/apache/commons/dbutils/BeanProcessor.java index e9f6db5..483719c 100644 --- a/src/main/java/org/apache/commons/dbutils/BeanProcessor.java +++ b/src/main/java/org/apache/commons/dbutils/BeanProcessor.java @@ -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++) { http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/504838bd/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java b/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java index f8ef47e..d73eca4 100644 --- a/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java +++ b/src/test/java/org/apache/commons/dbutils/BeanProcessorTest.java @@ -27,7 +27,7 @@ public class BeanProcessorTest extends BaseTestCase { 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); @@ -42,6 +42,22 @@ public class BeanProcessorTest extends BaseTestCase { 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()); + } + public static class MapColumnToPropertiesBean { private String one;
