001: /**
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 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: */package net.sf.hajdbc.sql;
021:
022: import java.sql.Connection;
023: import java.sql.SQLException;
024:
025: import javax.naming.spi.ObjectFactory;
026: import javax.sql.DataSource;
027:
028: import org.easymock.EasyMock;
029: import org.testng.annotations.Test;
030:
031: /**
032: * Unit test for {@link DataSourceDatabase}.
033: * @author Paul Ferraro
034: * @since 1.1
035: */
036: @SuppressWarnings("nls")
037: public class TestDataSourceDatabase extends
038: TestCommonDataSourceDatabase<DataSourceDatabase, DataSource> {
039: public TestDataSourceDatabase() {
040: super (EasyMock.createStrictMock(DataSource.class));
041: }
042:
043: /**
044: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#createDatabase()
045: */
046: @Override
047: protected DataSourceDatabase createDatabase() {
048: return new DataSourceDatabase();
049: }
050:
051: /**
052: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#mockDataSourceClass()
053: */
054: @Override
055: protected Class<? extends DataSource> mockDataSourceClass() {
056: return MockDataSource.class;
057: }
058:
059: /**
060: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#objectFactoryClass()
061: */
062: @Override
063: protected Class<? extends ObjectFactory> objectFactoryClass() {
064: return MockDataSourceFactory.class;
065: }
066:
067: @Override
068: @Test(dataProvider="datasource")
069: public Connection connect(DataSource dataSource)
070: throws SQLException {
071: DataSourceDatabase database = this .createDatabase("1");
072:
073: Connection connection = EasyMock.createMock(Connection.class);
074:
075: EasyMock.expect(this .dataSource.getConnection()).andReturn(
076: connection);
077:
078: this .replay();
079:
080: Connection result = database.connect(dataSource);
081:
082: this .verify();
083:
084: assert result == connection : result.getClass().getName();
085:
086: this .reset();
087:
088: database.setUser("user");
089: database.setPassword("password");
090:
091: EasyMock.expect(
092: this .dataSource.getConnection("user", "password"))
093: .andReturn(connection);
094:
095: this.replay();
096:
097: result = database.connect(dataSource);
098:
099: this.verify();
100:
101: assert result == connection : result.getClass().getName();
102:
103: return result;
104: }
105: }
|