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.lang.reflect.Proxy;
024: import java.sql.SQLException;
025: import java.util.Map;
026:
027: import javax.sql.PooledConnection;
028: import javax.sql.XAConnection;
029: import javax.sql.XADataSource;
030: import javax.transaction.xa.XAResource;
031:
032: import net.sf.hajdbc.sql.Invoker;
033: import net.sf.hajdbc.sql.SQLProxy;
034: import net.sf.hajdbc.sql.pool.AbstractPooledConnectionInvocationHandler;
035: import net.sf.hajdbc.sql.pool.TestPooledConnection;
036:
037: import org.easymock.EasyMock;
038:
039: /**
040: * @author Paul Ferraro
041: *
042: */
043: @SuppressWarnings("unchecked")
044: public class TestXAConnection extends TestPooledConnection implements
045: XAConnection {
046: protected XAConnection getXAConnection() {
047: return (XAConnection) this .connection;
048: }
049:
050: protected XAConnection getConnection1() {
051: return (XAConnection) this .connection1;
052: }
053:
054: protected XAConnection getConnection2() {
055: return (XAConnection) this .connection2;
056: }
057:
058: /**
059: * @see net.sf.hajdbc.sql.pool.TestPooledConnection#getConnectionClass()
060: */
061: @Override
062: protected Class<? extends PooledConnection> getConnectionClass() {
063: return XAConnection.class;
064: }
065:
066: /**
067: * @see net.sf.hajdbc.sql.pool.TestPooledConnection#getInvocationHandler(java.util.Map)
068: */
069: @Override
070: protected AbstractPooledConnectionInvocationHandler getInvocationHandler(
071: Map map) throws Exception {
072: return new XAConnectionInvocationHandler(EasyMock
073: .createStrictMock(XADataSource.class), this .parent,
074: EasyMock.createMock(Invoker.class), map);
075: }
076:
077: /**
078: * @see javax.sql.XAConnection#getXAResource()
079: */
080: @Override
081: public XAResource getXAResource() throws SQLException {
082: XAResource resource1 = EasyMock
083: .createStrictMock(XAResource.class);
084: XAResource resource2 = EasyMock
085: .createStrictMock(XAResource.class);
086:
087: EasyMock.expect(this .cluster.isActive()).andReturn(true);
088:
089: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
090: .andReturn(this .executor);
091:
092: EasyMock.expect(this .cluster.getBalancer()).andReturn(
093: this .balancer);
094: EasyMock.expect(this .balancer.all())
095: .andReturn(this .databaseSet);
096:
097: EasyMock.expect(this .getConnection1().getXAResource())
098: .andReturn(resource1);
099: EasyMock.expect(this .getConnection2().getXAResource())
100: .andReturn(resource2);
101:
102: this .replay();
103:
104: XAResource result = this .getXAConnection().getXAResource();
105:
106: this .verify();
107:
108: assert Proxy.isProxyClass(result.getClass());
109:
110: SQLProxy proxy = SQLProxy.class.cast(Proxy
111: .getInvocationHandler(result));
112:
113: assert proxy.getObject(this.database1) == resource1;
114: assert proxy.getObject(this.database2) == resource2;
115:
116: return result;
117: }
118: }
|