KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > DataSourceTest


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.simple;
26
27 import testsuite.BaseTestCase;
28
29 import java.io.File JavaDoc;
30
31 import java.sql.Connection JavaDoc;
32
33 import java.util.Hashtable JavaDoc;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.Name JavaDoc;
38 import javax.naming.NameParser JavaDoc;
39 import javax.naming.Reference JavaDoc;
40 import javax.naming.spi.ObjectFactory JavaDoc;
41
42 import javax.sql.DataSource JavaDoc;
43 import javax.sql.PooledConnection JavaDoc;
44
45 import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
46
47 /**
48  *
49  * @author Mark Matthews
50  * @version $Id: DataSourceTest.java,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
51  */

52 public class DataSourceTest extends BaseTestCase {
53     // ~ Instance fields
54
// --------------------------------------------------------
55

56     private Context JavaDoc ctx;
57
58     private File JavaDoc tempDir;
59
60     // ~ Constructors
61
// -----------------------------------------------------------
62

63     /**
64      * Creates a new DataSourceTest object.
65      *
66      * @param name
67      * DOCUMENT ME!
68      */

69     public DataSourceTest(String JavaDoc name) {
70         super(name);
71     }
72
73     // ~ Methods
74
// ----------------------------------------------------------------
75

76     /**
77      * Runs all test cases in this test suite
78      *
79      * @param args
80      */

81     public static void main(String JavaDoc[] args) {
82         junit.textui.TestRunner.run(DataSourceTest.class);
83     }
84
85     /**
86      * Sets up this test, calling registerDataSource() to bind a DataSource into
87      * JNDI, using the FSContext JNDI provider from Sun
88      *
89      * @throws Exception
90      * if an error occurs.
91      */

92     public void setUp() throws Exception JavaDoc {
93         super.setUp();
94         registerDataSource();
95     }
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 JavaDoc {
104         this.ctx.unbind(this.tempDir.getAbsolutePath() + "/test");
105         this.ctx.close();
106         this.tempDir.delete();
107         super.tearDown();
108     }
109
110     /**
111      * Tests that we can get a connection from the DataSource bound in JNDI
112      * during test setup
113      *
114      * @throws Exception
115      * if an error occurs
116      */

117     public void testDataSource() throws Exception JavaDoc {
118         NameParser JavaDoc nameParser = this.ctx.getNameParser("");
119         Name JavaDoc datasourceName = nameParser.parse(this.tempDir.getAbsolutePath()
120                 + "/test");
121         Object JavaDoc obj = this.ctx.lookup(datasourceName);
122         DataSource JavaDoc boundDs = null;
123
124         if (obj instanceof DataSource JavaDoc) {
125             boundDs = (DataSource JavaDoc) obj;
126         } else if (obj instanceof Reference JavaDoc) {
127             //
128
// For some reason, this comes back as a Reference
129
// instance under CruiseControl !?
130
//
131
Reference JavaDoc objAsRef = (Reference JavaDoc) obj;
132             ObjectFactory JavaDoc factory = (ObjectFactory JavaDoc) Class.forName(
133                     objAsRef.getFactoryClassName()).newInstance();
134             boundDs = (DataSource JavaDoc) factory.getObjectInstance(objAsRef,
135                     datasourceName, this.ctx, new Hashtable JavaDoc());
136         }
137
138         assertTrue("Datasource not bound", boundDs != null);
139
140         Connection JavaDoc con = boundDs.getConnection();
141         con.close();
142         assertTrue("Connection can not be obtained from data source",
143                 con != null);
144     }
145
146     /**
147      * Tests whether Connection.changeUser() (and thus pooled connections)
148      * restore character set information correctly.
149      *
150      * @throws Exception
151      * if the test fails.
152      */

153     public void testChangeUserAndCharsets() throws Exception JavaDoc {
154         if (versionMeetsMinimum(4, 1)) {
155             MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
156             ds.setURL(BaseTestCase.dbUrl);
157             ds.setCharacterEncoding("utf-8");
158             PooledConnection JavaDoc pooledConnection = ds.getPooledConnection();
159
160             Connection JavaDoc connToMySQL = pooledConnection.getConnection();
161             this.rs = connToMySQL.createStatement().executeQuery(
162                     "SHOW VARIABLES LIKE 'character_set_results'");
163             assertTrue(this.rs.next());
164             assertTrue("NULL".equalsIgnoreCase(this.rs.getString(2)));
165
166             this.rs = connToMySQL.createStatement().executeQuery(
167                     "SHOW VARIABLES LIKE 'character_set_client'");
168             assertTrue(this.rs.next());
169             assertTrue("utf8".equalsIgnoreCase(this.rs.getString(2)));
170
171             connToMySQL.close();
172
173             connToMySQL = pooledConnection.getConnection();
174             this.rs = connToMySQL.createStatement().executeQuery(
175                     "SHOW VARIABLES LIKE 'character_set_results'");
176             assertTrue(this.rs.next());
177             assertTrue("NULL".equalsIgnoreCase(this.rs.getString(2)));
178
179             this.rs = connToMySQL.createStatement().executeQuery(
180                     "SHOW VARIABLES LIKE 'character_set_client'");
181             assertTrue(this.rs.next());
182             assertTrue("utf8".equalsIgnoreCase(this.rs.getString(2)));
183
184             pooledConnection.getConnection().close();
185         }
186     }
187
188     /**
189      * This method is separated from the rest of the example since you normally
190      * would NOT register a JDBC driver in your code. It would likely be
191      * configered into your naming and directory service using some GUI.
192      *
193      * @throws Exception
194      * if an error occurs
195      */

196     private void registerDataSource() throws Exception JavaDoc {
197         this.tempDir = File.createTempFile("jnditest", null);
198         this.tempDir.delete();
199         this.tempDir.mkdir();
200         this.tempDir.deleteOnExit();
201
202         com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds;
203         Hashtable JavaDoc env = new Hashtable JavaDoc();
204         env.put(Context.INITIAL_CONTEXT_FACTORY,
205                 "com.sun.jndi.fscontext.RefFSContextFactory");
206         this.ctx = new InitialContext JavaDoc(env);
207         assertTrue("Naming Context not created", this.ctx != null);
208         ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
209         ds.setUrl(dbUrl); // from BaseTestCase
210
this.ctx.bind(this.tempDir.getAbsolutePath() + "/test", ds);
211     }
212 }
213
Popular Tags