dgraham     2003/10/22 19:23:53

  Modified:    dbutils/src/test/org/apache/commons/dbutils
                        MockResultSet.java
  Log:
  Added implementation of getObject(String).
  
  Revision  Changes    Path
  1.4       +48 -16    
jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSet.java
  
  Index: MockResultSet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSet.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MockResultSet.java        23 Oct 2003 01:13:33 -0000      1.3
  +++ MockResultSet.java        23 Oct 2003 02:23:53 -0000      1.4
  @@ -65,6 +65,7 @@
   import java.lang.reflect.Method;
   import java.sql.ResultSet;
   import java.sql.ResultSetMetaData;
  +import java.sql.SQLException;
   import java.util.Arrays;
   import java.util.Collections;
   import java.util.Iterator;
  @@ -102,11 +103,11 @@
                        new MockResultSet(metaData, rows));
        }
   
  -    /**
  -     * MockResultSet constructor.
  -     * @param metaData
  -     * @param rows A null value indicates an empty <code>ResultSet</code>.
  -     */
  +     /**
  +      * MockResultSet constructor.
  +      * @param metaData
  +      * @param rows A null value indicates an empty <code>ResultSet</code>.
  +      */
        public MockResultSet(ResultSetMetaData metaData, Object[][] rows) {
                super();
                this.metaData = metaData;
  @@ -132,8 +133,14 @@
                } else if (methodName.equals("close")) {
   
                } else if (methodName.equals("getObject")) {
  -                     int col = ((Integer) args[0]).intValue() - 1;
  -                     return this.getObject(col);
  +
  +                     if (args[0] instanceof Integer) {
  +                             int col = ((Integer) args[0]).intValue();
  +                             return this.getObject(col);
  +
  +                     } else if (args[0] instanceof String) {
  +                             return this.getObject((String) args[0]);
  +                     }
   
                } else if (methodName.equals("wasNull")) {
                        return this.wasNull();
  @@ -145,12 +152,17 @@
                return null;
        }
   
  -     protected Boolean isLast() {
  +     protected Boolean isLast() throws SQLException {
                return this.iter.hasNext() ? Boolean.FALSE : Boolean.TRUE;
        }
   
  -     protected Object getObject(int columnIndex) {
  -             Object obj = this.currentRow[columnIndex];
  +    /**
  +     * Gets the object at the given column index.
  +     * @param columnIndex A 1 based index.
  +     * @throws SQLException
  +     */
  +     protected Object getObject(int columnIndex) throws SQLException {
  +             Object obj = this.currentRow[columnIndex - 1];
                if (obj == null) {
                        this.wasNull = (obj == null) ? Boolean.TRUE : Boolean.FALSE;
                }
  @@ -158,7 +170,27 @@
                return obj;
        }
   
  -     protected Boolean next() {
  +     protected Object getObject(String columnName) throws SQLException {
  +             return this.getObject(this.findColumnIndex(columnName));
  +     }
  +
  +     /**
  +      * Returns the column index for the given column name.
  +     * @return A 1 based index
  +      * @throws SQLException if the column name is invalid
  +      */
  +     private int findColumnIndex(String columnName) throws SQLException {
  +             for (int i = 0; i < this.currentRow.length; i++) {
  +                     int c = i + 1;
  +                     if 
(this.metaData.getColumnName(c).equalsIgnoreCase(columnName)) {
  +                             return c;
  +                     }
  +             }
  +
  +             throw new SQLException(columnName + " is not a valid column name.");
  +     }
  +
  +     protected Boolean next() throws SQLException {
                if (!this.iter.hasNext()) {
                        return Boolean.FALSE;
                } else {
  @@ -167,11 +199,11 @@
                }
        }
   
  -     protected ResultSetMetaData getMetaData() {
  +     protected ResultSetMetaData getMetaData() throws SQLException {
                return this.metaData;
        }
   
  -     protected Boolean wasNull() {
  +     protected Boolean wasNull() throws SQLException {
                return this.wasNull;
        }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to