use of io.seata.core.model.ResourceManager in project seata by seata.
the class ConnectionProxyTest method initBeforeEach.
@BeforeEach
public void initBeforeEach() throws Exception {
branchRollbackFlagField = ConnectionProxy.LockRetryPolicy.class.getDeclaredField("LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(branchRollbackFlagField, branchRollbackFlagField.getModifiers() & ~Modifier.FINAL);
branchRollbackFlagField.setAccessible(true);
boolean branchRollbackFlag = (boolean) branchRollbackFlagField.get(null);
Assertions.assertTrue(branchRollbackFlag);
dataSourceProxy = Mockito.mock(DataSourceProxy.class);
Mockito.when(dataSourceProxy.getResourceId()).thenReturn(TEST_RESOURCE_ID);
ResourceManager rm = Mockito.mock(ResourceManager.class);
Mockito.when(rm.branchRegister(BranchType.AT, dataSourceProxy.getResourceId(), null, TEST_XID, null, lockKey)).thenThrow(new TransactionException(TransactionExceptionCode.LockKeyConflict));
DefaultResourceManager defaultResourceManager = DefaultResourceManager.get();
Assertions.assertNotNull(defaultResourceManager);
DefaultResourceManager.mockResourceManager(BranchType.AT, rm);
}
use of io.seata.core.model.ResourceManager 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));
}
use of io.seata.core.model.ResourceManager 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));
}
Aggregations