1 16 package scriptella.driver.jndi; 17 18 import scriptella.AbstractTestCase; 19 import scriptella.configuration.MockConnectionEl; 20 import scriptella.jdbc.JdbcConnection; 21 import scriptella.jdbc.JdbcException; 22 import scriptella.spi.ConnectionParameters; 23 import scriptella.spi.MockDriverContext; 24 25 import javax.naming.Context ; 26 import javax.naming.NamingException ; 27 import javax.naming.spi.InitialContextFactory ; 28 import javax.sql.DataSource ; 29 import java.lang.reflect.InvocationHandler ; 30 import java.lang.reflect.Method ; 31 import java.lang.reflect.Proxy ; 32 import java.sql.DriverManager ; 33 import java.sql.SQLException ; 34 import java.util.HashMap ; 35 import java.util.Hashtable ; 36 import java.util.Map ; 37 import java.util.logging.Logger ; 38 39 45 public class JNDIDriverTest extends AbstractTestCase { 46 47 50 public void testGetConnection() throws NamingException { 51 Logger.getAnonymousLogger().fine("Initializing " + new scriptella.driver.hsqldb.Driver()); 53 Map <String , String > env = new HashMap <String , String >(); 55 env.put(Context.INITIAL_CONTEXT_FACTORY, CtxFactory.class.getName()); 57 CtxFactory.jndiName = "datasourceName"; 58 CtxFactory.connections = 0; 59 CtxFactory.lookups = 0; 60 ConnectionParameters params = new ConnectionParameters(new MockConnectionEl(env, CtxFactory.jndiName), MockDriverContext.INSTANCE); 61 Driver drv = new Driver(); 62 JdbcConnection con1 = drv.connect(params); 63 con1.close(); 64 assertNotNull(con1); 65 JdbcConnection con2 = drv.connect(params); 66 con2.close(); 67 assertNotNull(con2); 68 assertTrue("con1 and con2 must be different connections", con1 != con2); 69 assertEquals("Illegal number of lookups", 2, CtxFactory.lookups); 71 assertEquals("Illegal number of created connections", 2, CtxFactory.connections); 72 73 } 74 75 79 public void testValidation() throws SQLException { 80 Driver drv = new Driver(); 81 try { 82 drv.getConnection(null , null); 83 } catch (JdbcException e) { 84 } 86 87 } 88 89 93 public static class CtxFactory implements InitialContextFactory , InvocationHandler { 94 public static String jndiName; 95 public static int lookups; 96 public static int connections; 97 98 public Context getInitialContext(Hashtable <?, ?> environment) throws NamingException { 99 return (Context) Proxy.newProxyInstance(getClass().getClassLoader(), 100 new Class <?>[]{Context.class, DataSource .class}, this); 101 } 102 103 109 public Object invoke(Object proxy, Method method, Object [] args) throws SQLException { 110 if ("lookup".equals(method.getName())) { 111 lookups++; 112 String name = (String ) args[0]; 113 if (!jndiName.equals(name)) { 114 fail("Expected " + jndiName + " JNDI name but was " + name); 115 } 116 return proxy; 117 } else if ("getConnection".equals(method.getName())) { 118 connections++; 119 return DriverManager.getConnection("jdbc:hsqldb:mem:jnditest", "sa", ""); 120 } else { 121 throw new UnsupportedOperationException (); 122 } 123 } 124 } 125 126 } 127 | Popular Tags |