KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.sql.DriverManager JavaDoc;
12 import java.sql.Connection JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 /**
17  * Test that we can update the definition of a pool
18  *
19  * @version $Revision: 1.9 $, $Date: 2006/01/18 14:40:06 $
20  * @author bill
21  * @author $Author: billhorsman $ (current maintainer)
22  * @since Proxool 0.8
23  */

24 public class UpdateDefinitionTest extends AbstractProxoolTest {
25
26     private static final Log LOG = LogFactory.getLog(UpdateDefinitionTest.class);
27
28     public UpdateDefinitionTest(String JavaDoc alias) {
29         super(alias);
30     }
31
32     /**
33      * Can we change the delegate URL of a pool
34      */

35     public void testChangeUrl() throws Exception JavaDoc {
36
37         String JavaDoc testName = "changeUrl";
38         String JavaDoc alias = testName;
39
40         String JavaDoc url1 = TestHelper.buildProxoolUrl(alias,
41                 TestConstants.HYPERSONIC_DRIVER,
42                 TestConstants.HYPERSONIC_TEST_URL);
43
44         String JavaDoc url2 = TestHelper.buildProxoolUrl(alias,
45                 TestConstants.HYPERSONIC_DRIVER,
46                 TestConstants.HYPERSONIC_TEST_URL2);
47
48         Properties JavaDoc info = new Properties JavaDoc();
49         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
50         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
51         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "2");
52
53         // register pool
54
ProxoolFacade.registerConnectionPool(url1, info);
55
56         // Get one connection
57
DriverManager.getConnection(url1).close();
58         assertEquals("connectionsServedCount", 1L, ProxoolFacade.getSnapshot(alias, false).getServedCount());
59
60         ProxoolFacade.updateConnectionPool(url2, null);
61
62         // Get another connection
63
DriverManager.getConnection(url2).close();
64         assertEquals("connectionsServedCount", 2L, ProxoolFacade.getSnapshot(alias, false).getServedCount());
65
66         ProxoolFacade.updateConnectionPool(url1, null);
67         DriverManager.getConnection(url1).close();
68         assertEquals("connectionsServedCount", 3L, ProxoolFacade.getSnapshot(alias, false).getServedCount());
69
70     }
71
72     /**
73      * Can we update a pool definition by passing a new Properties object?
74      */

75     public void testUpdate() throws Exception JavaDoc, ClassNotFoundException JavaDoc {
76
77         String JavaDoc testName = "update";
78         String JavaDoc alias = testName;
79
80         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
81                 TestConstants.HYPERSONIC_DRIVER,
82                 TestConstants.HYPERSONIC_TEST_URL);
83         Properties JavaDoc info = new Properties JavaDoc();
84         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
85         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
86         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "1");
87         ProxoolFacade.registerConnectionPool(url, info);
88
89         // Open a connection. Just for the hell of it
90
DriverManager.getConnection(url).close();
91
92         assertEquals("maximumConnectionCount", 1, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount());
93
94         // Update using facade
95
info = new Properties JavaDoc();
96         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "2");
97         ProxoolFacade.updateConnectionPool(url, info);
98         assertEquals("maximumConnectionCount", 2, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount());
99
100         // Now do it on the fly (this is a redefine really)
101
info = new Properties JavaDoc();
102         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
103         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
104         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "3");
105         DriverManager.getConnection(url, info).close();
106         assertEquals("maximumConnectionCount", 3, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount());
107
108     }
109
110     /**
111      * If we request a connection using exactly the same URL and properties check that it doesn't trigger an update
112      * which forces the pool to be restarted (all existing connections destroyed).
113      */

