commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r556221 - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/ test/org/apache/commons/beanutils/
Date Sat, 14 Jul 2007 04:19:25 GMT
Author: niallp
Date: Fri Jul 13 21:19:21 2007
New Revision: 556221

URL: http://svn.apache.org/viewvc?view=rev&rev=556221
Log:
BEANUTILS-289 - JDBCDynaClass "lowerCase" option causes problems when using the DynaProperty
name to as the column name when accessing the ResultSet
- add a Cross Reference to JDBCDynaClass when the lower case DynaProperty name is different
from the column name
- provide lookup methods for column names and values
- modify RowSetDynaClass and ResultSetIterator to use the new methods

Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/JDBCDynaClass.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetIterator.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/RowSetDynaClass.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaResultSetTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaRowSetTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestResultSet.java

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/JDBCDynaClass.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/JDBCDynaClass.java?view=diff&rev=556221&r1=556220&r2=556221
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/JDBCDynaClass.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/JDBCDynaClass.java
Fri Jul 13 21:19:21 2007
@@ -57,6 +57,12 @@
      */
     protected Map propertiesMap = new HashMap();
 
+    /**
+     * Cross Refernece for column name --> dyna property name
+     * (needed when lowerCase option is true)
+     */
+    private Map columnNameXref;
+
     // ------------------------------------------------------ DynaClass Methods
 
     /**
@@ -158,11 +164,13 @@
                                     int i)
                                     throws SQLException {
 
-        String name = null;
-        if (lowerCase) {
-            name = metadata.getColumnName(i).toLowerCase();
-        } else {
-            name = metadata.getColumnName(i);
+        String columnName = metadata.getColumnName(i);
+        String name = lowerCase ? columnName.toLowerCase() : columnName;
+        if (!name.equals(columnName)) {
+            if (columnNameXref == null) {
+                columnNameXref = new HashMap();
+            }
+            columnNameXref.put(name, columnName);
         }
         String className = null;
         try {
@@ -213,6 +221,35 @@
             propertiesMap.put(properties[i].getName(), properties[i]);
         }
 
+    }
+
+    /**
+     * Get a column value from a {@link ResultSet} for the specified name.
+     *
+     * @param resultSet The result set
+     * @param name The property name
+     * @return The value
+     * @throws SQLException if an error occurs
+     */
+    protected Object getObject(ResultSet resultSet, String name) throws SQLException {
+
+        String columnName = getColumnName(name);
+        return resultSet.getObject(columnName);
+    }
+
+    /**
+     * Get the table column name for the specified property name.
+     * 
+     * @param name The property name
+     * @return The column name (which can be different if the <i>lowerCase</i>
+     * option is used).
+     */
+    protected String getColumnName(String name) {
+        if (columnNameXref != null && columnNameXref.containsKey(name)) {
+            return (String)columnNameXref.get(name);
+        } else {
+            return name;
+        }
     }
 
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java?view=diff&rev=556221&r1=556220&r2=556221
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java
Fri Jul 13 21:19:21 2007
@@ -166,6 +166,18 @@
     }
 
 
