001: /*
002: *
003: * Derby - Class ScrollResultSetTest
004: *
005: * Licensed to the Apache Software Foundation (ASF) under one or more
006: * contributor license agreements. See the NOTICE file distributed with
007: * this work for additional information regarding copyright ownership.
008: * The ASF licenses this file to You under the Apache License, Version 2.0
009: * (the "License"); you may not use this file except in compliance with
010: * the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
021:
022: import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
023: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
024: import org.apache.derbyTesting.junit.JDBC;
025:
026: import junit.framework.*;
027:
028: import java.sql.*;
029:
030: /**
031: * Tests scrollable result sets
032: *
033: * @author Fernanda Pizzorno
034: *
035: * Tests:
036: * - testNextOnLastRowForwardOnly: tests that the result set is closed when all
037: * rows have been retreived and next has been called from the last row,
038: * autocommit = true, the result set is not holdable and type forward
039: * only. (DERBY-1295)
040: * - testNextOnLastRowScrollable: tests that the result set is not closed when
041: * next is called while the result set is positioned in the last row,
042: * autocommit = true, the result set is not holdable type scrollable
043: * insensitive. (DERBY-1295)
044: *
045: */
046: public class ScrollResultSetTest extends BaseJDBCTestCase {
047:
048: /** Creates a new instance of ScrollResultSetTest */
049: public ScrollResultSetTest(String name) {
050: super (name);
051: }
052:
053: public static Test suite() {
054: TestSuite suite = new TestSuite();
055:
056: // Requires holdability
057: if (JDBC.vmSupportsJDBC3() || JDBC.vmSupportsJSR169()) {
058: suite.addTestSuite(ScrollResultSetTest.class);
059: }
060:
061: return suite;
062: }
063:
064: /**
065: * Set up the connection to the database.
066: */
067: public void setUp() throws Exception {
068: Connection con = getConnection();
069: con.setAutoCommit(true);
070:
071: String createTableWithPK = "CREATE TABLE tableWithPK ("
072: + "c1 int primary key," + "c2 int)";
073: String insertData = "INSERT INTO tableWithPK values "
074: + "(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)";
075: Statement stmt = con.createStatement();
076: stmt.execute(createTableWithPK);
077:
078: stmt.execute(insertData);
079: stmt.close();
080: }
081:
082: /**
083: * Drop the table
084: */
085: public void tearDown() throws Exception {
086: println("TearDown");
087: Statement s = getConnection().createStatement();
088: try {
089:
090: s.executeUpdate("DROP TABLE tableWithPK");
091: } catch (SQLException e) {
092: printStackTrace(e);
093: }
094: s.close();
095: super .tearDown();
096:
097: }
098:
099: /**
100: * Test that moving to next row after positioned at the last row on a
101: * forward only result set will close the result set
102: */
103: public void testNextOnLastRowForwardOnly() throws SQLException {
104:
105: Connection con = getConnection();
106: con.setAutoCommit(true);
107: con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
108: Statement roStmt = con
109: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
110: ResultSet.CONCUR_READ_ONLY);
111:
112: ResultSet rs = roStmt
113: .executeQuery("SELECT c1 FROM tableWithPK");
114:
115: // call next until positioned after last
116: while (rs.next())
117: ;
118:
119: try {
120: // forward only result set should be closed now, an exception will
121: // be thrown
122: rs.next();
123: assertTrue(
124: "Excepted exception to be thrown - result set is closed",
125: false);
126: } catch (SQLException se) {
127: if (!usingDerbyNet()) {
128: assertSQLState("Unexpected SQL State",
129: SQLStateConstants.RESULT_SET_IS_CLOSED, se);
130: }
131: }
132:
133: }
134:
135: /**
136: * Test that moving to next row after positioned at the last row on a
137: * scrollable result set will not close the result set
138: */
139: public void testNextOnLastRowScrollable() throws SQLException {
140:
141: Connection con = getConnection();
142: con.setAutoCommit(true);
143: con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
144: Statement roStmt = con.createStatement(
145: ResultSet.TYPE_SCROLL_INSENSITIVE,
146: ResultSet.CONCUR_READ_ONLY);
147:
148: ResultSet rs = roStmt
149: .executeQuery("SELECT c1 FROM tableWithPK");
150: // move to last position and then call next
151: rs.last();
152: rs.next();
153:
154: // scrollable result set should still be open and not throw no
155: // exception will be thrown
156: assertFalse("Calling next while positioned after last returns "
157: + "false", rs.next());
158: assertTrue("Moving to absolute(2) returns true", rs.absolute(2));
159: rs.close();
160:
161: }
162:
163: }
|