0001: /*
0002:
0003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.scrollCursors2
0004:
0005: Licensed to the Apache Software Foundation (ASF) under one or more
0006: contributor license agreements. See the NOTICE file distributed with
0007: this work for additional information regarding copyright ownership.
0008: The ASF licenses this file to You under the Apache License, Version 2.0
0009: (the "License"); you may not use this file except in compliance with
0010: the License. You may obtain a copy of the License at
0011:
0012: http://www.apache.org/licenses/LICENSE-2.0
0013:
0014: Unless required by applicable law or agreed to in writing, software
0015: distributed under the License is distributed on an "AS IS" BASIS,
0016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017: See the License for the specific language governing permissions and
0018: limitations under the License.
0019:
0020: */
0021:
0022: package org.apache.derbyTesting.functionTests.tests.lang;
0023:
0024: import java.io.IOException;
0025:
0026: import java.sql.CallableStatement;
0027: import java.sql.Connection;
0028: import java.sql.DriverManager;
0029: import java.sql.Statement;
0030: import java.sql.PreparedStatement;
0031: import java.sql.ResultSet;
0032: import java.sql.ResultSetMetaData;
0033: import java.sql.SQLException;
0034: import java.sql.SQLWarning;
0035: import java.sql.Types;
0036: import java.sql.Date;
0037: import java.sql.Time;
0038: import java.sql.Timestamp;
0039:
0040: import org.apache.derby.tools.ij;
0041: import org.apache.derby.tools.JDBCDisplayUtil;
0042: import org.apache.derbyTesting.functionTests.util.TestUtil;
0043:
0044: /**
0045: * Test of scroll cursors.
0046: *
0047: * @author Jerry Brenner
0048: */
0049:
0050: public class scrollCursors2 {
0051:
0052: private static boolean isDerbyNetClient = false;
0053:
0054: public static void main(String[] args) {
0055: boolean passed = true;
0056: Connection conn = null;
0057: Statement s_i_r = null;
0058:
0059: /* Run all parts of this test, and catch any exceptions */
0060: try {
0061: System.out.println("Test scrollCurors2 starting");
0062:
0063: isDerbyNetClient = TestUtil.isDerbyNetClientFramework();
0064: // use the ij utility to read the property file and
0065: // make the initial connection.
0066: ij.getPropertyArg(args);
0067: conn = ij.startJBMS();
0068: cleanUp(conn);
0069: conn.setAutoCommit(false);
0070:
0071: /* Create the table and do any other set-up */
0072: passed = passed && setUpTest(conn);
0073:
0074: // Negative tests with forward only cursors.
0075: passed = passed && forwardOnlyNegative(conn);
0076:
0077: // Positive tests with forward only cursors.
0078: passed = passed && forwardOnlyPositive(conn);
0079:
0080: // Tests with scroll sensitive cursors
0081: passed = passed && scrollSensitiveTest(conn);
0082:
0083: // Positive tests for scroll insensitive cursors
0084: passed = passed && scrollInsensitivePositive(conn);
0085:
0086: // Negative tests for scroll insensitive cursors
0087: passed = passed && scrollInsensitiveNegative(conn);
0088:
0089: // "test" scrolling and CallableStatements
0090: passed = passed && testCallableStatements(conn);
0091:
0092: // tests for PreparedStatement.getMetaData()
0093: passed = passed && getMetaDataTests(conn);
0094:
0095: // test scrollable with different maxRows and fetchSize
0096: passed = passed
0097: && scrollVerifyMaxRowWithFetchSize(conn, 10, 10);
0098: passed = passed
0099: && scrollVerifyMaxRowWithFetchSize(conn, 10, 5);
0100: passed = passed
0101: && scrollVerifyMaxRowWithFetchSize(conn, 10, 0);
0102: passed = passed
0103: && scrollVerifyMaxRowWithFetchSize(conn, 0, 0);
0104: passed = passed
0105: && scrollVerifyMaxRowWithFetchSize(conn, 0, 5);
0106: passed = passed
0107: && scrollVerifyMaxRowWithFetchSize(conn, 0, 10);
0108: passed = passed
0109: && scrollVerifyMaxRowWithFetchSize(conn, 0, 15);
0110:
0111: } catch (SQLException se) {
0112: passed = false;
0113: dumpSQLExceptions(se);
0114: } catch (Throwable e) {
0115: System.out
0116: .println("FAIL -- unexpected exception caught in main():\n");
0117: System.out.println(e.getMessage());
0118: e.printStackTrace();
0119: passed = false;
0120: } finally {
0121:
0122: /* Test is finished - clean up after ourselves */
0123: passed = passed && cleanUp(conn, s_i_r);
0124: }
0125:
0126: if (passed)
0127: System.out.println("PASS");
0128:
0129: System.out.println("Test scrollCursors2 finished");
0130: }
0131:
0132: static private void dumpSQLExceptions(SQLException se) {
0133: System.out.println("FAIL -- unexpected exception");
0134: while (se != null) {
0135: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
0136: se.printStackTrace();
0137: se = se.getNextException();
0138: }
0139: }
0140:
0141: /**
0142: * Set up the test.
0143: *
0144: * This method creates the table used by the rest of the test.
0145: *
0146: * @param conn The Connection
0147: *
0148: * @return true if it succeeds, false if it doesn't
0149: *
0150: * @exception SQLException Thrown if some unexpected error happens
0151: */
0152:
0153: static boolean setUpTest(Connection conn) throws SQLException {
0154: boolean passed = true;
0155: int rows;
0156: PreparedStatement ps;
0157: Statement s_i_r;
0158:
0159: s_i_r = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
0160: ResultSet.CONCUR_READ_ONLY);
0161:
0162: /* Create a table */
0163: s_i_r.execute("create table t (i int, c50 char(50))");
0164:
0165: /* Populate the table */
0166: s_i_r
0167: .execute("insert into t (i) values (2), (3), (4), (5), (6)");
0168: s_i_r.execute("update t set c50 = RTRIM(CAST (i AS CHAR(50)))");
0169: s_i_r.close();
0170:
0171: return passed;
0172: }
0173:
0174: /**
0175: * Negative tests for forward only cursors.
0176: *
0177: * This method tests forward only cursors.
0178: *
0179: * @param conn The Connection
0180: *
0181: * @return true if it succeeds, false if it doesn't
0182: *
0183: * @exception SQLException Thrown if some unexpected error happens
0184: */
0185:
0186: static boolean forwardOnlyNegative(Connection conn)
0187: throws SQLException {
0188: boolean passed = true;
0189: PreparedStatement ps_f_r = null;
0190: ResultSet rs;
0191: SQLWarning warning;
0192: Statement s_f_r = null;
0193:
0194: s_f_r = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
0195: ResultSet.CONCUR_READ_ONLY);
0196: // We should have gotten no warnings and a read only forward only cursor
0197: warning = conn.getWarnings();
0198: while (warning != null) {
0199: System.out.println("warning = " + warning);
0200: warning = warning.getNextWarning();
0201: }
0202: conn.clearWarnings();
0203:
0204: // Verify that setMaxRows(-1) fails
0205: try {
0206: s_f_r.setMaxRows(-1);
0207: // Should never get here
0208: System.out.println("setMaxRows(-1) expected to fail");
0209: passed = false;
0210: } catch (SQLException sqle) {
0211: /* Check to be sure the exception is the one we expect */
0212: if (!isDerbyNetClient) {
0213: passed = passed && checkException(sqle, "XJ063");
0214: } else {
0215: System.out.println(sqle.getMessage());
0216: }
0217: }
0218: // Verify maxRows still 0
0219: if (s_f_r.getMaxRows() != 0) {
0220: System.out.println("getMaxRows() expected to return 0");
0221: passed = false;
0222: }
0223:
0224: // Verify that result set from statement is
0225: // scroll insensitive and read only
0226: rs = s_f_r.executeQuery("select * from t");
0227: if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
0228: System.out.println("cursor type = " + rs.getType()
0229: + ", not " + ResultSet.TYPE_FORWARD_ONLY);
0230: }
0231: if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) {
0232: System.out.println("concurrency = " + rs.getConcurrency()
0233: + ", not " + ResultSet.CONCUR_READ_ONLY);
0234: }
0235:
0236: // Verify that first(), etc. don't work
0237: try {
0238: rs.first();
0239: // Should never get here
0240: System.out.println("first() expected to fail");
0241: passed = false;
0242: } catch (SQLException sqle) {
0243: /* Check to be sure the exception is the one we expect */
0244: if (!isDerbyNetClient) {
0245: passed = passed && checkException(sqle, "XJ061");
0246: } else {
0247: System.out.println(sqle.getMessage());
0248: }
0249:
0250: }
0251: try {
0252: rs.beforeFirst();
0253: // Should never get here
0254: System.out.println("beforeFirst() expected to fail");
0255: passed = false;
0256: } catch (SQLException sqle) {
0257: /* Check to be sure the exception is the one we expect */
0258: if (!isDerbyNetClient) {
0259: passed = passed && checkException(sqle, "XJ061");
0260: } else {
0261: System.out.println(sqle.getMessage());
0262: }
0263:
0264: }
0265: try {
0266: rs.isBeforeFirst();
0267: // Should never get here
0268: System.out.println("isBeforeFirst() expected to fail");
0269: passed = false;
0270: } catch (SQLException sqle) {
0271: /* Check to be sure the exception is the one we expect */
0272: if (!isDerbyNetClient) {
0273: passed = passed && checkException(sqle, "XJ061");
0274: } else {
0275: System.out.println(sqle.getMessage());
0276: }
0277:
0278: }
0279: try {
0280: rs.isAfterLast();
0281: // Should never get here
0282: System.out.println("isAfterLast() expected to fail");
0283: passed = false;
0284: } catch (SQLException sqle) {
0285: /* Check to be sure the exception is the one we expect */
0286: if (!isDerbyNetClient) {
0287: passed = passed && checkException(sqle, "XJ061");
0288: } else {
0289: System.out.println(sqle.getMessage());
0290: }
0291:
0292: }
0293: try {
0294: rs.isFirst();
0295: // Should never get here
0296: System.out.println("isFirst() expected to fail");
0297: passed = false;
0298: } catch (SQLException sqle) {
0299: /* Check to be sure the exception is the one we expect */
0300: if (!isDerbyNetClient) {
0301: passed = passed && checkException(sqle, "XJ061");
0302: } else {
0303: System.out.println(sqle.getMessage());
0304: }
0305: }
0306: try {
0307: rs.isLast();
0308: // Should never get here
0309: System.out.println("isLast() expected to fail");
0310: passed = false;
0311: } catch (SQLException sqle) {
0312: /* Check to be sure the exception is the one we expect */
0313: if (!isDerbyNetClient) {
0314: passed = passed && checkException(sqle, "XJ061");
0315: } else {
0316: System.out.println(sqle.getMessage());
0317: }
0318: }
0319: try {
0320: rs.absolute(1);
0321: // Should never get here
0322: System.out.println("absolute() expected to fail");
0323: passed = false;
0324: } catch (SQLException sqle) {
0325: /* Check to be sure the exception is the one we expect */
0326: if (!isDerbyNetClient) {
0327: passed = passed && checkException(sqle, "XJ061");
0328: } else {
0329: System.out.println(sqle.getMessage());
0330: }
0331: }
0332: try {
0333: rs.relative(1);
0334: // Should never get here
0335: System.out.println("relative() expected to fail");
0336: passed = false;
0337: } catch (SQLException sqle) {
0338: /* Check to be sure the exception is the one we expect */
0339: if (!isDerbyNetClient) {
0340: passed = passed && checkException(sqle, "XJ061");
0341: } else {
0342: System.out.println(sqle.getMessage());
0343: }
0344: }
0345:
0346: // setFetchDirection should fail
0347: try {
0348: rs.setFetchDirection(ResultSet.FETCH_FORWARD);
0349: // Should never get here
0350: System.out.println("setFetchDirection() expected to fail");
0351: passed = false;
0352: } catch (SQLException sqle) {
0353: /* Check to be sure the exception is the one we expect */
0354: if (!isDerbyNetClient) {
0355: passed = passed && checkException(sqle, "XJ061");
0356: } else {
0357: System.out.println(sqle.getMessage());
0358: }
0359: }
0360:
0361: /* Book says that getFetchDirection(), getFetchSize() and
0362: * setFetchSize() are all okay.
0363: */
0364: if ((rs.getFetchSize() != 1 && !isDerbyNetClient)
0365: || (rs.getFetchSize() != 0 && isDerbyNetClient)) {
0366: if (!isDerbyNetClient) {
0367: System.out
0368: .println("getFetchSize() expected to return 1");
0369: } else {
0370: System.out
0371: .println("getFetchSize() expected to return 0");
0372: }
0373: passed = false;
0374: }
0375: rs.setFetchSize(5);
0376: if (rs.getFetchSize() != 5) {
0377: System.out.println("getFetchSize() expected to return 5");
0378: passed = false;
0379: }
0380:
0381: if (rs.getFetchDirection() != ResultSet.FETCH_FORWARD) {
0382: System.out
0383: .println("getFetchDirection() expected to return FETCH_FORWARD, not "
0384: + rs.getFetchDirection());
0385: passed = false;
0386: }
0387:
0388: rs.close();
0389: s_f_r.close();
0390:
0391: ps_f_r = conn
0392: .prepareStatement("select * from t",
0393: ResultSet.TYPE_FORWARD_ONLY,
0394: ResultSet.CONCUR_READ_ONLY);
0395: // We should have gotten no warnings and a read only forward only cursor
0396: warning = conn.getWarnings();
0397: while (warning != null) {
0398: System.out.println("warning = " + warning);
0399: warning = warning.getNextWarning();
0400: }
0401: conn.clearWarnings();
0402:
0403: // Verify that result set from statement is
0404: // scroll insensitive and read only
0405: rs = ps_f_r.executeQuery();
0406: if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
0407: System.out.println("cursor type = " + rs.getType()
0408: + ", not " + ResultSet.TYPE_FORWARD_ONLY);
0409: }
0410: if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) {
0411: System.out.println("concurrency = " + rs.getConcurrency()
0412: + ", not " + ResultSet.CONCUR_READ_ONLY);
0413: }
0414:
0415: // Verify that first() doesn't work
0416: try {
0417: rs.first();
0418: // Should never get here
0419: System.out.println("first() expected to fail");
0420: passed = false;
0421: } catch (SQLException sqle) {
0422: /* Check to be sure the exception is the one we expect */
0423: if (!isDerbyNetClient) {
0424: passed = passed && checkException(sqle, "XJ061");
0425: } else {
0426: System.out.println(sqle.getMessage());
0427: }
0428:
0429: }
0430: rs.close();
0431: ps_f_r.close();
0432:
0433: return passed;
0434: }
0435:
0436: /**
0437: * Positive tests for forward only cursors.
0438: *
0439: * This method tests forward only cursors.
0440: *
0441: * @param conn The Connection
0442: *
0443: * @return true if it succeeds, false if it doesn't
0444: *
0445: * @exception SQLException Thrown if some unexpected error happens
0446: */
0447:
0448: static boolean forwardOnlyPositive(Connection conn)
0449: throws SQLException {
0450: boolean passed = true;
0451: ResultSet rs;
0452: SQLWarning warning;
0453: Statement s_f_r = null;
0454:
0455: s_f_r = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
0456: ResultSet.CONCUR_READ_ONLY);
0457: // We should have gotten no warnings and a read only forward only cursor
0458: warning = conn.getWarnings();
0459: while (warning != null) {
0460: System.out.println("warning = " + warning);
0461: warning = warning.getNextWarning();
0462: }
0463: conn.clearWarnings();
0464:
0465: // Verify that setMaxRows(4) succeeds
0466: s_f_r.setMaxRows(5);
0467: if (s_f_r.getMaxRows() != 5) {
0468: System.out.println("getMaxRows() expected to return 5");
0469: passed = false;
0470: }
0471: rs = s_f_r.executeQuery("values 1, 2, 3, 4, 5, 6");
0472: if (rs == null) {
0473: System.out.println("rs expected to be non-null.");
0474: passed = false;
0475: }
0476: // Iterate straight thru RS, expect only 5 rows.
0477: for (int index = 1; index < 6; index++) {
0478: if (!rs.next()) {
0479: System.out
0480: .println("rs.next() failed, index = " + index);
0481: passed = false;
0482: break;
0483: }
0484: }
0485: // We should not see another row (only 5, not 6)
0486: if (rs.next()) {
0487: System.out
0488: .println("rs.next() failed, should not have seen 6th row.");
0489: passed = false;
0490: }
0491: rs.close();
0492: s_f_r.close();
0493: return passed;
0494: }
0495:
0496: /**
0497: * Scroll sensitive cursor tests
0498: *
0499: * This method tests scroll sensitive cursors.
0500: * (Not implemented, so we should get back
0501: * scroll insensitive curors with read only concurrency.)
0502: *
0503: * @param conn The Connection
0504: *
0505: * @return true if it succeeds, false if it doesn't
0506: *
0507: * @exception SQLException Thrown if some unexpected error happens
0508: */
0509:
0510: static boolean scrollSensitiveTest(Connection conn)
0511: throws SQLException {
0512: ResultSet rs;
0513: SQLWarning warning;
0514: Statement s_s_r = null; // sensitive, read only
0515: Statement s_s_u = null; // sensitive, updatable
0516:
0517: s_s_r = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
0518: ResultSet.CONCUR_READ_ONLY);
0519:
0520: // We should have gotten a warning and a scroll insensitive cursor
0521: warning = conn.getWarnings();
0522: while (warning != null) {
0523: System.out.println("warning = " + warning);
0524: warning = warning.getNextWarning();
0525: }
0526: conn.clearWarnings();
0527:
0528: // Verify that result set from statement is
0529: // scroll insensitive and read only
0530: rs = s_s_r.executeQuery("select * from t");
0531: if (rs.getType() != ResultSet.TYPE_SCROLL_INSENSITIVE) {
0532: System.out.println("cursor type = " + rs.getType()
0533: + ", not " + ResultSet.TYPE_SCROLL_INSENSITIVE);
0534: }
0535: if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) {
0536: System.out.println("concurrency = " + rs.getConcurrency()
0537: + ", not " + ResultSet.CONCUR_READ_ONLY);
0538: }
0539: rs.close();
0540:
0541: // Close the statement
0542: s_s_r.close();
0543:
0544: s_s_u = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
0545: ResultSet.CONCUR_UPDATABLE);
0546: // We should have gotten 1 warning and a updatable scroll
0547: // insensitive cursor.
0548: warning = conn.getWarnings();
0549: while (warning != null) {
0550: System.out.println("warning = " + warning);
0551: warning = warning.getNextWarning();
0552: }
0553: conn.clearWarnings();
0554:
0555: // Verify that result set from statement is
0556: // scroll insensitive and read only
0557: rs = s_s_u.executeQuery("select * from t");
0558: if (rs.getType() != ResultSet.TYPE_SCROLL_INSENSITIVE) {
0559: System.out.println("cursor type = " + rs.getType()
0560: + ", not " + ResultSet.TYPE_SCROLL_INSENSITIVE);
0561: }
0562: if (rs.getConcurrency() != ResultSet.CONCUR_UPDATABLE) {
0563: System.out.println("concurrency = " + rs.getConcurrency()
0564: + ", not " + ResultSet.CONCUR_UPDATABLE);
0565: }
0566: rs.close();
0567:
0568: return true;
0569: }
0570:
0571: /**
0572: * Positive tests for scroll insensitive cursor.
0573: *
0574: * @param conn The connection to use.
0575: *
0576: * @return Whether or not we were successful.
0577: *
0578: * @exception SQLException Thrown if some unexpected error happens
0579: */
0580: static boolean scrollInsensitivePositive(Connection conn)
0581: throws SQLException {
0582: boolean passed = true;
0583: PreparedStatement ps_i_r = null;
0584: ResultSet rs;
0585: SQLWarning warning;
0586: Statement s_i_r = null; // insensitive, read only
0587:
0588: s_i_r = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
0589: ResultSet.CONCUR_READ_ONLY);
0590:
0591: // We should not have gotten any warnings
0592: // and should have gotten a scroll insensitive cursor
0593: warning = conn.getWarnings();
0594: while (warning != null) {
0595: System.out.println("unexpected warning = " + warning);
0596: warning = warning.getNextWarning();
0597: passed = false;
0598: }
0599: conn.clearWarnings();
0600:
0601: // run a query
0602: rs = s_i_r.executeQuery("select * from t");
0603: // verify scroll insensitive and read only
0604: if (rs.getType() != ResultSet.TYPE_SCROLL_INSENSITIVE) {
0605: System.out
0606: .println("rs.getType() expected to return TYPE_SCROLL_INSENSITIVE, not "
0607: + rs.getType());
0608: passed = false;
0609: }
0610: if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) {
0611: System.out
0612: .println("rs.getConcurrency() expected to return CONCUR_READ_ONLY, not "
0613: + rs.getConcurrency());
0614: passed = false;
0615: }
0616:
0617: // We should be positioned before the 1st row
0618: if (!rs.isBeforeFirst()) {
0619: System.out.println("expected to be before the 1st row");
0620: passed = false;
0621: }
0622: if (rs.absolute(0)) {
0623: System.out.println("absolute(0) expected to return false");
0624: passed = false;
0625: }
0626: if (!rs.isBeforeFirst()) {
0627: System.out
0628: .println("still expected to be before the 1st row");
0629: passed = false;
0630: }
0631: // go to first row
0632: if (!rs.first()) {
0633: System.out.println("expected first() to succeed");
0634: passed = false;
0635: }
0636: if (rs.getInt(1) != 2) {
0637: System.out
0638: .println("rs.getInt(1) expected to return 2, not "
0639: + rs.getInt(1));
0640: passed = false;
0641: }
0642: if (!rs.isFirst()) {
0643: System.out.println("expected to be on the 1st row");
0644: passed = false;
0645: }
0646: // move to before first
0647: rs.beforeFirst();
0648: if (!rs.isBeforeFirst()) {
0649: System.out.println("expected to be before the 1st row");
0650: passed = false;
0651: }
0652: // move to last row
0653: if (!rs.last()) {
0654: System.out.println("expected last() to succeed");
0655: passed = false;
0656: }
0657: if (!rs.isLast()) {
0658: System.out.println("expected to be on the last row");
0659: passed = false;
0660: }
0661: if (rs.isAfterLast()) {
0662: System.out.println("not expected to be after the last row");
0663: passed = false;
0664: }
0665: if (rs.getInt(1) != 6) {
0666: System.out
0667: .println("rs.getInt(1) expected to return 6, not "
0668: + rs.getInt(1));
0669: passed = false;
0670: }
0671: if (rs.next()) {
0672: System.out.println("not expected to find another row");
0673: passed = false;
0674: }
0675: if (!rs.isAfterLast()) {
0676: System.out.println("expected to be after the last row");
0677: passed = false;
0678: }
0679:
0680: // We're after the last row, verify that only isAfterLast()
0681: // returns true
0682: if (rs.isLast()) {
0683: System.out.println("not expected to be on the last row");
0684: passed = false;
0685: }
0686: if (rs.isFirst()) {
0687: System.out.println("not expected to be on the first row");
0688: passed = false;
0689: }
0690: if (rs.isBeforeFirst()) {
0691: System.out
0692: .println("not expected to be before the first row");
0693: passed = false;
0694: }
0695:
0696: // get/setFetchDirection()
0697: if (rs.getFetchDirection() != ResultSet.FETCH_FORWARD) {
0698: System.out
0699: .println("getFetchDirection() expected to return FETCH_FORWARD, not "
0700: + rs.getFetchDirection());
0701: passed = false;
0702: }
0703: rs.setFetchDirection(ResultSet.FETCH_UNKNOWN);
0704: if (rs.getFetchDirection() != ResultSet.FETCH_UNKNOWN) {
0705: System.out
0706: .println("getFetchDirection() expected to return FETCH_UNKNOWN, not "
0707: + rs.getFetchDirection());
0708: passed = false;
0709: }
0710:
0711: // get/setFetchSize()
0712: if ((rs.getFetchSize() != 1 && !isDerbyNetClient)
0713: || (rs.getFetchSize() != 64 && isDerbyNetClient)) {
0714: if (!isDerbyNetClient) {
0715: System.out
0716: .println("getFetchSize() expected to return 1, not "
0717: + rs.getFetchSize());
0718: } else {
0719: System.out
0720: .println("getFetchSize() expected to return 64, not "
0721: + rs.getFetchSize());
0722: }
0723: passed = false;
0724: }
0725: rs.setFetchSize(5);
0726: if (rs.getFetchSize() != 5) {
0727: System.out
0728: .println("getFetchSize() expected to return 5, not "
0729: + rs.getFetchSize());
0730: passed = false;
0731: }
0732: // setFetchSize() to 0 should have no effect.
0733: // for client server, fetchSize should have to 64
0734: rs.setFetchSize(0);
0735: if ((rs.getFetchSize() != 5 && !isDerbyNetClient)
0736: || (rs.getFetchSize() != 64 && isDerbyNetClient)) {
0737: if (!isDerbyNetClient) {
0738: System.out
0739: .println("getFetchSize() expected to return 5, not "
0740: + rs.getFetchSize());
0741: } else {
0742: System.out
0743: .println("getFetchSize() expected to return 64, not "
0744: + rs.getFetchSize());
0745: }
0746:
0747: passed = false;
0748: }
0749: // done
0750: rs.close();
0751:
0752: // Empty result set tests (DERBY-992)
0753: rs = s_i_r.executeQuery("select * from t where 1=0");
0754: rs.afterLast();
0755: if (rs.isAfterLast()) {
0756: System.out
0757: .println("afterLast() on empty RS should be no-op");
0758: }
0759:
0760: rs.beforeFirst();
0761: if (rs.isBeforeFirst()) {
0762: System.out
0763: .println("beforeFirst() on empty RS should be no-op");
0764: }
0765:
0766: rs.close();
0767:
0768: ps_i_r = conn.prepareStatement("select * from t",
0769: ResultSet.TYPE_SCROLL_INSENSITIVE,
0770: ResultSet.CONCUR_READ_ONLY);
0771:
0772: // We should not have gotten any warnings
0773: // and should have gotten a prepared scroll insensitive cursor
0774: warning = conn.getWarnings();
0775: while (warning != null) {
0776: System.out.println("unexpected warning = " + warning);
0777: warning = warning.getNextWarning();
0778: passed = false;
0779: }
0780: conn.clearWarnings();
0781:
0782: rs = ps_i_r.executeQuery();
0783: // make sure it's scrollable
0784: rs.last();
0785: rs.close();
0786: ps_i_r.close();
0787:
0788: // Check setMaxRows()/getMaxRows()
0789: if (s_i_r.getMaxRows() != 0) {
0790: System.out.println("getMaxRows() expected to return 0");
0791: passed = false;
0792: }
0793: s_i_r.setMaxRows(5);
0794: if (s_i_r.getMaxRows() != 5) {
0795: System.out.println("getMaxRows() expected to return 5");
0796: passed = false;
0797: }
0798: rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6");
0799: if (rs == null) {
0800: System.out.println("rs expected to be non-null.");
0801: passed = false;
0802: }
0803: // Iterate straight thru RS, expect only 5 rows.
0804: for (int index = 1; index < 6; index++) {
0805: if (!rs.next()) {
0806: System.out
0807: .println("rs.next() failed, index = " + index);
0808: passed = false;
0809: break;
0810: }
0811: }
0812: // We should not see another row (only 5, not 6)
0813: if (rs.next()) {
0814: System.out
0815: .println("rs.next() failed, should not have seen 6th row.");
0816: passed = false;
0817: }
0818: rs.close();
0819: // Jump around and verify setMaxRows() works.
0820: rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6");
0821: if (rs == null) {
0822: System.out.println("rs expected to be non-null.");
0823: passed = false;
0824: }
0825: if (!rs.last()) {
0826: System.out.println("rs.last() failed.");
0827: passed = false;
0828: }
0829: // Iterate backwards thru RS, expect only 4 more (5 total) rows.
0830: for (int index = 1; index < 5; index++) {
0831: if (!rs.previous()) {
0832: System.out.println("rs.previous() failed, index = "
0833: + index);
0834: passed = false;
0835: break;
0836: }
0837: }
0838: // We should not see another row (only 5, not 6)
0839: if (rs.previous()) {
0840: System.out
0841: .println("rs.previous() failed, should not have seen 6th row.");
0842: passed = false;
0843: }
0844: rs.close();
0845: rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6");
0846: if (rs == null) {
0847: System.out.println("rs expected to be non-null.");
0848: passed = false;
0849: }
0850: rs.afterLast();
0851: // Iterate backwards thru RS, expect only 5 rows.
0852: for (int index = 1; index < 6; index++) {
0853: if (!rs.previous()) {
0854: System.out.println("rs.previous() failed, index = "
0855: + index);
0856: passed = false;
0857: break;
0858: }
0859: }
0860: // We should not see another row (only 5, not 6)
0861: if (rs.previous()) {
0862: System.out
0863: .println("rs.previous() failed, should not have seen 6th row.");
0864: passed = false;
0865: }
0866: rs.close();
0867: // Verify setting maxRows back to 0 works.
0868: s_i_r.setMaxRows(0);
0869: rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6");
0870: if (rs == null) {
0871: System.out.println("rs expected to be non-null.");
0872: passed = false;
0873: }
0874: // Iterate straight thru RS, expect 6 rows.
0875: for (int index = 1; index < 7; index++) {
0876: if (!rs.next()) {
0877: System.out
0878: .println("rs.next() failed, index = " + index);
0879: passed = false;
0880: break;
0881: }
0882: }
0883: // We should not see another row
0884: if (rs.next()) {
0885: System.out
0886: .println("rs.next() failed, should not have seen another row.");
0887: passed = false;
0888: }
0889: rs.close();
0890:
0891: return passed;
0892: }
0893:
0894: /**
0895: * Negative tests for scroll insensitive cursor.
0896: *
0897: * @param conn The connection to use.
0898: *
0899: * @return Whether or not we were successful.
0900: *
0901: * @exception SQLException Thrown if some unexpected error happens
0902: */
0903: static boolean scrollInsensitiveNegative(Connection conn)
0904: throws SQLException {
0905: boolean passed = true;
0906: ResultSet rs;
0907: SQLWarning warning;
0908: Statement s_i_r = null; // insensitive, read only
0909:
0910: s_i_r = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
0911: ResultSet.CONCUR_READ_ONLY);
0912:
0913: // We should not have gotten any warnings
0914: // and should have gotten a scroll insensitive cursor
0915: warning = conn.getWarnings();
0916: while (warning != null) {
0917: System.out.println("unexpected warning = " + warning);
0918: warning = warning.getNextWarning();
0919: passed = false;
0920: }
0921: conn.clearWarnings();
0922:
0923: // Verify that setMaxRows(-1) fails
0924: try {
0925: s_i_r.setMaxRows(-1);
0926: // Should never get here
0927: System.out.println("setMaxRows(-1) expected to fail");
0928: passed = false;
0929: } catch (SQLException sqle) {
0930: /* Check to be sure the exception is the one we expect */
0931: if (!isDerbyNetClient) {
0932: passed = passed && checkException(sqle, "XJ063");
0933: } else {
0934: System.out.println(sqle.getMessage());
0935: }
0936:
0937: }
0938: // Verify maxRows still 0
0939: if (s_i_r.getMaxRows() != 0) {
0940: System.out.println("getMaxRows() expected to return 0");
0941: passed = false;
0942: }
0943:
0944: // Empty result set
0945: rs = s_i_r.executeQuery("select * from t where 1=0");
0946: // isBeforeFirst() and isAfterLast() should always return false
0947: // when result set is empty
0948: if (rs.isBeforeFirst()) {
0949: System.out
0950: .println("isBeforeFirst() expected to return false on empty result set");
0951: passed = false;
0952: }
0953: if (rs.next()) {
0954: System.out
0955: .println("rs.next() expected to show result set is empty");
0956: passed = false;
0957: }
0958: if (rs.previous()) {
0959: System.out
0960: .println("rs.previous() expected to show result set is empty");
0961: passed = false;
0962: }
0963: if (rs.isAfterLast()) {
0964: System.out
0965: .println("isAfterLast() expected to return false on empty result set");
0966: passed = false;
0967: }
0968: if (rs.isFirst()) {
0969: System.out
0970: .println("isFirst() expected to return false on empty result set");
0971: passed = false;
0972: }
0973: if (rs.isLast()) {
0974: System.out
0975: .println("isLast() expected to return false on empty result set");
0976: passed = false;
0977: }
0978:
0979: if (rs.relative(0)) {
0980: System.out
0981: .println("relative(0) expected to return false on empty result set");
0982: passed = false;
0983: }
0984:
0985: if (rs.relative(1)) {
0986: System.out
0987: .println("relative(1) expected to return false on empty result set");
0988: passed = false;
0989: }
0990:
0991: if (rs.relative(-1)) {
0992: System.out
0993: .println("relative(-1) expected to return false on empty result set");
0994: passed = false;
0995: }
0996:
0997: if (rs.absolute(0)) {
0998: System.out
0999: .println("absolute(0) expected to return false on empty result set");
1000: passed = false;
1001: }
1002: if (rs.absolute(1)) {
1003: System.out
1004: .println("absolute(1) expected to return false on empty result set");
1005: passed = false;
1006: }
1007:
1008: if (rs.absolute(-1)) {
1009: System.out
1010: .println("absolute(-1) expected to return false on empty result set");
1011: passed = false;
1012: }
1013:
1014: rs.close();
1015: // End of empty result set tests
1016:
1017: // Non-empty result set
1018: rs = s_i_r.executeQuery("select * from t");
1019: // Negative fetch size
1020: try {
1021: rs.setFetchSize(-5);
1022: System.out.println("setFetchSize(-5) expected to fail");
1023: passed = false;
1024: } catch (SQLException sqle) {
1025: /* Check to be sure the exception is the one we expect */
1026: if (!isDerbyNetClient) {
1027: passed = passed && checkException(sqle, "XJ062");
1028: } else {
1029: System.out.println(sqle.getMessage());
1030: }
1031:
1032: }
1033:
1034: s_i_r.close();
1035:
1036: return passed;
1037: }
1038:
1039: /**
1040: * CallableStatement tests.
1041: *
1042: * @param conn The Connection
1043: *
1044: * @return true if it succeeds, false if it doesn't
1045: *
1046: * @exception SQLException Thrown if some unexpected error happens
1047: */
1048:
1049: public static boolean testCallableStatements(Connection conn)
1050: throws SQLException {
1051: boolean passed = true;
1052: int warningCount = 0;
1053: SQLWarning warning;
1054: CallableStatement cs_s_r = null; // sensitive, read only
1055: CallableStatement cs_s_u = null; // sensitive, updatable
1056: CallableStatement cs_i_r = null; // insensitive, read only
1057: CallableStatement cs_f_r = null; // forward only, read only
1058:
1059: cs_s_r = conn.prepareCall("values cast (? as Integer)",
1060: ResultSet.TYPE_SCROLL_SENSITIVE,
1061: ResultSet.CONCUR_READ_ONLY);
1062:
1063: // We should have gotten 1 warnings
1064: warning = conn.getWarnings();
1065: while (warning != null) {
1066: System.out.println("warning = " + warning);
1067: warning = warning.getNextWarning();
1068: warningCount++;
1069: }
1070: if (warningCount != 1) {
1071: System.out.println("warningCount expected to be 1, not "
1072: + warningCount);
1073: passed = false;
1074: }
1075: conn.clearWarnings();
1076: cs_s_r.close();
1077:
1078: cs_s_u = conn.prepareCall("values cast (? as Integer)",
1079: ResultSet.TYPE_SCROLL_SENSITIVE,
1080: ResultSet.CONCUR_UPDATABLE);
1081:
1082: // We should have gotten 2 warnings
1083: warningCount = 0;
1084: warning = conn.getWarnings();
1085: while (warning != null) {
1086: System.out.println("warning = " + warning);
1087: warning = warning.getNextWarning();
1088: warningCount++;
1089: }
1090: // SCROLL_INSENSITIVE and UPDATABLE implemented
1091: if (warningCount != 1) {
1092: System.out.println("warningCount expected to be 1, not "
1093: + warningCount);
1094: passed = false;
1095: }
1096: conn.clearWarnings();
1097: cs_s_u.close();
1098:
1099: cs_i_r = conn.prepareCall("values cast (? as Integer)",
1100: ResultSet.TYPE_SCROLL_INSENSITIVE,
1101: ResultSet.CONCUR_READ_ONLY);
1102:
1103: // We should have gotten 0 warnings
1104: warningCount = 0;
1105: warning = conn.getWarnings();
1106: while (warning != null) {
1107: System.out.println("warning = " + warning);
1108: warning = warning.getNextWarning();
1109: warningCount++;
1110: }
1111: if (warningCount != 0) {
1112: System.out.println("warningCount expected to be 0, not "
1113: + warningCount);
1114: passed = false;
1115: }
1116: conn.clearWarnings();
1117: cs_i_r.close();
1118:
1119: cs_f_r = conn
1120: .prepareCall("values cast (? as Integer)",
1121: ResultSet.TYPE_FORWARD_ONLY,
1122: ResultSet.CONCUR_READ_ONLY);
1123:
1124: // We should have gotten 0 warnings
1125: warningCount = 0;
1126: warning = conn.getWarnings();
1127: while (warning != null) {
1128: System.out.println("warning = " + warning);
1129: warning = warning.getNextWarning();
1130: warningCount++;
1131: }
1132: if (warningCount != 0) {
1133: System.out.println("warningCount expected to be 0, not "
1134: + warningCount);
1135: passed = false;
1136: }
1137: conn.clearWarnings();
1138: cs_f_r.close();
1139:
1140: return passed;
1141: }
1142:
1143: /**
1144: * Tests for PreparedStatement.getMetaData().
1145: *
1146: * @param conn The connection to use.
1147: *
1148: * @return Whether or not we were successful.
1149: *
1150: * @exception SQLException Thrown if some unexpected error happens
1151: */
1152: static boolean getMetaDataTests(Connection conn)
1153: throws SQLException {
1154: boolean passed = true;
1155: PreparedStatement ps_f_r = null; // forward only, read only
1156: ResultSet rs;
1157: ResultSetMetaData rsmd_ps;
1158: ResultSetMetaData rsmd_rs;
1159: SQLWarning warning;
1160:
1161: ps_f_r = conn
1162: .prepareStatement("select c50, i, 43 from t",
1163: ResultSet.TYPE_FORWARD_ONLY,
1164: ResultSet.CONCUR_READ_ONLY);
1165:
1166: rsmd_ps = ps_f_r.getMetaData();
1167: if (rsmd_ps == null) {
1168: System.out.println("rsmd_ps expected to be non-null");
1169: return false;
1170: }
1171:
1172: // Now get meta data from result set
1173: rs = ps_f_r.executeQuery();
1174: rsmd_rs = rs.getMetaData();
1175: if (rsmd_rs == null) {
1176: System.out.println("rsmd_rs expected to be non-null");
1177: return false;
1178: }
1179:
1180: // check column count
1181: if (rsmd_ps.getColumnCount() != rsmd_rs.getColumnCount()) {
1182: System.out.println("column count expected to be same, not "
1183: + rsmd_ps.getColumnCount() + " and "
1184: + rsmd_rs.getColumnCount());
1185: passed = false;
1186: }
1187:
1188: // get column name for 2nd column
1189: if (!rsmd_ps.getColumnName(2).equals(rsmd_rs.getColumnName(2))) {
1190: System.out.println("column name expected to be same, not "
1191: + rsmd_ps.getColumnName(2) + " and "
1192: + rsmd_rs.getColumnName(2));
1193: passed = false;
1194: }
1195:
1196: if (rsmd_ps.isReadOnly(2) != rsmd_rs.isReadOnly(2)) {
1197: System.out.println("isReadOnly() expected to be same, not "
1198: + rsmd_ps.isReadOnly(2) + " and "
1199: + rsmd_rs.isReadOnly(2));
1200: passed = false;
1201: }
1202:
1203: rs.close();
1204: ps_f_r.close();
1205:
1206: return passed;
1207: }
1208:
1209: /**
1210: * Tests for maxRow and fetchSize with scrollable cursors
1211: *
1212: * @param conn The connection to use.
1213: * @param maxRows The maxRows value to use
1214: * @param fetchSize The fetchSize value to use
1215: *
1216: * @return Whether or not we were successful.
1217: *
1218: * @exception SQLException Thrown if some unexpected error happens
1219: */
1220: private static boolean scrollVerifyMaxRowWithFetchSize(
1221: Connection conn, int maxRows, int fetchSize) {
1222: ResultSet rs;
1223: boolean passed = true;
1224: Statement s_i_r = null;
1225:
1226: try {
1227: s_i_r = conn.createStatement(
1228: ResultSet.TYPE_SCROLL_INSENSITIVE,
1229: ResultSet.CONCUR_READ_ONLY);
1230: s_i_r.setMaxRows(maxRows);
1231:
1232: // Execute query
1233: rs = s_i_r
1234: .executeQuery("values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15");
1235: rs.setFetchSize(fetchSize);
1236:
1237: // this should not affect the ResultSet because
1238: s_i_r.setMaxRows(2);
1239: if (maxRows == 0)
1240: maxRows = 15;
1241:
1242: if (rs == null) {
1243: System.out.println("rs expected to be non-null.");
1244: passed = false;
1245: }
1246: // Start from before first
1247: // Iterate straight thru RS, expect only maxRows rows.
1248: for (int index = 1; index < maxRows + 1; index++) {
1249: if (!rs.next()) {
1250: System.out.println("rs.next() failed, index = "
1251: + index);
1252: passed = false;
1253: break;
1254: } else {
1255: if (index != rs.getInt(1)) {
1256: System.out.println("Expected: " + index
1257: + " not: " + rs.getInt(1));
1258: }
1259: }
1260: }
1261: // We should not see another row (only maxRows, not total)
1262: if (rs.next()) {
1263: System.out.println("Error with maxRows = " + maxRows
1264: + " and fetchSize = " + fetchSize + "\n"
1265: + "rs.next() failed, should not have seen "
1266: + (maxRows + 1) + "th row.");
1267: passed = false;
1268: }
1269:
1270: // Start from first and verify maxRows
1271: if (!rs.first()) {
1272: System.out.println("rs.first() failed.");
1273: passed = false;
1274: }
1275: // Iterate forward thru RS, expect only (maxRows - 1) more rows.
1276: for (int index = 1; index < maxRows; index++) {
1277: if (!rs.next()) {
1278: System.out.println("rs.previous() failed, index = "
1279: + index);
1280: passed = false;
1281: break;
1282: } else {
1283: if ((index + 1) != rs.getInt(1))
1284: System.out.println("Error with maxRows = "
1285: + maxRows + " and fetchSize = "
1286: + fetchSize + "\n"
1287: + "Error with maxRows = " + maxRows
1288: + " and fetchSize = " + fetchSize
1289: + "\n" + "Expected: " + (index + 1)
1290: + " not: " + rs.getInt(1));
1291: }
1292: }
1293: // We should not see another row (only maxRows, not total)
1294: if (rs.next()) {
1295: System.out.println("Error with maxRows = " + maxRows
1296: + " and fetchSize = " + fetchSize + "\n"
1297: + "rs.next() failed, should not have seen "
1298: + (maxRows + 1) + "th row.");
1299: passed = false;
1300: }
1301:
1302: // Start from afterLast and verify maxRows
1303: rs.afterLast();
1304: // Iterate backwards thru RS, expect only (maxRows - 1) rows.
1305: for (int index = 1; index < maxRows + 1; index++) {
1306: if (!rs.previous()) {
1307: System.out.println("rs.previous() failed, index = "
1308: + index);
1309: passed = false;
1310: break;
1311: } else {
1312: if (((maxRows - index) + 1) != rs.getInt(1)) {
1313: System.out.println("Error with maxRows = "
1314: + maxRows + " and fetchSize = "
1315: + fetchSize + "\n" + "Expected: "
1316: + ((maxRows - index) + 1) + " not: "
1317: + rs.getInt(1));
1318: }
1319: }
1320: }
1321: // We should not see another row (only maxRows, not total)
1322: if (rs.previous()) {
1323: System.out.println("Error with maxRows = " + maxRows
1324: + " and fetchSize = " + fetchSize + "\n"
1325: + "rs.previous() failed, should not have seen "
1326: + (maxRows + 1) + "th row.");
1327: passed = false;
1328: }
1329:
1330: // Start from last and verify maxRows
1331: if (!rs.last()) {
1332: System.out.println("rs.last() failed.");
1333: passed = false;
1334: }
1335: // Iterate backwards thru RS, expect only (maxRows - 1) more rows.
1336: for (int index = 1; index < maxRows; index++) {
1337: if (!rs.previous()) {
1338: System.out.println("rs.previous() failed, index = "
1339: + index);
1340: passed = false;
1341: break;
1342: } else {
1343: if ((maxRows - index) != rs.getInt(1)) {
1344: System.out.println("Error with maxRows = "
1345: + maxRows + " and fetchSize = "
1346: + fetchSize + "\n" + "Expected: "
1347: + (maxRows - index) + " not: "
1348: + rs.getInt(1));
1349: }
1350: }
1351: }
1352: // We should not see another row (only 5, not 6)
1353: if (rs.previous()) {
1354: System.out.println("Error with maxRows = " + maxRows
1355: + " and fetchSize = " + fetchSize + "\n"
1356: + "rs.previous() failed, should not have seen "
1357: + (maxRows + 1) + "th row.");
1358: passed = false;
1359: }
1360:
1361: rs.last();
1362: int rows = rs.getRow();
1363:
1364: rs.absolute(rows / 2);
1365: if (rs.relative(-1 * (rows))) {
1366: System.out
1367: .println("relative("
1368: + -1
1369: * (rows)
1370: + ") should return false, position outside of the resultSet");
1371:
1372: }
1373: if (!rs.isBeforeFirst()) {
1374: System.out.println("isBeforeFirst should be true");
1375: }
1376:
1377: rs.absolute(rows / 2);
1378: if (rs.relative(rows)) {
1379: System.out
1380: .println("relative("
1381: + (rows)
1382: + ") should return false, position outside of the resultSet");
1383: }
1384: if (!rs.isAfterLast()) {
1385: System.out.println("isAfterLast should be true");
1386: }
1387: rs.absolute(rows / 2);
1388: if (rs.absolute(rows + 1)) {
1389: System.out
1390: .println("absolute("
1391: + (rows + 1)
1392: + ") should return false, position outside of the resultSet");
1393: System.out.println("Current row: " + rs.getInt(1));
1394: }
1395: if (!rs.isAfterLast()) {
1396: System.out.println("isAfterLast should be true");
1397: }
1398: rs.absolute(rows / 2);
1399: if (rs.absolute((-1) * (rows + 1))) {
1400: System.out
1401: .println("absolute("
1402: + (((-1) * (rows + 1)))
1403: + ") should return false, position outside of the resultSet");
1404: System.out.println("Current row: " + rs.getInt(1));
1405: }
1406: if (!rs.isBeforeFirst()) {
1407: System.out.println("isBeforeFirst should be true");
1408: }
1409:
1410: rs.close();
1411:
1412: } catch (SQLException e) {
1413: System.out.println(e.getMessage());
1414: }
1415:
1416: return passed;
1417: }
1418:
1419: /**
1420: * Check to make sure that the given SQLException is an exception
1421: * with the expected sqlstate.
1422: *
1423: * @param e The SQLException to check
1424: * @param SQLState The sqlstate to look for
1425: *
1426: * @return true means the exception is the expected one
1427: */
1428:
1429: private static boolean checkException(SQLException e,
1430: String SQLState) {
1431: String state;
1432: String nextState;
1433: SQLException next;
1434: boolean passed = true;
1435:
1436: state = e.getSQLState();
1437:
1438: if (!SQLState.equals(state)) {
1439: System.out.println("FAIL -- unexpected exception " + e
1440: + "sqlstate: " + state + SQLState);
1441: passed = false;
1442: }
1443:
1444: return passed;
1445: }
1446:
1447: /**
1448: * Clean up after ourselves when testing is done.
1449: *
1450: * @param conn The Connection
1451: * @param s A Statement on the Connection
1452: *
1453: * @return true if it succeeds, false if it doesn't
1454: *
1455: * @exception SQLException Thrown if some unexpected error happens
1456: */
1457:
1458: static boolean cleanUp(Connection conn, Statement s) {
1459: try {
1460: /* Drop the table we created */
1461: if (s == null) {
1462: // well, then, we'll have to restart
1463: s = conn.createStatement();
1464: }
1465: s.execute("drop table t");
1466: /* Close the connection */
1467: conn.commit();
1468: conn.close();
1469: } catch (Throwable e) {
1470: System.out
1471: .println("FAIL -- unexpected exception caught in cleanup()");
1472: JDBCDisplayUtil.ShowException(System.out, e);
1473: return false;
1474: }
1475:
1476: return true;
1477: }
1478:
1479: /*
1480: * cleanup also before test start, just in case
1481: * @param conn The Connection
1482: */
1483: static void cleanUp(Connection conn) throws SQLException {
1484: Statement cleanupStmt = conn.createStatement();
1485: String[] testObjects = { "table t" };
1486: TestUtil.cleanUpTest(cleanupStmt, testObjects);
1487: cleanupStmt.close();
1488: }
1489:
1490: }
|