commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1358253 - in /commons/proper/dbutils/trunk/src: changes/changes.xml main/java/org/apache/commons/dbutils/BeanProcessor.java test/java/org/apache/commons/dbutils/BeanProcessorTest.java
Date Fri, 06 Jul 2012 15:05:15 GMT
Author: simonetripodi
Date: Fri Jul  6 15:05:14 2012
New Revision: 1358253

URL: http://svn.apache.org/viewvc?rev=1358253&view=rev
Log:
[DBUTILS-91] Enhance BasicRowProcessor to have row mapping easier to configure - patch provided
by Stevo Slavic

Modified:
    commons/proper/dbutils/trunk/src/changes/changes.xml
    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/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/changes/changes.xml?rev=1358253&r1=1358252&r2=1358253&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/changes/changes.xml (original)
+++ commons/proper/dbutils/trunk/src/changes/changes.xml Fri Jul  6 15:05:14 2012
@@ -44,6 +44,9 @@ The <action> type attribute can be add,u
   </properties>
   <body>
     <release version="1.5" date="2012-??-??" description="Bugfixes and addition of BeanMapHandler">
+      <action dev="simonetripodi" due-to="Stevo Slavic" type="update" issue="DBUTILS-91">
+        Enhance BasicRowProcessor to have row mapping easier to configure
+      </action>
       <action due-to="wspeirs" type="update">
         Updated pom.xml: Java 1.6 now required, clirr and compiler plugin removed 
       </action>

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=1358253&r1=1358252&r2=1358253&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
Fri Jul  6 15:05:14 2012
@@ -65,6 +65,11 @@ public class BeanProcessor {
      */
     private static final Map<Class<?>, Object> primitiveDefaults = new HashMap<Class<?>,
Object>();
 
+    /**
+     * ResultSet column to bean property name overrides.
+     */
+    private Map<String, String> columnToPropertyOverrides = new HashMap<String,
String>();
+
     static {
         primitiveDefaults.put(Integer.TYPE, Integer.valueOf(0));
         primitiveDefaults.put(Short.TYPE, Short.valueOf((short) 0));
@@ -84,6 +89,20 @@ public class BeanProcessor {
     }
 
     /**
+     * Constructor for BeanProcessor configured with column to property name overrides.
+     *
+     * @param columnToPropertyOverrides ResultSet column to bean property name overrides
+     * @since 1.5
+     */
+    public BeanProcessor(Map<String, String> columnToPropertyOverrides) {
+        super();
+        if (columnToPropertyOverrides == null) {
+            throw new IllegalArgumentException("columnToPropertyOverrides map cannot be null");
+        }
+        this.columnToPropertyOverrides = columnToPropertyOverrides;
+    }
+
+    /**
      * Convert a <code>ResultSet</code> row into a JavaBean.  This
      * implementation uses reflection and <code>BeanInfo</code> classes to
      * match column names to bean property names.  Properties are matched to
@@ -391,9 +410,13 @@ public class BeanProcessor {
             if (null == columnName || 0 == columnName.length()) {
               columnName = rsmd.getColumnName(col);
             }
+            String propertyName = columnToPropertyOverrides.get(columnName);
+            if (propertyName == null) {
+                propertyName = columnName;
+            }
             for (int i = 0; i < props.length; i++) {
 
-                if (columnName.equalsIgnoreCase(props[i].getName())) {
+                if (propertyName.equalsIgnoreCase(props[i].getName())) {
                     columnToProperty[col] = i;
                     break;
                 }

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=1358253&r1=1358252&r2=1358253&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
Fri Jul  6 15:05:14 2012
@@ -20,6 +20,8 @@ import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
 
 public class BeanProcessorTest extends BaseTestCase {
 
@@ -45,6 +47,8 @@ public class BeanProcessorTest extends B
 
         private String three;
 
+        private String four;
+
         public String getOne() {
             return one;
         }
@@ -68,6 +72,14 @@ public class BeanProcessorTest extends B
         public void setThree(String three) {
             this.three = three;
         }
+
+        public String getFour() {
+            return four;
+        }
+
+        public void setFour(String four) {
+            this.four = four;
+        }
     }
 
     public void testMapColumnToProperties() throws Exception {
@@ -82,4 +94,20 @@ public class BeanProcessorTest extends B
             assertTrue(columns[i] != BeanProcessor.PROPERTY_NOT_FOUND);
         }
     }
+
+    public void testMapColumnToPropertiesWithOverrides() throws Exception {
+        Map<String, String> columnToPropertyOverrides = new HashMap<String, String>();
+        columnToPropertyOverrides.put("five", "four");
+        BeanProcessor beanProc = new BeanProcessor(columnToPropertyOverrides);
+        String[] columnNames = { "test", "test", "three", "five" };
+        String[] columnLabels = { "one", "two", null, 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);
+        }
+    }
 }



Mime
View raw message