Search in sources :

Example 11 with MockDriver

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

the class OracleTableMetaCacheTest method getTableMetaTest.

@Test
public void getTableMetaTest() throws SQLException {
    MockDriver mockDriver = new MockDriver(columnMetas, indexMetas, pkMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    TableMetaCache tableMetaCache = TableMetaCacheFactory.getTableMetaCache(JdbcConstants.ORACLE);
    TableMeta tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.ot1", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
    tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.\"ot1\"", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMetaCache(io.seata.rm.datasource.sql.struct.TableMetaCache) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Test(org.junit.jupiter.api.Test)

Example 12 with MockDriver

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

the class PostgresqlTableMetaCacheTest method getTableMetaTest.

@Test
public void getTableMetaTest() throws SQLException {
    MockDriver mockDriver = new MockDriver(columnMetas, indexMetas, pkMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    TableMetaCache tableMetaCache = TableMetaCacheFactory.getTableMetaCache(JdbcConstants.POSTGRESQL);
    TableMeta tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "pt1", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
    tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.pt1", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
    tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.\"pt1\"", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMetaCache(io.seata.rm.datasource.sql.struct.TableMetaCache) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Test(org.junit.jupiter.api.Test)

Example 13 with MockDriver

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

the class MysqlTableMetaCacheTest method refreshTest_0.

@Test
public void refreshTest_0() throws SQLException {
    MockDriver mockDriver = new MockDriver(columnMetas, indexMetas);
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl("jdbc:mock:xxx");
    druidDataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(druidDataSource);
    TableMeta tableMeta = getTableMetaCache().getTableMeta(dataSourceProxy.getPlainConnection(), "t1", dataSourceProxy.getResourceId());
    // change the columns meta
    columnMetas = new Object[][] { new Object[] { "", "", "mt1", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "mt1", "name1", Types.VARCHAR, "VARCHAR", 65, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" }, new Object[] { "", "", "mt1", "name2", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 3, "YES", "NO" }, new Object[] { "", "", "mt1", "name3", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 4, "YES", "NO" } };
    mockDriver.setMockColumnsMetasReturnValue(columnMetas);
    getTableMetaCache().refresh(dataSourceProxy.getPlainConnection(), dataSourceProxy.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 14 with MockDriver

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

the class PreparedStatementProxyTest 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());
    String sql = "update prepared_statement_proxy set name = ?";
    PreparedStatement preparedStatement = mockDriver.createSeataMockPreparedStatement((MockConnection) connectionProxy.getTargetConnection(), sql);
    preparedStatementProxy = new PreparedStatementProxy(connectionProxy, preparedStatement, sql);
    unusedConstructorPreparedStatementProxy = new TestUnusedConstructorPreparedStatementProxy(connectionProxy, preparedStatement);
    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) SQLOperateRecognizerHolderFactory(io.seata.sqlparser.druid.SQLOperateRecognizerHolderFactory) DruidDelegatingSQLRecognizerFactory(io.seata.sqlparser.druid.DruidDelegatingSQLRecognizerFactory) SQLRecognizerFactory(io.seata.sqlparser.SQLRecognizerFactory) PreparedStatement(java.sql.PreparedStatement) DruidDelegatingSQLRecognizerFactory(io.seata.sqlparser.druid.DruidDelegatingSQLRecognizerFactory) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 15 with MockDriver

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

the class DataSourceProxyTest method getResourceIdTest.

@Test
public void getResourceIdTest() throws SQLException, NoSuchFieldException, IllegalAccessException {
    MockDriver mockDriver = new MockDriver();
    String username = "username";
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    dataSource.setUsername(username);
    dataSource.setPassword("password");
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    Field dbTypeField = proxy.getClass().getDeclaredField("dbType");
    dbTypeField.setAccessible(true);
    dbTypeField.set(proxy, io.seata.sqlparser.util.JdbcConstants.ORACLE);
    String userName = dataSource.getConnection().getMetaData().getUserName();
    Assertions.assertEquals(userName, username);
    Field userNameField = proxy.getClass().getDeclaredField("userName");
    userNameField.setAccessible(true);
    userNameField.set(proxy, username);
    Assertions.assertEquals(proxy.getResourceId(), "jdbc:mock:xxx/username");
    dbTypeField.set(proxy, io.seata.sqlparser.util.JdbcConstants.MYSQL);
    Assertions.assertEquals(proxy.getResourceId(), "jdbc:mock:xxx");
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) Field(java.lang.reflect.Field) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Test(org.junit.jupiter.api.Test)

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