1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbc4; 23 24 import org.apache.derby.impl.jdbc.Util; 25 26 import java.io.Reader ; 27 import java.io.InputStream ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.sql.PreparedStatement ; 31 import java.sql.CallableStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.sql.Statement ; 35 import org.apache.derby.shared.common.reference.SQLState; 36 import java.lang.reflect.InvocationTargetException ; 37 import java.lang.reflect.Method ; 38 import java.util.HashMap ; 39 import org.apache.derby.tools.ij; 40 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 41 42 46 public class TestResultSetMethods { 47 48 Connection conn=null; 49 PreparedStatement ps=null; 50 ResultSet rs=null; 51 52 60 private static void assert_(boolean expr, String msg) { 61 if (!expr) { 62 throw new RuntimeException ("Assertion failed: " + msg); 63 } 64 } 65 66 70 void t_getHoldability() { 71 Boolean savedAutoCommit = null; 72 try { 73 savedAutoCommit = conn.getAutoCommit(); 74 conn.setAutoCommit(false); 75 76 Statement stmt = conn.createStatement(); 78 ResultSet rs = stmt.executeQuery("values(1)"); 79 assert_(rs.getHoldability() == ResultSet.HOLD_CURSORS_OVER_COMMIT, 80 "default holdability is HOLD_CURSORS_OVER_COMMIT"); 81 rs.close(); 82 try { 83 rs.getHoldability(); 84 assert_(false, "getHoldability() should fail when closed"); 85 } catch (SQLException sqle) { 86 String sqlState = sqle.getSQLState(); 87 if (sqlState != null && 90 !sqlState.equals("XCL16")) { 91 throw sqle; 92 } 93 } 94 95 final int[] holdabilities = { 97 ResultSet.HOLD_CURSORS_OVER_COMMIT, 98 ResultSet.CLOSE_CURSORS_AT_COMMIT, 99 }; 100 for (int h : holdabilities) { 101 Statement s = 102 conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 103 ResultSet.CONCUR_READ_ONLY, h); 104 rs = s.executeQuery("values(1)"); 105 assert_(rs.getHoldability() == h, 106 "holdability " + holdabilityString(h)); 107 rs.close(); 108 s.close(); 109 } 110 111 stmt.execute("create procedure getresultsetwithhold(in hold int) " + 114 "parameter style java language java external name " + 115 "'org.apache.derbyTesting.functionTests.tests." + 116 "jdbc4.TestResultSetMethods." + 117 "getResultSetWithHoldability' " + 118 "dynamic result sets 1 reads sql data"); 119 for (int statementHoldability : holdabilities) { 120 for (int procHoldability : holdabilities) { 121 CallableStatement cs = 122 conn.prepareCall("call getresultsetwithhold(?)", 123 ResultSet.TYPE_FORWARD_ONLY, 124 ResultSet.CONCUR_READ_ONLY, 125 statementHoldability); 126 cs.setInt(1, procHoldability); 127 cs.execute(); 128 rs = cs.getResultSet(); 129 int holdability = rs.getHoldability(); 130 assert_(holdability == procHoldability, 131 "holdability of ResultSet from stored proc: " + 132 holdabilityString(holdability)); 133 conn.commit(); 134 boolean holdable; 135 try { 136 rs.next(); 137 holdable = true; 138 } catch (SQLException sqle) { 139 String sqlstate = sqle.getSQLState(); 140 if (sqlstate == null || sqlstate.equals("XCL16")) { 143 holdable = false; 144 } else { 145 throw sqle; 146 } 147 } 148 if (holdable) { 149 assert_(holdability == 150 ResultSet.HOLD_CURSORS_OVER_COMMIT, 151 "non-holdable result set not closed on commit"); 152 } else { 153 assert_(holdability == 154 ResultSet.CLOSE_CURSORS_AT_COMMIT, 155 "holdable result set closed on commit"); 156 } 157 rs.close(); 158 cs.close(); 159 } 160 } 161 stmt.execute("drop procedure getresultsetwithhold"); 162 stmt.close(); 163 conn.commit(); 164 } catch(Exception e) { 165 System.out.println("Unexpected exception caught " + e); 166 e.printStackTrace(System.out); 167 } finally { 168 if (savedAutoCommit != null) { 169 try { 170 conn.setAutoCommit(savedAutoCommit); 171 } catch (SQLException sqle) { 172 sqle.printStackTrace(System.out); 173 } 174 } 175 } 176 } 177 178 184 private static String holdabilityString(int holdability) { 185 switch (holdability) { 186 case ResultSet.HOLD_CURSORS_OVER_COMMIT: 187 return "HOLD_CURSORS_OVER_COMMIT"; 188 case ResultSet.CLOSE_CURSORS_AT_COMMIT: 189 return "CLOSE_CURSORS_AT_COMMIT"; 190 default: 191 return "UNKNOWN HOLDABILITY"; 192 } 193 } 194 195 199 void t_isClosed(){ 200 try { 201 Statement stmt = conn.createStatement(); 202 203 ResultSet rs = stmt.executeQuery("values(1)"); 205 assert_(!rs.isClosed(), "rs should be open"); 206 while (rs.next()); 207 assert_(!rs.isClosed(), "rs should be open"); 208 rs.close(); 209 assert_(rs.isClosed(), "rs should be closed"); 210 211 rs = stmt.executeQuery("values(1)"); 213 assert_(!rs.isClosed(), "rs should be open"); 214 ResultSet rs2 = stmt.executeQuery("values(1)"); 215 assert_(rs.isClosed(), "rs should be closed"); 216 assert_(!rs2.isClosed(), "rs2 should be open"); 217 218 Statement stmt2 = conn.createStatement(); 220 rs = stmt2.executeQuery("values(1)"); 221 assert_(!rs2.isClosed(), "rs2 should be open"); 222 assert_(!rs.isClosed(), "rs should be open"); 223 224 stmt.execute("create procedure retrieve_result_sets() " + 226 "parameter style java language java external name " + 227 "'org.apache.derbyTesting.functionTests.tests." + 228 "jdbc4.TestResultSetMethods.threeResultSets' " + 229 "dynamic result sets 3 reads sql data"); 230 stmt.execute("call retrieve_result_sets()"); 231 ResultSet [] rss = new ResultSet [3]; 232 int count = 0; 233 do { 234 rss[count] = stmt.getResultSet(); 235 assert_(!rss[count].isClosed(), 236 "rss[" + count + "] should be open"); 237 if (count > 0) { 238 assert_(rss[count-1].isClosed(), 239 "rss[" + (count-1) + "] should be closed"); 240 } 241 ++count; 242 } while (stmt.getMoreResults()); 243 assert_(count == 3, "expected three result sets"); 244 stmt.execute("drop procedure retrieve_result_sets"); 245 246 rs = stmt2.executeQuery("values(1)"); 248 stmt2.close(); 249 assert_(rs.isClosed(), "rs should be closed"); 250 251 Connection conn2 = ij.startJBMS(); 253 stmt2 = conn2.createStatement(); 254 rs = stmt2.executeQuery("values(1)"); 255 conn2.close(); 256 assert_(rs.isClosed(), "rs should be closed"); 257 258 stmt.close(); 259 stmt2.close(); 260 261 } catch(Exception e) { 262 System.out.println("Unexpected exception caught"+e); 263 e.printStackTrace(); 264 } 265 } 266 267 271 private void testExceptionWhenClosed() { 272 try { 273 Statement stmt = conn.createStatement(); 275 ResultSet rs = stmt.executeQuery("values(1)"); 276 rs.close(); 277 278 HashMap <String , Class []> params = new HashMap <String , Class []>(); 280 HashMap <String , Object []> args = new HashMap <String , Object []>(); 282 283 String [] zeroArgMethods = { 285 "getWarnings", "clearWarnings", "getStatement", 286 "getMetaData", "getConcurrency", "getHoldability", 287 "getRow", "getType", "rowDeleted", "rowInserted", 288 "rowUpdated", "getFetchDirection", "getFetchSize", 289 }; 290 for (String name : zeroArgMethods) { 291 params.put(name, null); 292 args.put(name, null); 293 } 294 295 for (String name : new String [] { "setFetchDirection", 297 "setFetchSize" }) { 298 params.put(name, new Class [] { Integer.TYPE }); 299 args.put(name, new Integer [] { 0 }); 300 } 301 302 for (String name : params.keySet()) { 304 try { 305 Method method = 306 rs.getClass().getMethod(name, params.get(name)); 307 try { 308 method.invoke(rs, args.get(name)); 309 } catch (InvocationTargetException ite) { 310 Throwable cause = ite.getCause(); 311 if (cause instanceof SQLException ) { 312 SQLException sqle = (SQLException ) cause; 313 String state = sqle.getSQLState(); 314 if (state == null || 318 state.equals("XCL16")) { 319 continue; 320 } 321 } 322 throw cause; 323 } 324 System.out.println("no exception thrown for " + name + 325 "() when ResultSet is closed"); 326 } catch (Throwable t) { 327 System.out.println("Unexpected exception when " + 328 "invoking " + name + "():"); 329 t.printStackTrace(System.out); 330 } 331 } 332 stmt.close(); 333 } catch (SQLException e) { 334 System.out.println("Unexpected exception caught:"); 335 e.printStackTrace(System.out); 336 } 337 } 338 339 348 void t_wrapper(ResultSet rs) { 349 Class <ResultSet > wrap_class = ResultSet .class; 350 351 try { 353 if(rs.isWrapperFor(wrap_class)) { 354 ResultSet rs1 = 355 (ResultSet )rs.unwrap(wrap_class); 356 } 357 else { 358 System.out.println("isWrapperFor wrongly returns false"); 359 } 360 } 361 catch(SQLException sqle) { 362 sqle.printStackTrace(); 363 } 364 365 Class <PreparedStatement > wrap_class1 = PreparedStatement .class; 370 371 try { 372 if(rs.isWrapperFor(wrap_class1)) { 375 System.out.println("isWrapperFor wrongly returns true"); 376 } 377 else { 378 PreparedStatement ps1 = (PreparedStatement ) 379 rs.unwrap(wrap_class1); 380 System.out.println("unwrap does not throw the expected " + 381 "exception"); 382 } 383 } 384 catch (SQLException sqle) { 385 if(!SQLStateConstants.UNABLE_TO_UNWRAP.equals(sqle.getSQLState())) { 389 sqle.printStackTrace(); 390 } 391 } 392 } 393 void startTestResultSetMethods(Connection conn_in,PreparedStatement ps_in,ResultSet rs_in) { 394 conn = conn_in; 395 ps = ps_in; 396 rs = rs_in; 397 398 t_getHoldability(); 399 t_isClosed(); 400 401 testExceptionWhenClosed(); 402 } 403 404 413 public static void threeResultSets(ResultSet [] rs1, 414 ResultSet [] rs2, 415 ResultSet [] rs3) 416 throws SQLException 417 { 418 Connection c = DriverManager.getConnection("jdbc:default:connection"); 419 Statement stmt1 = c.createStatement(); 420 rs1[0] = stmt1.executeQuery("values(1)"); 421 Statement stmt2 = c.createStatement(); 422 rs2[0] = stmt2.executeQuery("values(1)"); 423 Statement stmt3 = c.createStatement(); 424 rs3[0] = stmt3.executeQuery("values(1)"); 425 c.close(); 426 } 427 428 436 public static void getResultSetWithHoldability(int holdability, 437 ResultSet [] rs) 438 throws SQLException 439 { 440 Connection c = DriverManager.getConnection("jdbc:default:connection"); 441 Statement s = c.createStatement(ResultSet.TYPE_FORWARD_ONLY, 442 ResultSet.CONCUR_READ_ONLY, 443 holdability); 444 rs[0] = s.executeQuery("values (1), (2), (3)"); 445 c.close(); 446 } 447 448 455 private static boolean usingEmbeddedClient() 456 { 457 return "embedded".equals(System.getProperty("framework" ) ); 458 } 459 460 public static void main(String args[]) { 461 try { 462 ij.getPropertyArg(args); 465 466 Connection conn_main = ij.startJBMS(); 467 468 PreparedStatement ps_main=null; 469 ResultSet rs_main=null; 470 471 ps_main = conn_main.prepareStatement("select count(*) from sys.systables"); 472 rs_main = ps_main.executeQuery(); 473 474 TestResultSetMethods trsm = new TestResultSetMethods(); 475 476 trsm.startTestResultSetMethods(conn_main,ps_main,rs_main); 477 trsm.t_wrapper(rs_main); 478 } catch(Exception e) { 479 System.out.println(""+e); 480 e.printStackTrace(); 481 } 482 483 484 } 485 } 486 | Popular Tags |