001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import javax.naming.Context;
009: import javax.naming.InitialContext;
010: import javax.naming.Reference;
011: import javax.naming.StringRefAddr;
012: import javax.naming.spi.NamingManager;
013: import javax.sql.DataSource;
014: import java.util.Hashtable;
015: import java.util.Properties;
016:
017: import tyrex.naming.MemoryContextFactory;
018:
019: /**
020: * Tests the Proxool datasources.
021: *
022: * @author Christian Nedregaard (christian_nedregaard@email.com)
023: * @author $Author: billhorsman $ (current maintainer)
024: * @version $Revision: 1.2 $, $Date: 2006/03/23 11:51:24 $
025: * @since Proxool 0.9
026: */
027: public class ProxoolDataSourceTest extends AbstractProxoolTest {
028: public ProxoolDataSourceTest(String alias) {
029: super (alias);
030: }
031:
032: /**
033: * Test the Proxool managed DataSource
034: */
035: public void testPreconfiguredDataSource() throws Exception {
036: String alias = "preconfiguredDatasourceTest";
037: String jndiName = "TestDB";
038: String url = TestHelper.buildProxoolUrl(alias,
039: TestConstants.HYPERSONIC_DRIVER,
040: TestConstants.HYPERSONIC_TEST_URL);
041: Properties info = new Properties();
042: info.setProperty(ProxoolConstants.USER_PROPERTY,
043: TestConstants.HYPERSONIC_USER);
044: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
045: TestConstants.HYPERSONIC_PASSWORD);
046: info.setProperty(ProxoolConstants.JNDI_NAME_PROPERTY, jndiName);
047: info.setProperty(ProxoolConstants.JNDI_PROPERTY_PREFIX
048: + "java.naming.factory.initial",
049: MemoryContextFactory.class.getName());
050: info.setProperty(ProxoolConstants.JNDI_PROPERTY_PREFIX
051: + "java.naming.factory.url.pkgs", "tyrex.naming");
052: info.setProperty(ProxoolConstants.JNDI_PROPERTY_PREFIX
053: + "java.naming.provider.url", alias);
054: ProxoolFacade.registerConnectionPool(url, info);
055:
056: Hashtable env = new Hashtable();
057: env.put(Context.INITIAL_CONTEXT_FACTORY,
058: MemoryContextFactory.class.getName());
059: env.put(Context.URL_PKG_PREFIXES, "tyrex.naming");
060: env.put(Context.PROVIDER_URL, alias);
061: Context context = new InitialContext(env);
062: DataSource dataSource = (DataSource) context.lookup(jndiName);
063: assertNotNull("Connection was null.", dataSource
064: .getConnection());
065: ProxoolFacade.removeConnectionPool(alias);
066: context.close();
067: }
068:
069: /**
070: * Test the bean configured DataSource.
071: */
072: public void testBeanConfiguredDataSource() throws Exception {
073: String alias = "beanConfiguredDataSourceTest";
074: String jndiName = "jdbc/beanConfiguredTestDB";
075:
076: // pretend to be a J2EE server that instantites the data source
077: // populates its properties and binds it to JNDI
078: ProxoolDataSource dataSource = new ProxoolDataSource();
079: dataSource.setAlias(alias);
080: dataSource.setDriver(TestConstants.HYPERSONIC_DRIVER);
081: dataSource.setDriverUrl(TestConstants.HYPERSONIC_TEST_URL);
082: dataSource.setUser(TestConstants.HYPERSONIC_USER);
083: dataSource.setPassword(TestConstants.HYPERSONIC_PASSWORD);
084: dataSource.setDelegateProperties("dummy=foo,anotherDummy=bar");
085:
086: Hashtable env = new Hashtable();
087: env.put(Context.INITIAL_CONTEXT_FACTORY,
088: MemoryContextFactory.class.getName());
089: env.put(Context.URL_PKG_PREFIXES, "tyrex.naming");
090: env.put(Context.PROVIDER_URL, alias);
091: Context context = new InitialContext(env);
092: context.createSubcontext("jdbc");
093: context.bind(jndiName, dataSource);
094: context.close();
095: // end J2EE server
096:
097: // now... pretend to be a client.
098: context = new InitialContext(env);
099: DataSource clientDataSource = (DataSource) context
100: .lookup(jndiName);
101: assertNotNull("Connection was null.", clientDataSource
102: .getConnection());
103:
104: ConnectionPoolDefinitionIF cpd = ProxoolFacade
105: .getConnectionPoolDefinition(alias);
106: assertEquals("dummy", "foo", cpd.getDelegateProperty("dummy"));
107: assertEquals("dummy", "bar", cpd
108: .getDelegateProperty("anotherDummy"));
109:
110: ProxoolFacade.removeConnectionPool(alias);
111: context.close();
112: }
113:
114: /**
115: * Test the bean configured DataSource.
116: */
117: public void testFactoryConfiguredDataSource() throws Exception {
118: String alias = "factoryConfiguredDataSourceTest";
119: String jndiName = "jdbc/factoryConfiguredTestDB";
120:
121: Reference reference = new Reference(ProxoolDataSource.class
122: .getName(), ProxoolDataSource.class.getName(), null);
123: reference.add(new StringRefAddr(
124: ProxoolConstants.ALIAS_PROPERTY, alias));
125: reference.add(new StringRefAddr(
126: ProxoolConstants.DRIVER_CLASS_PROPERTY,
127: TestConstants.HYPERSONIC_DRIVER));
128: reference.add(new StringRefAddr(
129: ProxoolConstants.DRIVER_URL_PROPERTY,
130: TestConstants.HYPERSONIC_TEST_URL));
131: reference.add(new StringRefAddr(ProxoolConstants.USER_PROPERTY,
132: TestConstants.HYPERSONIC_USER));
133: reference.add(new StringRefAddr(
134: ProxoolConstants.PASSWORD_PROPERTY,
135: TestConstants.HYPERSONIC_PASSWORD));
136: reference.add(new StringRefAddr("dummy", "foo"));
137: reference.add(new StringRefAddr("anotherDummy", "bar"));
138:
139: // pretend to be a JNDI aware container that builds the DataSource
140: // using its factory
141: Hashtable env = new Hashtable();
142: env.put(Context.INITIAL_CONTEXT_FACTORY,
143: MemoryContextFactory.class.getName());
144: env.put(Context.URL_PKG_PREFIXES, "tyrex.naming");
145: env.put(Context.PROVIDER_URL, alias);
146: Context context = new InitialContext(env);
147: context.createSubcontext("jdbc");
148: context.bind(jndiName, NamingManager.getObjectInstance(
149: reference, null, null, null));
150: context.close();
151: // end JNDI aware container
152:
153: // now... pretend to be a client.
154: context = new InitialContext(env);
155: DataSource clientDataSource = (DataSource) context
156: .lookup(jndiName);
157: assertNotNull("Connection was null.", clientDataSource
158: .getConnection());
159:
160: ConnectionPoolDefinitionIF cpd = ProxoolFacade
161: .getConnectionPoolDefinition(alias);
162: assertEquals("dummy", "foo", cpd.getDelegateProperty("dummy"));
163: assertEquals("dummy", "bar", cpd
164: .getDelegateProperty("anotherDummy"));
165:
166: ProxoolFacade.removeConnectionPool(alias);
167: context.close();
168: }
169: }
170:
171: /*
172: Revision history:
173: $Log: ProxoolDataSourceTest.java,v $
174: Revision 1.2 2006/03/23 11:51:24 billhorsman
175: Allow for delegate properties
176:
177: Revision 1.1 2004/03/18 17:10:01 chr32
178: Renamed DataSourceTest -> ProxoolDataSourceTest. Added test for factory-configured mode.
179:
180: Revision 1.4 2004/03/15 23:56:33 chr32
181: Added test for ProxoolDataSource.
182:
183: Revision 1.3 2004/03/15 02:46:09 chr32
184: Added initial ProxoolDataSourceTest.
185:
186: */
|