Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6479834

ResultSet#isAfterLast() should reflect value of internal rowPosition

    XMLWordPrintable

    Details

      Description

      A DESCRIPTION OF THE REQUEST :
      I want to insert a row to a ResultSet, which is scrolled to the end.
        To ensure that after an insert in this ResultSet the rowpointer is located on the just inserted row, method isAfterLast() should return true before the insertion.

      JUSTIFICATION :
      As method isAfterLast() returns true for a non-empty ResultSet, method isAfterLast() should also return true for an empty ResultSet, if internal rowPosition is numberOfRows+1.

      Same for ResultSet#isBeforeFirst().


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Can use same code on empty ResultSet as on non-empty ResultSet.
      ACTUAL -
      Empty ResultSet must be treated different than non-empty ResultSet.

      ---------- BEGIN SOURCE ----------
      /*
       * JdbcOdbcResultSetTest.java
       * JUnit based test
       *
       * Created on 11. Juni 2006, 02:33
       *
       * $Id: JdbcOdbcResultSetTest.java 83 2006-08-24 20:56:16Z jdk6_JdbcOdbcBugs_ulf $
       */

      package sun.jdbc.odbc;

      import junit.framework.*;
      import java.sql.*;

      /**
       *
       * @author Ulf Zibis, ###@###.###
       */
      public class JdbcOdbcResultSetTest extends TestCase {
        
        private static final String URL = "jdbc:odbc:TestData";
        private static final String TABLE = "Test1";
        private static final String ID = "id";
        private static final String STRING1 = "string1";
        private static final String INT1 = "int1";
        private static final int STRING1_LEN = 100;
        private static final String EMPTY_QUERY = "SELECT * FROM "+TABLE+" WHERE "+ID+"=0";
        private static final String ORDERED_QUERY = "SELECT * FROM "+TABLE+" ORDER BY "+ID;
        private static final String STRING32CHARS = "Here are only 16 + 16 more chars";
        private static final String UPDATED19CHARS = "updated to 19 chars";
        private static final String INSERTED8CHARS = "inserted";
        
        private Connection myCon = null;
        private Statement myStmt;
        private JdbcOdbcResultSet myRs;
        
        static {
          System.out.println("JDK Version: "+System.getProperty("java.version"));
          try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Obsolete in JDK 6.0
          }
          catch (Exception e) { e.printStackTrace(); }
        }
        
        public JdbcOdbcResultSetTest(String testName) {
          super(testName);
          System.out.println("Construct JdbcOdbcResultSetTest("+testName+") !!");
        }
        protected void finalize() throws Throwable {
          super.finalize();
          try { if (myCon != null) myCon.close(); }
          catch (Throwable t) { t.printStackTrace(); }
        }
        
        protected void setUp() throws Exception {

          myCon = DriverManager.getConnection( URL);
          Statement stmt = myCon.createStatement();
          try {
            ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM "+TABLE);
            rs.next();
            //System.out.println( TABLE+" had "+rs.getInt( 1)+" record(s).");
            stmt.executeUpdate("DROP TABLE "+TABLE);
          }
          catch (SQLException se) {
            if (se.getErrorCode() == -1305)
              System.out.println( TABLE+" was not existent.");
            else throw se;
          }
          stmt.executeUpdate("CREATE TABLE "+TABLE+"( "+ID+" COUNTER PRIMARY KEY, "
                                                      +STRING1+" VARCHAR("+STRING1_LEN+"), "
                                                      +INT1+" INT)");
          //System.out.println("New empty table "+TABLE+" created !");
          stmt.close();
          myStmt = myCon.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        }
        protected void tearDown() throws Exception {
          try { if (myCon != null) myCon.close(); myCon = null;}
          catch (Throwable t) { t.printStackTrace(); }
        }
        
        public void testIsAfterLast() throws Exception {
          System.out.println("testIsAfterLast() <Bug ???>");
          try {
            myRs = (JdbcOdbcResultSet)myStmt.executeQuery( ORDERED_QUERY);
            boolean result;
            
            myRs.beforeFirst(); // define same start position
            myRs.afterLast();
      // assertTrue("myRs.isAfterLast(): ", myRs.isAfterLast());
            
            myRs.beforeFirst(); // define same start position
            result = myRs.next();
            assertFalse("myRs.next(): ", result);
      // assertTrue("myRs.isAfterLast(): ", myRs.isAfterLast());
            
            myRs.beforeFirst(); // define same start position
            result = myRs.first();
            assertFalse("myRs.first(): ", result);
      // assertTrue("myRs.isAfterLast(): ", myRs.isAfterLast());

            int rowPosition;
            String resultSTRING;
            // ensure that ResultSet is after the end:
            myRs.afterLast(); // internal rowPosition is set to 1

      // assertTrue("myRs.isAfterLast(): ", myRs.isAfterLast()); // Bug: has to be true
            myRs.moveToInsertRow(); // internal lastRowPosition is set to 1
            rowPosition = myRs.getRow();
            assertEquals("myRs.getRow(): ", 0, rowPosition);
            myRs.updateString( STRING1, INSERTED8CHARS);
            myRs.insertRow(); // internal numberOfRows is set to 1
            rowPosition = myRs.getRow();
            assertEquals("myRs.getRow(): ", 1, rowPosition);
      // assertTrue("myRs.rowInserted(): ", myRs.rowInserted()); // Bug 4894354
            assertTrue("myRs.isLast(): ", myRs.isLast());
            assertFalse("myRs.isAfterLast(): ", myRs.isAfterLast());
      // resultSTRING = myRs.getString( STRING1); // Bug
      // assertEquals("myRs.getString( STRING1): ",
      // INSERTED8CHARS, resultSTRING);

            myRs.moveToCurrentRow(); // internal lastRowPosition is set to 0
            rowPosition = myRs.getRow();
            assertEquals("myRs.getRow(): ", 1, rowPosition);
            assertTrue("myRs.isLast(): ", myRs.isLast());
            assertFalse("myRs.isAfterLast(): ", myRs.isAfterLast());
            resultSTRING = myRs.getString( STRING1); // OK
            assertEquals("myRs.getString( STRING1): ",
                         INSERTED8CHARS, resultSTRING);

            // ensure that ResultSet is after the end:
            myRs.afterLast(); // internal rowPosition is set to 2

            assertTrue("myRs.isAfterLast(): ", myRs.isAfterLast()); // OK: is true
            myRs.moveToInsertRow(); // internal lastRowPosition is set to 2
            rowPosition = myRs.getRow();
            assertEquals("myRs.getRow(): ", 0, rowPosition);
            myRs.updateString( STRING1, STRING32CHARS);
            myRs.insertRow(); // internal numberOfRows is set to 2
            rowPosition = myRs.getRow();
            assertEquals("myRs.getRow(): ", 2, rowPosition);
      // assertTrue("myRs.rowInserted(): ", myRs.rowInserted()); // Bug 4894354
            assertTrue("myRs.isLast(): ", myRs.isLast());
            assertFalse("myRs.isAfterLast(): ", myRs.isAfterLast());
      // resultSTRING = myRs.getString( STRING1); // Bug
      // assertEquals("myRs.getString( STRING1): ",
      // STRING32CHARS, resultSTRING);

            myRs.moveToCurrentRow(); // internal lastRowPosition is set to 0
            rowPosition = myRs.getRow();
            assertEquals("myRs.getRow(): ", 2, rowPosition);
            assertTrue("myRs.isLast(): ", myRs.isLast());
            assertFalse("myRs.isAfterLast(): ", myRs.isAfterLast());
            String resultSTRING2 = myRs.getString( STRING1); // OK
            assertEquals("myRs.getString( STRING1): ",
                         STRING32CHARS, resultSTRING2);

            // ResultSet is not after the end:
            assertFalse("myRs.isAfterLast(): ", myRs.isAfterLast());
            myRs.moveToInsertRow(); // internal lastRowPosition is set to 2
            rowPosition = myRs.getRow();
      // assertEquals("myRs.getRow(): ", 0, rowPosition); // Bug
            myRs.updateString( STRING1, INSERTED8CHARS);
            myRs.insertRow(); // internal numberOfRows is set to 3
            rowPosition = myRs.getRow();
      // assertEquals("myRs.getRow(): ", 3, rowPosition); // Bug
      // assertTrue("myRs.rowInserted(): ", myRs.rowInserted()); // Bug 4894354
      // assertTrue("myRs.isLast(): ", myRs.isLast()); // Bug
            assertFalse("myRs.isAfterLast(): ", myRs.isAfterLast());
      // resultSTRING = myRs.getString( STRING1); // Bug
      // assertEquals("myRs.getString( STRING1): ",
      // INSERTED8CHARS, resultSTRING);

            myRs.moveToCurrentRow(); // internal lastRowPosition is set to 0
            rowPosition = myRs.getRow();
            assertEquals("myRs.getRow(): ", 2, rowPosition);
            assertFalse("myRs.isLast(): ", myRs.isLast());
            assertFalse("myRs.isAfterLast(): ", myRs.isAfterLast());
            resultSTRING = myRs.getString( STRING1); // OK
            assertEquals("myRs.getString( STRING1): ",
                         STRING32CHARS, resultSTRING);

          } catch (SQLException se) {
            Exception e = new Exception( se+", Error: "+se.getErrorCode()+", SQLState: "+se.getSQLState());
            e.setStackTrace( se.getStackTrace());
            //e.initCause( se);
            throw e;
          }
        }

        
      /*
        public static Test suite() {
          TestSuite suite = new TestSuite(JdbcOdbcResultSetTest.class);
          
          return suite;
        }
        
        public static void main (String[] args) {
                junit.textui.TestRunner.run(suite());
        }
      */
      }

      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: