001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.testRelative
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, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import java.sql.*;
025:
026: import org.apache.derby.tools.ij;
027: import org.apache.derby.tools.JDBCDisplayUtil;
028: import org.apache.derbyTesting.functionTests.util.TestUtil;
029:
030: public class testRelative {
031:
032: static final String NO_CURRENT_ROW_SQL_STATE = (TestUtil
033: .isNetFramework() ? "XJ121" : "24000");
034:
035: public static void main(String[] args) {
036: System.out.println("Test testRelative starting");
037: Connection con = null;
038: try {
039: // use the ij utility to read the property file and
040: // make the initial connection.
041: ij.getPropertyArg(args);
042: con = ij.startJBMS();
043: test1(con);
044: } catch (Exception e) {
045: unexpectedException(e);
046: }
047: }
048:
049: public static void test1(Connection con) {
050: ResultSet rs = null;
051: PreparedStatement pStmt = null;
052: Statement stmt1 = null;
053: String returnValue = null;
054:
055: try {
056: con.setAutoCommit(false);
057: pStmt = con
058: .prepareStatement("create table testRelative(name varchar(10), i int)");
059: pStmt.executeUpdate();
060: con.commit();
061:
062: pStmt = con
063: .prepareStatement("insert into testRelative values (?,?)");
064:
065: pStmt.setString(1, "work1");
066: pStmt.setNull(2, 1);
067: pStmt.addBatch();
068:
069: pStmt.setString(1, "work2");
070: pStmt.setNull(2, 2);
071: pStmt.addBatch();
072:
073: pStmt.setString(1, "work3");
074: pStmt.setNull(2, 3);
075: pStmt.addBatch();
076:
077: pStmt.setString(1, "work4");
078: pStmt.setNull(2, 4);
079: pStmt.addBatch();
080:
081: pStmt.executeBatch();
082: con.commit();
083: } catch (SQLException se) {
084: unexpectedSQLException(se);
085: } catch (Throwable t) {
086: System.out.println("FAIL--unexpected exception: "
087: + t.getMessage());
088: t.printStackTrace(System.out);
089: }
090: try {
091: testScrolling(ResultSet.CONCUR_READ_ONLY, con);
092: testScrolling(ResultSet.CONCUR_UPDATABLE, con);
093: } catch (Throwable e) {
094: System.out.println("FAIL -- unexpected exception: "
095: + e.getMessage());
096: e.printStackTrace(System.out);
097:
098: }
099: }
100:
101: private static void testScrolling(int concurrency, Connection con)
102: throws SQLException {
103: Statement stmt1 = con.createStatement(
104: ResultSet.TYPE_SCROLL_INSENSITIVE, concurrency);
105: ResultSet rs = stmt1.executeQuery("select * from testRelative");
106:
107: rs.next(); // First Record
108: System.out.println("Value = " + rs.getString("name"));
109:
110: rs.relative(2);
111: System.out.println("Value = " + rs.getString("name"));
112: System.out.println("isFirst = " + rs.isFirst() + " isLast = "
113: + rs.isLast() + " isAfterLast = " + rs.isAfterLast());
114: rs.relative(-2);
115: System.out.println("Value = " + rs.getString("name"));
116:
117: try {
118: rs.relative(10);
119: System.out.println("Value = " + rs.getString("name"));
120: System.out.println("isFirst = " + rs.isFirst()
121: + " isLast = " + rs.isLast() + " isAfterLast = "
122: + rs.isAfterLast());
123: } catch (SQLException sqle) {
124:
125: expectedException(sqle, NO_CURRENT_ROW_SQL_STATE);
126: }
127: }
128:
129: /**
130: * Print the expected Exception's details if the SQLException SQLState
131: * matches the expected SQLState. Otherwise fail
132: *
133: * @param se SQLException that was thrown by the test
134: * @param expectedSQLState The SQLState that we expect.
135: *
136: **/
137: static private void expectedException(SQLException se,
138: String expectedSQLState) {
139: if (se.getSQLState() != null
140: && (se.getSQLState().equals(expectedSQLState))) {
141: System.out.println("PASS -- expected exception");
142: } else {
143: System.out.println("FAIL--Unexpected SQLException: "
144: + "SQLSTATE(" + se.getSQLState() + ")"
145: + se.getMessage());
146: while (se != null) {
147: System.out.println("SQLSTATE(" + se.getSQLState()
148: + "): " + se.getMessage());
149: se.printStackTrace(System.out);
150: se = se.getNextException();
151: }
152:
153: }
154: }
155:
156: /**
157: * We are here because we got an exception when did not expect one.
158: * Hence printing the message and stack trace here.
159: **/
160: static private void unexpectedSQLException(SQLException se) {
161: System.out.println("FAIL -- Unexpected Exception: "
162: + "SQLSTATE(" + se.getSQLState() + ")"
163: + se.getMessage());
164: se.printStackTrace(System.out);
165: }
166:
167: static private void unexpectedException(Exception e) {
168: System.out.println("FAIL -- Unexpected Exception: "
169: + e.getMessage());
170: }
171:
172: }
|