Search in sources :

Example 1 with DataSourceProxy

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

the class MultiExecutorTest method init.

@BeforeAll
public static void init() throws Throwable {
    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 = 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 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");
    }
}
Also used : 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) SQLException(java.sql.SQLException) NotSupportYetException(io.seata.common.exception.NotSupportYetException) 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 2 with DataSourceProxy

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

Example 3 with DataSourceProxy

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

the class MySQLUndoLogManagerTest method init.

@BeforeEach
public void init() throws SQLException {
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    dataSourceProxy = new DataSourceProxy(dataSource);
    connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
    undoLogManager = new MySQLUndoLogManager();
    tableMeta = new TableMeta();
    tableMeta.setTableName("table_plain_executor_test");
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with DataSourceProxy

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

the class MysqlTableMetaCacheTest method getTableMetaTest_0.

/**
 * The table meta fetch test.
 */
@Test
public void getTableMetaTest_0() throws SQLException {
    MockDriver mockDriver = new MockDriver(columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    TableMeta tableMeta = getTableMetaCache().getTableMeta(proxy.getPlainConnection(), "mt1", proxy.getResourceId());
    Assertions.assertEquals("mt1", tableMeta.getTableName());
    Assertions.assertEquals("id", tableMeta.getPrimaryKeyOnlyName().get(0));
    Assertions.assertEquals("id", tableMeta.getColumnMeta("id").getColumnName());
    Assertions.assertEquals("id", tableMeta.getAutoIncreaseColumn().getColumnName());
    Assertions.assertEquals(1, tableMeta.getPrimaryKeyMap().size());
    Assertions.assertEquals(Collections.singletonList("id"), tableMeta.getPrimaryKeyOnlyName());
    Assertions.assertEquals(columnMetas.length, tableMeta.getAllColumns().size());
    assertColumnMetaEquals(columnMetas[0], tableMeta.getAllColumns().get("id"));
    assertColumnMetaEquals(columnMetas[1], tableMeta.getAllColumns().get("name1"));
    assertColumnMetaEquals(columnMetas[2], tableMeta.getAllColumns().get("name2"));
    assertColumnMetaEquals(columnMetas[3], tableMeta.getAllColumns().get("name3"));
    Assertions.assertEquals(indexMetas.length, tableMeta.getAllIndexes().size());
    assertIndexMetaEquals(indexMetas[0], tableMeta.getAllIndexes().get("PRIMARY"));
    Assertions.assertEquals(IndexType.PRIMARY, tableMeta.getAllIndexes().get("PRIMARY").getIndextype());
    assertIndexMetaEquals(indexMetas[1], tableMeta.getAllIndexes().get("name1"));
    Assertions.assertEquals(IndexType.UNIQUE, tableMeta.getAllIndexes().get("name1").getIndextype());
    indexMetas = new Object[][] {};
    mockDriver.setMockIndexMetasReturnValue(indexMetas);
    Assertions.assertThrows(ShouldNeverHappenException.class, () -> {
        getTableMetaCache().getTableMeta(proxy.getPlainConnection(), "mt2", proxy.getResourceId());
    });
    mockDriver.setMockColumnsMetasReturnValue(null);
    Assertions.assertThrows(ShouldNeverHappenException.class, () -> {
        getTableMetaCache().getTableMeta(proxy.getPlainConnection(), "mt2", proxy.getResourceId());
    });
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Test(org.junit.jupiter.api.Test)

Example 5 with DataSourceProxy

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

the class TableRecordsTest method testBuildRecords.

@Test
public void testBuildRecords() 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.assertNotNull(tableRecords);
}
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)

Aggregations

DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)16 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)12 MockDriver (io.seata.rm.datasource.mock.MockDriver)12 Test (org.junit.jupiter.api.Test)10 MockStatement (com.alibaba.druid.mock.MockStatement)7 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)7 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)6 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)6 StatementProxy (io.seata.rm.datasource.StatementProxy)5 Field (java.lang.reflect.Field)5 SQLException (java.sql.SQLException)5 Lists (com.google.common.collect.Lists)4 Types (java.sql.Types)4 List (java.util.List)4 BeforeAll (org.junit.jupiter.api.BeforeAll)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 BeforeEach (org.junit.jupiter.api.BeforeEach)3