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
|
/*
* Bitronix Transaction Manager
*
* Copyright (c) 2010, Bitronix Software.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package bitronix.tm.mock.resource.jdbc;
import static org.mockito.Mockito.*;
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.transaction.xa.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import bitronix.tm.mock.events.*;
import bitronix.tm.mock.resource.MockXAResource;
/**
*
* @author lorban
*/
public class MockitoXADataSource implements XADataSource {
private List xaConnections = new ArrayList();
private String userName;
private String password;
private String database;
private Object uselessThing;
private List inDoubtXids = new ArrayList();
private SQLException getXAConnectionException;
private static SQLException staticGetXAConnectionException;
private static SQLException staticCloseXAConnectionException;
public int getLoginTimeout() throws SQLException {
return 0;
}
public void setLoginTimeout(int seconds) throws SQLException {
}
public PrintWriter getLogWriter() throws SQLException {
return null;
}
public void setLogWriter(PrintWriter out) throws SQLException {
}
public XAConnection getXAConnection() throws SQLException {
if (staticGetXAConnectionException != null)
throw staticGetXAConnectionException;
if (getXAConnectionException != null)
throw getXAConnectionException;
// Create an XAResource
XAResource xaResource = new MockXAResource(this);
// Setup mock XAConnection
final XAConnection mockXAConnection = mock(XAConnection.class);
// Handle XAConnection.close(), first time we answer, after that we throw
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) throws Throwable {
EventRecorder eventRecorder = EventRecorder.getEventRecorder(mockXAConnection);
eventRecorder.addEvent(new XAConnectionCloseEvent(mockXAConnection));
return null;
}
}).doThrow(new SQLException("XAConnection is already closed")).when(mockXAConnection).close();
when(mockXAConnection.getXAResource()).thenReturn(xaResource);
Connection mockConnection = createMockConnection();
when(mockXAConnection.getConnection()).thenReturn(mockConnection);
if (staticCloseXAConnectionException != null)
doThrow(staticCloseXAConnectionException).when(mockXAConnection).close();
xaConnections.add(mockXAConnection);
return mockXAConnection;
}
public XAConnection getXAConnection(String user, String password) throws SQLException {
return getXAConnection();
}
public void setXaConnections(List xaConnections) {
this.xaConnections = xaConnections;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public void addInDoubtXid(Xid xid) {
inDoubtXids.add(xid);
}
public boolean removeInDoubtXid(Xid xid) {
for (int i = 0; i < inDoubtXids.size(); i++) {
Xid xid1 = (Xid) inDoubtXids.get(i);
if (Arrays.equals(xid1.getGlobalTransactionId(), xid.getGlobalTransactionId()) && Arrays.equals(xid1.getBranchQualifier(), xid.getBranchQualifier()) ) {
inDoubtXids.remove(xid1);
return true;
}
}
return false;
}
public Xid[] getInDoubtXids() {
return (Xid[]) inDoubtXids.toArray(new Xid[inDoubtXids.size()]);
}
public void setGetXAConnectionException(SQLException ex) {
this.getXAConnectionException = ex;
}
public static void setStaticGetXAConnectionException(SQLException ex) {
staticGetXAConnectionException = ex;
}
public static void setStaticCloseXAConnectionException(SQLException ex) {
staticCloseXAConnectionException = ex;
}
public static Connection createMockConnection() throws SQLException {
// Setup mock connection
final Connection mockConnection = mock(Connection.class);
// Autocommit is always true by default
when(mockConnection.getAutoCommit()).thenReturn(true);
// Handle Connection.createStatement()
Statement statement = mock(Statement.class);
when(mockConnection.createStatement()).thenReturn(statement);
when(mockConnection.createStatement(anyInt(), anyInt())).thenReturn(statement);
when(mockConnection.createStatement(anyInt(), anyInt(), anyInt())).thenReturn(statement);
// Handle Connection.prepareStatement()
PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);
when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), anyInt())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), (int[]) anyObject())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), (String[]) anyObject())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
// Handle Connection.prepareCall()
CallableStatement mockCallableStatement = mock(CallableStatement.class);
when(mockConnection.prepareCall(anyString())).thenReturn(mockCallableStatement);
when(mockConnection.prepareCall(anyString(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
when(mockConnection.prepareCall(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
// Handle Connection.close()
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) throws Throwable {
EventRecorder eventRecorder = EventRecorder.getEventRecorder(mockConnection);
eventRecorder.addEvent(new ConnectionCloseEvent(mockConnection));
return null;
}
}).doThrow(new SQLException("Connection is already closed")).when(mockConnection).close();
// Handle Connection.commit()
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) throws Throwable {
EventRecorder eventRecorder = EventRecorder.getEventRecorder(mockConnection);
eventRecorder.addEvent(new LocalCommitEvent(mockConnection, new Exception()));
return null;
}
}).doThrow(new SQLException("Transaction already commited")).when(mockConnection).commit();
// Handle Connection.rollback()
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) throws Throwable {
EventRecorder eventRecorder = EventRecorder.getEventRecorder(mockConnection);
eventRecorder.addEvent(new LocalRollbackEvent(mockConnection, new Exception()));
return null;
}
}).doThrow(new SQLException("Transaction already rolledback")).when(mockConnection).rollback();
return mockConnection;
}
public Object getUselessThing() {
return uselessThing;
}
public void setUselessThing(Object uselessThing) {
this.uselessThing = uselessThing;
}
@Override
public java.util.logging.Logger getParentLogger()
throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}
}
|