1 25 package testsuite.regression; 26 27 import java.io.File ; 28 import java.lang.reflect.Method ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.SQLException ; 32 import java.sql.Statement ; 33 import java.util.Hashtable ; 34 import java.util.Properties ; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.Name ; 39 import javax.naming.NameParser ; 40 import javax.naming.Reference ; 41 import javax.naming.spi.ObjectFactory ; 42 import javax.sql.ConnectionPoolDataSource ; 43 import javax.sql.DataSource ; 44 import javax.sql.PooledConnection ; 45 46 import testsuite.BaseTestCase; 47 import testsuite.simple.DataSourceTest; 48 49 import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource; 50 import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; 51 52 60 public class DataSourceRegressionTest extends BaseTestCase { 61 62 public final static String DS_DATABASE_PROP_NAME = "com.mysql.jdbc.test.ds.db"; 63 64 public final static String DS_HOST_PROP_NAME = "com.mysql.jdbc.test.ds.host"; 65 66 public final static String DS_PASSWORD_PROP_NAME = "com.mysql.jdbc.test.ds.password"; 67 68 public final static String DS_PORT_PROP_NAME = "com.mysql.jdbc.test.ds.port"; 69 70 public final static String DS_USER_PROP_NAME = "com.mysql.jdbc.test.ds.user"; 71 72 private Context ctx; 73 74 private File tempDir; 75 76 82 public DataSourceRegressionTest(String name) { 83 super(name); 84 85 } 87 88 93 public static void main(String [] args) { 94 junit.textui.TestRunner.run(DataSourceTest.class); 95 } 96 97 104 public void setUp() throws Exception { 105 super.setUp(); 106 createJNDIContext(); 107 } 108 109 115 public void tearDown() throws Exception { 116 this.ctx.unbind(this.tempDir.getAbsolutePath() + "/test"); 117 this.ctx.unbind(this.tempDir.getAbsolutePath() + "/testNoUrl"); 118 this.ctx.close(); 119 this.tempDir.delete(); 120 121 super.tearDown(); 122 } 123 124 131 public void testBug4808() throws Exception { 132 MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); 133 ds.setURL(BaseTestCase.dbUrl); 134 PooledConnection closeMeTwice = ds.getPooledConnection(); 135 closeMeTwice.close(); 136 closeMeTwice.close(); 137 138 } 139 140 146 public void testBug3848() throws Exception { 147 String jndiName = "/testBug3848"; 148 149 String databaseName = System.getProperty(DS_DATABASE_PROP_NAME); 150 String userName = System.getProperty(DS_USER_PROP_NAME); 151 String password = System.getProperty(DS_PASSWORD_PROP_NAME); 152 String port = System.getProperty(DS_PORT_PROP_NAME); 153 154 if ((databaseName != null) || (userName != null) || (password != null) 156 || (port != null)) { 157 MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); 158 159 if (databaseName != null) { 160 ds.setDatabaseName(databaseName); 161 } 162 163 if (userName != null) { 164 ds.setUser(userName); 165 } 166 167 if (password != null) { 168 ds.setPassword(password); 169 } 170 171 if (port != null) { 172 ds.setPortNumber(Integer.parseInt(port)); 173 } 174 175 bindDataSource(jndiName, ds); 176 177 ConnectionPoolDataSource boundDs = null; 178 179 try { 180 boundDs = (ConnectionPoolDataSource ) lookupDatasourceInJNDI(jndiName); 181 182 assertTrue("Datasource not bound", boundDs != null); 183 184 Connection dsConn = null; 185 186 try { 187 dsConn = boundDs.getPooledConnection().getConnection(); 188 } finally { 189 if (dsConn != null) { 190 dsConn.close(); 191 } 192 } 193 } finally { 194 if (boundDs != null) { 195 this.ctx.unbind(jndiName); 196 } 197 } 198 } 199 } 200 201 208 public void testBug3920() throws Exception { 209 String jndiName = "/testBug3920"; 210 211 String databaseName = System.getProperty(DS_DATABASE_PROP_NAME); 212 String userName = System.getProperty(DS_USER_PROP_NAME); 213 String password = System.getProperty(DS_PASSWORD_PROP_NAME); 214 String port = System.getProperty(DS_PORT_PROP_NAME); 215 String serverName = System.getProperty(DS_HOST_PROP_NAME); 216 217 if ((databaseName != null) || (serverName != null) 219 || (userName != null) || (password != null) || (port != null)) { 220 MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); 221 222 if (databaseName != null) { 223 ds.setDatabaseName(databaseName); 224 } 225 226 if (userName != null) { 227 ds.setUser(userName); 228 } 229 230 if (password != null) { 231 ds.setPassword(password); 232 } 233 234 if (port != null) { 235 ds.setPortNumber(Integer.parseInt(port)); 236 } 237 238 if (serverName != null) { 239 ds.setServerName(serverName); 240 } 241 242 bindDataSource(jndiName, ds); 243 244 ConnectionPoolDataSource boundDs = null; 245 246 try { 247 boundDs = (ConnectionPoolDataSource ) lookupDatasourceInJNDI(jndiName); 248 249 assertTrue("Datasource not bound", boundDs != null); 250 251 Connection dsCon = null; 252 Statement dsStmt = null; 253 254 try { 255 dsCon = boundDs.getPooledConnection().getConnection(); 256 dsStmt = dsCon.createStatement(); 257 dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920"); 258 dsStmt 259 .executeUpdate("CREATE TABLE testBug3920 (field1 varchar(32))"); 260 261 assertTrue( 262 "Connection can not be obtained from data source", 263 dsCon != null); 264 } finally { 265 dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920"); 266 267 dsStmt.close(); 268 dsCon.close(); 269 } 270 } finally { 271 if (boundDs != null) { 272 this.ctx.unbind(jndiName); 273 } 274 } 275 } 276 } 277 278 private void bindDataSource(String name, DataSource ds) throws Exception { 279 this.ctx.bind(this.tempDir.getAbsolutePath() + name, ds); 280 } 281 282 290 private void createJNDIContext() throws Exception { 291 this.tempDir = File.createTempFile("jnditest", null); 292 this.tempDir.delete(); 293 this.tempDir.mkdir(); 294 this.tempDir.deleteOnExit(); 295 296 MysqlConnectionPoolDataSource ds; 297 Hashtable env = new Hashtable (); 298 env.put(Context.INITIAL_CONTEXT_FACTORY, 299 "com.sun.jndi.fscontext.RefFSContextFactory"); 300 this.ctx = new InitialContext (env); 301 assertTrue("Naming Context not created", this.ctx != null); 302 ds = new MysqlConnectionPoolDataSource(); 303 ds.setUrl(dbUrl); ds.setDatabaseName("test"); 305 this.ctx.bind(this.tempDir.getAbsolutePath() + "/test", ds); 306 } 307 308 private DataSource lookupDatasourceInJNDI(String jndiName) throws Exception { 309 NameParser nameParser = this.ctx.getNameParser(""); 310 Name datasourceName = nameParser.parse(this.tempDir.getAbsolutePath() 311 + jndiName); 312 Object obj = this.ctx.lookup(datasourceName); 313 DataSource boundDs = null; 314 315 if (obj instanceof DataSource ) { 316 boundDs = (DataSource ) obj; 317 } else if (obj instanceof Reference ) { 318 Reference objAsRef = (Reference ) obj; 323 ObjectFactory factory = (ObjectFactory ) Class.forName( 324 objAsRef.getFactoryClassName()).newInstance(); 325 boundDs = (DataSource ) factory.getObjectInstance(objAsRef, 326 datasourceName, this.ctx, new Hashtable ()); 327 } 328 329 return boundDs; 330 } 331 332 public void testCSC4616() throws Exception { 333 MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); 334 ds.setURL(BaseTestCase.dbUrl); 335 PooledConnection pooledConn = ds.getPooledConnection(); 336 Connection physConn = pooledConn.getConnection(); 337 Statement physStatement = physConn.createStatement(); 338 339 Method enableStreamingResultsMethodStmt = Class.forName( 340 "com.mysql.jdbc.jdbc2.optional.StatementWrapper").getMethod( 341 "enableStreamingResults", new Class [0]); 342 enableStreamingResultsMethodStmt.invoke(physStatement, new Class [0]); 343 this.rs = physStatement.executeQuery("SELECT 1"); 344 345 try { 346 physConn.createStatement().executeQuery("SELECT 2"); 347 fail("Should have caught a streaming exception here"); 348 } catch (SQLException sqlEx) { 349 assertTrue(sqlEx.getMessage() != null 350 && sqlEx.getMessage().indexOf("Streaming") != -1); 351 } finally { 352 if (this.rs != null) { 353 this.rs.close(); 354 this.rs = null; 355 } 356 } 357 358 PreparedStatement physPrepStmt = physConn.prepareStatement("SELECT 1"); 359 Method enableStreamingResultsMethodPstmt = Class.forName( 360 "com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper") 361 .getMethod("enableStreamingResults", new Class [0]); 362 enableStreamingResultsMethodPstmt.invoke(physPrepStmt, new Class [0]); 363 364 this.rs = physPrepStmt.executeQuery(); 365 366 try { 367 physConn.createStatement().executeQuery("SELECT 2"); 368 fail("Should have caught a streaming exception here"); 369 } catch (SQLException sqlEx) { 370 assertTrue(sqlEx.getMessage() != null 371 && sqlEx.getMessage().indexOf("Streaming") != -1); 372 } finally { 373 if (this.rs != null) { 374 this.rs.close(); 375 this.rs = null; 376 } 377 } 378 } 379 } 380 | Popular Tags |