DBUTILS-82 Applied user-submitted patch and made minor fixes git-svn-id: https://svn.apache.org/repos/asf/commons/proper/dbutils/trunk@1673285 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/f8e826ec Tree: http://git-wip-us.apache.org/repos/asf/commons-dbutils/tree/f8e826ec Diff: http://git-wip-us.apache.org/repos/asf/commons-dbutils/diff/f8e826ec Branch: refs/heads/master Commit: f8e826ec114e9d6f86f61676b4c106dc4260c736 Parents: 8022314 Author: Carl Franklin Hall <[email protected]> Authored: Mon Apr 13 21:19:22 2015 +0000 Committer: Carl Franklin Hall <[email protected]> Committed: Mon Apr 13 21:19:22 2015 +0000 ---------------------------------------------------------------------- .../commons/dbutils/BasicRowProcessor.java | 4 +- .../apache/commons/dbutils/BeanProcessor.java | 4 +- .../apache/commons/dbutils/RowProcessor.java | 4 +- .../commons/dbutils/handlers/BeanHandler.java | 6 +- .../dbutils/handlers/BeanListHandler.java | 6 +- .../dbutils/handlers/BeanHandlerTest.java | 33 +++++++++ .../dbutils/handlers/BeanListHandlerTest.java | 71 ++++++++++++++++++++ 7 files changed, 116 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/f8e826ec/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java b/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java index cd6ec96..5533f94 100644 --- a/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java +++ b/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java @@ -119,7 +119,7 @@ public class BasicRowProcessor implements RowProcessor { * @return the newly created bean */ @Override - public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException { + public <T> T toBean(ResultSet rs, Class<? extends T> type) throws SQLException { return this.convert.toBean(rs, type); } @@ -136,7 +136,7 @@ public class BasicRowProcessor implements RowProcessor { * they were returned by the <code>ResultSet</code>. */ @Override - public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException { + public <T> List<T> toBeanList(ResultSet rs, Class<? extends T> type) throws SQLException { return this.convert.toBeanList(rs, type); } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/f8e826ec/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 f1dfe47..e9f6db5 100644 --- a/src/main/java/org/apache/commons/dbutils/BeanProcessor.java +++ b/src/main/java/org/apache/commons/dbutils/BeanProcessor.java @@ -135,7 +135,7 @@ public class BeanProcessor { * @throws SQLException if a database access error occurs * @return the newly created bean */ - public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException { + public <T> T toBean(ResultSet rs, Class<? extends T> type) throws SQLException { PropertyDescriptor[] props = this.propertyDescriptors(type); @@ -178,7 +178,7 @@ public class BeanProcessor { * @throws SQLException if a database access error occurs * @return the newly created List of beans */ - public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException { + public <T> List<T> toBeanList(ResultSet rs, Class<? extends T> type) throws SQLException { List<T> results = new ArrayList<T>(); if (!rs.next()) { http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/f8e826ec/src/main/java/org/apache/commons/dbutils/RowProcessor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils/RowProcessor.java b/src/main/java/org/apache/commons/dbutils/RowProcessor.java index 02e9382..2434dc4 100644 --- a/src/main/java/org/apache/commons/dbutils/RowProcessor.java +++ b/src/main/java/org/apache/commons/dbutils/RowProcessor.java @@ -55,7 +55,7 @@ public interface RowProcessor { * @throws SQLException if a database access error occurs * @return the newly created bean */ - <T> T toBean(ResultSet rs, Class<T> type) throws SQLException; + <T> T toBean(ResultSet rs, Class<? extends T> type) throws SQLException; /** * Create a <code>List</code> of JavaBeans from the column values in all @@ -68,7 +68,7 @@ public interface RowProcessor { * @return A <code>List</code> of beans with the given type in the order * they were returned by the <code>ResultSet</code>. */ - <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException; + <T> List<T> toBeanList(ResultSet rs, Class<? extends T> type) throws SQLException; /** * Create a <code>Map</code> from the column values in one http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/f8e826ec/src/main/java/org/apache/commons/dbutils/handlers/BeanHandler.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils/handlers/BeanHandler.java b/src/main/java/org/apache/commons/dbutils/handlers/BeanHandler.java index daa3027..c2f0436 100644 --- a/src/main/java/org/apache/commons/dbutils/handlers/BeanHandler.java +++ b/src/main/java/org/apache/commons/dbutils/handlers/BeanHandler.java @@ -34,7 +34,7 @@ public class BeanHandler<T> implements ResultSetHandler<T> { /** * The Class of beans produced by this handler. */ - private final Class<T> type; + private final Class<? extends T> type; /** * The RowProcessor implementation to use when converting rows @@ -48,7 +48,7 @@ public class BeanHandler<T> implements ResultSetHandler<T> { * @param type The Class that objects returned from <code>handle()</code> * are created from. */ - public BeanHandler(Class<T> type) { + public BeanHandler(Class<? extends T> type) { this(type, ArrayHandler.ROW_PROCESSOR); } @@ -60,7 +60,7 @@ public class BeanHandler<T> implements ResultSetHandler<T> { * @param convert The <code>RowProcessor</code> implementation * to use when converting rows into beans. */ - public BeanHandler(Class<T> type, RowProcessor convert) { + public BeanHandler(Class<? extends T> type, RowProcessor convert) { this.type = type; this.convert = convert; } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/f8e826ec/src/main/java/org/apache/commons/dbutils/handlers/BeanListHandler.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils/handlers/BeanListHandler.java b/src/main/java/org/apache/commons/dbutils/handlers/BeanListHandler.java index 1541a66..c990dbd 100644 --- a/src/main/java/org/apache/commons/dbutils/handlers/BeanListHandler.java +++ b/src/main/java/org/apache/commons/dbutils/handlers/BeanListHandler.java @@ -36,7 +36,7 @@ public class BeanListHandler<T> implements ResultSetHandler<List<T>> { /** * The Class of beans produced by this handler. */ - private final Class<T> type; + private final Class<? extends T> type; /** * The RowProcessor implementation to use when converting rows @@ -50,7 +50,7 @@ public class BeanListHandler<T> implements ResultSetHandler<List<T>> { * @param type The Class that objects returned from <code>handle()</code> * are created from. */ - public BeanListHandler(Class<T> type) { + public BeanListHandler(Class<? extends T> type) { this(type, ArrayHandler.ROW_PROCESSOR); } @@ -62,7 +62,7 @@ public class BeanListHandler<T> implements ResultSetHandler<List<T>> { * @param convert The <code>RowProcessor</code> implementation * to use when converting rows into beans. */ - public BeanListHandler(Class<T> type, RowProcessor convert) { + public BeanListHandler(Class<? extends T> type, RowProcessor convert) { this.type = type; this.convert = convert; } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/f8e826ec/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java index 64b0133..6eb7135 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/BeanHandlerTest.java @@ -45,4 +45,37 @@ public class BeanHandlerTest extends BaseTestCase { assertNull(results); } + public void testHandleToSuperClass() throws SQLException { + ResultSetHandler<TestBean> h = new BeanHandler<TestBean>(SubTestBean.class); + TestBean results = h.handle(this.rs); + + assertNotNull(results); + assertEquals("1", results.getOne()); + assertEquals("2", results.getTwo()); + assertEquals(TestBean.Ordinal.THREE, results.getThree()); + assertEquals("not set", results.getDoNotSet()); + } + + public void testHandleToInterface() throws SQLException { + ResultSetHandler<SubTestBeanInterface> h = new BeanHandler<SubTestBeanInterface>(SubTestBean.class); + SubTestBeanInterface results = h.handle(this.rs); + + assertNotNull(results); + assertEquals("1", results.getOne()); + assertEquals("2", results.getTwo()); + assertEquals(TestBean.Ordinal.THREE, results.getThree()); + assertEquals("not set", results.getDoNotSet()); + } + + public static interface SubTestBeanInterface { + public String getOne(); + + public TestBean.Ordinal getThree(); + + public String getTwo(); + + public String getDoNotSet(); + } + + public static class SubTestBean extends TestBean implements SubTestBeanInterface { } } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/f8e826ec/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java index 0ba7eb3..ef3a9e5 100644 --- a/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java +++ b/src/test/java/org/apache/commons/dbutils/handlers/BeanListHandlerTest.java @@ -64,4 +64,75 @@ public class BeanListHandlerTest extends BaseTestCase { assertTrue(results.isEmpty()); } + public void testHandleToSuperClass() throws SQLException { + ResultSetHandler<List<TestBean>> h = new BeanListHandler<TestBean>(SubTestBean.class); + List<TestBean> results = h.handle(this.rs); + + assertNotNull(results); + assertEquals(ROWS, results.size()); + + Iterator<TestBean> iter = results.iterator(); + TestBean row = null; + assertTrue(iter.hasNext()); + row = iter.next(); + assertSame(SubTestBean.class, row.getClass()); + + assertEquals("1", row.getOne()); + assertEquals("2", row.getTwo()); + assertEquals(TestBean.Ordinal.THREE, row.getThree()); + assertEquals("not set", row.getDoNotSet()); + + assertTrue(iter.hasNext()); + row = iter.next(); + assertSame(SubTestBean.class, row.getClass()); + + assertEquals("4", row.getOne()); + assertEquals("5", row.getTwo()); + assertEquals(TestBean.Ordinal.SIX, row.getThree()); + assertEquals("not set", row.getDoNotSet()); + + assertFalse(iter.hasNext()); + } + + public void testHandleToInterface() throws SQLException { + ResultSetHandler<List<SubTestBeanInterface>> h = new BeanListHandler<SubTestBeanInterface>(SubTestBean.class); + List<SubTestBeanInterface> results = h.handle(this.rs); + + assertNotNull(results); + assertEquals(ROWS, results.size()); + + Iterator<SubTestBeanInterface> iter = results.iterator(); + SubTestBeanInterface row = null; + assertTrue(iter.hasNext()); + row = iter.next(); + assertSame(SubTestBean.class, row.getClass()); + + assertEquals("1", row.getOne()); + assertEquals("2", row.getTwo()); + assertEquals(TestBean.Ordinal.THREE, row.getThree()); + assertEquals("not set", row.getDoNotSet()); + + assertTrue(iter.hasNext()); + row = iter.next(); + assertSame(SubTestBean.class, row.getClass()); + + assertEquals("4", row.getOne()); + assertEquals("5", row.getTwo()); + assertEquals(TestBean.Ordinal.SIX, row.getThree()); + assertEquals("not set", row.getDoNotSet()); + + assertFalse(iter.hasNext()); + } + + public static interface SubTestBeanInterface { + public String getOne(); + + public TestBean.Ordinal getThree(); + + public String getTwo(); + + public String getDoNotSet(); + } + + public static class SubTestBean extends TestBean implements SubTestBeanInterface { } }
