001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
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.arcsde.pool;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.logging.Logger;
026:
027: import junit.framework.TestCase;
028:
029: import org.geotools.arcsde.pool.ArcSDEConnectionConfig;
030: import org.geotools.arcsde.pool.ArcSDEConnectionPool;
031: import org.geotools.arcsde.pool.ArcSDEConnectionPoolFactory;
032: import org.geotools.arcsde.pool.ArcSDEPooledConnection;
033: import org.geotools.arcsde.pool.UnavailableArcSDEConnectionException;
034: import org.geotools.data.DataSourceException;
035:
036: /**
037: * Tests de functionality of a pool of ArcSDE connection objects over a live
038: * ArcSDE database
039: *
040: * @author Gabriel Roldan, Axios Engineering
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/test/java/org/geotools/arcsde/pool/ArcSDEConnectionPoolTest.java $
042: * @version $Id: ArcSDEConnectionPoolTest.java 27863 2007-11-12 20:34:34Z desruisseaux $
043: */
044: public class ArcSDEConnectionPoolTest extends TestCase {
045: /** DOCUMENT ME! */
046: private static Logger LOGGER = org.geotools.util.logging.Logging
047: .getLogger("org.geotools.data.sde");
048:
049: /** DOCUMENT ME! */
050: private Map connectionParameters;
051:
052: /** DOCUMENT ME! */
053: private ArcSDEConnectionConfig connectionConfig = null;
054:
055: /** DOCUMENT ME! */
056: private ArcSDEConnectionPool pool = null;
057:
058: /**
059: * Creates a new ArcSDEConnectionPoolTest object.
060: *
061: * @param name DOCUMENT ME!
062: */
063: public ArcSDEConnectionPoolTest(String name) {
064: super (name);
065: }
066:
067: /**
068: * loads {@code test-data/testparams.properties} to get connection parameters and
069: * sets up a ArcSDEConnectionConfig with them for tests to set up ArcSDEConnectionPool's
070: * as requiered
071: *
072: * @throws Exception DOCUMENT ME!
073: * @throws IllegalStateException DOCUMENT ME!
074: */
075: protected void setUp() throws Exception {
076: super .setUp();
077:
078: Properties conProps = new Properties();
079: String propsFile = "testparams.properties";
080: URL conParamsSource = org.geotools.test.TestData.url(null,
081: propsFile);
082:
083: LOGGER.fine("loading connection parameters from "
084: + conParamsSource.toExternalForm());
085:
086: InputStream in = conParamsSource.openStream();
087:
088: if (in == null) {
089: throw new IllegalStateException("cannot find test params: "
090: + conParamsSource.toExternalForm());
091: }
092:
093: conProps.load(in);
094: in.close();
095: connectionParameters = conProps;
096:
097: //test that mandatory connection parameters are set
098: try {
099: connectionConfig = new ArcSDEConnectionConfig(conProps);
100: } catch (Exception ex) {
101: throw new IllegalStateException(
102: "No valid connection parameters found in "
103: + conParamsSource.toExternalForm() + ": "
104: + ex.getMessage());
105: }
106: }
107:
108: /**
109: * closes the connection pool if it's still open
110: *
111: * @throws Exception DOCUMENT ME!
112: */
113: protected void tearDown() throws Exception {
114: connectionConfig = null;
115:
116: if (pool != null) {
117: pool.close();
118: }
119:
120: pool = null;
121: super .tearDown();
122: }
123:
124: /**
125: * Sets up a new ArcSDEConnectionPool with the connection parameters stored
126: * in <code>connParams</code> and throws an exception if something goes
127: * wrong
128: *
129: * @param connParams a set of connection parameters from where the new
130: * ArcSDEConnectionPool will connect to the SDE database and create
131: * connections
132: *
133: * @return DOCUMENT ME!
134: *
135: * @throws IllegalArgumentException if the set of connection parameters are
136: * not propperly set
137: * @throws NullPointerException if <code>connParams</code> is null
138: * @throws DataSourceException if the pool can't create the connections
139: * with the passed arguments (i.e. can't connect to SDE database)
140: */
141: private ArcSDEConnectionPool createPool(Map connParams)
142: throws IllegalArgumentException, NullPointerException,
143: DataSourceException {
144: this .connectionConfig = new ArcSDEConnectionConfig(connParams);
145: LOGGER.fine("creating a new ArcSDEConnectionPool with "
146: + connectionConfig);
147:
148: if (this .pool != null) {
149: LOGGER.fine("pool already created, closing it");
150: this .pool.close();
151: }
152:
153: this .pool = new ArcSDEConnectionPool(connectionConfig);
154: LOGGER.fine("pool created");
155:
156: return this .pool;
157: }
158:
159: /**
160: * tests that a connection to a live ArcSDE database can be established
161: * with the parameters defined int testparams.properties, and a
162: * ArcSDEConnectionPool can be properly setted up
163: *
164: * @throws IOException DOCUMENT ME!
165: */
166: public void testConnect() throws IOException {
167: LOGGER.fine("testing connection to the sde database");
168:
169: ArcSDEConnectionPoolFactory pf = ArcSDEConnectionPoolFactory
170: .getInstance();
171: ArcSDEConnectionConfig congfig = null;
172:
173: congfig = new ArcSDEConnectionConfig(connectionParameters);
174:
175: try {
176: ArcSDEConnectionPool pool = pf.createPool(congfig);
177: LOGGER.fine("connection succeed " + pool.getPoolSize()
178: + " connections ready");
179: } catch (DataSourceException ex) {
180: throw ex;
181: } finally {
182: pf.clear(); //close and remove all pools
183: }
184: }
185:
186: /**
187: * Checks that after creation the pool has the specified initial number of
188: * connections.
189: *
190: * @throws DataSourceException DOCUMENT ME!
191: * @throws UnavailableArcSDEConnectionException DOCUMENT ME!
192: */
193: public void testInitialCount() throws DataSourceException,
194: UnavailableArcSDEConnectionException {
195: int MIN_CONNECTIONS = 2;
196: int MAX_CONNECTIONS = 6;
197:
198: //override pool.minConnections and pool.maxConnections from
199: //the configured parameters to test the connections' pool
200: //availability
201: Map params = new HashMap(this .connectionParameters);
202: params.put(ArcSDEConnectionConfig.MIN_CONNECTIONS_PARAM,
203: new Integer(MIN_CONNECTIONS));
204: params.put(ArcSDEConnectionConfig.MAX_CONNECTIONS_PARAM,
205: new Integer(MAX_CONNECTIONS));
206:
207: createPool(params);
208:
209: //check that after creation, the pool contains the minimun number
210: //of connections specified
211: assertEquals(
212: "after creation, the pool must contain the minimun number of connections specified",
213: MIN_CONNECTIONS, this .pool.getPoolSize());
214: }
215:
216: /**
217: * Tests that the pool creation fails if a wrong set of parameters is
218: * passed (i.e. maxConnections is lower than minConnections)
219: *
220: * @throws DataSourceException
221: * @throws UnavailableArcSDEConnectionException
222: */
223: public void testChecksLimits() throws DataSourceException,
224: UnavailableArcSDEConnectionException {
225: int MIN_CONNECTIONS = 2;
226:
227: //override pool.minConnections and pool.maxConnections from
228: //the configured parameters to test the connections' pool
229: //availability
230: Map params = new HashMap(this .connectionParameters);
231: params.put(ArcSDEConnectionConfig.MIN_CONNECTIONS_PARAM,
232: new Integer(MIN_CONNECTIONS));
233: params.put(ArcSDEConnectionConfig.MAX_CONNECTIONS_PARAM,
234: new Integer(1));
235:
236: //this MUST fail, since maxConnections is lower than minConnections
237: try {
238: LOGGER
239: .fine("testing parameters' sanity check at pool creation time");
240: createPool(params);
241: fail("the connection pool creation must have failed since a wrong set of arguments was passed");
242: } catch (IllegalArgumentException ex) {
243: //it's ok, it is what's expected
244: LOGGER.fine("pramams assertion passed");
245: }
246: }
247:
248: /**
249: * tests that no more than pool.maxConnections connections can be created,
250: * and once one connection is freed, it is ready to be used again.
251: *
252: * @throws DataSourceException DOCUMENT ME!
253: * @throws UnavailableArcSDEConnectionException DOCUMENT ME!
254: */
255: public void testMaxConnections() throws DataSourceException,
256: UnavailableArcSDEConnectionException {
257: final int MIN_CONNECTIONS = 2;
258: final int MAX_CONNECTIONS = 2;
259:
260: Map params = new HashMap(this .connectionParameters);
261: params.put(ArcSDEConnectionConfig.MIN_CONNECTIONS_PARAM,
262: new Integer(MIN_CONNECTIONS));
263: params.put(ArcSDEConnectionConfig.MAX_CONNECTIONS_PARAM,
264: new Integer(MAX_CONNECTIONS));
265:
266: createPool(params);
267:
268: ArcSDEPooledConnection[] conns = new ArcSDEPooledConnection[MAX_CONNECTIONS];
269: //try to get the maximun number of connections specified, and do not
270: //release anyone
271: for (int i = 0; i < MAX_CONNECTIONS; i++) {
272: conns[i] = pool.getConnection();
273: }
274:
275: //now that the max number of connections is reached, the pool
276: //should throw an UnavailableArcSDEConnectionException
277: try {
278: this .pool.getConnection();
279: fail("since the max number of connections was reached, the pool should have throwed an UnavailableArcSDEConnectionException");
280: } catch (UnavailableArcSDEConnectionException ex) {
281: LOGGER
282: .fine("maximun number of connections reached, got an UnavailableArcSDEConnectionException, it's OK");
283: }
284:
285: //now, free one and check the same conection is returned on the
286: //next call to getConnection()
287: ArcSDEPooledConnection expected = conns[0];
288: expected.close();
289:
290: ArcSDEPooledConnection conn = this .pool.getConnection();
291: assertEquals(expected, conn);
292: }
293:
294: /**
295: * a null database name should not be an impediment to create the pool
296: *
297: * @throws DataSourceException
298: */
299: public void testCreateWithNullDBName() throws DataSourceException {
300: Map params = new HashMap(this .connectionParameters);
301: params.remove(ArcSDEConnectionConfig.INSTANCE_NAME_PARAM);
302: createPool(params);
303: }
304:
305: /**
306: * an empty database name should not be an impediment to create the pool
307: *
308: * @throws DataSourceException
309: */
310: public void testCreateWithEmptyDBName() throws DataSourceException {
311: Map params = new HashMap(this .connectionParameters);
312: params.put(ArcSDEConnectionConfig.INSTANCE_NAME_PARAM, "");
313: createPool(params);
314: }
315: }
|