use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class ConnectionProxyXATest method testXARollback.
@Test
public void testXARollback() throws Throwable {
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(true);
XAResource xaResource = Mockito.mock(XAResource.class);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
Mockito.when(xaConnection.getXAResource()).thenReturn(xaResource);
BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
String xid = "xxx";
ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
connectionProxyXA.init();
connectionProxyXA.xaRollback("xxx", 123L, null);
Mockito.verify(xaResource, times(0)).commit(any(Xid.class), any(Boolean.class));
Mockito.verify(xaResource).rollback(any(Xid.class));
}
use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class ConnectionProxyXATest method testCreateStatement.
@Test
public void testCreateStatement() throws Throwable {
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(true);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
String xid = "xxx";
ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
Statement statement = connectionProxyXA.createStatement();
Assertions.assertTrue(statement instanceof StatementProxyXA);
}
use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class DataSourceProxyXANativeTest method testGetConnection.
@Test
public void testGetConnection() throws SQLException {
// Mock
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(true);
DatabaseMetaData metaData = Mockito.mock(DatabaseMetaData.class);
Mockito.when(metaData.getURL()).thenReturn("jdbc:mysql:xxx");
Mockito.when(connection.getMetaData()).thenReturn(metaData);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
Mockito.when(xaConnection.getConnection()).thenReturn(connection);
XADataSource xaDataSource = Mockito.mock(XADataSource.class);
Mockito.when(xaDataSource.getXAConnection()).thenReturn(xaConnection);
DataSourceProxyXANative dataSourceProxyXANative = new DataSourceProxyXANative(xaDataSource);
Connection connFromDataSourceProxyXANative = dataSourceProxyXANative.getConnection();
Assertions.assertTrue(connFromDataSourceProxyXANative instanceof ConnectionProxyXA);
XAConnection xaConnectionFromProxy = ((ConnectionProxyXA) connFromDataSourceProxyXANative).getWrappedXAConnection();
Assertions.assertTrue(xaConnection == xaConnectionFromProxy);
Connection connectionFromProxy = ((ConnectionProxyXA) connFromDataSourceProxyXANative).getWrappedConnection();
Assertions.assertTrue(connection == connectionFromProxy);
}
use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class ConnectionProxyXATest method testClose.
@Test
public void testClose() throws Throwable {
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(true);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
String xid = "xxx";
ConnectionProxyXA connectionProxyXA1 = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
connectionProxyXA1.init();
// Kept
connectionProxyXA1.setHeld(true);
// call close on proxy
connectionProxyXA1.close();
// Assert the original connection was NOT closed
Mockito.verify(connection, times(0)).close();
ConnectionProxyXA connectionProxyXA2 = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
connectionProxyXA2.init();
// Kept
connectionProxyXA2.setHeld(false);
// call close on proxy
connectionProxyXA2.close();
// Assert the original connection was ALSO closed
Mockito.verify(connection).close();
}
use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class ConnectionProxyXATest method testXABranchRollback.
@Test
public void testXABranchRollback() throws Throwable {
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(true);
XAResource xaResource = Mockito.mock(XAResource.class);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
Mockito.when(xaConnection.getXAResource()).thenReturn(xaResource);
BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
String xid = "xxx";
ResourceManager resourceManager = Mockito.mock(ResourceManager.class);
Mockito.doNothing().when(resourceManager).registerResource(any(Resource.class));
DefaultResourceManager.get();
DefaultResourceManager.mockResourceManager(BranchType.XA, resourceManager);
ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
connectionProxyXA.init();
connectionProxyXA.setAutoCommit(false);
// Assert setAutoCommit = false was NEVER invoked on the wrapped connection
Mockito.verify(connection, times(0)).setAutoCommit(false);
// Assert XA start was invoked
Mockito.verify(xaResource).start(any(Xid.class), any(Integer.class));
connectionProxyXA.rollback();
Mockito.verify(xaResource).end(any(Xid.class), any(Integer.class));
// Not prepared
Mockito.verify(xaResource, times(0)).prepare(any(Xid.class));
}
Aggregations