Source Code Cross Referenced for DataSourceTest.java in  » Database-JDBC-Connection-Pool » mysql-connector-java-5.1.3 » testsuite » simple » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » mysql connector java 5.1.3 » testsuite.simple 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.