001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2008 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql.pool;
022:
023: import java.sql.Connection;
024: import java.sql.SQLException;
025:
026: import javax.naming.spi.ObjectFactory;
027: import javax.sql.ConnectionPoolDataSource;
028: import javax.sql.PooledConnection;
029:
030: import net.sf.hajdbc.sql.TestCommonDataSourceDatabase;
031:
032: import org.easymock.EasyMock;
033:
034: /**
035: * @author Paul Ferraro
036: *
037: */
038: @SuppressWarnings("nls")
039: public class TestConnectionPoolDataSourceDatabase
040: extends
041: TestCommonDataSourceDatabase<ConnectionPoolDataSourceDatabase, ConnectionPoolDataSource> {
042: private PooledConnection connection = EasyMock
043: .createStrictMock(PooledConnection.class);
044:
045: /**
046: *
047: */
048: public TestConnectionPoolDataSourceDatabase() {
049: super (EasyMock.createStrictMock(ConnectionPoolDataSource.class));
050: }
051:
052: /**
053: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#objects()
054: */
055: @Override
056: protected Object[] objects() {
057: return new Object[] { this .dataSource, this .connection };
058: }
059:
060: /**
061: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#createDatabase()
062: */
063: @Override
064: protected ConnectionPoolDataSourceDatabase createDatabase() {
065: return new ConnectionPoolDataSourceDatabase();
066: }
067:
068: /**
069: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#mockDataSourceClass()
070: */
071: @Override
072: protected Class<? extends ConnectionPoolDataSource> mockDataSourceClass() {
073: return MockConnectionPoolDataSource.class;
074: }
075:
076: /**
077: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#objectFactoryClass()
078: */
079: @Override
080: protected Class<? extends ObjectFactory> objectFactoryClass() {
081: return MockConnectionPoolDataSourceFactory.class;
082: }
083:
084: /**
085: * @see net.sf.hajdbc.Database#connect(java.lang.Object)
086: */
087: @Override
088: public Connection connect(ConnectionPoolDataSource dataSource)
089: throws SQLException {
090: ConnectionPoolDataSourceDatabase database = this
091: .createDatabase("1");
092:
093: Connection connection = EasyMock.createMock(Connection.class);
094:
095: EasyMock.expect(this .dataSource.getPooledConnection())
096: .andReturn(this .connection);
097: EasyMock.expect(this .connection.getConnection()).andReturn(
098: connection);
099:
100: this .replay();
101:
102: Connection result = database.connect(dataSource);
103:
104: this .verify();
105:
106: assert result == connection : result.getClass().getName();
107:
108: database.setUser("user");
109: database.setPassword("password");
110:
111: EasyMock
112: .expect(
113: this .dataSource.getPooledConnection("user",
114: "password")).andReturn(this.connection);
115: EasyMock.expect(this.connection.getConnection()).andReturn(
116: connection);
117:
118: this.replay();
119:
120: result = database.connect(dataSource);
121:
122: this.verify();
123:
124: assert result == connection : result.getClass().getName();
125:
126: return result;
127: }
128: }
|