Search in sources :

Example 6 with MockDriver

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

the class TableRecordsTest method testPkRow.

@Test
public void testPkRow() throws SQLException {
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    TableMeta tableMeta = TableMetaCacheFactory.getTableMetaCache(JdbcConstants.MYSQL).getTableMeta(proxy.getPlainConnection(), "table_records_test", proxy.getResourceId());
    ResultSet resultSet = mockDriver.executeQuery(mockStatement, "select * from table_records_test");
    TableRecords tableRecords = TableRecords.buildRecords(tableMeta, resultSet);
    Assertions.assertEquals(returnValue.length, tableRecords.pkRows().size());
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) ResultSet(java.sql.ResultSet) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) Test(org.junit.jupiter.api.Test)

Example 7 with MockDriver

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

the class UpdateExecutorTest 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_update_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_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 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 = "update table_update_executor_test set name = 'WILL'";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLUpdateRecognizer recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
    updateExecutor = new UpdateExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) StatementProxy(io.seata.rm.datasource.StatementProxy) 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) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with MockDriver

use of io.seata.rm.datasource.mock.MockDriver 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 9 with MockDriver

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

the class StatementProxyTest method init.

@BeforeAll
public static void init() throws SQLException {
    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());
    Statement statement = mockDriver.createMockStatement((MockConnection) connectionProxy.getTargetConnection());
    MockResultSet mockResultSet = new MockResultSet(statement);
    ((ResultSetMetaDataBase) mockResultSet.getMetaData()).getColumns().add(new ResultSetMetaDataBase.ColumnMetaData());
    ((MockStatement) statement).setGeneratedKeys(mockResultSet);
    statementProxy = new StatementProxy(connectionProxy, statement);
    EnhancedServiceLoader.load(SQLOperateRecognizerHolder.class, JdbcConstants.MYSQL, SQLOperateRecognizerHolderFactory.class.getClassLoader());
    DruidDelegatingSQLRecognizerFactory recognizerFactory = (DruidDelegatingSQLRecognizerFactory) EnhancedServiceLoader.load(SQLRecognizerFactory.class, SqlParserType.SQL_PARSER_TYPE_DRUID);
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) ResultSetMetaDataBase(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase) SQLOperateRecognizerHolderFactory(io.seata.sqlparser.druid.SQLOperateRecognizerHolderFactory) MockStatement(com.alibaba.druid.mock.MockStatement) Statement(java.sql.Statement) MockResultSet(com.alibaba.druid.mock.MockResultSet) DruidDelegatingSQLRecognizerFactory(io.seata.sqlparser.druid.DruidDelegatingSQLRecognizerFactory) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DruidDelegatingSQLRecognizerFactory(io.seata.sqlparser.druid.DruidDelegatingSQLRecognizerFactory) SQLRecognizerFactory(io.seata.sqlparser.SQLRecognizerFactory) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 10 with MockDriver

use of io.seata.rm.datasource.mock.MockDriver 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)

Aggregations

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