use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class ConnectionProxyXATest method testXACommit.
@Test
public void testXACommit() 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.xaCommit("xxx", 123L, null);
Mockito.verify(xaResource).commit(any(Xid.class), any(Boolean.class));
Mockito.verify(xaResource, times(0)).rollback(any(Xid.class));
}
use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class ConnectionProxyXATest method testXABranchCommit.
@Test
public void testXABranchCommit() 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.commit();
Mockito.verify(xaResource).end(any(Xid.class), any(Integer.class));
Mockito.verify(xaResource).prepare(any(Xid.class));
}
use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class ConnectionProxyXATest method testInit.
@Test
public void testInit() throws Throwable {
Connection connection = Mockito.mock(Connection.class);
Mockito.when(connection.getAutoCommit()).thenReturn(false);
XAConnection xaConnection = Mockito.mock(XAConnection.class);
BaseDataSourceResource<ConnectionProxyXA> baseDataSourceResource = Mockito.mock(BaseDataSourceResource.class);
String xid = "xxx";
ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, baseDataSourceResource, xid);
Assertions.assertThrows(IllegalStateException.class, connectionProxyXA::init, "Connection[autocommit=false] as default is NOT supported");
}
use of io.seata.rm.datasource.xa.ConnectionProxyXA in project seata by seata.
the class DataSourceProxyXATest method testGetConnection.
@Test
public void testGetConnection() throws SQLException {
// Mock
Driver driver = Mockito.mock(Driver.class);
JDBC4MySQLConnection connection = Mockito.mock(JDBC4MySQLConnection.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);
Mockito.when(driver.connect(any(), any())).thenReturn(connection);
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriver(driver);
DataSourceProxyXA dataSourceProxyXA = new DataSourceProxyXA(druidDataSource);
Connection connFromDataSourceProxyXA = dataSourceProxyXA.getConnection();
Assertions.assertFalse(connFromDataSourceProxyXA instanceof ConnectionProxyXA);
RootContext.bind("test");
connFromDataSourceProxyXA = dataSourceProxyXA.getConnection();
Assertions.assertTrue(connFromDataSourceProxyXA instanceof ConnectionProxyXA);
ConnectionProxyXA connectionProxyXA = (ConnectionProxyXA) dataSourceProxyXA.getConnection();
Connection wrappedConnection = connectionProxyXA.getWrappedConnection();
Assertions.assertTrue(wrappedConnection instanceof PooledConnection);
Connection wrappedPhysicalConn = ((PooledConnection) wrappedConnection).getConnection();
Assertions.assertTrue(wrappedPhysicalConn == connection);
XAConnection xaConnection = connectionProxyXA.getWrappedXAConnection();
Connection connectionInXA = xaConnection.getConnection();
Assertions.assertTrue(connectionInXA instanceof JDBC4ConnectionWrapper);
}
Aggregations