Search in sources :

Example 1 with MockConnectionProxy

use of io.seata.rm.datasource.mock.MockConnectionProxy in project seata by seata.

the class SelectForUpdateExecutorTest method init.

@BeforeAll
public static void init() {
    RootContext.unbind();
    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_select_for_update_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_select_for_update_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 = new MockConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
        connectionProxy.bind("xid");
        MockStatement mockStatement = new MockStatement(dataSource.getConnection().getConnection());
        statementProxy = new StatementProxy(connectionProxy, mockStatement);
    } catch (Exception e) {
        throw new RuntimeException("init failed");
    }
    String sql = "select * from table_select_for_update_executor_test where id = 1";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLSelectForUpdateRecognizer recognizer = new MySQLSelectForUpdateRecognizer(sql, asts.get(0));
    selectForUpdateExecutor = new SelectForUpdateExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
Also used : MockConnectionProxy(io.seata.rm.datasource.mock.MockConnectionProxy) 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) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

MockStatement (com.alibaba.druid.mock.MockStatement)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 JdbcConstants (com.alibaba.druid.util.JdbcConstants)1 Lists (com.google.common.collect.Lists)1 RootContext (io.seata.core.context.RootContext)1 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)1 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)1 StatementProxy (io.seata.rm.datasource.StatementProxy)1 MockConnectionProxy (io.seata.rm.datasource.mock.MockConnectionProxy)1 MockDriver (io.seata.rm.datasource.mock.MockDriver)1 MockLockConflictConnectionProxy (io.seata.rm.datasource.mock.MockLockConflictConnectionProxy)1 MySQLSelectForUpdateRecognizer (io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer)1 Field (java.lang.reflect.Field)1 Types (java.sql.Types)1 List (java.util.List)1 Assertions (org.junit.jupiter.api.Assertions)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 Test (org.junit.jupiter.api.Test)1