KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.logicalcobwebs.proxool.admin.SnapshotIF;
11 import org.logicalcobwebs.proxool.admin.SnapshotResultMonitor;
12
13 import java.sql.Connection JavaDoc;
14 import java.sql.DriverManager JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * Test the prototyper in ConnectionPool
19  *
20  * @version $Revision: 1.11 $, $Date: 2006/01/18 14:40:06 $
21  * @author bill
22  * @author $Author: billhorsman $ (current maintainer)
23  * @since Proxool 0.8
24  */

25 public class PrototyperTest extends AbstractProxoolTest {
26
27     private static final Log LOG = LogFactory.getLog(PrototyperTest.class);
28
29     public PrototyperTest(String JavaDoc alias) {
30         super(alias);
31     }
32
33     /**
34      * Test that spare connections are made as we run out of them
35      */

36     public void testPrototypeCount() throws Exception JavaDoc {
37
38         final String JavaDoc testName = "prototypeCount";
39         final String JavaDoc alias = testName;
40
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.VERBOSE_PROPERTY, Boolean.TRUE.toString());
45         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "0");
46         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "5");
47         info.setProperty(ProxoolConstants.PROTOTYPE_COUNT_PROPERTY, "2");
48         info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000");
49         info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, TestConstants.HYPERSONIC_TEST_SQL);
50         
51         String JavaDoc url = ProxoolConstants.PROXOOL
52                 + ProxoolConstants.ALIAS_DELIMITER
53                 + alias
54                 + ProxoolConstants.URL_DELIMITER
55                 + TestConstants.HYPERSONIC_DRIVER
56                 + ProxoolConstants.URL_DELIMITER
57                 + TestConstants.HYPERSONIC_TEST_URL;
58         ProxoolFacade.registerConnectionPool(url, info);
59
60         Connection JavaDoc[] connections = new Connection JavaDoc[6];
61
62         SnapshotResultMonitor srm = new SnapshotResultMonitor(alias) {
63             public boolean check(SnapshotIF snapshot) throws Exception JavaDoc {
64                 SnapshotIF s = ProxoolFacade.getSnapshot(alias);
65                 return (s.getActiveConnectionCount() == 0
66                         && s.getAvailableConnectionCount() >= 2);
67             }
68         };
69         assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
70         assertEquals("activeConnectionCount", 0, srm.getSnapshot().getActiveConnectionCount());
71         assertEquals("availableConnectionCount", 2, srm.getSnapshot().getAvailableConnectionCount());
72
73         connections[0] = DriverManager.getConnection(url);
74
75         srm = new SnapshotResultMonitor(alias) {
76             public boolean check(SnapshotIF snapshot) throws Exception JavaDoc {
77                 SnapshotIF s = ProxoolFacade.getSnapshot(alias);
78                 return (s.getActiveConnectionCount() == 1
79                         && s.getAvailableConnectionCount() >= 2);
80             }
81         };
82         assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
83         assertEquals("activeConnectionCount", 1, srm.getSnapshot().getActiveConnectionCount());
84         assertEquals("availableConnectionCount", 2, srm.getSnapshot().getAvailableConnectionCount());
85
86         connections[1] = DriverManager.getConnection(url);
87         connections[2] = DriverManager.getConnection(url);
88         connections[3] = DriverManager.getConnection(url);
89
90         srm = new SnapshotResultMonitor(alias) {
91             public boolean check(SnapshotIF snapshot) throws Exception JavaDoc {
92                 SnapshotIF s = ProxoolFacade.getSnapshot(alias);
93                 return (s.getActiveConnectionCount() == 4
94                         && s.getAvailableConnectionCount() == 1);
95             }
96         };
97         assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
98         assertEquals("activeConnectionCount", 4, srm.getSnapshot().getActiveConnectionCount());
99         assertEquals("availableConnectionCount", 1, srm.getSnapshot().getAvailableConnectionCount());
100
101     }
102
103     /**
104      * Test that the minimum number of connections is maintained
105      */

106     public void testMinimumConnectionCount() throws Exception JavaDoc {
107
108         String JavaDoc testName = "miniumumConnectionCount";
109         final String JavaDoc alias = testName;
110
111         Properties JavaDoc info = new Properties JavaDoc();
112         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
113         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
114         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString());
115         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "2");
116         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "5");
117         info.setProperty(ProxoolConstants.PROTOTYPE_COUNT_PROPERTY, "0");
118         info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000");
119         info.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, TestConstants.HYPERSONIC_TEST_SQL);
120         String JavaDoc url = TestHelper.buildProxoolUrl(alias, TestConstants.HYPERSONIC_DRIVER, TestConstants.HYPERSONIC_TEST_URL);
121         ProxoolFacade.registerConnectionPool(url, info);
122
123         ResultMonitor srm = new SnapshotResultMonitor(alias) {
124             public boolean check(SnapshotIF snapshot) throws Exception JavaDoc {
125                 SnapshotIF s = ProxoolFacade.getSnapshot(alias);
126                 return (s.getAvailableConnectionCount() == 2);
127             }
128         };
129         assertEquals("Timeout", ResultMonitor.SUCCESS, srm.getResult());
130
131         assertEquals("availableConnectionCount", 2, ProxoolFacade.getSnapshot(alias, false).getAvailableConnectionCount());
132
133     }
134
135 }
136
137
138 /*
139  Revision history:
140  $Log: PrototyperTest.java,v $
141  Revision 1.11 2006/01/18 14:40:06 billhorsman
142  Unbundled Jakarta's Commons Logging.
143
144  Revision 1.10 2004/05/26 17:19:09 brenuart
145  Allow JUnit tests to be executed against another database.
146  By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
147  This behavior can be overriden by setting the 'testConfig' environment property to another location.
148
149  Revision 1.9 2003/03/10 15:31:26 billhorsman
150  fixes
151
152  Revision 1.8 2003/03/06 10:11:24 billhorsman
153  trap timeouts better
154
155  Revision 1.7 2003/03/05 18:45:17 billhorsman
156  better threading
157
158  Revision 1.6 2003/03/04 10:24:40 billhorsman
159  removed try blocks around each test
160
161  Revision 1.5 2003/03/03 17:08:57 billhorsman
162  all tests now extend AbstractProxoolTest
163
164  Revision 1.4 2003/03/03 11:12:04 billhorsman
165  fixed licence
166
167  Revision 1.3 2003/03/01 15:14:15 billhorsman
168  new ResultMonitor to help cope with test threads
169
170  Revision 1.2 2003/03/01 00:39:23 billhorsman
171  made more robust
172
173  Revision 1.1 2003/02/27 18:01:48 billhorsman
174  completely rethought the test structure. it's now
175  more obvious. no new tests yet though.
176
177  */
Popular Tags