Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx committed Aug 3, 2024
1 parent f54d4ad commit 11213ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@
import org.apache.seata.rm.datasource.util.JdbcUtils;
import org.apache.seata.sqlparser.util.JdbcConstants;

/**
* datasource proxy metadata factory
*/
public class SeataDataSourceProxyMetadataFactory {

public static SeataDataSourceProxyMetadata create(DataSource dataSource) throws SQLException {
SeataDataSourceProxyMetadata dataSourceProxyMetadata = null;
try (Connection connection = dataSource.getConnection()) {
String jdbcUrl = connection.getMetaData().getURL();
String dbType = JdbcUtils.getDbType(jdbcUrl);
if (JdbcConstants.MYSQL.equals(dbType)) {
if (JdbcConstants.MYSQL.equals(dbType)
|| JdbcConstants.MARIADB.equals(dbType)
|| JdbcConstants.POLARDBX.equals(dbType)) {
dataSourceProxyMetadata = new MySQLDataSourceProxyMetadata();
} else {
dataSourceProxyMetadata = new DefaultDataSourceProxyMetadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

Expand All @@ -32,19 +31,16 @@
import org.apache.seata.rm.datasource.DataSourceProxyTest;
import org.apache.seata.rm.datasource.PreparedStatementProxy;
import org.apache.seata.rm.datasource.StatementProxy;
import org.apache.seata.rm.datasource.exec.StatementCallback;
import org.apache.seata.rm.datasource.exec.mysql.MySQLInsertExecutor;
import org.apache.seata.rm.datasource.mock.MockDriver;
import org.apache.seata.rm.datasource.mock.MockMariadbDataSource;
import org.apache.seata.rm.datasource.mock.MockResultSet;
import org.apache.seata.sqlparser.struct.TableMeta;
import org.apache.seata.sqlparser.SQLInsertRecognizer;
import org.apache.seata.sqlparser.struct.TableMeta;
import org.apache.seata.sqlparser.util.JdbcConstants;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;


import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -63,10 +59,6 @@ public void init() throws SQLException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
Expand All @@ -33,10 +32,9 @@
import org.apache.seata.rm.datasource.DataSourceProxyTest;
import org.apache.seata.rm.datasource.PreparedStatementProxy;
import org.apache.seata.rm.datasource.StatementProxy;
import org.apache.seata.rm.datasource.exec.StatementCallback;
import org.apache.seata.rm.datasource.exec.polardbx.PolarDBXInsertExecutor;
import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata;
import org.apache.seata.rm.datasource.mock.MockDriver;
import org.apache.seata.rm.datasource.mock.MockResultSet;
import org.apache.seata.sqlparser.SQLInsertRecognizer;
import org.apache.seata.sqlparser.struct.TableMeta;
import org.apache.seata.sqlparser.util.JdbcConstants;
Expand All @@ -48,7 +46,6 @@

/**
* Insert executor test for PolarDB-X
*
*/
public class PolarDBXInsertExecutorTest extends MySQLInsertExecutorTest {
@BeforeEach
Expand All @@ -60,17 +57,16 @@ public void init() throws SQLException {
DataSourceProxy dataSourceProxy = mock(DataSourceProxy.class);
when(dataSourceProxy.getResourceId()).thenReturn("jdbc:mysql://127.0.0.1:3306/seata");
when(dataSourceProxy.getDbType()).thenReturn(JdbcConstants.POLARDBX);
MySQLDataSourceProxyMetadata dataSourceProxyMetadata = Mockito.mock(MySQLDataSourceProxyMetadata.class);
when(dataSourceProxyMetadata.getVariableValue("auto_increment_increment")).thenReturn("1");
when(dataSourceProxy.getDataSourceProxyMetadata()).thenReturn(dataSourceProxyMetadata);

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);
Expand All @@ -84,21 +80,21 @@ public void init() throws SQLException {

// new test init property
List<String> returnValueColumnLabels = Lists.newArrayList("id", "user_id", "name", "sex", "update_time");
Object[][] returnValue = new Object[][] {
new Object[] {1, 1, "will", 1, 0},
Object[][] returnValue = new Object[][]{
new Object[]{1, 1, "will", 1, 0},
};
Object[][] columnMetas = new Object[][] {
new Object[] {"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[] {"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[] {"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[] {"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[] {"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"},
Object[][] columnMetas = new Object[][]{
new Object[]{"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[]{"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[]{"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[]{"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"},
new Object[]{"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"},
};
Object[][] indexMetas = new Object[][] {
new Object[] {"PRIMARY", "id", false, "", 3, 1, "A", 34},
new Object[] {"PRIMARY", "user_id", false, "", 3, 1, "A", 34},
Object[][] indexMetas = new Object[][]{
new Object[]{"PRIMARY", "id", false, "", 3, 1, "A", 34},
new Object[]{"PRIMARY", "user_id", false, "", 3, 1, "A", 34},
};
Object[][] onUpdateColumnsReturnValue = new Object[][] {
Object[][] onUpdateColumnsReturnValue = new Object[][]{
new Object[]{0, "update_time", Types.INTEGER, "INTEGER", 64, 10, 0, 0}
};

Expand Down

0 comments on commit 11213ab

Please sign in to comment.