001: /*
002: Copyright (C) 2002-2007 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package testsuite.simple;
026:
027: import testsuite.BaseTestCase;
028:
029: import java.io.File;
030:
031: import java.sql.Connection;
032:
033: import java.util.Hashtable;
034:
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037: import javax.naming.Name;
038: import javax.naming.NameParser;
039: import javax.naming.Reference;
040: import javax.naming.spi.ObjectFactory;
041:
042: import javax.sql.DataSource;
043: import javax.sql.PooledConnection;
044:
045: import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
046: import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
047:
048: /**
049: *
050: * @author Mark Matthews
051: * @version $Id: DataSourceTest.java 6562 2007-09-05 16:02:17Z mmatthews $
052: */
053: public class DataSourceTest extends BaseTestCase {
054: // ~ Instance fields
055: // --------------------------------------------------------
056:
057: private Context ctx;
058:
059: private File tempDir;
060:
061: // ~ Constructors
062: // -----------------------------------------------------------
063:
064: /**
065: * Creates a new DataSourceTest object.
066: *
067: * @param name
068: * DOCUMENT ME!
069: */
070: public DataSourceTest(String name) {
071: super (name);
072: }
073:
074: // ~ Methods
075: // ----------------------------------------------------------------
076:
077: /**
078: * Runs all test cases in this test suite
079: *
080: * @param args
081: */
082: public static void main(String[] args) {
083: junit.textui.TestRunner.run(DataSourceTest.class);
084: }
085:
086: /**
087: * Sets up this test, calling registerDataSource() to bind a DataSource into
088: * JNDI, using the FSContext JNDI provider from Sun
089: *
090: * @throws Exception
091: * if an error occurs.
092: */
093: public void setUp() throws Exception {
094: super .setUp();
095: registerDataSource();
096: }
097:
098: /**
099: * Un-binds the DataSource, and cleans up the filesystem
100: *
101: * @throws Exception
102: * if an error occurs
103: */
104: public void tearDown() throws Exception {
105: this .ctx.unbind(this .tempDir.getAbsolutePath() + "/test");
106: this .ctx.close();
107: this .tempDir.delete();
108: super .tearDown();
109: }
110:
111: /**
112: * Tests that we can get a connection from the DataSource bound in JNDI
113: * during test setup
114: *
115: * @throws Exception
116: * if an error occurs
117: */
118: public void testDataSource() throws Exception {
119: NameParser nameParser = this .ctx.getNameParser("");
120: Name datasourceName = nameParser.parse(this .tempDir
121: .getAbsolutePath()
122: + "/test");
123: Object obj = this .ctx.lookup(datasourceName);
124: DataSource boundDs = null;
125:
126: if (obj instanceof DataSource) {
127: boundDs = (DataSource) obj;
128: } else if (obj instanceof Reference) {
129: //
130: // For some reason, this comes back as a Reference
131: // instance under CruiseControl !?
132: //
133: Reference objAsRef = (Reference) obj;
134: ObjectFactory factory = (ObjectFactory) Class.forName(
135: objAsRef.getFactoryClassName()).newInstance();
136: boundDs = (DataSource) factory.getObjectInstance(objAsRef,
137: datasourceName, this .ctx, new Hashtable());
138: }
139:
140: assertTrue("Datasource not bound", boundDs != null);
141:
142: Connection con = boundDs.getConnection();
143: con.close();
144: assertTrue("Connection can not be obtained from data source",
145: con != null);
146: }
147:
148: /**
149: * Tests whether Connection.changeUser() (and thus pooled connections)
150: * restore character set information correctly.
151: *
152: * @throws Exception
153: * if the test fails.
154: */
155: public void testChangeUserAndCharsets() throws Exception {
156: if (versionMeetsMinimum(4, 1)) {
157: MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
158: ds.setURL(BaseTestCase.dbUrl);
159: ds.setCharacterEncoding("utf-8");
160: PooledConnection pooledConnection = ds
161: .getPooledConnection();
162:
163: Connection connToMySQL = pooledConnection.getConnection();
164: this .rs = connToMySQL.createStatement().executeQuery(
165: "SELECT @@character_set_results");
166: assertTrue(this .rs.next());
167:
168: String toCheck = null;
169:
170: if (versionMeetsMinimum(4, 1, 15)) {
171: if (versionMeetsMinimum(5, 0)) {
172: if (versionMeetsMinimum(5, 0, 13)) {
173: toCheck = null;
174: } else {
175: toCheck = "NULL";
176: }
177: } else {
178: toCheck = null;
179: }
180: } else {
181: toCheck = "NULL";
182: }
183:
184: assertEquals(toCheck, this .rs.getString(1));
185:
186: this .rs = connToMySQL
187: .createStatement()
188: .executeQuery(
189: "SHOW SESSION VARIABLES LIKE 'character_set_client'");
190: assertTrue(this .rs.next());
191: assertEquals("utf8", this .rs.getString(2));
192:
193: connToMySQL.close();
194:
195: connToMySQL = pooledConnection.getConnection();
196: this .rs = connToMySQL.createStatement().executeQuery(
197: "SELECT @@character_set_results");
198: assertTrue(this .rs.next());
199: assertEquals(toCheck, this .rs.getString(1));
200:
201: this .rs = connToMySQL
202: .createStatement()
203: .executeQuery(
204: "SHOW SESSION VARIABLES LIKE 'character_set_client'");
205: assertTrue(this .rs.next());
206: assertEquals("utf8", this .rs.getString(2));
207:
208: pooledConnection.getConnection().close();
209: }
210: }
211:
212: /**
213: * Tests whether XADataSources can be bound into JNDI
214: *
215: * @throws Exception if the test fails.
216: */
217: public void testXADataSource() throws Exception {
218:
219: MysqlXADataSource ds = new MysqlXADataSource();
220: ds.setUrl(dbUrl);
221:
222: String name = "XA";
223: this .ctx.rebind(name, ds);
224:
225: Object result = this .ctx.lookup(name);
226:
227: assertNotNull("XADataSource not bound into JNDI", result);
228: }
229:
230: /**
231: * This method is separated from the rest of the example since you normally
232: * would NOT register a JDBC driver in your code. It would likely be
233: * configered into your naming and directory service using some GUI.
234: *
235: * @throws Exception
236: * if an error occurs
237: */
238: private void registerDataSource() throws Exception {
239: this .tempDir = File.createTempFile("jnditest", null);
240: this .tempDir.delete();
241: this .tempDir.mkdir();
242: this .tempDir.deleteOnExit();
243:
244: com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds;
245: Hashtable env = new Hashtable();
246: env.put(Context.INITIAL_CONTEXT_FACTORY,
247: "com.sun.jndi.fscontext.RefFSContextFactory");
248: this .ctx = new InitialContext(env);
249: assertTrue("Naming Context not created", this .ctx != null);
250: ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
251: ds.setUrl(dbUrl); // from BaseTestCase
252: this .ctx.bind(this .tempDir.getAbsolutePath() + "/test", ds);
253: }
254: }
|