KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ProxoolDataSourceTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.naming.Reference JavaDoc;
11 import javax.naming.StringRefAddr JavaDoc;
12 import javax.naming.spi.NamingManager JavaDoc;
13 import javax.sql.DataSource JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import tyrex.naming.MemoryContextFactory;
18
19 /**
20  * Tests the Proxool datasources.
21  *
22  * @author Christian Nedregaard (christian_nedregaard@email.com)
23  * @author $Author: billhorsman $ (current maintainer)
24  * @version $Revision: 1.2 $, $Date: 2006/03/23 11:51:24 $
25  * @since Proxool 0.9
26  */

27 public class ProxoolDataSourceTest extends AbstractProxoolTest {
28     public ProxoolDataSourceTest(String JavaDoc alias) {
29         super(alias);
30     }
31
32     /**
33      * Test the Proxool managed DataSource
34      */

35     public void testPreconfiguredDataSource() throws Exception JavaDoc {
36         String JavaDoc alias = "preconfiguredDatasourceTest";
37         String JavaDoc jndiName = "TestDB";
38         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
39                 TestConstants.HYPERSONIC_DRIVER,
40                 TestConstants.HYPERSONIC_TEST_URL);
41         Properties JavaDoc info = new Properties JavaDoc();
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 JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc context = new InitialContext JavaDoc(env);
58         DataSource JavaDoc dataSource = (DataSource JavaDoc) context.lookup(jndiName);
59         assertNotNull("Connection was null.", dataSource.getConnection());
60         ProxoolFacade.removeConnectionPool(alias);
61         context.close();
62     }
63
64     /**
65      * Test the bean configured DataSource.
66      */

67     public void testBeanConfiguredDataSource() throws Exception JavaDoc {
68         String JavaDoc alias = "beanConfiguredDataSourceTest";
69         String JavaDoc jndiName = "jdbc/beanConfiguredTestDB";
70
71         // pretend to be a J2EE server that instantites the data source
72
// populates its properties and binds it to JNDI
73
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 JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc context = new InitialContext JavaDoc(env);
86         context.createSubcontext("jdbc");
87         context.bind(jndiName, dataSource);
88         context.close();
89         // end J2EE server
90

91         // now... pretend to be a client.
92
context = new InitialContext JavaDoc(env);
93         DataSource JavaDoc clientDataSource = (DataSource JavaDoc) 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     /**
105      * Test the bean configured DataSource.
106      */

107     public void testFactoryConfiguredDataSource() throws Exception JavaDoc {
108         String JavaDoc alias = "factoryConfiguredDataSourceTest";
109         String JavaDoc jndiName = "jdbc/factoryConfiguredTestDB";
110
111         Reference JavaDoc reference = new Reference JavaDoc(ProxoolDataSource.class.getName(), ProxoolDataSource.class.getName(), null);
112         reference.add(new StringRefAddr JavaDoc(ProxoolConstants.ALIAS_PROPERTY, alias));
113         reference.add(new StringRefAddr JavaDoc(ProxoolConstants.DRIVER_CLASS_PROPERTY, TestConstants.HYPERSONIC_DRIVER));
114         reference.add(new StringRefAddr JavaDoc(ProxoolConstants.DRIVER_URL_PROPERTY, TestConstants.HYPERSONIC_TEST_URL));
115         reference.add(new StringRefAddr JavaDoc(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER));
116         reference.add(new StringRefAddr JavaDoc(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD));
117         reference.add(new StringRefAddr JavaDoc("dummy", "foo"));
118         reference.add(new StringRefAddr JavaDoc("anotherDummy", "bar"));
119
120         // pretend to be a JNDI aware container that builds the DataSource
121
// using its factory
122
Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc context = new InitialContext JavaDoc(env);
127         context.createSubcontext("jdbc");
128         context.bind(jndiName, NamingManager.getObjectInstance(reference, null, null, null));
129         context.close();
130         // end JNDI aware container
131

132         // now... pretend to be a client.
133
context = new InitialContext JavaDoc(env);
134         DataSource JavaDoc clientDataSource = (DataSource JavaDoc) 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 /*
148  Revision history:
149  $Log: ProxoolDataSourceTest.java,v $
150  Revision 1.2 2006/03/23 11:51:24 billhorsman
151  Allow for delegate properties
152
153  Revision 1.1 2004/03/18 17:10:01 chr32
154  Renamed DataSourceTest -> ProxoolDataSourceTest. Added test for factory-configured mode.
155
156  Revision 1.4 2004/03/15 23:56:33 chr32
157  Added test for ProxoolDataSource.
158
159  Revision 1.3 2004/03/15 02:46:09 chr32
160  Added initial ProxoolDataSourceTest.
161
162  */
Popular Tags