001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) Copyright IBM Corporation, 2005. All rights reserved.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.data.db2;
018:
019: import com.ibm.db2.jcc.DB2ConnectionPoolDataSource;
020: import junit.framework.TestCase;
021: import org.geotools.data.db2.DB2ConnectionFactory;
022: import org.geotools.data.jdbc.ConnectionPool;
023: import org.geotools.data.jdbc.JDBCDataStoreConfig;
024: import org.geotools.data.jdbc.datasource.DataSourceUtil;
025: import org.geotools.data.jdbc.datasource.ManageableDataSource;
026:
027: import java.io.IOException;
028: import java.sql.Connection;
029: import java.sql.SQLException;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.PropertyResourceBundle;
034: import java.util.Set;
035:
036: import javax.sql.DataSource;
037: import javax.sql.PooledConnection;
038:
039: /**
040: * Provide common functionality for DB2 plug-in testcases.
041: *
042: * @author David Adler - IBM Corporation
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/test/java/org/geotools/data/db2/DB2TestCase.java $
044: */
045: public class DB2TestCase extends TestCase {
046: protected static final String DB2_URL_PREFIX = "jdbc:db2://";
047: protected Map allParams = null;
048: protected String dbname = null;
049: protected String user = null;
050: protected String pw = null;
051: protected String host = null;
052: protected int portnum = 50000;
053: protected String tabSchema = null;
054: protected String dbURL = null;
055: protected boolean mock = false;
056: protected ManageableDataSource pool = null;
057:
058: protected void setUp() throws Exception {
059: super .setUp();
060: setPropertyValues();
061: setAllParamValues();
062: }
063:
064: protected void tearDown() throws Exception {
065: if (pool != null)
066: pool.close();
067: super .tearDown();
068: }
069:
070: protected String getDbURL() {
071: return DB2_URL_PREFIX + host + ":" + portnum + "/" + dbname;
072: }
073:
074: /**
075: * Sets local values used in testing from a properties file
076: *
077: * @throws IOException
078: * @throws SQLException
079: */
080: protected void setPropertyValues() throws IOException, SQLException {
081: String propertyFile = "db2test.properties";
082: PropertyResourceBundle resource = new PropertyResourceBundle(
083: this .getClass().getResourceAsStream(propertyFile));
084: host = resource.getString("host");
085: portnum = Integer.parseInt(resource.getString("portnum"));
086: dbname = resource.getString("dbname");
087: user = resource.getString("user");
088: pw = resource.getString("password");
089: tabSchema = resource.getString("tabschema");
090:
091: if (resource.getString("mock").equals("1")) {
092: mock = true;
093: } else {
094: mock = false;
095: }
096: }
097:
098: protected void setAllParamValues() {
099: allParams = new HashMap();
100: allParams.put("database", dbname);
101: allParams.put("dbtype", "db2");
102: allParams.put("host", host);
103: allParams.put("port", String.valueOf(portnum));
104: allParams.put("user", user);
105: allParams.put("passwd", pw);
106: allParams.put("tabschema", tabSchema);
107: }
108:
109: /**
110: * Common local method to get a Connection for testing
111: *
112: *
113: * @throws Exception
114: */
115: protected Connection getLocalConnection() throws Exception {
116: DB2ConnectionPoolDataSource poolDataSource;
117: poolDataSource = new DB2ConnectionPoolDataSource();
118: poolDataSource.setDatabaseName(dbname);
119: poolDataSource.setUser(user);
120: poolDataSource.setPassword(pw);
121: poolDataSource.setPortNumber(portnum);
122: poolDataSource.setServerName(host);
123: poolDataSource.setDriverType(4);
124:
125: PooledConnection pc = poolDataSource.getPooledConnection();
126: Connection conn = pc.getConnection();
127:
128: return conn;
129: }
130:
131: /**
132: * Common local method to get a ConnectionPool for testing
133: *
134: *
135: * @throws Exception
136: */
137: protected ManageableDataSource getLocalConnectionPool()
138: throws Exception {
139: String url = DB2DataStoreFactory.getJDBCUrl(host, portnum,
140: dbname);
141: return DB2DataStoreFactory.getDefaultDataSource(url, user, pw,
142: 10, 2, false);
143: }
144:
145: /**
146: * Local utility method to make a copy of a Map object.
147: *
148: * <p>
149: * Used to make copies of a HashMap of Param objects but should work for
150: * any Map.
151: * </p>
152: *
153: * @param params an arbitrary Map object
154: *
155: * @return a copy of the input Map object
156: */
157: protected Map copyParams(Map params) {
158: Map p2 = new HashMap();
159: Set keys = params.keySet();
160: Iterator it = keys.iterator();
161:
162: while (it.hasNext()) {
163: String key = (String) it.next();
164: p2.put(key, params.get(key));
165: }
166:
167: return p2;
168: }
169:
170: protected DB2DataStore getDataStore() throws Exception {
171: JDBCDataStoreConfig config = new JDBCDataStoreConfig(tabSchema,
172: tabSchema, 100000);
173:
174: if (pool == null) {
175: pool = getLocalConnectionPool();
176: }
177:
178: return new DB2DataStore(pool, config, getDbURL());
179: }
180: }
|