1 6 package org.logicalcobwebs.proxool; 7 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.naming.Reference ; 11 import javax.naming.StringRefAddr ; 12 import javax.naming.spi.NamingManager ; 13 import javax.sql.DataSource ; 14 import java.util.Hashtable ; 15 import java.util.Properties ; 16 17 import tyrex.naming.MemoryContextFactory; 18 19 27 public class ProxoolDataSourceTest extends AbstractProxoolTest { 28 public ProxoolDataSourceTest(String alias) { 29 super(alias); 30 } 31 32 35 public void testPreconfiguredDataSource() throws Exception { 36 String alias = "preconfiguredDatasourceTest"; 37 String jndiName = "TestDB"; 38 String url = TestHelper.buildProxoolUrl(alias, 39 TestConstants.HYPERSONIC_DRIVER, 40 TestConstants.HYPERSONIC_TEST_URL); 41 Properties info = new Properties (); 42 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 43 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 44 info.setProperty(ProxoolConstants.JNDI_NAME_PROPERTY, jndiName); 45 info.setProperty(ProxoolConstants.JNDI_PROPERTY_PREFIX + "java.naming.factory.initial", 46 MemoryContextFactory.class.getName()); 47 info.setProperty(ProxoolConstants.JNDI_PROPERTY_PREFIX + "java.naming.factory.url.pkgs", 48 "tyrex.naming"); 49 info.setProperty(ProxoolConstants.JNDI_PROPERTY_PREFIX + "java.naming.provider.url", 50 alias); 51 ProxoolFacade.registerConnectionPool(url, info); 52 53 Hashtable env = new Hashtable (); 54 env.put(Context.INITIAL_CONTEXT_FACTORY, MemoryContextFactory.class.getName()); 55 env.put(Context.URL_PKG_PREFIXES, "tyrex.naming"); 56 env.put(Context.PROVIDER_URL, alias); 57 Context context = new InitialContext (env); 58 DataSource dataSource = (DataSource ) context.lookup(jndiName); 59 assertNotNull("Connection was null.", dataSource.getConnection()); 60 ProxoolFacade.removeConnectionPool(alias); 61 context.close(); 62 } 63 64 67 public void testBeanConfiguredDataSource() throws Exception { 68 String alias = "beanConfiguredDataSourceTest"; 69 String jndiName = "jdbc/beanConfiguredTestDB"; 70 71 ProxoolDataSource dataSource = new ProxoolDataSource(); 74 dataSource.setAlias(alias); 75 dataSource.setDriver(TestConstants.HYPERSONIC_DRIVER); 76 dataSource.setDriverUrl(TestConstants.HYPERSONIC_TEST_URL); 77 dataSource.setUser(TestConstants.HYPERSONIC_USER); 78 dataSource.setPassword(TestConstants.HYPERSONIC_PASSWORD); 79 dataSource.setDelegateProperties("dummy=foo,anotherDummy=bar"); 80 81 Hashtable env = new Hashtable (); 82 env.put(Context.INITIAL_CONTEXT_FACTORY, MemoryContextFactory.class.getName()); 83 env.put(Context.URL_PKG_PREFIXES, "tyrex.naming"); 84 env.put(Context.PROVIDER_URL, alias); 85 Context context = new InitialContext (env); 86 context.createSubcontext("jdbc"); 87 context.bind(jndiName, dataSource); 88 context.close(); 89 91 context = new InitialContext (env); 93 DataSource clientDataSource = (DataSource ) context.lookup(jndiName); 94 assertNotNull("Connection was null.", clientDataSource.getConnection()); 95 96 ConnectionPoolDefinitionIF cpd = ProxoolFacade.getConnectionPoolDefinition(alias); 97 assertEquals("dummy", "foo", cpd.getDelegateProperty("dummy")); 98 assertEquals("dummy", "bar", cpd.getDelegateProperty("anotherDummy")); 99 100 ProxoolFacade.removeConnectionPool(alias); 101 context.close(); 102 } 103 104 107 public void testFactoryConfiguredDataSource() throws Exception { 108 String alias = "factoryConfiguredDataSourceTest"; 109 String jndiName = "jdbc/factoryConfiguredTestDB"; 110 111 Reference reference = new Reference (ProxoolDataSource.class.getName(), ProxoolDataSource.class.getName(), null); 112 reference.add(new StringRefAddr (ProxoolConstants.ALIAS_PROPERTY, alias)); 113 reference.add(new StringRefAddr (ProxoolConstants.DRIVER_CLASS_PROPERTY, TestConstants.HYPERSONIC_DRIVER)); 114 reference.add(new StringRefAddr (ProxoolConstants.DRIVER_URL_PROPERTY, TestConstants.HYPERSONIC_TEST_URL)); 115 reference.add(new StringRefAddr (ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER)); 116 reference.add(new StringRefAddr (ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD)); 117 reference.add(new StringRefAddr ("dummy", "foo")); 118 reference.add(new StringRefAddr ("anotherDummy", "bar")); 119 120 Hashtable env = new Hashtable (); 123 env.put(Context.INITIAL_CONTEXT_FACTORY, MemoryContextFactory.class.getName()); 124 env.put(Context.URL_PKG_PREFIXES, "tyrex.naming"); 125 env.put(Context.PROVIDER_URL, alias); 126 Context context = new InitialContext (env); 127 context.createSubcontext("jdbc"); 128 context.bind(jndiName, NamingManager.getObjectInstance(reference, null, null, null)); 129 context.close(); 130 132 context = new InitialContext (env); 134 DataSource clientDataSource = (DataSource ) context.lookup(jndiName); 135 assertNotNull("Connection was null.", clientDataSource.getConnection()); 136 137 ConnectionPoolDefinitionIF cpd = ProxoolFacade.getConnectionPoolDefinition(alias); 138 assertEquals("dummy", "foo", cpd.getDelegateProperty("dummy")); 139 assertEquals("dummy", "bar", cpd.getDelegateProperty("anotherDummy")); 140 141 ProxoolFacade.removeConnectionPool(alias); 142 context.close(); 143 } 144 } 145 146 147 | Popular Tags |