PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/regression/DataSourceRegressionTest.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 516 lines | 317 code | 80 blank | 119 comment | 66 complexity | 364d2761c0db2c1848616864b9b98383 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. /*
  2. Copyright 2002-2006 MySQL AB, 2008 Sun Microsystems
  3. All rights reserved. Use is subject to license terms.
  4. The MySQL Connector/J is licensed under the terms of the GPL,
  5. like most MySQL Connectors. There are special exceptions to the
  6. terms and conditions of the GPL as it is applied to this software,
  7. see the FLOSS License Exception available on mysql.com.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; version 2 of the
  11. License.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. 02110-1301 USA
  20. */
  21. package testsuite.regression;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.ObjectInputStream;
  26. import java.io.ObjectOutputStream;
  27. import java.lang.reflect.Method;
  28. import java.sql.Connection;
  29. import java.sql.PreparedStatement;
  30. import java.sql.ResultSet;
  31. import java.sql.SQLException;
  32. import java.sql.Statement;
  33. import java.util.Hashtable;
  34. import javax.naming.Context;
  35. import javax.naming.InitialContext;
  36. import javax.naming.Name;
  37. import javax.naming.NameParser;
  38. import javax.naming.RefAddr;
  39. import javax.naming.Reference;
  40. import javax.naming.spi.ObjectFactory;
  41. import javax.sql.ConnectionPoolDataSource;
  42. import javax.sql.DataSource;
  43. import javax.sql.PooledConnection;
  44. import testsuite.BaseTestCase;
  45. import testsuite.simple.DataSourceTest;
  46. import com.mysql.jdbc.ConnectionProperties;
  47. import com.mysql.jdbc.NonRegisteringDriver;
  48. import com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker;
  49. import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
  50. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  51. import com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory;
  52. import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
  53. /**
  54. * Tests fixes for bugs related to datasources.
  55. *
  56. * @author Mark Matthews
  57. *
  58. * @version $Id: DataSourceRegressionTest.java,v 1.1.2.1 2005/05/13 18:58:38
  59. * mmatthews Exp $
  60. */
  61. public class DataSourceRegressionTest extends BaseTestCase {
  62. public final static String DS_DATABASE_PROP_NAME = "com.mysql.jdbc.test.ds.db";
  63. public final static String DS_HOST_PROP_NAME = "com.mysql.jdbc.test.ds.host";
  64. public final static String DS_PASSWORD_PROP_NAME = "com.mysql.jdbc.test.ds.password";
  65. public final static String DS_PORT_PROP_NAME = "com.mysql.jdbc.test.ds.port";
  66. public final static String DS_USER_PROP_NAME = "com.mysql.jdbc.test.ds.user";
  67. private Context ctx;
  68. private File tempDir;
  69. /**
  70. * Creates a new DataSourceRegressionTest suite for the given test name
  71. *
  72. * @param name
  73. * the name of the testcase to run.
  74. */
  75. public DataSourceRegressionTest(String name) {
  76. super(name);
  77. }
  78. /**
  79. * Runs all test cases in this test suite
  80. *
  81. * @param args
  82. */
  83. public static void main(String[] args) {
  84. junit.textui.TestRunner.run(DataSourceTest.class);
  85. }
  86. /**
  87. * Sets up this test, calling registerDataSource() to bind a DataSource into
  88. * JNDI, using the FSContext JNDI provider from Sun
  89. *
  90. * @throws Exception
  91. * if an error occurs.
  92. */
  93. public void setUp() throws Exception {
  94. super.setUp();
  95. createJNDIContext();
  96. }
  97. /**
  98. * Un-binds the DataSource, and cleans up the filesystem
  99. *
  100. * @throws Exception
  101. * if an error occurs
  102. */
  103. public void tearDown() throws Exception {
  104. this.ctx.unbind(this.tempDir.getAbsolutePath() + "/test");
  105. this.ctx.unbind(this.tempDir.getAbsolutePath() + "/testNoUrl");
  106. this.ctx.close();
  107. this.tempDir.delete();
  108. super.tearDown();
  109. }
  110. /**
  111. * Tests fix for BUG#4808- Calling .close() twice on a PooledConnection
  112. * causes NPE.
  113. *
  114. * @throws Exception
  115. * if an error occurs.
  116. */
  117. public void testBug4808() throws Exception {
  118. MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
  119. ds.setURL(BaseTestCase.dbUrl);
  120. PooledConnection closeMeTwice = ds.getPooledConnection();
  121. closeMeTwice.close();
  122. closeMeTwice.close();
  123. }
  124. /**
  125. * Tests fix for Bug#3848, port # alone parsed incorrectly
  126. *
  127. * @throws Exception
  128. * ...
  129. */
  130. public void testBug3848() throws Exception {
  131. String jndiName = "/testBug3848";
  132. String databaseName = System.getProperty(DS_DATABASE_PROP_NAME);
  133. String userName = System.getProperty(DS_USER_PROP_NAME);
  134. String password = System.getProperty(DS_PASSWORD_PROP_NAME);
  135. String port = System.getProperty(DS_PORT_PROP_NAME);
  136. // Only run this test if at least one of the above are set
  137. if ((databaseName != null) || (userName != null) || (password != null)
  138. || (port != null)) {
  139. MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
  140. if (databaseName != null) {
  141. ds.setDatabaseName(databaseName);
  142. }
  143. if (userName != null) {
  144. ds.setUser(userName);
  145. }
  146. if (password != null) {
  147. ds.setPassword(password);
  148. }
  149. if (port != null) {
  150. ds.setPortNumber(Integer.parseInt(port));
  151. }
  152. bindDataSource(jndiName, ds);
  153. ConnectionPoolDataSource boundDs = null;
  154. try {
  155. boundDs = (ConnectionPoolDataSource) lookupDatasourceInJNDI(jndiName);
  156. assertTrue("Datasource not bound", boundDs != null);
  157. Connection dsConn = null;
  158. try {
  159. dsConn = boundDs.getPooledConnection().getConnection();
  160. } finally {
  161. if (dsConn != null) {
  162. dsConn.close();
  163. }
  164. }
  165. } finally {
  166. if (boundDs != null) {
  167. this.ctx.unbind(jndiName);
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * Tests that we can get a connection from the DataSource bound in JNDI
  174. * during test setup
  175. *
  176. * @throws Exception
  177. * if an error occurs
  178. */
  179. public void testBug3920() throws Exception {
  180. String jndiName = "/testBug3920";
  181. String databaseName = System.getProperty(DS_DATABASE_PROP_NAME);
  182. String userName = System.getProperty(DS_USER_PROP_NAME);
  183. String password = System.getProperty(DS_PASSWORD_PROP_NAME);
  184. String port = System.getProperty(DS_PORT_PROP_NAME);
  185. String serverName = System.getProperty(DS_HOST_PROP_NAME);
  186. // Only run this test if at least one of the above are set
  187. if ((databaseName != null) || (serverName != null)
  188. || (userName != null) || (password != null) || (port != null)) {
  189. MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
  190. if (databaseName != null) {
  191. ds.setDatabaseName(databaseName);
  192. }
  193. if (userName != null) {
  194. ds.setUser(userName);
  195. }
  196. if (password != null) {
  197. ds.setPassword(password);
  198. }
  199. if (port != null) {
  200. ds.setPortNumber(Integer.parseInt(port));
  201. }
  202. if (serverName != null) {
  203. ds.setServerName(serverName);
  204. }
  205. bindDataSource(jndiName, ds);
  206. ConnectionPoolDataSource boundDs = null;
  207. try {
  208. boundDs = (ConnectionPoolDataSource) lookupDatasourceInJNDI(jndiName);
  209. assertTrue("Datasource not bound", boundDs != null);
  210. Connection dsCon = null;
  211. Statement dsStmt = null;
  212. try {
  213. dsCon = boundDs.getPooledConnection().getConnection();
  214. dsStmt = dsCon.createStatement();
  215. dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920");
  216. dsStmt
  217. .executeUpdate("CREATE TABLE testBug3920 (field1 varchar(32))");
  218. assertTrue(
  219. "Connection can not be obtained from data source",
  220. dsCon != null);
  221. } finally {
  222. dsStmt.executeUpdate("DROP TABLE IF EXISTS testBug3920");
  223. dsStmt.close();
  224. dsCon.close();
  225. }
  226. } finally {
  227. if (boundDs != null) {
  228. this.ctx.unbind(jndiName);
  229. }
  230. }
  231. }
  232. }
  233. /**
  234. * Tests fix for BUG#19169 - ConnectionProperties (and thus some
  235. * subclasses) are not serializable, even though some J2EE containers
  236. * expect them to be.
  237. *
  238. * @throws Exception if the test fails.
  239. */
  240. public void testBug19169() throws Exception {
  241. MysqlDataSource toSerialize = new MysqlDataSource();
  242. toSerialize.setZeroDateTimeBehavior("convertToNull");
  243. boolean testBooleanFlag = !toSerialize.getAllowLoadLocalInfile();
  244. toSerialize.setAllowLoadLocalInfile(testBooleanFlag);
  245. int testIntFlag = toSerialize.getBlobSendChunkSize() + 1;
  246. toSerialize.setBlobSendChunkSize(String.valueOf(testIntFlag));
  247. ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  248. ObjectOutputStream objOut = new ObjectOutputStream(bOut);
  249. objOut.writeObject(toSerialize);
  250. objOut.flush();
  251. ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray()));
  252. MysqlDataSource thawedDs = (MysqlDataSource)objIn.readObject();
  253. assertEquals("convertToNull", thawedDs.getZeroDateTimeBehavior());
  254. assertEquals(testBooleanFlag, thawedDs.getAllowLoadLocalInfile());
  255. assertEquals(testIntFlag, thawedDs.getBlobSendChunkSize());
  256. }
  257. /**
  258. * Tests fix for BUG#20242 - MysqlValidConnectionChecker for JBoss doesn't
  259. * work with MySQLXADataSources.
  260. *
  261. * @throws Exception if the test fails.
  262. */
  263. public void testBug20242() throws Exception {
  264. if (versionMeetsMinimum(5, 0)) {
  265. try {
  266. Class.forName("org.jboss.resource.adapter.jdbc.ValidConnectionChecker");
  267. } catch (Exception ex) {
  268. return; // class not available for testing
  269. }
  270. MysqlXADataSource xaDs = new MysqlXADataSource();
  271. xaDs.setUrl(dbUrl);
  272. MysqlValidConnectionChecker checker = new MysqlValidConnectionChecker();
  273. assertNull(checker.isValidConnection(xaDs.getXAConnection().getConnection()));
  274. }
  275. }
  276. private void bindDataSource(String name, DataSource ds) throws Exception {
  277. this.ctx.bind(this.tempDir.getAbsolutePath() + name, ds);
  278. }
  279. /**
  280. * This method is separated from the rest of the example since you normally
  281. * would NOT register a JDBC driver in your code. It would likely be
  282. * configered into your naming and directory service using some GUI.
  283. *
  284. * @throws Exception
  285. * if an error occurs
  286. */
  287. private void createJNDIContext() throws Exception {
  288. this.tempDir = File.createTempFile("jnditest", null);
  289. this.tempDir.delete();
  290. this.tempDir.mkdir();
  291. this.tempDir.deleteOnExit();
  292. MysqlConnectionPoolDataSource ds;
  293. Hashtable env = new Hashtable();
  294. env.put(Context.INITIAL_CONTEXT_FACTORY,
  295. "com.sun.jndi.fscontext.RefFSContextFactory");
  296. this.ctx = new InitialContext(env);
  297. assertTrue("Naming Context not created", this.ctx != null);
  298. ds = new MysqlConnectionPoolDataSource();
  299. ds.setUrl(dbUrl); // from BaseTestCase
  300. ds.setDatabaseName("test");
  301. this.ctx.bind(this.tempDir.getAbsolutePath() + "/test", ds);
  302. }
  303. private DataSource lookupDatasourceInJNDI(String jndiName) throws Exception {
  304. NameParser nameParser = this.ctx.getNameParser("");
  305. Name datasourceName = nameParser.parse(this.tempDir.getAbsolutePath()
  306. + jndiName);
  307. Object obj = this.ctx.lookup(datasourceName);
  308. DataSource boundDs = null;
  309. if (obj instanceof DataSource) {
  310. boundDs = (DataSource) obj;
  311. } else if (obj instanceof Reference) {
  312. //
  313. // For some reason, this comes back as a Reference
  314. // instance under CruiseControl !?
  315. //
  316. Reference objAsRef = (Reference) obj;
  317. ObjectFactory factory = (ObjectFactory) Class.forName(
  318. objAsRef.getFactoryClassName()).newInstance();
  319. boundDs = (DataSource) factory.getObjectInstance(objAsRef,
  320. datasourceName, this.ctx, new Hashtable());
  321. }
  322. return boundDs;
  323. }
  324. public void testCSC4616() throws Exception {
  325. MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
  326. ds.setURL(BaseTestCase.dbUrl);
  327. PooledConnection pooledConn = ds.getPooledConnection();
  328. Connection physConn = pooledConn.getConnection();
  329. Statement physStatement = physConn.createStatement();
  330. Method enableStreamingResultsMethodStmt = Class.forName(
  331. "com.mysql.jdbc.jdbc2.optional.StatementWrapper").getMethod(
  332. "enableStreamingResults", new Class[0]);
  333. enableStreamingResultsMethodStmt.invoke(physStatement, (Object[])null);
  334. this.rs = physStatement.executeQuery("SELECT 1");
  335. try {
  336. physConn.createStatement().executeQuery("SELECT 2");
  337. fail("Should have caught a streaming exception here");
  338. } catch (SQLException sqlEx) {
  339. assertTrue(sqlEx.getMessage() != null
  340. && sqlEx.getMessage().indexOf("Streaming") != -1);
  341. } finally {
  342. if (this.rs != null) {
  343. this.rs.close();
  344. this.rs = null;
  345. }
  346. }
  347. PreparedStatement physPrepStmt = physConn.prepareStatement("SELECT 1");
  348. Method enableStreamingResultsMethodPstmt = Class.forName(
  349. "com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper")
  350. .getMethod("enableStreamingResults", (Class[])null);
  351. enableStreamingResultsMethodPstmt.invoke(physPrepStmt, (Object[])null);
  352. this.rs = physPrepStmt.executeQuery();
  353. try {
  354. physConn.createStatement().executeQuery("SELECT 2");
  355. fail("Should have caught a streaming exception here");
  356. } catch (SQLException sqlEx) {
  357. assertTrue(sqlEx.getMessage() != null
  358. && sqlEx.getMessage().indexOf("Streaming") != -1);
  359. } finally {
  360. if (this.rs != null) {
  361. this.rs.close();
  362. this.rs = null;
  363. }
  364. }
  365. }
  366. /**
  367. * Tests fix for BUG#16791 - NullPointerException in MysqlDataSourceFactory
  368. * due to Reference containing RefAddrs with null content.
  369. *
  370. * @throws Exception if the test fails
  371. */
  372. public void testBug16791() throws Exception {
  373. MysqlDataSource myDs = new MysqlDataSource();
  374. myDs.setUrl(dbUrl);
  375. Reference asRef = myDs.getReference();
  376. System.out.println(asRef);
  377. removeFromRef(asRef, "port");
  378. removeFromRef(asRef, NonRegisteringDriver.USER_PROPERTY_KEY);
  379. removeFromRef(asRef, NonRegisteringDriver.PASSWORD_PROPERTY_KEY);
  380. removeFromRef(asRef, "serverName");
  381. removeFromRef(asRef, "databaseName");
  382. MysqlDataSource newDs = (MysqlDataSource)new MysqlDataSourceFactory().getObjectInstance(asRef, null, null, null);
  383. }
  384. private void removeFromRef(Reference ref, String key) {
  385. int size = ref.size();
  386. for (int i = 0; i < size; i++) {
  387. RefAddr refAddr = ref.get(i);
  388. if (refAddr.getType().equals(key)) {
  389. ref.remove(i);
  390. break;
  391. }
  392. }
  393. }
  394. /**
  395. * Tests fix for BUG#32101 - When using a connection from our ConnectionPoolDataSource,
  396. * some Connection.prepareStatement() methods would return null instead of
  397. * a prepared statement.
  398. *
  399. * @throws Exception
  400. */
  401. public void testBug32101() throws Exception {
  402. MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
  403. ds.setURL(BaseTestCase.dbUrl);
  404. PooledConnection pc = ds.getPooledConnection();
  405. assertNotNull(pc.getConnection().prepareStatement("SELECT 1"));
  406. assertNotNull(pc.getConnection().prepareStatement("SELECT 1", Statement.RETURN_GENERATED_KEYS));
  407. assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new int[0]));
  408. assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new String[0]));
  409. assertNotNull(pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
  410. assertNotNull(pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT));
  411. }
  412. public void testBug35810() throws Exception {
  413. int defaultConnectTimeout = ((ConnectionProperties) this.conn).getConnectTimeout();
  414. int nonDefaultConnectTimeout = defaultConnectTimeout + 1000 * 2;
  415. MysqlConnectionPoolDataSource cpds = new MysqlConnectionPoolDataSource();
  416. String dsUrl = BaseTestCase.dbUrl;
  417. if (dsUrl.indexOf("?") == -1) {
  418. dsUrl += "?";
  419. } else {
  420. dsUrl += "&";
  421. }
  422. dsUrl += "connectTimeout=" + nonDefaultConnectTimeout;
  423. cpds.setUrl(dsUrl);
  424. Connection dsConn = cpds.getPooledConnection().getConnection();
  425. int configuredConnectTimeout = ((ConnectionProperties) dsConn).getConnectTimeout();
  426. assertEquals("Connect timeout spec'd by URL didn't take", nonDefaultConnectTimeout, configuredConnectTimeout);
  427. assertFalse("Connect timeout spec'd by URL didn't take", defaultConnectTimeout == configuredConnectTimeout);
  428. }
  429. }