You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/11/28 22:11:33 UTC

cvs commit: jakarta-commons/dbutils/src/java/org/apache/commons/dbutils BasicColumnProcessor.java

dgraham     2003/11/28 13:11:33

  Modified:    dbutils/src/test/org/apache/commons/dbutils
                        BaseTestCase.java TestBean.java
  Added:       dbutils/src/test/org/apache/commons/dbutils
                        BasicColumnProcessorTest.java
               dbutils/src/java/org/apache/commons/dbutils
                        BasicColumnProcessor.java
  Log:
  Added BasicColumnProcessor implementation and
  test cases.  PR# 24997
  
  Revision  Changes    Path
  1.4       +11 -6     jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BaseTestCase.java
  
  Index: BaseTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BaseTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BaseTestCase.java	9 Nov 2003 04:50:46 -0000	1.3
  +++ BaseTestCase.java	28 Nov 2003 21:11:33 -0000	1.4
  @@ -61,6 +61,7 @@
   
   package org.apache.commons.dbutils;
   
  +import java.math.BigInteger;
   import java.sql.ResultSet;
   import java.sql.ResultSetMetaData;
   import java.util.Date;
  @@ -97,7 +98,8 @@
               "integerTest",
               "nullObjectTest",
               "nullPrimitiveTest",
  -            "notDate" };
  +            "notDate",
  +            "columnProcessorDoubleTest" };
   
       /**
        * The number of columns in the MockResultSet.
  @@ -117,7 +119,8 @@
               new Integer(2),
               null,
               null,
  -            new Date()};
  +            new Date(),
  +            BigInteger.valueOf(13)};
   
       private static final Object[] row2 =
           new Object[] {
  @@ -129,7 +132,8 @@
               new Integer(4),
               null,
               null,
  -            new Date()};
  +            new Date(),
  +            BigInteger.valueOf(13)};
   
       private static final Object[][] rows = new Object[][] { row1, row2 };
   
  @@ -181,6 +185,7 @@
           TestSuite suite = new TestSuite("All DbUtils Tests");
   
           suite.addTestSuite(BasicRowProcessorTest.class);
  +        suite.addTestSuite(BasicColumnProcessorTest.class);
           suite.addTestSuite(ProxyFactoryTest.class);
           suite.addTestSuite(ResultSetIteratorTest.class);
           suite.addTestSuite(QueryLoaderTest.class);
  
  
  
  1.4       +18 -3     jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/TestBean.java
  
  Index: TestBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/TestBean.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestBean.java	9 Nov 2003 04:50:46 -0000	1.3
  +++ TestBean.java	28 Nov 2003 21:11:33 -0000	1.4
  @@ -98,6 +98,13 @@
        * a Date will be returned but the property is a String.
        */
       private String notDate = "not a date";
  +    
  +    /**
  +     * The ResultSet will have a BigDecimal in this column and the 
  +     * BasicColumnProcessor should convert that to a double and store the value
  +     * in this property.
  +     */
  +    private double columnProcessorDoubleTest = -1;
   
       /**
        * Constructor for TestBean.
  @@ -176,6 +183,14 @@
   
       public void setNotDate(String string) {
           notDate = string;
  +    }
  +
  +    public double getColumnProcessorDoubleTest() {
  +        return columnProcessorDoubleTest;
  +    }
  +
  +    public void setColumnProcessorDoubleTest(double d) {
  +        columnProcessorDoubleTest = d;
       }
   
   }
  
  
  
  1.1                  jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BasicColumnProcessorTest.java
  
  Index: BasicColumnProcessorTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BasicColumnProcessorTest.java,v 1.1 2003/11/28 21:11:33 dgraham Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/28 21:11:33 $
   * 
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.dbutils;
  
  import java.sql.SQLException;
  
  /**
   * BasicColumnProcessorTest
   * 
   * @author David Graham
   */
  public class BasicColumnProcessorTest extends BaseTestCase {
  
      private static final RowProcessor rowProc =
          new BasicRowProcessor(new BasicColumnProcessor());
  
      /**
       * Constructor for BasicColumnProcessorTest.
       * @param name
       */
      public BasicColumnProcessorTest(String name) {
          super(name);
      }
  
      public void testProcess() throws SQLException {
          int rowCount = 0;
          TestBean b = null;
          while (this.rs.next()) {
              b = (TestBean) rowProc.toBean(this.rs, TestBean.class);
              assertNotNull(b);
              rowCount++;
          }
  
          assertEquals(ROWS, rowCount);
          assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0);
      }
  
  }
  
  
  
  1.1                  jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/BasicColumnProcessor.java
  
  Index: BasicColumnProcessor.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/BasicColumnProcessor.java,v 1.1 2003/11/28 21:11:33 dgraham Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/28 21:11:33 $
   * 
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.dbutils;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  /**
   * <code>ColumnProcessor</code> implementation that delegates to the JDBC
   * driver to perform type conversions.  This calls the type specific methods on 
   * the <code>ResultSet</code> such as getDouble(), getInt(), etc. depending on 
   * the supplied JavaBean property type.
   * 
   * @author Corby Page
   * @author David Graham
   * 
   * @see ColumnProcessor
   * 
   * @since DbUtils 1.1
   */
  public class BasicColumnProcessor implements ColumnProcessor {
  
      /**
       * BasicColumnProcessor constructor.
       */
      public BasicColumnProcessor() {
          super();
      }
  
      /**
       * @see org.apache.commons.dbutils.ColumnProcessor#process(java.sql.ResultSet, int, java.lang.Class)
       */
      public Object process(ResultSet rs, int index, Class propType)
          throws SQLException {
  
          if (propType.equals(Integer.TYPE) || propType.equals(Integer.class)) {
              return new Integer(rs.getInt(index));
  
          } else if (
              propType.equals(Boolean.TYPE) || propType.equals(Boolean.class)) {
              return new Boolean(rs.getBoolean(index));
  
          } else if (propType.equals(Long.TYPE) || propType.equals(Long.class)) {
              return new Long(rs.getLong(index));
  
          } else if (
              propType.equals(Double.TYPE) || propType.equals(Double.class)) {
              return new Double(rs.getDouble(index));
  
          } else if (
              propType.equals(Float.TYPE) || propType.equals(Float.class)) {
              return new Float(rs.getFloat(index));
  
          } else if (
              propType.equals(Short.TYPE) || propType.equals(Short.class)) {
              return new Short(rs.getShort(index));
  
          } else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class)) {
              return new Byte(rs.getByte(index));
  
          } else {
              return rs.getObject(index);
          }
  
      }
  
  }
  
  
  

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