KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > ScrollResultSetTest


1 /*
2  *
3  * Derby - Class ScrollResultSetTest
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
21
22 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
23 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
24 import org.apache.derbyTesting.junit.JDBC;
25
26 import junit.framework.*;
27
28 import java.sql.*;
29
30 /**
31  * Tests scrollable result sets
32  *
33  * @author Fernanda Pizzorno
34  *
35  * Tests:
36  * - testNextOnLastRowForwardOnly: tests that the result set is closed when all
37  * rows have been retreived and next has been called from the last row,
38  * autocommit = true, the result set is not holdable and type forward
39  * only. (DERBY-1295)
40  * - testNextOnLastRowScrollable: tests that the result set is not closed when
41  * next is called while the result set is positioned in the last row,
42  * autocommit = true, the result set is not holdable type scrollable
43  * insensitive. (DERBY-1295)
44  *
45  */

46 public class ScrollResultSetTest extends BaseJDBCTestCase {
47     
48     /** Creates a new instance of ScrollResultSetTest */
49     public ScrollResultSetTest(String JavaDoc name) {
50         super(name);
51     }
52     
53     public static Test suite() {
54         TestSuite suite = new TestSuite();
55                 
56         // Requires holdability
57
if (JDBC.vmSupportsJDBC3() || JDBC.vmSupportsJSR169()) {
58             suite.addTestSuite(ScrollResultSetTest.class);
59         }
60         
61         return suite;
62     }
63
64     /**
65      * Set up the connection to the database.
66      */

67     public void setUp() throws Exception JavaDoc {
68         Connection con = getConnection();
69         con.setAutoCommit(true);
70
71         String JavaDoc createTableWithPK = "CREATE TABLE tableWithPK (" +
72                 "c1 int primary key," +
73                 "c2 int)";
74         String JavaDoc insertData = "INSERT INTO tableWithPK values " +
75                 "(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)";
76         Statement stmt = con.createStatement();
77         stmt.execute(createTableWithPK);
78         
79         stmt.execute(insertData);
80         stmt.close();
81     }
82     
83     /**
84      * Drop the table
85      */

86     public void tearDown() throws Exception JavaDoc {
87         println("TearDown");
88         Statement s = getConnection().createStatement();
89         try {
90             
91             s.executeUpdate("DROP TABLE tableWithPK");
92          } catch (SQLException e) {
93             printStackTrace(e);
94         }
95         s.close();
96         super.tearDown();
97
98     }
99     
100     /**
101      * Test that moving to next row after positioned at the last row on a
102      * forward only result set will close the result set
103      */

104     public void testNextOnLastRowForwardOnly() throws SQLException{
105
106         Connection con = getConnection();
107         con.setAutoCommit(true);
108         con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
109         Statement roStmt = con.createStatement(
110                 ResultSet.TYPE_FORWARD_ONLY,
111                 ResultSet.CONCUR_READ_ONLY);
112
113         ResultSet rs = roStmt.executeQuery("SELECT c1 FROM tableWithPK");
114
115         // call next until positioned after last
116
while (rs.next());
117         
118         try {
119             // forward only result set should be closed now, an exception will
120
// be thrown
121
rs.next();
122             assertTrue("Excepted exception to be thrown - result set is closed",
123                        false);
124         } catch (SQLException se) {
125             if (!usingDerbyNet()) {
126                 assertSQLState("Unexpected SQL State",
127                                SQLStateConstants.RESULT_SET_IS_CLOSED, se);
128             }
129         }
130
131     }
132
133     /**
134      * Test that moving to next row after positioned at the last row on a
135      * scrollable result set will not close the result set
136      */

137     public void testNextOnLastRowScrollable() throws SQLException{
138
139         Connection con = getConnection();
140         con.setAutoCommit(true);
141         con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
142         Statement roStmt = con.createStatement(
143                 ResultSet.TYPE_SCROLL_INSENSITIVE,
144                 ResultSet.CONCUR_READ_ONLY);
145
146         ResultSet rs = roStmt.executeQuery("SELECT c1 FROM tableWithPK");
147         // move to last position and then call next
148
rs.last();
149         rs.next();
150         
151         // scrollable result set should still be open and not throw no
152
// exception will be thrown
153
assertFalse("Calling next while positioned after last returns " +
154                 "false", rs.next());
155         assertTrue("Moving to absolute(2) returns true", rs.absolute(2));
156         rs.close();
157
158     }
159        
160 }
161
Popular Tags