KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.logicalcobwebs.proxool;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.DriverManager JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 /**
12  * Helper for all tests
13  *
14  * @version $Revision: 1.22 $, $Date: 2006/01/18 14:40:06 $
15  * @author bill
16  * @author $Author: billhorsman $ (current maintainer)
17  */

18 public class TestHelper {
19
20     private static final Log LOG = LogFactory.getLog(TestHelper.class);
21
22     /**
23      * Builds a complete set of proxool properties, with all values set to
24      * something different than the default vaule.
25      * @return the properties that was buildt.
26      */

27     public static Properties JavaDoc buildCompleteAlternativeProperties() {
28         Properties JavaDoc properties = new Properties JavaDoc();
29         properties.setProperty("user", "sa");
30         properties.setProperty("password", "");
31         properties.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "40000");
32         properties.setProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, TestConstants.HYPERSONIC_TEST_SQL);
33         properties.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "10");
34         properties.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "3");
35         properties.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY, "18000000");
36         properties.setProperty(ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY, "5");
37         properties.setProperty(ProxoolConstants.RECENTLY_STARTED_THRESHOLD_PROPERTY, "40000");
38         properties.setProperty(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY, "50000");
39         properties.setProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, "60000");
40         properties.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
41         properties.setProperty(ProxoolConstants.TRACE_PROPERTY, "true");
42         properties.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, "Fatal error");
43         properties.setProperty(ProxoolConstants.PROTOTYPE_COUNT_PROPERTY, "2");
44         return properties;
45     }
46
47     /**
48      * Test that the given ConnectionPoolDefinitionIF contains all the properties as defined in
49      * {@link #buildCompleteAlternativeProperties}.
50      * @param connectionPoolDefinition the ConnectionPoolDefinitionIF to be tested.
51      * @throws ProxoolException if any properties are missing or have the wrong value.
52      */

53     public static void equalsCompleteAlternativeProperties(ConnectionPoolDefinitionIF connectionPoolDefinition)
54             throws ProxoolException {
55         checkProperty("user", "sa", connectionPoolDefinition.getDelegateProperties().getProperty("user"));
56         checkProperty("password", "", connectionPoolDefinition.getDelegateProperties().getProperty("password"));
57         checkProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME, 40000,
58                 connectionPoolDefinition.getHouseKeepingSleepTime());
59         checkProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL, "select CURRENT_DATE",
60                 connectionPoolDefinition.getHouseKeepingTestSql());
61         checkProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, 10,
62                 connectionPoolDefinition.getMaximumConnectionCount());
63         checkProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT, 3,
64                 connectionPoolDefinition.getMinimumConnectionCount());
65         checkProperty(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME, 18000000,
66                 connectionPoolDefinition.getMaximumConnectionLifetime());
67         checkProperty(ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE, 5,
68                 connectionPoolDefinition.getSimultaneousBuildThrottle());
69         checkProperty(ProxoolConstants.RECENTLY_STARTED_THRESHOLD, 40000,
70                 connectionPoolDefinition.getRecentlyStartedThreshold());
71         checkProperty(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME, 50000,
72                 connectionPoolDefinition.getOverloadWithoutRefusalLifetime());
73         checkProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME, 60000,
74                 connectionPoolDefinition.getMaximumActiveTime());
75         checkProperty(ProxoolConstants.VERBOSE, "true",
76                 new Boolean JavaDoc(connectionPoolDefinition.isVerbose()).toString());
77         checkProperty(ProxoolConstants.TRACE, "true",
78                 new Boolean JavaDoc(connectionPoolDefinition.isTrace() == true).toString());
79         checkProperty(ProxoolConstants.FATAL_SQL_EXCEPTION, "Fatal error",
80                 connectionPoolDefinition.getFatalSqlExceptions().iterator().next().toString());
81         checkProperty(ProxoolConstants.PROTOTYPE_COUNT, 2,
82                 connectionPoolDefinition.getPrototypeCount());
83     }
84
85     private static void checkProperty(String JavaDoc name, String JavaDoc correctValue, String JavaDoc candidateValue) throws ProxoolException {
86         if (candidateValue == null) {
87             throw new ProxoolException(name + " was null.");
88         } else if (!candidateValue.equals(correctValue)) {
89             throw new ProxoolException("Expected value for " + name + " was " + correctValue + " but the value was "
90                     + candidateValue + ".");
91         }
92     }
93
94     private static void checkProperty(String JavaDoc name, int correctValue, int candidateValue) throws ProxoolException {
95         checkProperty(name, String.valueOf(correctValue), String.valueOf(candidateValue));
96     }
97
98     /**
99      * Build a valid Proxool URL
100      * @param alias identifies the pool
101      * @param driver the delegate driver
102      * @param delegateUrl the url to send to the delegate driver
103      * @return proxool.alias:driver:delegateUrl
104      */

105     public static String JavaDoc buildProxoolUrl(String JavaDoc alias, String JavaDoc driver, String JavaDoc delegateUrl) {
106         String JavaDoc url = ProxoolConstants.PROXOOL
107                 + ProxoolConstants.ALIAS_DELIMITER
108                 + alias
109                 + ProxoolConstants.URL_DELIMITER
110                 + driver
111                 + ProxoolConstants.URL_DELIMITER
112                 + delegateUrl;
113         return url;
114     }
115
116     /**
117      * Build a valid Proxool URL
118      * @param alias identifies the pool
119      * @return proxool.alias
120      */

121     public static String JavaDoc buildProxoolUrl(String JavaDoc alias) {
122         String JavaDoc url = ProxoolConstants.PROXOOL
123                 + ProxoolConstants.ALIAS_DELIMITER
124                 + alias;
125         return url;
126     }
127
128     public static Connection JavaDoc getDirectConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc {
129         Connection JavaDoc connection = null;
130         Class.forName(TestConstants.HYPERSONIC_DRIVER);
131         Properties JavaDoc info = new Properties JavaDoc();
132         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
133         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
134         connection = DriverManager.getConnection(TestConstants.HYPERSONIC_TEST_URL, info);
135         return connection;
136     }
137
138 }
139
140 /*
141  Revision history:
142  $Log: TestHelper.java,v $
143  Revision 1.22 2006/01/18 14:40:06 billhorsman
144  Unbundled Jakarta's Commons Logging.
145
146  Revision 1.21 2003/09/30 18:39:50 billhorsman
147  New test sql syntax constant
148
149  Revision 1.20 2003/03/05 23:28:55 billhorsman
150  deprecated maximum-new-connections property in favour of
151  more descriptive simultaneous-build-throttle
152
153  Revision 1.19 2003/03/04 10:58:44 billhorsman
154  checkstyle
155
156   */
Popular Tags