001: package org.smartlib.pool.core;
002:
003: import junit.framework.TestCase;
004: import org.apache.log4j.LogManager;
005: import org.apache.log4j.PropertyConfigurator;
006: import org.apache.log4j.Logger;
007: import org.smartlib.pool.core.PoolConstants;
008: import org.smartlib.pool.core.PoolConfig;
009:
010: import java.util.Properties;
011: import java.sql.Connection;
012: import java.sql.Statement;
013: import java.sql.ResultSet;
014: import java.sql.PreparedStatement;
015:
016: /**
017: * Created by IntelliJ IDEA.
018: * User: kerneldebugger
019: * Date: Sep 28, 2005
020: * Time: 8:54:09 PM
021: * To change this template use File | Settings | File Templates.
022: */
023: public class PoolTestFixture extends TestCase {
024:
025: Logger logger = Logger.getLogger(PoolTestFixture.class);
026:
027: protected PoolConfig getBasicPoolConfig() {
028: PoolConfig config = new PoolConfig();
029: config.setMultiPoolName("MultiPool-1");
030: config.setAllowAnonymousConnections(true);
031: config.setAutoClose(true);
032: PoolConfig.ConnectionString connectionString = new PoolConfig.ConnectionString();
033: connectionString.setName("MultiPool.Pool-1");
034: connectionString
035: .setConnectString("jdbc:oracle:thin:@db01.dev:1521:dev1");
036: config
037: .setConnectionString(new PoolConfig.ConnectionString[] { connectionString });
038: config.setDefaultPool(true);
039: config.setDetectLeaks(false);
040: config.setDriver("oracle.jdbc.driver.OracleDriver");
041: config.setMaxConnections(3);
042: config.setMinConnections(1);
043: config.setPassword("passw0rd");
044: config.setUserName("sachin");
045: return config;
046: }
047:
048: protected PoolConfig getPoolConfigWithMultiplePools() {
049: PoolConfig config = getBasicPoolConfig();
050:
051: PoolConfig.ConnectionString connectionString = new PoolConfig.ConnectionString();
052: connectionString.setName("MultiPool.Pool-1");
053: connectionString
054: .setConnectString("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=10.102.15.212)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=DEV)(INSTANCE_NAME=DEV1)))");
055: PoolConfig.ConnectionString connectionString1 = new PoolConfig.ConnectionString();
056: connectionString1
057: .setConnectString("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=10.102.15.213)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=DEV)(INSTANCE_NAME=DEV1)))");
058: connectionString1.setName("MultiPool.Pool-2");
059: config.setConnectionString(new PoolConfig.ConnectionString[] {
060: connectionString, connectionString1 });
061: return config;
062:
063: }
064:
065: protected PoolConfig getPoolConfigWithMultipleExternalPools() {
066: PoolConfig config = getBasicPoolConfig();
067:
068: PoolConfig.ConnectionLoaderClass connectionLoaderClass = new PoolConfig.ConnectionLoaderClass();
069:
070: connectionLoaderClass
071: .setName("MultiPool.ConnectionProviderImpl-1");
072: connectionLoaderClass
073: .setConnectionLoaderClass("org.smartlib.pool.core.ConnectionProviderImpl1");
074:
075: PoolConfig.ConnectionLoaderClass connectionLoaderClass1 = new PoolConfig.ConnectionLoaderClass();
076: connectionLoaderClass1
077: .setConnectionLoaderClass("org.smartlib.pool.core.ConnectionProviderImpl2");
078: connectionLoaderClass1
079: .setName("MultiPool.ConnectionProviderImpl-2");
080: config
081: .setConnectionLoaderClass(new PoolConfig.ConnectionLoaderClass[] {
082: connectionLoaderClass, connectionLoaderClass1 });
083: return config;
084:
085: }
086:
087: protected void testConnection(Connection conn) throws Exception {
088:
089: logger.debug("Testing Connection: " + conn);
090: Statement stmt = null;
091: try {
092: stmt = conn.createStatement();
093: stmt.execute("Select count(*) from version");
094: logger.debug("Test Passed: " + conn);
095: } finally {
096: if (stmt != null) {
097: try {
098: stmt.close();
099: } catch (Exception e) {
100: //Ignore
101: }
102: }
103: }
104:
105: }
106:
107: protected void setUp() throws Exception {
108: super .setUp();
109: LogManager.resetConfiguration();
110: Properties log4j = new Properties();
111:
112: // ----------Basic dont change
113: log4j.put("log4j.appender.A1",
114: "org.apache.log4j.DailyRollingFileAppender");
115: log4j.put("log4j.appender.A1.File", "c:\\smartpool.log");
116: log4j.put("log4j.appender.A1.layout",
117: "org.apache.log4j.PatternLayout");
118: log4j.put("log4j.appender.A1.layout.ConversionPattern",
119: "[%d{ISO8601}] <%t> %-5p %c: %m%n");
120: log4j.put("log4j.appender.A1.DatePattern", "'.'yyyy-MM-dd");
121: log4j.put("log4j.appender.SYSTEMOUT",
122: "org.apache.log4j.ConsoleAppender");
123: log4j.put("log4j.appender.SYSTEMOUT.layout",
124: "org.apache.log4j.PatternLayout");
125: log4j.put("log4j.appender.SYSTEMOUT.layout.ConversionPattern",
126: "[%d{ISO8601}] <%t> %-5p %c: %m%n");
127: log4j.put("log4j.appender.KILL",
128: "org.apache.log4j.FileAppender");
129: log4j.put("log4j.appender.KILL.File", "/dev/null");
130: log4j.put("log4j.appender.KILL.layout",
131: "org.apache.log4j.PatternLayout");
132: log4j.put("log4j.appender.KILL.layout.ConversionPattern",
133: "[%d] %m%n");
134: //--------------------------
135:
136: log4j.put("log4j.rootLogger", "WARN, SYSTEMOUT");
137: log4j.put("log4j.logger.org.smartlib", "DEBUG");
138:
139: PropertyConfigurator.configure(log4j);
140: if (logger.isDebugEnabled()) {
141: logger.debug("Log4J Configured");
142: }
143:
144: //Set the test config file name to main config file name
145: System
146: .setProperty(
147: PoolConstants.CONFIG_FILE_SYSTEM_PROPERTY,
148: System
149: .getProperty(PoolConstants.TEST_CONFIG_FILE_SYSTEM_PROPERTY));
150:
151: }
152:
153: /**
154: * Gets the instance name from the V$instance view, the connected user should have the required prevelige
155: * @param conn
156: * @return
157: * @throws Exception
158: */
159: protected String getInstanceName(Connection conn) throws Exception {
160: PreparedStatement stmt = null;
161: ResultSet rs = null;
162: try {
163: stmt = conn
164: .prepareStatement("select instance_name from v$instance");
165: rs = stmt.executeQuery();
166: if (rs.next()) {
167: return rs.getString(1);
168: } else {
169: throw new Exception(
170: "No Rows found, should never happen");
171: }
172: } finally {
173: stmt.close();
174: rs.close();
175: }
176: }
177:
178: }
|