114     public void testDefinitionNotChanging() throws SQLException JavaDoc, ProxoolException {
115
116         String JavaDoc testName = "definitionNotChanging";
117         String JavaDoc alias = testName;
118
119
120         Connection JavaDoc c1 = null;
121         try {
122             String JavaDoc url = TestHelper.buildProxoolUrl(alias,
123                     TestConstants.HYPERSONIC_DRIVER,
124                     TestConstants.HYPERSONIC_TEST_URL);
125             Properties JavaDoc info = new Properties JavaDoc();
126             info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
127             info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
128             info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "1");
129             c1 = DriverManager.getConnection(url, info);
130             assertEquals("id=1", 1L, ProxoolFacade.getId(c1));
131         } finally {
132             c1.close();
133         }
134         // The second attempt (using the same definition) should give back the same connection ID
135
Connection JavaDoc c2 = null;
136         try {
137             String JavaDoc url = TestHelper.buildProxoolUrl(alias,
138                     TestConstants.HYPERSONIC_DRIVER,
139                     TestConstants.HYPERSONIC_TEST_URL);
140             Properties JavaDoc info = new Properties JavaDoc();
141             info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
142             info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
143             info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "1");
144             c2 = DriverManager.getConnection(url, info);
145             assertEquals("id=1", 1L, ProxoolFacade.getId(c2));
146         } finally {
147             c2.close();
148         }
149         // Not the same object. It's wrapped.
150
assertNotSame("c1!=c2", c1, c2);
151     }
152
153     /**
154      * Can we update a pool definition by calling updateConnectionPool?
155      */

156     public void testUpdateUsingAPI() throws Exception JavaDoc, ClassNotFoundException JavaDoc {
157
158         String JavaDoc testName = "updateUsingAPI";
159         String JavaDoc alias = testName;
160
161         String JavaDoc url = ProxoolConstants.PROXOOL
162                 + ProxoolConstants.ALIAS_DELIMITER
163                 + alias
164                 + ProxoolConstants.URL_DELIMITER
165                 + TestConstants.HYPERSONIC_DRIVER
166                 + ProxoolConstants.URL_DELIMITER
167                 + TestConstants.HYPERSONIC_TEST_URL2; //"jdbc:hsqldb:db/update";
168

169
170         LOG.debug("Register pool");
171         Properties JavaDoc info = new Properties JavaDoc();
172         String JavaDoc checkAlias = ProxoolFacade.registerConnectionPool(url, info);
173         assertEquals(alias, checkAlias);
174
175         LOG.debug("setConfigurationListener");
176         ProxoolFacade.addConfigurationListener(alias, new ConfigurationListenerIF() {
177             public void definitionUpdated(ConnectionPoolDefinitionIF connectionPoolDefinition, Properties JavaDoc completeInfo, Properties JavaDoc changedInfo) {
178             }
179         });
180
181         LOG.debug("setStateListener");
182         ProxoolFacade.addStateListener(alias, new StateListenerIF() {
183             public void upStateChanged(int upState) {
184             }
185         });
186
187         LOG.debug("Update pool");
188         ProxoolFacade.updateConnectionPool(url, info);
189
190     }
191
192 }
193
194
195 /*
196  Revision history:
197  $Log: UpdateDefinitionTest.java,v $
198  Revision 1.9 2006/01/18 14:40:06 billhorsman
199  Unbundled Jakarta's Commons Logging.
200
201  Revision 1.8 2005/09/25 21:48:09 billhorsman
202  New test to check that asking for a connection using the same URL and properties doesn't redefine the pool.
203
204  Revision 1.7 2004/05/26 17:19:10 brenuart
205  Allow JUnit tests to be executed against another database.
206  By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
207  This behavior can be overriden by setting the 'testConfig' environment property to another location.
208
209  Revision 1.6 2003/03/04 12:50:44 billhorsman
210  fix
211
212  Revision 1.5 2003/03/04 10:24:40 billhorsman
213  removed try blocks around each test
214
215  Revision 1.4 2003/03/03 17:09:07 billhorsman
216  all tests now extend AbstractProxoolTest
217
218  Revision 1.3 2003/03/03 11:12:05 billhorsman
219  fixed licence
220
221  Revision 1.2 2003/03/01 15:27:24 billhorsman
222  checkstyle
223
224  Revision 1.1 2003/02/27 18:01:48 billhorsman
225  completely rethought the test structure. it's now
226  more obvious. no new tests yet though.
227
228  */
Popular Tags