KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > regression > DataSourceRegressionTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
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.
7
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.
12
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.
17
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
21
22
23
24  */

25 package testsuite.regression;
26
27 import java.io.File JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Statement JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.Name JavaDoc;
39 import javax.naming.NameParser JavaDoc;
40 import javax.naming.Reference JavaDoc;
41 import javax.naming.spi.ObjectFactory JavaDoc;
42 import javax.sql.ConnectionPoolDataSource JavaDoc;
43 import javax.sql.DataSource JavaDoc;
44 import javax.sql.PooledConnection JavaDoc;
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 /**
53  * Tests fixes for bugs related to datasources.
54  *
55  * @author Mark Matthews
56  *
57  * @version $Id: DataSourceRegressionTest.java,v 1.1.2.1 2005/05/13 18:58:38
58  * mmatthews Exp $
59  */

60 public class DataSourceRegressionTest extends BaseTestCase {
61
62     public final static String JavaDoc DS_DATABASE_PROP_NAME = "com.mysql.jdbc.test.ds.db";
63
64     public final static String JavaDoc DS_HOST_PROP_NAME = "com.mysql.jdbc.test.ds.host";
65
66     public final static String JavaDoc DS_PASSWORD_PROP_NAME = "com.mysql.jdbc.test.ds.password";
67
68     public final static String JavaDoc DS_PORT_PROP_NAME = "com.mysql.jdbc.test.ds.port";
69
70     public final static String JavaDoc DS_USER_PROP_NAME = "com.mysql.jdbc.test.ds.user";
71
72     private Context JavaDoc ctx;
73
74     private File JavaDoc tempDir;
75
76     /**
77      * Creates a new DataSourceRegressionTest suite for the given test name
78      *
79      * @param name
80      * the name of the testcase to run.
81      */

82     public DataSourceRegressionTest(String JavaDoc name) {
83         super(name);
84
85         // TODO Auto-generated constructor stub
86
}
87
88     /**
89      * Runs all test cases in this test suite
90      *
91      * @param args
92      */

93     public static void main(String JavaDoc[] args) {
94         junit.textui.TestRunner.run(DataSourceTest.class);
95     }
96
97     /**
98      * Sets up this test, calling registerDataSource() to bind a DataSource into
99      * JNDI, using the FSContext JNDI provider from Sun
100      *
101      * @throws Exception
102      * if an error occurs.
103      */

104     public void setUp() throws Exception JavaDoc {
105         super.setUp();
106         createJNDIContext();
107     }
108
109     /**
110      * Un-binds the DataSource, and cleans up the filesystem
111      *
112      * @throws Exception
113      * if an error occurs
114      */

115     public void tearDown() throws Exception JavaDoc {
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     /**
125      * Tests fix for BUG#4808- Calling .close() twice on a PooledConnection
126      * causes NPE.
127      *
128      * @throws Exception
129      * if an error occurs.
130      */

131     public void testBug4808() throws Exception JavaDoc {
132         MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
133         ds.setURL(BaseTestCase.dbUrl);
134         PooledConnection JavaDoc closeMeTwice = ds.getPooledConnection();
135         closeMeTwice.close();
136         closeMeTwice.close();
137
138     }
139
140     /**
141      * Tests fix for Bug#3848, port # alone parsed incorrectly
142      *
143      * @throws Exception
144      * ...
145      */

146     public void testBug3848() throws Exception JavaDoc {
147         String JavaDoc jndiName = "/testBug3848";
148
149         String JavaDoc databaseName = System.getProperty(DS_DATABASE_PROP_NAME);
150         String JavaDoc userName = System.getProperty(DS_USER_PROP_NAME);
151         String JavaDoc password = System.getProperty(DS_PASSWORD_PROP_NAME);
152         String JavaDoc port = System.getProperty(DS_PORT_PROP_NAME);
153
154         // Only run this test if at least one of the above are set
155
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 JavaDoc boundDs = null;
178
179             try {
180                 boundDs = (ConnectionPoolDataSource JavaDoc) lookupDatasourceInJNDI(jndiName);
181
182                 assertTrue("Datasource not bound", boundDs != null);
183
184                 Connection JavaDoc 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     /**
202      * Tests that we can get a connection from the DataSource bound in JNDI
203      * during test setup
204      *
205      * @throws Exception
206      * if an error occurs
207      */

208     public void testBug3920() throws Exception JavaDoc {
209         String JavaDoc jndiName = "/testBug3920";
210
211         String JavaDoc databaseName = System.getProperty(DS_DATABASE_PROP_NAME);
212         String JavaDoc userName = System.getProperty(DS_USER_PROP_NAME);
213         String JavaDoc password = System.getProperty(DS_PASSWORD_PROP_NAME);
214         String JavaDoc port = System.getProperty(DS_PORT_PROP_NAME);
215         String JavaDoc serverName = System.getProperty(DS_HOST_PROP_NAME);
216
217         // Only run this test if at least one of the above are set
218
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 JavaDoc boundDs = null;
245
246             try {
247                 boundDs = (ConnectionPoolDataSource JavaDoc) lookupDatasourceInJNDI(jndiName);
248
249                 assertTrue("Datasource not bound", boundDs != null);
250
251                 Connection JavaDoc dsCon = null;
252                 Statement JavaDoc 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 JavaDoc name, DataSource JavaDoc ds) throws Exception JavaDoc {
279         this.ctx.bind(this.tempDir.getAbsolutePath() + name, ds);
280     }
281
282     /**
283      * This method is separated from the rest of the example since you normally
284      * would NOT register a JDBC driver in your code. It would likely be
285      * configered into your naming and directory service using some GUI.
286      *
287      * @throws Exception
288      * if an error occurs
289      */

290     private void createJNDIContext() throws Exception JavaDoc {
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 JavaDoc env = new Hashtable JavaDoc();
298         env.put(Context.INITIAL_CONTEXT_FACTORY,
299                 "com.sun.jndi.fscontext.RefFSContextFactory");
300         this.ctx = new InitialContext JavaDoc(env);
301         assertTrue("Naming Context not created", this.ctx != null);
302         ds = new MysqlConnectionPoolDataSource();
303         ds.setUrl(dbUrl); // from BaseTestCase
304
ds.setDatabaseName("test");
305         this.ctx.bind(this.tempDir.getAbsolutePath() + "/test", ds);
306     }
307
308     private DataSource JavaDoc lookupDatasourceInJNDI(String JavaDoc jndiName) throws Exception JavaDoc {
309         NameParser JavaDoc nameParser = this.ctx.getNameParser("");
310         Name JavaDoc datasourceName = nameParser.parse(this.tempDir.getAbsolutePath()
311                 + jndiName);
312         Object JavaDoc obj = this.ctx.lookup(datasourceName);
313         DataSource JavaDoc boundDs = null;
314
315         if (obj instanceof DataSource JavaDoc) {
316             boundDs = (DataSource JavaDoc) obj;
317         } else if (obj instanceof Reference JavaDoc) {
318             //
319
// For some reason, this comes back as a Reference
320
// instance under CruiseControl !?
321
//
322
Reference JavaDoc objAsRef = (Reference JavaDoc) obj;
323             ObjectFactory JavaDoc factory = (ObjectFactory JavaDoc) Class.forName(
324                     objAsRef.getFactoryClassName()).newInstance();
325             boundDs = (DataSource JavaDoc) factory.getObjectInstance(objAsRef,
326                     datasourceName, this.ctx, new Hashtable JavaDoc());
327         }
328
329         return boundDs;
330     }
331
332     public void testCSC4616() throws Exception JavaDoc {
333         MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
334         ds.setURL(BaseTestCase.dbUrl);
335         PooledConnection JavaDoc pooledConn = ds.getPooledConnection();
336         Connection JavaDoc physConn = pooledConn.getConnection();
337         Statement JavaDoc physStatement = physConn.createStatement();
338
339         Method JavaDoc enableStreamingResultsMethodStmt = Class.forName(
340                 "com.mysql.jdbc.jdbc2.optional.StatementWrapper").getMethod(
341                 "enableStreamingResults", new Class JavaDoc[0]);
342         enableStreamingResultsMethodStmt.invoke(physStatement, new Class JavaDoc[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 JavaDoc 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 JavaDoc physPrepStmt = physConn.prepareStatement("SELECT 1");
359         Method JavaDoc enableStreamingResultsMethodPstmt = Class.forName(
360                 "com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper")
361                 .getMethod("enableStreamingResults", new Class JavaDoc[0]);
362         enableStreamingResultsMethodPstmt.invoke(physPrepStmt, new Class JavaDoc[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 JavaDoc 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