001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2004-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java,v 1.7 2005/01/11 08:25:48 jurka Exp $
007: *
008: *-------------------------------------------------------------------------
009: */
010: package org.postgresql.test.jdbc2.optional;
011:
012: import java.sql.SQLException;
013: import java.sql.Statement;
014: import org.postgresql.test.TestUtil;
015: import org.postgresql.jdbc2.optional.PoolingDataSource;
016: import org.postgresql.ds.common.BaseDataSource;
017:
018: /**
019: * Minimal tests for pooling DataSource. Needs many more.
020: *
021: * @author Aaron Mulder (ammulder@chariotsolutions.com)
022: */
023: public class PoolingDataSourceTest extends BaseDataSourceTest {
024: private final static String DS_NAME = "JDBC 2 SE Test DataSource";
025:
026: /**
027: * Constructor required by JUnit
028: */
029: public PoolingDataSourceTest(String name) {
030: super (name);
031: }
032:
033: protected void tearDown() throws Exception {
034: super .tearDown();
035: if (bds instanceof PoolingDataSource) {
036: ((PoolingDataSource) bds).close();
037: }
038: }
039:
040: /**
041: * Creates and configures a new SimpleDataSource.
042: */
043: protected void initializeDataSource() {
044: if (bds == null) {
045: bds = new PoolingDataSource();
046: bds.setServerName(TestUtil.getServer());
047: bds.setPortNumber(TestUtil.getPort());
048: bds.setDatabaseName(TestUtil.getDatabase());
049: bds.setUser(TestUtil.getUser());
050: bds.setPassword(TestUtil.getPassword());
051: ((PoolingDataSource) bds).setDataSourceName(DS_NAME);
052: ((PoolingDataSource) bds).setInitialConnections(2);
053: ((PoolingDataSource) bds).setMaxConnections(10);
054: }
055: }
056:
057: /**
058: * In this case, we *do* want it to be pooled.
059: */
060: public void testNotPooledConnection() {
061: try {
062: con = getDataSourceConnection();
063: String name = con.toString();
064: con.close();
065: con = getDataSourceConnection();
066: String name2 = con.toString();
067: con.close();
068: assertTrue(
069: "Pooled DS doesn't appear to be pooling connections!",
070: name.equals(name2));
071: } catch (SQLException e) {
072: fail(e.getMessage());
073: }
074: }
075:
076: /**
077: * In this case, the desired behavior is dereferencing.
078: */
079: protected void compareJndiDataSource(BaseDataSource oldbds,
080: BaseDataSource bds) {
081: assertTrue(
082: "DataSource was serialized or recreated, should have been dereferenced",
083: bds == oldbds);
084: }
085:
086: /**
087: * Check that 2 DS instances can't use the same name.
088: */
089: public void testCantReuseName() {
090: initializeDataSource();
091: PoolingDataSource pds = new PoolingDataSource();
092: try {
093: pds.setDataSourceName(DS_NAME);
094: fail("Should have denied 2nd DataSource with same name");
095: } catch (IllegalArgumentException e) {
096: }
097: }
098:
099: /**
100: * Closing a Connection twice is not an error.
101: */
102: public void testDoubleConnectionClose() throws SQLException {
103: con = getDataSourceConnection();
104: con.close();
105: con.close();
106: }
107:
108: /**
109: * Closing a Statement twice is not an error.
110: */
111: public void testDoubleStatementClose() throws SQLException {
112: con = getDataSourceConnection();
113: Statement stmt = con.createStatement();
114: stmt.close();
115: stmt.close();
116: con.close();
117: }
118: }
|