1 25 package testsuite.simple; 26 27 import testsuite.BaseTestCase; 28 29 import java.io.File ; 30 31 import java.sql.Connection ; 32 33 import java.util.Hashtable ; 34 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.Name ; 38 import javax.naming.NameParser ; 39 import javax.naming.Reference ; 40 import javax.naming.spi.ObjectFactory ; 41 42 import javax.sql.DataSource ; 43 import javax.sql.PooledConnection ; 44 45 import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource; 46 47 52 public class DataSourceTest extends BaseTestCase { 53 56 private Context ctx; 57 58 private File tempDir; 59 60 63 69 public DataSourceTest(String name) { 70 super(name); 71 } 72 73 76 81 public static void main(String [] args) { 82 junit.textui.TestRunner.run(DataSourceTest.class); 83 } 84 85 92 public void setUp() throws Exception { 93 super.setUp(); 94 registerDataSource(); 95 } 96 97 103 public void tearDown() throws Exception { 104 this.ctx.unbind(this.tempDir.getAbsolutePath() + "/test"); 105 this.ctx.close(); 106 this.tempDir.delete(); 107 super.tearDown(); 108 } 109 110 117 public void testDataSource() throws Exception { 118 NameParser nameParser = this.ctx.getNameParser(""); 119 Name datasourceName = nameParser.parse(this.tempDir.getAbsolutePath() 120 + "/test"); 121 Object obj = this.ctx.lookup(datasourceName); 122 DataSource boundDs = null; 123 124 if (obj instanceof DataSource ) { 125 boundDs = (DataSource ) obj; 126 } else if (obj instanceof Reference ) { 127 Reference objAsRef = (Reference ) obj; 132 ObjectFactory factory = (ObjectFactory ) Class.forName( 133 objAsRef.getFactoryClassName()).newInstance(); 134 boundDs = (DataSource ) factory.getObjectInstance(objAsRef, 135 datasourceName, this.ctx, new Hashtable ()); 136 } 137 138 assertTrue("Datasource not bound", boundDs != null); 139 140 Connection con = boundDs.getConnection(); 141 con.close(); 142 assertTrue("Connection can not be obtained from data source", 143 con != null); 144 } 145 146 153 public void testChangeUserAndCharsets() throws Exception { 154 if (versionMeetsMinimum(4, 1)) { 155 MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); 156 ds.setURL(BaseTestCase.dbUrl); 157 ds.setCharacterEncoding("utf-8"); 158 PooledConnection pooledConnection = ds.getPooledConnection(); 159 160 Connection 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 196 private void registerDataSource() throws Exception { 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 env = new Hashtable (); 204 env.put(Context.INITIAL_CONTEXT_FACTORY, 205 "com.sun.jndi.fscontext.RefFSContextFactory"); 206 this.ctx = new InitialContext (env); 207 assertTrue("Naming Context not created", this.ctx != null); 208 ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource(); 209 ds.setUrl(dbUrl); this.ctx.bind(this.tempDir.getAbsolutePath() + "/test", ds); 211 } 212 } 213 | Popular Tags |