use of com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper 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