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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java,v 1.13 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2.optional;
import junit.framework.TestCase;
import org.postgresql.test.TestUtil;
import org.postgresql.test.util.MiniJndiContextFactory;
import org.postgresql.ds.common.BaseDataSource;
import org.postgresql.PGConnection;
import java.sql.*;
import java.util.*;
import javax.naming.*;
/**
* Common tests for all the BaseDataSource implementations. This is
* a small variety to make sure that a connection can be opened and
* some basic queries run. The different BaseDataSource subclasses
* have different subclasses of this which add additional custom
* tests.
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
*/
public abstract class BaseDataSourceTest extends TestCase
{
public static String DATA_SOURCE_JNDI = "BaseDataSource";
protected Connection con;
protected BaseDataSource bds;
/**
* Constructor required by JUnit
*/
public BaseDataSourceTest(String name)
{
super(name);
}
/**
* Creates a test table using a standard connection (not from a
* DataSource).
*/
protected void setUp() throws Exception
{
con = TestUtil.openDB();
TestUtil.createTable(con, "poolingtest", "id int4 not null primary key, name varchar(50)");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO poolingtest VALUES (1, 'Test Row 1')");
stmt.executeUpdate("INSERT INTO poolingtest VALUES (2, 'Test Row 2')");
TestUtil.closeDB(con);
}
/**
* Removes the test table using a standard connection (not from
* a DataSource)
*/
protected void tearDown() throws Exception
{
TestUtil.closeDB(con);
con = TestUtil.openDB();
TestUtil.dropTable(con, "poolingtest");
TestUtil.closeDB(con);
}
/**
* Gets a connection from the current BaseDataSource
*/
protected Connection getDataSourceConnection() throws SQLException
{
if (bds == null)
{
initializeDataSource();
}
return bds.getConnection();
}
/**
* Creates an instance of the current BaseDataSource for
* testing. Must be customized by each subclass.
*/
protected abstract void initializeDataSource();
/**
* Test to make sure you can instantiate and configure the
* appropriate DataSource
*/
public void testCreateDataSource()
{
initializeDataSource();
}
/**
* Test to make sure you can get a connection from the DataSource,
* which in turn means the DataSource was able to open it.
*/
public void testGetConnection()
{
try
{
con = getDataSourceConnection();
con.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* A simple test to make sure you can execute SQL using the
* Connection from the DataSource
*/
public void testUseConnection()
{
try
{
con = getDataSourceConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM poolingtest");
if (rs.next())
{
int count = rs.getInt(1);
if (rs.next())
{
fail("Should only have one row in SELECT COUNT result set");
}
if (count != 2)
{
fail("Count returned " + count + " expecting 2");
}
}
else
{
fail("Should have one row in SELECT COUNT result set");
}
rs.close();
st.close();
con.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* A test to make sure you can execute DDL SQL using the
* Connection from the DataSource.
*/
public void testDdlOverConnection()
{
try
{
con = getDataSourceConnection();
TestUtil.createTable(con, "poolingtest", "id int4 not null primary key, name varchar(50)");
con.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* A test to make sure the connections are not being pooled by the
* current DataSource. Obviously need to be overridden in the case
* of a pooling Datasource.
*/
public void testNotPooledConnection()
{
try
{
con = getDataSourceConnection();
String name = con.toString();
con.close();
con = getDataSourceConnection();
String name2 = con.toString();
con.close();
assertTrue(!name.equals(name2));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Test to make sure that PGConnection methods can be called on the
* pooled Connection.
*/
public void testPGConnection()
{
try
{
con = getDataSourceConnection();
((PGConnection)con).getNotifications();
con.close();
}
catch (Exception e)
{
fail("Unable to call PGConnection method on pooled connection due to " + e.getClass().getName() + " (" + e.getMessage() + ")");
}
}
/**
* Uses the mini-JNDI implementation for testing purposes
*/
protected InitialContext getInitialContext()
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, MiniJndiContextFactory.class.getName());
try
{
return new InitialContext(env);
}
catch (NamingException e)
{
fail("Unable to create InitialContext: " + e.getMessage());
return null;
}
}
/**
* Eventually, we must test stuffing the DataSource in JNDI and
* then getting it back out and make sure it's still usable. This
* should ideally test both Serializable and Referenceable
* mechanisms. Will probably be multiple tests when implemented.
*/
public void testJndi()
{
initializeDataSource();
BaseDataSource oldbds = bds;
InitialContext ic = getInitialContext();
try
{
ic.rebind(DATA_SOURCE_JNDI, bds);
bds = (BaseDataSource)ic.lookup(DATA_SOURCE_JNDI);
assertTrue("Got null looking up DataSource from JNDI!", bds != null);
compareJndiDataSource(oldbds, bds);
}
catch (NamingException e)
{
fail(e.getMessage());
}
oldbds = bds;
testUseConnection();
assertTrue("Test should not have changed DataSource (" + bds + " != " + oldbds + ")!", bds == oldbds);
}
/**
* Check whether a DS was dereferenced from JNDI or recreated.
*/
protected void compareJndiDataSource(BaseDataSource oldbds, BaseDataSource bds) {
assertTrue("DataSource was dereferenced, should have been serialized or recreated", bds != oldbds);
}
}
|