001: package net.sourceforge.jtds.test;
002:
003: import java.sql.Statement;
004: import java.sql.ResultSet;
005: import java.sql.SQLException;
006: import java.sql.PreparedStatement;
007: import java.sql.ParameterMetaData;
008: import java.sql.Types;
009: import java.math.BigDecimal;
010:
011: /**
012: * Test for miscellaneous JDBC 3.0 features.
013: *
014: * @version $Id: JDBC3Test.java,v 1.3 2005/07/04 11:31:09 alin_sinpalean Exp $
015: */
016: public class JDBC3Test extends TestBase {
017: public JDBC3Test(String name) {
018: super (name);
019: }
020:
021: /**
022: * Test return of multiple open result sets from one execute.
023: */
024: public void testMultipleResults() throws Exception {
025: Statement stmt = con.createStatement(
026: ResultSet.TYPE_SCROLL_INSENSITIVE,
027: ResultSet.CONCUR_READ_ONLY);
028: //
029: // Create 4 test tables
030: //
031: for (int rs = 1; rs <= 4; rs++) {
032: stmt.execute("CREATE TABLE #TESTRS" + rs
033: + " (id int, data varchar(255))");
034: for (int row = 1; row <= 10; row++) {
035: assertEquals(1, stmt
036: .executeUpdate("INSERT INTO #TESTRS" + rs
037: + " VALUES(" + row + ", 'TABLE " + rs
038: + " ROW " + row + "')"));
039: }
040: }
041:
042: assertTrue(stmt.execute("SELECT * FROM #TESTRS1\r\n"
043: + "SELECT * FROM #TESTRS2\r\n"
044: + "SELECT * FROM #TESTRS3\r\n"
045: + "SELECT * FROM #TESTRS4\r\n"));
046: ResultSet rs = stmt.getResultSet();
047: assertTrue(rs.next());
048: assertEquals("TABLE 1 ROW 1", rs.getString(2));
049: // Get RS 2 keeping RS 1 open
050: assertTrue(stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
051: ResultSet rs2 = stmt.getResultSet();
052: assertTrue(rs2.next());
053: assertEquals("TABLE 2 ROW 1", rs2.getString(2));
054: // Check RS 1 still open and on row 1
055: assertEquals("TABLE 1 ROW 1", rs.getString(2));
056: // Read a cached row from RS 1
057: assertTrue(rs.next());
058: assertEquals("TABLE 1 ROW 2", rs.getString(2));
059: // Close RS 2 but keep RS 1 open and get RS 3
060: assertTrue(stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
061: ResultSet rs3 = stmt.getResultSet();
062: assertTrue(rs3.next());
063: assertEquals("TABLE 3 ROW 1", rs3.getString(2));
064: // Check RS 2 is closed
065: try {
066: assertEquals("TABLE 2 ROW 1", rs2.getString(2));
067: fail("Expected RS 2 to be closed!");
068: } catch (SQLException e) {
069: // Ignore
070: }
071: // Check RS 1 is still open
072: assertEquals("TABLE 1 ROW 2", rs.getString(2));
073: // Close all result sets and get RS 4
074: assertTrue(stmt.getMoreResults(Statement.CLOSE_ALL_RESULTS));
075: ResultSet rs4 = stmt.getResultSet();
076: assertTrue(rs4.next());
077: assertEquals("TABLE 4 ROW 1", rs4.getString(2));
078: // check RS 1 is now closed as well
079: try {
080: assertEquals("TABLE 1 ROW 2", rs.getString(2));
081: fail("Expected RS 1 to be closed!");
082: } catch (SQLException e) {
083: // Ignore
084: }
085: assertFalse(stmt.getMoreResults());
086: stmt.close();
087: }
088:
089: /**
090: * Test closing a <code>ResultSet</code> when it's out of scope.
091: * <p/>
092: * If a finalize() method which tries to call close() is added to
093: * JtdsResultSet the next() calls will be executed concurrently with any
094: * other result processing, with no synchronization whatsoever.
095: */
096: public void testSocketConcurrency5() throws Exception {
097: Statement stmt = con.createStatement();
098: assertTrue(stmt.execute("SELECT 1 SELECT 2, 3"));
099: ResultSet rs = stmt.getResultSet();
100: assertTrue(stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
101: ResultSet rs2 = stmt.getResultSet();
102:
103: assertTrue(rs.next());
104: assertEquals(1, rs.getInt(1));
105: assertFalse(rs.next());
106: rs.close();
107:
108: assertTrue(rs2.next());
109: assertEquals(2, rs2.getInt(1));
110: assertEquals(3, rs2.getInt(2));
111: assertFalse(rs2.next());
112: rs2.close();
113:
114: stmt.close();
115: }
116:
117: /**
118: * Test for bug [1222205] getParameterMetaData returns not implemented.
119: */
120: public void testGetParamMetaData() throws SQLException {
121: PreparedStatement pstmt = con.prepareStatement("SELECT ?,?,?");
122: pstmt.setString(1, "TEST");
123: pstmt.setBigDecimal(2, new BigDecimal("123.45"));
124: pstmt.setBoolean(3, true);
125:
126: ParameterMetaData pmd = pstmt.getParameterMetaData();
127: assertEquals(3, pmd.getParameterCount());
128: assertEquals(Types.VARCHAR, pmd.getParameterType(1));
129: assertEquals("java.lang.String", pmd.getParameterClassName(1));
130: assertEquals(2, pmd.getScale(2));
131: assertEquals(38, pmd.getPrecision(2));
132: assertEquals(ParameterMetaData.parameterModeIn, pmd
133: .getParameterMode(3));
134:
135: pstmt.close();
136: }
137: }
|