1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java,v 1.10 2009/01/07 05:28:59 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2.optional;
import java.sql.*;
import org.postgresql.test.TestUtil;
import org.postgresql.jdbc2.optional.PoolingDataSource;
import org.postgresql.ds.common.BaseDataSource;
/**
* Minimal tests for pooling DataSource. Needs many more.
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
*/
public class PoolingDataSourceTest extends BaseDataSourceTest
{
private final static String DS_NAME = "JDBC 2 SE Test DataSource";
/**
* Constructor required by JUnit
*/
public PoolingDataSourceTest(String name)
{
super(name);
}
protected void tearDown() throws Exception
{
if (bds instanceof PoolingDataSource)
{
((PoolingDataSource) bds).close();
}
super.tearDown();
}
/**
* Creates and configures a new SimpleDataSource.
*/
protected void initializeDataSource()
{
if (bds == null)
{
bds = new PoolingDataSource();
bds.setServerName(TestUtil.getServer());
bds.setPortNumber(TestUtil.getPort());
bds.setDatabaseName(TestUtil.getDatabase());
bds.setUser(TestUtil.getUser());
bds.setPassword(TestUtil.getPassword());
((PoolingDataSource) bds).setDataSourceName(DS_NAME);
((PoolingDataSource) bds).setInitialConnections(2);
((PoolingDataSource) bds).setMaxConnections(10);
}
}
/**
* In this case, we *do* want it to be pooled.
*/
public void testNotPooledConnection()
{
try
{
con = getDataSourceConnection();
String name = con.toString();
con.close();
con = getDataSourceConnection();
String name2 = con.toString();
con.close();
assertTrue("Pooled DS doesn't appear to be pooling connections!", name.equals(name2));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* In this case, the desired behavior is dereferencing.
*/
protected void compareJndiDataSource(BaseDataSource oldbds, BaseDataSource bds)
{
assertTrue("DataSource was serialized or recreated, should have been dereferenced", bds == oldbds);
}
/**
* Check that 2 DS instances can't use the same name.
*/
public void testCantReuseName()
{
initializeDataSource();
PoolingDataSource pds = new PoolingDataSource();
try
{
pds.setDataSourceName(DS_NAME);
fail("Should have denied 2nd DataSource with same name");
}
catch (IllegalArgumentException e)
{
}
}
/**
* Closing a Connection twice is not an error.
*/
public void testDoubleConnectionClose() throws SQLException
{
con = getDataSourceConnection();
con.close();
con.close();
}
/**
* Closing a Statement twice is not an error.
*/
public void testDoubleStatementClose() throws SQLException
{
con = getDataSourceConnection();
Statement stmt = con.createStatement();
stmt.close();
stmt.close();
con.close();
}
public void testConnectionObjectMethods() throws SQLException
{
con = getDataSourceConnection();
Connection conRef = con;
assertEquals(con, conRef);
int hc1 = con.hashCode();
con.close();
int hc2 = con.hashCode();
assertEquals(con, conRef);
assertEquals(hc1, hc2);
}
public void testStatementObjectMethods() throws SQLException
{
con = getDataSourceConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
Statement stmtRef = stmt;
assertEquals(stmt, stmtRef);
// Currently we aren't proxying ResultSet, so this doesn't
// work, see Bug #1010542.
// assertEquals(stmt, rs.getStatement());
int hc1 = stmt.hashCode();
stmt.close();
int hc2 = stmt.hashCode();
assertEquals(stmt, stmtRef);
assertEquals(hc1, hc2);
}
}
|