2
Copyright (C) 2002-2006 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of version 2 of the GNU General Public License as
6
published by the Free Software Foundation.
8
There are special exceptions to the terms and conditions of the GPL
9
as it is applied to this software. View the full text of the
10
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11
software distribution.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
package testsuite.regression;
27
import java.io.ByteArrayInputStream;
28
import java.io.ByteArrayOutputStream;
30
import java.io.ObjectInputStream;
31
import java.io.ObjectOutputStream;
32
import java.lang.reflect.Method;
33
import java.sql.Connection;
34
import java.sql.PreparedStatement;
35
import java.sql.SQLException;
36
import java.sql.Statement;
37
import java.util.Hashtable;
38
import java.util.Properties;
40
import javax.naming.Context;
41
import javax.naming.InitialContext;
42
import javax.naming.Name;
43
import javax.naming.NameParser;
44
import javax.naming.RefAddr;
45
import javax.naming.Reference;
46
import javax.naming.spi.ObjectFactory;
47
import javax.sql.ConnectionPoolDataSource;
48
import javax.sql.DataSource;
49
import javax.sql.PooledConnection;
51
import testsuite.BaseTestCase;
52
import testsuite.simple.DataSourceTest;
54
import com.mysql.jdbc.NonRegisteringDriver;
55
import com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker;
56
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
57
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
58
import com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory;
59
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
62
* Tests fixes for bugs related to datasources.
64
* @author Mark Matthews
66
* @version $Id: DataSourceRegressionTest.java,v 1.1.2.1 2005/05/13 18:58:38
69
public class DataSourceRegressionTest extends BaseTestCase {
71
public final static String DS_DATABASE_PROP_NAME = "com.mysql.jdbc.test.ds.db";
73
public final static String DS_HOST_PROP_NAME = "com.mysql.jdbc.test.ds.host";
75
public final static String DS_PASSWORD_PROP_NAME = "com.mysql.jdbc.test.ds.password";
77
public final static String DS_PORT_PROP_NAME = "com.mysql.jdbc.test.ds.port";
79
public final static String DS_USER_PROP_NAME = "com.mysql.jdbc.test.ds.user";
86
* Creates a new DataSourceRegressionTest suite for the given test name
89
* the name of the testcase to run.
91
public DataSourceRegressionTest(String name) {
94
// TODO Auto-generated constructor stub
98
* Runs all test cases in this test suite
102
public static void main(String[] args) {
103
junit.textui.TestRunner.run(DataSourceTest.class);
107
* Sets up this test, calling registerDataSource() to bind a DataSource into
108
* JNDI, using the FSContext JNDI provider from Sun
111
* if an error occurs.
113
public void setUp() throws Exception {
119
* Un-binds the DataSource, and cleans up the filesystem
124
public void tearDown() throws Exception {
125
this.ctx.unbind(this.tempDir.getAbsolutePath() + "/test");
126
this.ctx.unbind(this.tempDir.getAbsolutePath() + "/testNoUrl");
128
this.tempDir.delete();
134
* Tests fix for BUG#4808- Calling .close() twice on a PooledConnection
138
* if an error occurs.
140
public void testBug4808() throws Exception {
141
MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
142
ds.setURL(BaseTestCase.dbUrl);
143
PooledConnection closeMeTwice = ds.getPooledConnection();
144
closeMeTwice.close();
145
closeMeTwice.close();
150
* Tests fix for Bug#3848, port # alone parsed incorrectly
155
public void testBug3848() throws Exception {
156
String jndiName = "/testBug3848";
158
String databaseName = System.getProperty(DS_DATABASE_PROP_NAME);
159
String userName = System.getProperty(DS_USER_PROP_NAME);
160
String password = System.getProperty(DS_PASSWORD_PROP_NAME);
161
String port = System.getProperty(DS_PORT_PROP_NAME);
163
// Only run this test if at least one of the above are set
164
if ((databaseName != null) || (userName != null) || (password != null)
166
MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
168
if (databaseName != null) {
169
ds.setDatabaseName(databaseName);
172
if (userName != null) {
173
ds.setUser(userName);
176
if (password != null) {
177
ds.setPassword(password);
181
ds.setPortNumber(Integer.parseInt(port));
184
bindDataSource(jndiName, ds);
186
ConnectionPoolDataSource boundDs = null;
189
boundDs = (ConnectionPoolDataSource) lookupDatasourceInJNDI(jndiName);
191
assertTrue("Datasource not bound", boundDs != null);
193
Connection dsConn = null;
196
dsConn = boundDs.getPooledConnection().getConnection();
198
if (dsConn != null) {
203
if (boundDs != null) {
204
this.ctx.unbind(jndiName);
211
* Tests that we can get a connection from the DataSource bound in JNDI
217
public void testBug3920() throws Exception {
218
String jndiName = "/testBug3920";
220
String databaseName = System.getProperty(DS_DATABASE_PROP_NAME);
221
String userName = System.getProperty(DS_USER_PROP_NAME);
222
String password = System.getProperty(DS_PASSWORD_PROP_NAME);
223
String port = System.getProperty(DS_PORT_PROP_NAME);
224
String serverName = System.getProperty(DS_HOST_PROP_NAME);
226
// Only run this test if at least one of the above are set
227
if ((databaseName != null) || (serverName != null)
228
|| (userName != null) || (password != null) || (port != null)) {
229
MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
231
if (databaseName != null) {
232
ds.setDatabaseName(databaseName);
235
if (userName != null) {
236
ds.setUser(userName);
239
if (password != null) {
240
ds.setPassword(password);
244
ds.setPortNumber(Integer.parseInt(port));
247
if (serverName != null) {
248
ds.setServerName(serverName);
251
bindDataSource(jndiName, ds);
253
ConnectionPoolDataSource boundDs = null;
256
boundDs = (ConnectionPoolDataSource) lookupDatasourceInJNDI(jndiName);
258
assertTrue("Datasource not bound", boundDs != null);
260
Connection dsCon = null;
261
Statement dsStmt = null;
264
dsCon = boundDs.getPooledConnection().getConnection();
265
dsStmt = dsCon.createStatement();
266
dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920");
268
.executeUpdate("CREATE TABLE testBug3920 (field1 varchar(32))");
271
"Connection can not be obtained from data source",
274
dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920");
280
if (boundDs != null) {
281
this.ctx.unbind(jndiName);
288
* Tests fix for BUG#19169 - ConnectionProperties (and thus some
289
* subclasses) are not serializable, even though some J2EE containers
292
* @throws Exception if the test fails.
294
public void testBug19169() throws Exception {
295
MysqlDataSource toSerialize = new MysqlDataSource();
296
toSerialize.setZeroDateTimeBehavior("convertToNull");
298
boolean testBooleanFlag = !toSerialize.getAllowLoadLocalInfile();
299
toSerialize.setAllowLoadLocalInfile(testBooleanFlag);
301
int testIntFlag = toSerialize.getBlobSendChunkSize() + 1;
302
toSerialize.setBlobSendChunkSize(String.valueOf(testIntFlag));
304
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
305
ObjectOutputStream objOut = new ObjectOutputStream(bOut);
306
objOut.writeObject(toSerialize);
309
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray()));
311
MysqlDataSource thawedDs = (MysqlDataSource)objIn.readObject();
313
assertEquals("convertToNull", thawedDs.getZeroDateTimeBehavior());
314
assertEquals(testBooleanFlag, thawedDs.getAllowLoadLocalInfile());
315
assertEquals(testIntFlag, thawedDs.getBlobSendChunkSize());
319
* Tests fix for BUG#20242 - MysqlValidConnectionChecker for JBoss doesn't
320
* work with MySQLXADataSources.
322
* @throws Exception if the test fails.
324
public void testBug20242() throws Exception {
325
if (versionMeetsMinimum(5, 0)) {
327
Class.forName("org.jboss.resource.adapter.jdbc.ValidConnectionChecker");
328
} catch (Exception ex) {
329
return; // class not available for testing
332
MysqlXADataSource xaDs = new MysqlXADataSource();
335
MysqlValidConnectionChecker checker = new MysqlValidConnectionChecker();
336
assertNull(checker.isValidConnection(xaDs.getXAConnection().getConnection()));
340
private void bindDataSource(String name, DataSource ds) throws Exception {
341
this.ctx.bind(this.tempDir.getAbsolutePath() + name, ds);
345
* This method is separated from the rest of the example since you normally
346
* would NOT register a JDBC driver in your code. It would likely be
347
* configered into your naming and directory service using some GUI.
352
private void createJNDIContext() throws Exception {
353
this.tempDir = File.createTempFile("jnditest", null);
354
this.tempDir.delete();
355
this.tempDir.mkdir();
356
this.tempDir.deleteOnExit();
358
MysqlConnectionPoolDataSource ds;
359
Hashtable env = new Hashtable();
360
env.put(Context.INITIAL_CONTEXT_FACTORY,
361
"com.sun.jndi.fscontext.RefFSContextFactory");
362
this.ctx = new InitialContext(env);
363
assertTrue("Naming Context not created", this.ctx != null);
364
ds = new MysqlConnectionPoolDataSource();
365
ds.setUrl(dbUrl); // from BaseTestCase
366
ds.setDatabaseName("test");
367
this.ctx.bind(this.tempDir.getAbsolutePath() + "/test", ds);
370
private DataSource lookupDatasourceInJNDI(String jndiName) throws Exception {
371
NameParser nameParser = this.ctx.getNameParser("");
372
Name datasourceName = nameParser.parse(this.tempDir.getAbsolutePath()
374
Object obj = this.ctx.lookup(datasourceName);
375
DataSource boundDs = null;
377
if (obj instanceof DataSource) {
378
boundDs = (DataSource) obj;
379
} else if (obj instanceof Reference) {
381
// For some reason, this comes back as a Reference
382
// instance under CruiseControl !?
384
Reference objAsRef = (Reference) obj;
385
ObjectFactory factory = (ObjectFactory) Class.forName(
386
objAsRef.getFactoryClassName()).newInstance();
387
boundDs = (DataSource) factory.getObjectInstance(objAsRef,
388
datasourceName, this.ctx, new Hashtable());
394
public void testCSC4616() throws Exception {
395
MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
396
ds.setURL(BaseTestCase.dbUrl);
397
PooledConnection pooledConn = ds.getPooledConnection();
398
Connection physConn = pooledConn.getConnection();
399
Statement physStatement = physConn.createStatement();
401
Method enableStreamingResultsMethodStmt = Class.forName(
402
"com.mysql.jdbc.jdbc2.optional.StatementWrapper").getMethod(
403
"enableStreamingResults", new Class[0]);
404
enableStreamingResultsMethodStmt.invoke(physStatement, new Class[0]);
405
this.rs = physStatement.executeQuery("SELECT 1");
408
physConn.createStatement().executeQuery("SELECT 2");
409
fail("Should have caught a streaming exception here");
410
} catch (SQLException sqlEx) {
411
assertTrue(sqlEx.getMessage() != null
412
&& sqlEx.getMessage().indexOf("Streaming") != -1);
414
if (this.rs != null) {
420
PreparedStatement physPrepStmt = physConn.prepareStatement("SELECT 1");
421
Method enableStreamingResultsMethodPstmt = Class.forName(
422
"com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper")
423
.getMethod("enableStreamingResults", new Class[0]);
424
enableStreamingResultsMethodPstmt.invoke(physPrepStmt, new Class[0]);
426
this.rs = physPrepStmt.executeQuery();
429
physConn.createStatement().executeQuery("SELECT 2");
430
fail("Should have caught a streaming exception here");
431
} catch (SQLException sqlEx) {
432
assertTrue(sqlEx.getMessage() != null
433
&& sqlEx.getMessage().indexOf("Streaming") != -1);
435
if (this.rs != null) {
443
* Tests fix for BUG#16791 - NullPointerException in MysqlDataSourceFactory
444
* due to Reference containing RefAddrs with null content.
446
* @throws Exception if the test fails
448
public void testBug16791() throws Exception {
449
MysqlDataSource myDs = new MysqlDataSource();
451
Reference asRef = myDs.getReference();
452
System.out.println(asRef);
454
removeFromRef(asRef, "port");
455
removeFromRef(asRef, NonRegisteringDriver.USER_PROPERTY_KEY);
456
removeFromRef(asRef, NonRegisteringDriver.PASSWORD_PROPERTY_KEY);
457
removeFromRef(asRef, "serverName");
458
removeFromRef(asRef, "databaseName");
460
MysqlDataSource newDs = (MysqlDataSource)new MysqlDataSourceFactory().getObjectInstance(asRef, null, null, null);
463
private void removeFromRef(Reference ref, String key) {
464
int size = ref.size();
466
for (int i = 0; i < size; i++) {
467
RefAddr refAddr = ref.get(i);
468
if (refAddr.getType().equals(key)) {