1 20 package org.apache.derbyTesting.junit; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.InputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.io.PrintStream ; 27 import java.io.Reader ; 28 import java.io.UnsupportedEncodingException ; 29 import java.sql.*; 30 31 import junit.framework.AssertionFailedError; 32 33 import org.apache.derby.tools.ij; 34 35 36 41 public abstract class BaseJDBCTestCase 42 extends BaseTestCase { 43 44 50 private Connection conn; 51 52 57 public BaseJDBCTestCase(String name) { 58 super(name); 59 } 60 61 73 public Connection getConnection() throws SQLException 74 { 75 if (conn != null) 76 { 77 if (!conn.isClosed()) 78 return conn; 79 conn = null; 80 } 81 return conn = openDefaultConnection(); 82 } 83 84 101 protected void initializeConnection(Connection conn) throws SQLException 102 { 103 } 104 105 111 public Statement createStatement() throws SQLException 112 { 113 return getConnection().createStatement(); 114 } 115 116 123 public Statement createStatement(int resultSetType, 124 int resultSetConcurrency) throws SQLException 125 { 126 return getConnection().createStatement(resultSetType, resultSetConcurrency); 127 } 128 135 public PreparedStatement prepareStatement(String sql) throws SQLException 136 { 137 return getConnection().prepareStatement(sql); 138 } 139 140 147 public CallableStatement prepareCall(String sql) throws SQLException 148 { 149 return getConnection().prepareCall(sql); 150 } 151 152 157 public void commit() throws SQLException 158 { 159 getConnection().commit(); 160 } 161 166 public void rollback() throws SQLException 167 { 168 getConnection().rollback(); 169 } 170 175 protected void tearDown() 176 throws java.lang.Exception  177 { 178 JDBC.cleanup(conn); 179 conn = null; 180 } 181 182 190 public Connection openDefaultConnection() 191 throws SQLException { 192 Connection conn = getTestConfiguration().openDefaultConnection(); 193 initializeConnection(conn); 194 return conn; 195 } 196 197 public Connection openConnection(String databaseName) throws SQLException 198 { 199 Connection conn = getTestConfiguration().openConnection(databaseName); 200 initializeConnection(conn); 201 return conn; 202 } 203 204 212 public Connection getDefaultConnection(String connAttrs) 213 throws SQLException { 214 Connection conn = getTestConfiguration(). 215 getDefaultConnection(connAttrs); 216 initializeConnection(conn); 217 return conn; 218 } 219 220 229 public Connection getConnection(String databaseName, String connAttrs) 230 throws SQLException 231 { 232 Connection conn = getTestConfiguration().getConnection(databaseName, 233 connAttrs); 234 initializeConnection(conn); 235 return conn; 236 } 237 238 245 public int runScript(InputStream script, String encoding) 246 throws UnsupportedEncodingException , SQLException 247 { 248 OutputStream sink = new OutputStream () { 250 public void write(byte[] b, int off, int len) {} 251 public void write(int b) {} 252 }; 253 254 return ij.runScript(getConnection(), script, encoding, 256 sink, encoding); 257 } 258 259 268 public int runSQLCommands(String sqlCommands) 269 throws UnsupportedEncodingException , SQLException 270 { 271 byte[] raw = sqlCommands.getBytes("UTF-8"); 272 ByteArrayInputStream in = new ByteArrayInputStream (raw); 273 274 return runScript(in, "UTF-8"); 275 } 276 277 283 public static boolean usingEmbedded() { 284 return TestConfiguration.getCurrent().getJDBCClient().isEmbedded(); 285 } 286 287 293 public static boolean usingDerbyNetClient() { 294 return TestConfiguration.getCurrent().getJDBCClient().isDerbyNetClient(); 295 } 296 297 303 public static boolean usingDerbyNet() { 304 return TestConfiguration.getCurrent().getJDBCClient().isDB2Client(); 305 } 306 307 318 public static void assertEquals(Blob b1, Blob b2) 319 throws IOException , SQLException { 320 if (b1 == null || b2 == null) { 321 assertNull("Blob b2 is null, b1 is not", b1); 322 assertNull("Blob b1 is null, b2 is not", b2); 323 return; 324 } 325 assertEquals("Blobs have different lengths", 326 b1.length(), b2.length()); 327 InputStream is1 = b1.getBinaryStream(); 328 InputStream is2 = b2.getBinaryStream(); 329 if (is1 == null || is2 == null) { 330 assertNull("Blob b2 has null-stream, blob b1 doesn't", is1); 331 assertNull("Blob b1 has null-stream, blob b2 doesn't", is2); 332 return; 333 } 334 long index = 1; 335 int by1 = is1.read(); 336 int by2 = is2.read(); 337 do { 338 if (by1 != by2) { 340 assertEquals("Blobs differ at index " + index, 341 by1, by2); 342 } 343 index++; 344 by1 = is1.read(); 345 by2 = is2.read(); 346 } while ( by1 != -1 || by2 != -1); 347 is1.close(); 348 is2.close(); 349 } 350 351 362 public static void assertEquals(Clob c1, Clob c2) 363 throws IOException , SQLException { 364 if (c1 == null || c2 == null) { 365 assertNull("Clob c2 is null, c1 is not", c1); 366 assertNull("Clob c1 is null, c2 is not", c2); 367 return; 368 } 369 assertEquals("Clobs have different lengths", 370 c1.length(), c2.length()); 371 Reader r1 = c1.getCharacterStream(); 372 Reader r2 = c2.getCharacterStream(); 373 if (r1 == null || r2 == null) { 374 assertNull("Clob c2 has null-stream, clob c1 doesn't", r1); 375 assertNull("Clob c1 has null-stream, clob c2 doesn't", r2); 376 return; 377 } 378 long index = 1; 379 int ch1 = r1.read(); 380 int ch2 = r2.read(); 381 do { 382 if (ch1 != ch2) { 384 assertEquals("Clobs differ at index " + index, 385 ch1, ch2); 386 } 387 index++; 388 ch1 = r1.read(); 389 ch2 = r2.read(); 390 } while (ch1 != -1 || ch2 != -1); 391 r1.close(); 392 r2.close(); 393 } 394 395 402 public static void assertSQLState(String message, 403 String expected, 404 SQLException exception) { 405 assertNotNull("Exception cannot be null when asserting on SQLState", 408 exception); 409 410 try { 411 String state = exception.getSQLState(); 412 413 if ( state != null ) 414 assertTrue("The exception's SQL state must be five characters long", 415 state.length() == 5); 416 417 if ( expected != null ) 418 assertTrue("The expected SQL state must be five characters long", 419 expected.length() == 5); 420 421 assertEquals(message, expected, state); 422 } catch (AssertionFailedError e) { 423 424 427 throw e; 428 } 429 } 430 431 437 public static void assertSQLState(String expected, SQLException exception) { 438 assertSQLState("Unexpected SQL state.", expected, exception); 439 } 440 447 public void assertCompileError(String sqlState, String query) { 448 449 try { 450 prepareStatement(query).close(); 451 fail("expected compile error: " + sqlState); 452 } catch (SQLException se) { 453 assertSQLState(sqlState, se); 454 } 455 } 456 457 } 459 460
| Popular Tags
|