Search in sources :

Example 11 with ConnectionProxy

use of io.seata.rm.datasource.ConnectionProxy in project seata by seata.

the class DeleteExecutorTest method init.

@BeforeAll
public static void init() {
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_delete_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_delete_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    try {
        Field field = dataSourceProxy.getClass().getDeclaredField("dbType");
        field.setAccessible(true);
        field.set(dataSourceProxy, "mysql");
        ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
        MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
        statementProxy = new StatementProxy(connectionProxy, mockStatement);
    } catch (Exception e) {
        throw new RuntimeException("init failed");
    }
    String sql = "delete from t where id = 1";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
    deleteExecutor = new DeleteExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) Field(java.lang.reflect.Field) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLException(java.sql.SQLException) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 12 with ConnectionProxy

use of io.seata.rm.datasource.ConnectionProxy in project seata by seata.

the class PostgresqlInsertExecutorTest method init.

@BeforeEach
public void init() {
    ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.POSTGRESQL);
    statementProxy = mock(PreparedStatementProxy.class);
    when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    StatementCallback statementCallback = mock(StatementCallback.class);
    sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
    tableMeta = mock(TableMeta.class);
    insertExecutor = Mockito.spy(new PostgresqlInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    pkIndexMap = new HashMap<String, Integer>() {

        {
            put(ID_COLUMN, pkIndex);
        }
    };
}
Also used : PostgresqlInsertExecutor(io.seata.rm.datasource.exec.postgresql.PostgresqlInsertExecutor) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with ConnectionProxy

use of io.seata.rm.datasource.ConnectionProxy in project seata by seata.

the class MySQLInsertExecutorTest method init.

@BeforeEach
public void init() throws SQLException {
    ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.MYSQL);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(new MockDataSource());
    when(connectionProxy.getDataSourceProxy()).thenReturn(dataSourceProxy);
    statementProxy = mock(PreparedStatementProxy.class);
    when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    when(statementProxy.getTargetStatement()).thenReturn(statementProxy);
    MockResultSet resultSet = new MockResultSet(statementProxy);
    resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][] { { "auto_increment_increment", "1" } });
    when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet);
    StatementCallback statementCallback = mock(StatementCallback.class);
    sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
    tableMeta = mock(TableMeta.class);
    insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    pkIndexMap = new HashMap<String, Integer>() {

        {
            put(ID_COLUMN, pkIndex);
        }
    };
}
Also used : DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MySQLInsertExecutor(io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor) MockDataSource(io.seata.rm.datasource.mock.MockDataSource) MockResultSet(io.seata.rm.datasource.mock.MockResultSet) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Mockito.anyString(org.mockito.Mockito.anyString) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with ConnectionProxy

use of io.seata.rm.datasource.ConnectionProxy in project seata by seata.

the class PlainExecutorTest method init.

@BeforeEach
public void init() throws SQLException {
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_plain_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_plain_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
    MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
    StatementProxy statementProxy = new StatementProxy(connectionProxy, mockStatement);
    plainExecutor = new PlainExecutor(statementProxy, (statement, args) -> null);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MockDriver(io.seata.rm.datasource.mock.MockDriver) StatementProxy(io.seata.rm.datasource.StatementProxy) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with ConnectionProxy

use of io.seata.rm.datasource.ConnectionProxy in project seata by seata.

the class SelectForUpdateExecutorTest method testDoExecute.

@Test
public void testDoExecute() throws Throwable {
    Assertions.assertThrows(RuntimeException.class, () -> selectForUpdateExecutor.doExecute((Object) null));
    RootContext.bind("xid");
    Assertions.assertDoesNotThrow(() -> {
        selectForUpdateExecutor.doExecute((Object) null);
    });
    RootContext.unbind();
    RootContext.bindGlobalLockFlag();
    Assertions.assertDoesNotThrow(() -> {
        selectForUpdateExecutor.doExecute((Object) null);
    });
    RootContext.unbindGlobalLockFlag();
    connectionProxy = new MockLockConflictConnectionProxy(connectionProxy.getDataSourceProxy(), connectionProxy.getTargetConnection());
    statementProxy = new StatementProxy(connectionProxy, statementProxy.getTargetStatement());
    statementProxy.getTargetStatement().getConnection().setAutoCommit(false);
    String sql = "select * from dual";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLSelectForUpdateRecognizer recognizer = new MySQLSelectForUpdateRecognizer(sql, asts.get(0));
    selectForUpdateExecutor = new SelectForUpdateExecutor(statementProxy, (statement, args) -> null, recognizer);
    RootContext.bind("xid");
    Assertions.assertThrows(LockWaitTimeoutException.class, () -> selectForUpdateExecutor.doExecute((Object) null));
    RootContext.unbind();
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MockConnectionProxy(io.seata.rm.datasource.mock.MockConnectionProxy) Field(java.lang.reflect.Field) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) MockLockConflictConnectionProxy(io.seata.rm.datasource.mock.MockLockConflictConnectionProxy) Test(org.junit.jupiter.api.Test) RootContext(io.seata.core.context.RootContext) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) StatementProxy(io.seata.rm.datasource.StatementProxy) MockLockConflictConnectionProxy(io.seata.rm.datasource.mock.MockLockConflictConnectionProxy) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)15 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)8 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)7 StatementProxy (io.seata.rm.datasource.StatementProxy)7 MockDriver (io.seata.rm.datasource.mock.MockDriver)7 MockStatement (com.alibaba.druid.mock.MockStatement)6 Field (java.lang.reflect.Field)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Test (org.junit.jupiter.api.Test)6 Lists (com.google.common.collect.Lists)5 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)5 SQLException (java.sql.SQLException)5 Types (java.sql.Types)5 List (java.util.List)5 BeforeAll (org.junit.jupiter.api.BeforeAll)5 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 JdbcConstants (com.alibaba.druid.util.JdbcConstants)4 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)4