001: package org.logicalcobwebs.proxool;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005:
006: import java.sql.Connection;
007: import java.sql.DriverManager;
008: import java.sql.SQLException;
009: import java.util.Properties;
010:
011: /**
012: * Helper for all tests
013: *
014: * @version $Revision: 1.22 $, $Date: 2006/01/18 14:40:06 $
015: * @author bill
016: * @author $Author: billhorsman $ (current maintainer)
017: */
018: public class TestHelper {
019:
020: private static final Log LOG = LogFactory.getLog(TestHelper.class);
021:
022: /**
023: * Builds a complete set of proxool properties, with all values set to
024: * something different than the default vaule.
025: * @return the properties that was buildt.
026: */
027: public static Properties buildCompleteAlternativeProperties() {
028: Properties properties = new Properties();
029: properties.setProperty("user", "sa");
030: properties.setProperty("password", "");
031: properties.setProperty(
032: ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY,
033: "40000");
034: properties.setProperty(
035: ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY,
036: TestConstants.HYPERSONIC_TEST_SQL);
037: properties.setProperty(
038: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
039: "10");
040: properties
041: .setProperty(
042: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
043: "3");
044: properties.setProperty(
045: ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY,
046: "18000000");
047: properties.setProperty(
048: ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY,
049: "5");
050: properties.setProperty(
051: ProxoolConstants.RECENTLY_STARTED_THRESHOLD_PROPERTY,
052: "40000");
053: properties
054: .setProperty(
055: ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY,
056: "50000");
057: properties.setProperty(
058: ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, "60000");
059: properties.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
060: "true");
061: properties.setProperty(ProxoolConstants.TRACE_PROPERTY, "true");
062: properties.setProperty(
063: ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
064: "Fatal error");
065: properties.setProperty(
066: ProxoolConstants.PROTOTYPE_COUNT_PROPERTY, "2");
067: return properties;
068: }
069:
070: /**
071: * Test that the given ConnectionPoolDefinitionIF contains all the properties as defined in
072: * {@link #buildCompleteAlternativeProperties}.
073: * @param connectionPoolDefinition the ConnectionPoolDefinitionIF to be tested.
074: * @throws ProxoolException if any properties are missing or have the wrong value.
075: */
076: public static void equalsCompleteAlternativeProperties(
077: ConnectionPoolDefinitionIF connectionPoolDefinition)
078: throws ProxoolException {
079: checkProperty("user", "sa", connectionPoolDefinition
080: .getDelegateProperties().getProperty("user"));
081: checkProperty("password", "", connectionPoolDefinition
082: .getDelegateProperties().getProperty("password"));
083: checkProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME, 40000,
084: connectionPoolDefinition.getHouseKeepingSleepTime());
085: checkProperty(ProxoolConstants.HOUSE_KEEPING_TEST_SQL,
086: "select CURRENT_DATE", connectionPoolDefinition
087: .getHouseKeepingTestSql());
088: checkProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, 10,
089: connectionPoolDefinition.getMaximumConnectionCount());
090: checkProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT, 3,
091: connectionPoolDefinition.getMinimumConnectionCount());
092: checkProperty(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME,
093: 18000000, connectionPoolDefinition
094: .getMaximumConnectionLifetime());
095: checkProperty(ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE, 5,
096: connectionPoolDefinition.getSimultaneousBuildThrottle());
097: checkProperty(ProxoolConstants.RECENTLY_STARTED_THRESHOLD,
098: 40000, connectionPoolDefinition
099: .getRecentlyStartedThreshold());
100: checkProperty(
101: ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME,
102: 50000, connectionPoolDefinition
103: .getOverloadWithoutRefusalLifetime());
104: checkProperty(ProxoolConstants.MAXIMUM_ACTIVE_TIME, 60000,
105: connectionPoolDefinition.getMaximumActiveTime());
106: checkProperty(ProxoolConstants.VERBOSE, "true", new Boolean(
107: connectionPoolDefinition.isVerbose()).toString());
108: checkProperty(ProxoolConstants.TRACE, "true", new Boolean(
109: connectionPoolDefinition.isTrace() == true).toString());
110: checkProperty(ProxoolConstants.FATAL_SQL_EXCEPTION,
111: "Fatal error", connectionPoolDefinition
112: .getFatalSqlExceptions().iterator().next()
113: .toString());
114: checkProperty(ProxoolConstants.PROTOTYPE_COUNT, 2,
115: connectionPoolDefinition.getPrototypeCount());
116: }
117:
118: private static void checkProperty(String name, String correctValue,
119: String candidateValue) throws ProxoolException {
120: if (candidateValue == null) {
121: throw new ProxoolException(name + " was null.");
122: } else if (!candidateValue.equals(correctValue)) {
123: throw new ProxoolException("Expected value for " + name
124: + " was " + correctValue + " but the value was "
125: + candidateValue + ".");
126: }
127: }
128:
129: private static void checkProperty(String name, int correctValue,
130: int candidateValue) throws ProxoolException {
131: checkProperty(name, String.valueOf(correctValue), String
132: .valueOf(candidateValue));
133: }
134:
135: /**
136: * Build a valid Proxool URL
137: * @param alias identifies the pool
138: * @param driver the delegate driver
139: * @param delegateUrl the url to send to the delegate driver
140: * @return proxool.alias:driver:delegateUrl
141: */
142: public static String buildProxoolUrl(String alias, String driver,
143: String delegateUrl) {
144: String url = ProxoolConstants.PROXOOL
145: + ProxoolConstants.ALIAS_DELIMITER + alias
146: + ProxoolConstants.URL_DELIMITER + driver
147: + ProxoolConstants.URL_DELIMITER + delegateUrl;
148: return url;
149: }
150:
151: /**
152: * Build a valid Proxool URL
153: * @param alias identifies the pool
154: * @return proxool.alias
155: */
156: public static String buildProxoolUrl(String alias) {
157: String url = ProxoolConstants.PROXOOL
158: + ProxoolConstants.ALIAS_DELIMITER + alias;
159: return url;
160: }
161:
162: public static Connection getDirectConnection()
163: throws ClassNotFoundException, SQLException {
164: Connection connection = null;
165: Class.forName(TestConstants.HYPERSONIC_DRIVER);
166: Properties info = new Properties();
167: info.setProperty(ProxoolConstants.USER_PROPERTY,
168: TestConstants.HYPERSONIC_USER);
169: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
170: TestConstants.HYPERSONIC_PASSWORD);
171: connection = DriverManager.getConnection(
172: TestConstants.HYPERSONIC_TEST_URL, info);
173: return connection;
174: }
175:
176: }
177:
178: /*
179: Revision history:
180: $Log: TestHelper.java,v $
181: Revision 1.22 2006/01/18 14:40:06 billhorsman
182: Unbundled Jakarta's Commons Logging.
183:
184: Revision 1.21 2003/09/30 18:39:50 billhorsman
185: New test sql syntax constant
186:
187: Revision 1.20 2003/03/05 23:28:55 billhorsman
188: deprecated maximum-new-connections property in favour of
189: more descriptive simultaneous-build-throttle
190:
191: Revision 1.19 2003/03/04 10:58:44 billhorsman
192: checkstyle
193:
194: */
|