1 25 package testsuite; 26 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.sql.Statement ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 import java.util.Properties ; 36 37 import junit.framework.TestCase; 38 39 46 public abstract class BaseTestCase extends TestCase { 47 51 protected static String dbUrl = "jdbc:mysql:///test"; 52 53 54 private static int instanceCount = 1; 55 56 private final static String ADMIN_CONNECTION_PROPERTY_NAME = "com.mysql.jdbc.testsuite.admin-url"; 57 58 59 protected Connection conn = null; 60 61 65 protected PreparedStatement pstmt = null; 66 67 70 protected ResultSet rs = null; 71 72 76 protected Statement stmt = null; 77 78 79 protected String dbClass = "com.mysql.jdbc.Driver"; 80 81 82 private int myInstanceNumber = 0; 83 84 85 private List createdTables; 86 87 93 public BaseTestCase(String name) { 94 super(name); 95 this.myInstanceNumber = instanceCount++; 96 97 String newDbUrl = System.getProperty("com.mysql.jdbc.testsuite.url"); 98 99 if ((newDbUrl != null) && (newDbUrl.trim().length() != 0)) { 100 dbUrl = newDbUrl; 101 } 102 103 String newDriver = System 104 .getProperty("com.mysql.jdbc.testsuite.driver"); 105 106 if ((newDriver != null) && (newDriver.trim().length() != 0)) { 107 this.dbClass = newDriver; 108 } 109 } 110 111 117 public void setUp() throws Exception { 118 System.out.println("Loading JDBC driver '" + this.dbClass + "'"); 119 Class.forName(this.dbClass).newInstance(); 120 System.out.println("Done.\n"); 121 createdTables = new ArrayList (); 122 123 126 if (this.dbClass.equals("gwe.sql.gweMysqlDriver")) { 127 try { 128 this.conn = DriverManager.getConnection(dbUrl, "", ""); 129 } catch (Exception ex) { 130 ex.printStackTrace(); 131 fail(); 132 } 133 } else { 134 try { 135 this.conn = DriverManager.getConnection(dbUrl); 136 } catch (Exception ex) { 137 ex.printStackTrace(); 138 fail(); 139 } 140 } 141 142 System.out.println("Done.\n"); 143 this.stmt = this.conn.createStatement(); 144 145 try { 146 this.rs = this.stmt.executeQuery("SELECT VERSION()"); 147 this.rs.next(); 148 logDebug("Connected to " + this.rs.getString(1)); 149 this.rs.close(); 150 this.rs = null; 151 } finally { 152 if (this.rs != null) { 153 this.rs.close(); 154 } 155 } 156 } 157 158 164 public void tearDown() throws Exception { 165 if (this.rs != null) { 166 try { 167 this.rs.close(); 168 } catch (SQLException SQLE) { 169 ; 170 } 171 } 172 173 for (int i = 0; i < createdTables.size(); i++) { 174 try { 175 dropTable((String ) createdTables.get(i)); 176 } catch (SQLException SQLE) { 177 ; 178 } 179 } 180 181 if (this.stmt != null) { 182 try { 183 this.stmt.close(); 184 } catch (SQLException SQLE) { 185 ; 186 } 187 } 188 189 if (this.pstmt != null) { 190 try { 191 this.pstmt.close(); 192 } catch (SQLException SQLE) { 193 ; 194 } 195 } 196 197 if (this.conn != null) { 198 try { 199 this.conn.close(); 200 } catch (SQLException SQLE) { 201 ; 202 } 203 } 204 } 205 206 protected Connection getAdminConnection() throws SQLException { 207 return getAdminConnectionWithProps(new Properties ()); 208 } 209 210 protected boolean isAdminConnectionConfigured() { 211 return System.getProperty(ADMIN_CONNECTION_PROPERTY_NAME) != null; 212 } 213 214 protected Connection getAdminConnectionWithProps(Properties props) 215 throws SQLException { 216 String adminUrl = System.getProperty(ADMIN_CONNECTION_PROPERTY_NAME); 217 218 if (adminUrl != null) { 219 return DriverManager.getConnection(adminUrl, props); 220 } else { 221 return null; 222 } 223 } 224 225 237 protected Connection getConnectionWithProps(Properties props) 238 throws SQLException { 239 return DriverManager.getConnection(dbUrl, props); 240 } 241 242 248 protected int getInstanceNumber() { 249 return this.myInstanceNumber; 250 } 251 252 263 protected String getMysqlVariable(String variableName) throws SQLException { 264 return getMysqlVariable(this.conn, variableName); 265 } 266 267 protected String getMysqlVariable(Connection c, String variableName) 268 throws SQLException { 269 Object value = getSingleIndexedValueWithQuery(c, 2, 270 "SHOW VARIABLES LIKE '" + variableName + "'"); 271 272 if (value != null) { 273 return value.toString(); 274 } 275 276 return null; 277 278 } 279 280 protected int getRowCount(String tableName) throws SQLException { 281 ResultSet countRs = null; 282 283 try { 284 countRs = this.stmt.executeQuery("SELECT COUNT(*) FROM " 285 + tableName); 286 287 countRs.next(); 288 289 return countRs.getInt(1); 290 } finally { 291 if (countRs != null) { 292 countRs.close(); 293 } 294 } 295 } 296 297 protected Object getSingleIndexedValueWithQuery(int columnIndex, 298 String query) throws SQLException { 299 return getSingleIndexedValueWithQuery(this.conn, columnIndex, query); 300 } 301 302 protected Object getSingleIndexedValueWithQuery(Connection c, 303 int columnIndex, String query) throws SQLException { 304 ResultSet valueRs = null; 305 306 Statement svStmt = null; 307 308 try { 309 svStmt = c.createStatement(); 310 311 valueRs = svStmt.executeQuery(query); 312 313 if (!valueRs.next()) { 314 return null; 315 } 316 317 return valueRs.getObject(columnIndex); 318 } finally { 319 if (valueRs != null) { 320 valueRs.close(); 321 } 322 323 if (svStmt != null) { 324 svStmt.close(); 325 } 326 } 327 } 328 329 protected Object getSingleValue(String tableName, String columnName, 330 String whereClause) throws SQLException { 331 return getSingleValueWithQuery("SELECT " + columnName + " FROM " 332 + tableName + ((whereClause == null) ? "" : whereClause)); 333 } 334 335 protected Object getSingleValueWithQuery(String query) throws SQLException { 336 return getSingleIndexedValueWithQuery(1, query); 337 } 338 339 348 protected boolean runTestIfSysPropDefined(String propName) { 349 String prop = System.getProperty(propName); 350 351 return (prop != null) && (prop.length() > 0); 352 } 353 354 368 protected boolean versionMeetsMinimum(int major, int minor) 369 throws SQLException { 370 return versionMeetsMinimum(major, minor, 0); 371 } 372 373 387 protected boolean versionMeetsMinimum(int major, int minor, int subminor) 388 throws SQLException { 389 return (((com.mysql.jdbc.Connection) this.conn).versionMeetsMinimum( 390 major, minor, subminor)); 391 } 392 393 protected void createTable(String tableName, String columnsAndOtherStuff) 394 throws SQLException { 395 createdTables.add(tableName); 396 dropTable(tableName); 397 398 StringBuffer createSql = new StringBuffer (tableName.length() 399 + columnsAndOtherStuff.length() + 10); 400 createSql.append("CREATE TABLE "); 401 createSql.append(tableName); 402 createSql.append(" "); 403 createSql.append(columnsAndOtherStuff); 404 this.stmt.executeUpdate(createSql.toString()); 405 } 406 407 protected void dropTable(String tableName) throws SQLException { 408 this.stmt.executeUpdate("DROP TABLE IF EXISTS " + tableName); 409 } 410 411 public void logDebug(String message) { 412 if (System.getProperty("com.mysql.jdbc.testsuite.noDebugOutput") == null) { 413 System.err.println(message); 414 } 415 } 416 417 protected final boolean runLongTests() { 418 return runTestIfSysPropDefined("com.mysql.jdbc.testsuite.runLongTests"); 419 } 420 421 protected Connection getConnectionWithProps(String url, Properties props) 422 throws SQLException { 423 return DriverManager.getConnection(url, props); 424 } 425 } 426 | Popular Tags |