commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dfabul...@apache.org
Subject svn commit: r832535 - in /commons/proper/dbutils/trunk/src: java/org/apache/commons/dbutils/BeanProcessor.java test/org/apache/commons/dbutils/BeanProcessorTest.java test/org/apache/commons/dbutils/MockResultSetMetaData.java
Date Tue, 03 Nov 2009 20:21:18 GMT
Author: dfabulich
Date: Tue Nov  3 20:21:17 2009
New Revision: 832535

URL: http://svn.apache.org/viewvc?rev=832535&view=rev
Log:
[DBUTILS-57] BeanProcessor not able to map an alias column from a HSQLDB query to the any
bean properties
Submitted by: Julien Aymé

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

Modified: commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/BeanProcessor.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/BeanProcessor.java?rev=832535&r1=832534&r2=832535&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/BeanProcessor.java (original)
+++ commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/BeanProcessor.java Tue
Nov  3 20:21:17 2009
@@ -390,7 +390,10 @@
         Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);
 
         for (int col = 1; col <= cols; col++) {
-            String columnName = rsmd.getColumnName(col);
+            String columnName = rsmd.getColumnLabel(col);
+            if (null == columnName || 0 == columnName.length()) {
+              columnName = rsmd.getColumnName(col);
+            }
             for (int i = 0; i < props.length; i++) {
 
                 if (columnName.equalsIgnoreCase(props[i].getName())) {

Modified: commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/BeanProcessorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/BeanProcessorTest.java?rev=832535&r1=832534&r2=832535&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/BeanProcessorTest.java
(original)
+++ commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/BeanProcessorTest.java
Tue Nov  3 20:21:17 2009
@@ -16,6 +16,9 @@
  */
 package org.apache.commons.dbutils;
 
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 
 public class BeanProcessorTest extends BaseTestCase {
@@ -25,14 +28,58 @@
     public void testProcess() throws SQLException {
         TestBean b = null;
         assertTrue(this.rs.next());
-        b = (TestBean) beanProc.toBean(this.rs, TestBean.class);
+        b = beanProc.toBean(this.rs, TestBean.class);
         assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0);
-        
+
         assertTrue(this.rs.next());
-        b = (TestBean) beanProc.toBean(this.rs, TestBean.class);
+        b = beanProc.toBean(this.rs, TestBean.class);
         assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0);
 
         assertFalse(this.rs.next());
     }
 
+    public static class MapColumnToPropertiesBean {
+        private String one;
+
+        private String two;
+
+        private String three;
+
+        public String getOne() {
+            return one;
+        }
+
+        public void setOne(String one) {
+            this.one = one;
+        }
+
+        public String getTwo() {
+            return two;
+        }
+
+        public void setTwo(String two) {
+            this.two = two;
+        }
+
+        public String getThree() {
+            return three;
+        }
+
+        public void setThree(String three) {
+            this.three = three;
+        }
+    }
+
+    public void testMapColumnToProperties() throws Exception {
+        String[] columnNames = { "test", "test", "three" };
+        String[] columnLabels = { "one", "two", null };
+        ResultSetMetaData rsmd = ProxyFactory.instance().createResultSetMetaData(
+                new MockResultSetMetaData(columnNames, columnLabels));
+        PropertyDescriptor[] props = Introspector.getBeanInfo(MapColumnToPropertiesBean.class).getPropertyDescriptors();
+
+        int[] columns = beanProc.mapColumnsToProperties(rsmd, props);
+        for (int i = 1; i < columns.length; i++) {
+            assertTrue(columns[i] != BeanProcessor.PROPERTY_NOT_FOUND);
+        }
+    }
 }

Modified: commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/MockResultSetMetaData.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/MockResultSetMetaData.java?rev=832535&r1=832534&r2=832535&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/MockResultSetMetaData.java
(original)
+++ commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/MockResultSetMetaData.java
Tue Nov  3 20:21:17 2009
@@ -27,6 +27,7 @@
 public class MockResultSetMetaData implements InvocationHandler {
 
     private String[] columnNames = null;
+    private String[] columnLabels = null;
 
     /**
      * Create a <code>MockResultSetMetaData</code> proxy object.  This is 
@@ -46,6 +47,14 @@
     public MockResultSetMetaData(String[] columnNames) {
         super();
         this.columnNames = columnNames;
+        this.columnLabels = new String[columnNames.length];
+
+    }
+    
+    public MockResultSetMetaData(String[] columnNames, String[] columnLabels) {
+        super();
+        this.columnNames = columnNames;
+        this.columnLabels = columnLabels;
 
     }
 
@@ -58,11 +67,16 @@
             return new Integer(this.columnNames.length);
 
         } else if (
-            methodName.equals("getColumnName")
-                || methodName.equals("getColumnLabel")) {
+                methodName.equals("getColumnName")) {
+
+                int col = ((Integer) args[0]).intValue() - 1;
+                return this.columnNames[col];
+
+        } else if (
+        		methodName.equals("getColumnLabel")) {
 
-            int col = ((Integer) args[0]).intValue() - 1;
-            return this.columnNames[col];
+                int col = ((Integer) args[0]).intValue() - 1;
+                return this.columnLabels[col];
 
         } else if (methodName.equals("hashCode")) {
             return new Integer(System.identityHashCode(proxy));



Mime
View raw message