dgraham     2003/11/09 10:18:04

  Modified:    dbutils/src/test/org/apache/commons/dbutils/wrappers
                        SqlNullCheckedResultSetTest.java
               dbutils/src/java/org/apache/commons/dbutils/wrappers
                        SqlNullCheckedResultSet.java
  Log:
  - Cleaned up nullMethods population code
  
  - Fixed bug in invoke() where we were potentially calling
  rs.wasNull() before a getter method was called which
  could lead to an SQLException
  
  - Added support for null URLs.
  
  Revision  Changes    Path
  1.2       +32 -15    
jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java
  
  Index: SqlNullCheckedResultSetTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SqlNullCheckedResultSetTest.java  2 Nov 2003 19:15:24 -0000       1.1
  +++ SqlNullCheckedResultSetTest.java  9 Nov 2003 18:18:04 -0000       1.2
  @@ -70,6 +70,8 @@
   import java.lang.reflect.InvocationHandler;
   import java.lang.reflect.Method;
   import java.math.BigDecimal;
  +import java.net.MalformedURLException;
  +import java.net.URL;
   import java.sql.Blob;
   import java.sql.Clob;
   import java.sql.Ref;
  @@ -445,14 +447,12 @@
           rs2.setNullShort(s);
           assertEquals(s, rs.getShort(1));
           assertEquals(s, rs.getShort("column"));
  -
       }
   
       /**
        * Tests the getString implementation.
        */
       public void testGetString() throws SQLException {
  -
           assertEquals(null, rs.getString(1));
           assertTrue(rs.wasNull());
           assertEquals(null, rs.getString("column"));
  @@ -462,7 +462,6 @@
           rs2.setNullString(s);
           assertEquals(s, rs.getString(1));
           assertEquals(s, rs.getString("column"));
  -
       }
   
       /**
  @@ -516,7 +515,21 @@
           assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
           assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
           assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
  -
  +    }
  +    
  +    /**
  +     * Tests the getURL implementation.
  +     */
  +    public void testGetURL() throws SQLException, MalformedURLException {
  +        assertEquals(null, rs.getURL(1));
  +        assertTrue(rs.wasNull());
  +        assertEquals(null, rs.getURL("column"));
  +        assertTrue(rs.wasNull());
  +        // Set what gets returned to something other than the default
  +        URL u = new URL("http://www.apache.org";);
  +        rs2.setNullURL(u);
  +        assertEquals(u, rs.getURL(1));
  +        assertEquals(u, rs.getURL("column"));
       }
   
       /**
  @@ -779,21 +792,18 @@
        * Tests the setNullString implementation.
        */
       public void testSetNullString() throws SQLException {
  -
           assertEquals(null, rs2.getNullString());
           // Set what gets returned to something other than the default
           String s = "hello, world";
           rs2.setNullString(s);
           assertEquals(s, rs.getString(1));
           assertEquals(s, rs.getString("column"));
  -
       }
   
       /**
        * Tests the setNullRef implementation.
        */
       public void testSetNullRef() throws SQLException {
  -
           assertNull(rs2.getNullRef());
           // Set what gets returned to something other than the default
           Ref ref = new SqlNullCheckedResultSetMockRef();
  @@ -802,14 +812,12 @@
           assertEquals(ref, rs.getRef(1));
           assertNotNull(rs.getRef("column"));
           assertEquals(ref, rs.getRef("column"));
  -
       }
   
       /**
        * Tests the setNullTime implementation.
        */
       public void testSetNullTime() throws SQLException {
  -
           assertEquals(null, rs2.getNullTime());
           // Set what gets returned to something other than the default
           Time time = new Time(new java.util.Date().getTime());
  @@ -822,14 +830,12 @@
           assertEquals(time, rs.getTime(1, Calendar.getInstance()));
           assertNotNull(rs.getTime("column", Calendar.getInstance()));
           assertEquals(time, rs.getTime("column", Calendar.getInstance()));
  -
       }
   
       /**
        * Tests the setNullTimestamp implementation.
        */
       public void testSetNullTimestamp() throws SQLException {
  -
           assertEquals(null, rs2.getNullTimestamp());
           // Set what gets returned to something other than the default
           Timestamp ts = new Timestamp(new java.util.Date().getTime());
  @@ -842,7 +848,18 @@
           assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
           assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
           assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
  -
  +    }
  +    
  +    /**
  +     * Tests the setNullString implementation.
  +     */
  +    public void testSetNullURL() throws SQLException, MalformedURLException {
  +        assertEquals(null, rs2.getNullURL());
  +        // Set what gets returned to something other than the default
  +        URL u = new URL("http://jakarta.apache.org";);
  +        rs2.setNullURL(u);
  +        assertEquals(u, rs.getURL(1));
  +        assertEquals(u, rs.getURL("column"));
       }
   }
   
  
  
  
  1.3       +31 -9     
jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSet.java
  
  Index: SqlNullCheckedResultSet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SqlNullCheckedResultSet.java      5 Nov 2003 00:40:43 -0000       1.2
  +++ SqlNullCheckedResultSet.java      9 Nov 2003 18:18:04 -0000       1.3
  @@ -65,6 +65,7 @@
   import java.lang.reflect.InvocationHandler;
   import java.lang.reflect.Method;
   import java.math.BigDecimal;
  +import java.net.URL;
   import java.sql.Blob;
   import java.sql.Clob;
   import java.sql.Date;
  @@ -127,10 +128,8 @@
           for (int i = 0; i < methods.length; i++) {
               String methodName = methods[i].getName();
   
  -            if (methodName.indexOf("getNull") == 0) {
  -                String normalName =
  -                    methodName.substring(0, 3) + methodName.substring(7);
  -
  +            if (methodName.startsWith("getNull")) {
  +                String normalName = "get" + methodName.substring(7);
                   nullMethods.put(normalName, methods[i]);
               }
           }
  @@ -174,6 +173,7 @@
       private String nullString = null;
       private Time nullTime = null;
       private Timestamp nullTimestamp = null;
  +    private URL nullURL = null;
   
       /**
        * The wrapped result. 
  @@ -391,6 +391,16 @@
       }
   
       /**
  +     * Returns the value when a SQL null is encountered as the result of
  +     * invoking a <code>getURL</code> method.
  +     *
  +     * @return the value
  +     */
  +    public URL getNullURL() {
  +        return this.nullURL;
  +    }
  +
  +    /**
        * Intercepts calls to <code>get*</code> methods and calls the appropriate
        * <code>getNull*</code> method if the <code>ResultSet</code> returned
        * <code>null</code>.
  @@ -405,7 +415,9 @@
   
           Method nullMethod = (Method) nullMethods.get(method.getName());
   
  -        return (this.rs.wasNull() && nullMethod != null)
  +        // Check nullMethod != null first so that we don't call wasNull()
  +        // before a true getter method was invoked on the ResultSet.
  +        return (nullMethod != null && this.rs.wasNull())
               ? nullMethod.invoke(this, null)
               : result;
       }
  @@ -608,6 +620,16 @@
        */
       public void setNullTimestamp(Timestamp nullTimestamp) {
           this.nullTimestamp = nullTimestamp;
  +    }
  +
  +    /**
  +     * Sets the value to return when a SQL null is encountered as the result of
  +     * invoking a <code>getURL</code> method.
  +     *
  +     * @param nullURL the value
  +     */
  +    public void setNullURL(URL nullURL) {
  +        this.nullURL = nullURL;
       }
   
   }
  
  
  

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

Reply via email to