+    /**
+     * Get a value from the {@link ResultSet} for the specified
+     * property name.
+     *
+     * @param name The property name
+     * @return The value
+     * @throws SQLException if an error occurs
+     */
+    public Object getObjectFromResultSet(String name) throws SQLException {
+        return getObject(getResultSet(), name);
+    }
+
     // -------------------------------------------------------- Package Methods
 
 

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetIterator.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetIterator.java?view=diff&rev=556221&r1=556220&r2=556221
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetIterator.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ResultSetIterator.java
Fri Jul 13 21:19:21 2007
@@ -116,7 +116,7 @@
             throw new IllegalArgumentException(name);
         }
         try {
-            return (dynaClass.getResultSet().getObject(name));
+            return dynaClass.getObjectFromResultSet(name);
         } catch (SQLException e) {
             throw new RuntimeException
                 ("get(" + name + "): SQLException: " + e);

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/RowSetDynaClass.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/RowSetDynaClass.java?view=diff&rev=556221&r1=556220&r2=556221
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/RowSetDynaClass.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/RowSetDynaClass.java
Fri Jul 13 21:19:21 2007
@@ -229,7 +229,7 @@
             DynaBean bean = createDynaBean();
             for (int i = 0; i < properties.length; i++) {
                 String name = properties[i].getName();
-                Object value = resultSet.getObject(name);
+                Object value = getObject(resultSet, name);
                 if (value != null) {
                     Class type = properties[i].getType();
                     try {

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaResultSetTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaResultSetTestCase.java?view=diff&rev=556221&r1=556220&r2=556221
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaResultSetTestCase.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaResultSetTestCase.java
Fri Jul 13 21:19:21 2007
@@ -242,4 +242,63 @@
     }
 
 
+    /**
+     * Test normal case column names (i.e. not converted to lower case)
+     */
+    public void testIteratorResultsNormalCase() {
+        ResultSetDynaClass dynaClass = null;
+        try {
+            dynaClass = new ResultSetDynaClass(TestResultSet.createProxy(), false);
+        } catch (Exception e) {
+            fail("Error creating ResultSetDynaClass: " + e);
+        }
+
+        // Grab the third row
+        Iterator rows = dynaClass.iterator();
+        rows.next();
+        rows.next();
+        DynaBean row = (DynaBean) rows.next();
+
+        // Invalid argument test
+        try {
+            row.get("unknownProperty");
+            fail("Did not throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected result
+        }
+
+        // Verify property values
+
+        Object bigDecimalProperty = row.get("bigDecimalProperty");
+        assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
+        assertTrue("bigDecimalProperty type",
+                   bigDecimalProperty instanceof BigDecimal);
+        assertEquals("bigDecimalProperty value",
+                     123.45,
+                     ((BigDecimal) bigDecimalProperty).doubleValue(),
+                     0.005);
+
+        Object intProperty = row.get("intProperty");
+        assertNotNull("intProperty exists", intProperty);
+        assertTrue("intProperty type",
+                   intProperty instanceof Integer);
+        assertEquals("intProperty value",
+                     103,
+                     ((Integer) intProperty).intValue());
+
+        Object nullProperty = row.get("nullProperty");
+        assertNull("nullProperty null", nullProperty);
+
+        Object stringProperty = row.get("stringProperty");
+        assertNotNull("stringProperty exists", stringProperty);
+        assertTrue("stringProperty type",
+                   stringProperty instanceof String);
+        assertEquals("stringProperty value",
+                     "This is a string",
+                     (String) stringProperty);
+
+
+    }
+
+
 }

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaRowSetTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaRowSetTestCase.java?view=diff&rev=556221&r1=556220&r2=556221
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaRowSetTestCase.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaRowSetTestCase.java
Fri Jul 13 21:19:21 2007
@@ -234,6 +234,62 @@
 
     }
 
+    /**
+     * Test normal case column names (i.e. not converted to lower case)
+     */
+    public void testListResultsNormalCase() {
+        RowSetDynaClass dynaClass = null;
+        try {
+            dynaClass = new RowSetDynaClass(TestResultSet.createProxy(), false);
+        } catch (Exception e) {
+            fail("Error creating RowSetDynaClass: " + e);
+        }
+
+        // Grab the third row
+        List rows = dynaClass.getRows();
+        DynaBean row = (DynaBean) rows.get(2);
+
+        // Invalid argument test
+        try {
+            row.get("unknownProperty");
+            fail("Did not throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected result
+        }
+
+        // Verify property values
+
+        Object bigDecimalProperty = row.get("bigDecimalProperty");
+        assertNotNull("bigDecimalProperty exists", bigDecimalProperty);
+        assertTrue("bigDecimalProperty type",
+                   bigDecimalProperty instanceof BigDecimal);
+        assertEquals("bigDecimalProperty value",
+                     123.45,
+                     ((BigDecimal) bigDecimalProperty).doubleValue(),
+                     0.005);
+
+        Object intProperty = row.get("intProperty");
+        assertNotNull("intProperty exists", intProperty);
+        assertTrue("intProperty type",
+                   intProperty instanceof Integer);
+        assertEquals("intProperty value",
+                     103,
+                     ((Integer) intProperty).intValue());
+
+        Object nullProperty = row.get("nullProperty");
+        assertNull("nullProperty null", nullProperty);
+
+        Object stringProperty = row.get("stringProperty");
+        assertNotNull("stringProperty exists", stringProperty);
+        assertTrue("stringProperty type",
+                   stringProperty instanceof String);
+        assertEquals("stringProperty value",
+                     "This is a string",
+                     (String) stringProperty);
+
+
+    }
+
     public void testLimitedRows() throws Exception {
         
         // created one with low limit
@@ -252,7 +308,7 @@
         int dateColIdx = 4;
         assertEquals("Meta Column Name",       "dateProperty",       metaData.getColumnName(dateColIdx));
         assertEquals("Meta Column Class Name", "java.sql.Timestamp", metaData.getColumnClassName(dateColIdx));
-        assertEquals("ResultSet Value",        java.sql.Date.class,  resultSet.getObject("dateproperty").getClass());
+        assertEquals("ResultSet Value",        java.sql.Date.class,  resultSet.getObject("dateProperty").getClass());
 
         RowSetDynaClass inconsistentDynaClass = new RowSetDynaClass(resultSet);
         DynaBean firstRow = (DynaBean)inconsistentDynaClass.getRows().get(0);

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestResultSet.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestResultSet.java?view=diff&rev=556221&r1=556220&r2=556221
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestResultSet.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestResultSet.java
Fri Jul 13 21:19:21 2007
@@ -161,35 +161,35 @@
         if (row > 5) {
             throw new SQLException("No current row");
         }
-        if ("bigdecimalproperty".equals(columnName)) {
+        if ("bigDecimalProperty".equals(columnName)) {
             return (new BigDecimal(123.45));
-        } else if ("booleanproperty".equals(columnName)) {
+        } else if ("booleanProperty".equals(columnName)) {
             if ((row % 2) == 0) {
                 return (Boolean.TRUE);
             } else {
                 return (Boolean.FALSE);
             }
-        } else if ("byteproperty".equals(columnName)) {
+        } else if ("byteProperty".equals(columnName)) {
             return (new Byte((byte) row));
-        } else if ("dateproperty".equals(columnName)) {
+        } else if ("dateProperty".equals(columnName)) {
             return (new Date(timestamp));
-        } else if ("doubleproperty".equals(columnName)) {
+        } else if ("doubleProperty".equals(columnName)) {
             return (new Double(321.0));
-        } else if ("floatproperty".equals(columnName)) {
+        } else if ("floatProperty".equals(columnName)) {
             return (new Float((float) 123.0));
-        } else if ("intproperty".equals(columnName)) {
+        } else if ("intProperty".equals(columnName)) {
             return (new Integer(100 + row));
-        } else if ("longproperty".equals(columnName)) {
+        } else if ("longProperty".equals(columnName)) {
             return (new Long(200 + row));
-        } else if ("nullproperty".equals(columnName)) {
+        } else if ("nullProperty".equals(columnName)) {
             return (null);
-        } else if ("shortproperty".equals(columnName)) {
+        } else if ("shortProperty".equals(columnName)) {
             return (new Short((short) (300 + row)));
-        } else if ("stringproperty".equals(columnName)) {
+        } else if ("stringProperty".equals(columnName)) {
             return ("This is a string");
-        } else if ("timeproperty".equals(columnName)) {
+        } else if ("timeProperty".equals(columnName)) {
             return (new Time(timestamp));
-        } else if ("timestampproperty".equals(columnName)) {
+        } else if ("timestampProperty".equals(columnName)) {
             return (new Timestamp(timestamp));
         } else {
             throw new SQLException("Unknown column name " + columnName);



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message