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.xa;
022:
023: import java.sql.Connection;
024: import java.sql.SQLException;
025:
026: import javax.naming.spi.ObjectFactory;
027: import javax.sql.XAConnection;
028: import javax.sql.XADataSource;
029:
030: import net.sf.hajdbc.sql.TestCommonDataSourceDatabase;
031:
032: import org.easymock.EasyMock;
033:
034: /**
035: * @author Paul Ferraro
036: *
037: */
038: public class TestXADataSourceDatabase
039: extends
040: TestCommonDataSourceDatabase<XADataSourceDatabase, XADataSource> {
041: private XAConnection connection = EasyMock
042: .createStrictMock(XAConnection.class);
043:
044: public TestXADataSourceDatabase() {
045: super (EasyMock.createStrictMock(XADataSource.class));
046: }
047:
048: /**
049: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#objects()
050: */
051: @Override
052: protected Object[] objects() {
053: return new Object[] { this .dataSource, this .connection };
054: }
055:
056: /**
057: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#createDatabase()
058: */
059: @Override
060: protected XADataSourceDatabase createDatabase() {
061: return new XADataSourceDatabase();
062: }
063:
064: /**
065: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#mockDataSourceClass()
066: */
067: @Override
068: protected Class<? extends XADataSource> mockDataSourceClass() {
069: return MockXADataSource.class;
070: }
071:
072: /**
073: * @see net.sf.hajdbc.sql.TestCommonDataSourceDatabase#objectFactoryClass()
074: */
075: @Override
076: protected Class<? extends ObjectFactory> objectFactoryClass() {
077: return MockXADataSourceFactory.class;
078: }
079:
080: /**
081: * @see net.sf.hajdbc.Database#connect(java.lang.Object)
082: */
083: @Override
084: public Connection connect(XADataSource dataSource)
085: throws SQLException {
086: XADataSourceDatabase database = this .createDatabase("1");
087:
088: Connection connection = EasyMock.createMock(Connection.class);
089:
090: EasyMock.expect(this .dataSource.getXAConnection()).andReturn(
091: this .connection);
092: EasyMock.expect(this .connection.getConnection()).andReturn(
093: connection);
094:
095: this .replay();
096:
097: Connection result = database.connect(dataSource);
098:
099: this .verify();
100:
101: assert result == connection : result.getClass().getName();
102:
103: database.setUser("user");
104: database.setPassword("password");
105:
106: EasyMock.expect(
107: this .dataSource.getXAConnection("user", "password"))
108: .andReturn(this.connection);
109: EasyMock.expect(this.connection.getConnection()).andReturn(
110: connection);
111:
112: this.replay();
113:
114: result = database.connect(dataSource);
115:
116: this.verify();
117:
118: assert result == connection : result.getClass().getName();
119:
120: return result;
121: }
122: }
|