Skip to content

Commit

Permalink
test: fix DataSourceProxyTest UT failed (#4794)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jul 20, 2022
1 parent 63ec93a commit 06dae93
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 35 deletions.
14 changes: 8 additions & 6 deletions changes/en-us/develop.md
@@ -1,24 +1,26 @@
Add changes here for all PR submitted to the develop branch.


<!-- Please add the `changes` to the following location(feature/bugfix/optimize/test) based on the type of PR -->

### feature
### feature:


### bugfix:

### bugfix:

### optimize:

### optimize:
- [[#4774](https://github.com/seata/seata/pull/4774)] optimize mysql8 dependencies for seataio/seata-server image


### test:
- [[#4794](https://github.com/seata/seata/pull/4794)] try to fix the test `DataSourceProxyTest.getResourceIdTest()`


Thanks to these contributors for their code commits. Please report an unintended omission.

<!-- Please make sure your Github ID is in the list below -->
- [slievrly](https://github.com/slievrly)
- [wangliang181230](https://github.com/wangliang181230)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
3 changes: 3 additions & 0 deletions changes/zh-cn/develop.md
Expand Up @@ -13,11 +13,14 @@


### test:
- [[#4794](https://github.com/seata/seata/pull/4794)] 重构代码,尝试修复单元测试 `DataSourceProxyTest.getResourceIdTest()`


非常感谢以下 contributors 的代码贡献。若有无意遗漏,请报告。

<!-- 请确保您的 GitHub ID 在以下列表中 -->
- [slievrly](https://github.com/slievrly)
- [wangliang181230](https://github.com/wangliang181230)


同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Expand Up @@ -176,8 +176,7 @@ private void initResourceId() {
if (JdbcConstants.POSTGRESQL.equals(dbType)) {
initPGResourceId();
} else if (JdbcConstants.ORACLE.equals(dbType) && userName != null) {
initDefaultResourceId();
resourceId = resourceId + "/" + userName;
initOracleResourceId();
} else if (JdbcConstants.MYSQL.equals(dbType)) {
initMysqlResourceId();
} else {
Expand All @@ -196,6 +195,17 @@ private void initDefaultResourceId() {
}
}

/**
* init the oracle resource id
*/
private void initOracleResourceId() {
if (jdbcUrl.contains("?")) {
resourceId = jdbcUrl.substring(0, jdbcUrl.indexOf('?')) + "/" + userName;
} else {
resourceId = jdbcUrl + "/" + userName;
}
}

/**
* prevent mysql url like
* jdbc:mysql:loadbalance://192.168.100.2:3306,192.168.100.1:3306/seata
Expand All @@ -216,7 +226,7 @@ private void initMysqlResourceId() {
initDefaultResourceId();
}
}

/**
* prevent pg sql url like
* jdbc:postgresql://127.0.0.1:5432/seata?currentSchema=public
Expand All @@ -230,6 +240,7 @@ private void initPGResourceId() {
if (jdbcUrl.contains("?")) {
StringBuilder jdbcUrlBuilder = new StringBuilder();
jdbcUrlBuilder.append(jdbcUrl, 0, jdbcUrl.indexOf('?'));

StringBuilder paramsBuilder = new StringBuilder();
String paramUrl = jdbcUrl.substring(jdbcUrl.indexOf('?') + 1);
String[] urlParams = paramUrl.split("&");
Expand Down
Expand Up @@ -18,6 +18,7 @@
import java.lang.reflect.Field;
import java.sql.SQLException;
import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.mock.MockDataSource;
import io.seata.rm.datasource.mock.MockDriver;
Expand All @@ -42,43 +43,66 @@ public void test_constructor() {

@Test
public void getResourceIdTest() throws SQLException, NoSuchFieldException, IllegalAccessException {
MockDriver mockDriver = new MockDriver();
String username = "username";
final MockDriver mockDriver = new MockDriver();
final String username = "username";
final String jdbcUrl = "jdbc:mock:xxx";

DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
// create data source
final DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(jdbcUrl);
dataSource.setDriver(mockDriver);
dataSource.setUsername(username);
dataSource.setPassword("password");

DataSourceProxy proxy = new DataSourceProxy(dataSource);
Field resourceId = proxy.getClass().getDeclaredField("resourceId");
resourceId.setAccessible(true);
resourceId.set(proxy, null);
// create data source proxy
final DataSourceProxy proxy = new DataSourceProxy(dataSource);

// get fields
Field resourceIdField = proxy.getClass().getDeclaredField("resourceId");
resourceIdField.setAccessible(true);
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);
Field jdbcUrlField = proxy.getClass().getDeclaredField("jdbcUrl");
jdbcUrlField.setAccessible(true);


// set userName
String userNameFromMetaData = dataSource.getConnection().getMetaData().getUserName();
Assertions.assertEquals(userNameFromMetaData, username);
userNameField.set(proxy, username);

Assertions.assertEquals(proxy.getResourceId(), "jdbc:mock:xxx/username");

dbTypeField.set(proxy, io.seata.sqlparser.util.JdbcConstants.MYSQL);
resourceId.setAccessible(true);
resourceId.set(proxy, null);
Assertions.assertEquals(proxy.getResourceId(), "jdbc:mock:xxx");
resourceId.setAccessible(true);
resourceId.set(proxy, null);
Field jdbUrl = proxy.getClass().getDeclaredField("jdbcUrl");
jdbUrl.setAccessible(true);
jdbUrl.set(proxy, "jdbc:mysql:loadbalance://192.168.100.2:3306,192.168.100.3:3306,192.168.100.1:3306/seata");
resourceId.setAccessible(true);
resourceId.set(proxy, null);
Assertions.assertEquals(proxy.getResourceId(), "jdbc:mysql:loadbalance://192.168.100.2:3306|192.168.100.3:3306|192.168.100.1:3306/seata");

// case: dbType = oracle
{
resourceIdField.set(proxy, null);
dbTypeField.set(proxy, io.seata.sqlparser.util.JdbcConstants.ORACLE);
Assertions.assertEquals("jdbc:mock:xxx/username", proxy.getResourceId(), "dbType=" + dbTypeField.get(proxy));
}

// case: dbType = postgresql
{
resourceIdField.set(proxy, null);
dbTypeField.set(proxy, io.seata.sqlparser.util.JdbcConstants.POSTGRESQL);
Assertions.assertEquals(jdbcUrl, proxy.getResourceId(), "dbType=" + dbTypeField.get(proxy));

resourceIdField.set(proxy, null);
jdbcUrlField.set(proxy, "jdbc:postgresql://mock/postgresql?xxx=1111&currentSchema=schema1,schema2&yyy=1");
Assertions.assertEquals("jdbc:postgresql://mock/postgresql?currentSchema=schema1!schema2", proxy.getResourceId(), "dbType=" + dbTypeField.get(proxy));
jdbcUrlField.set(proxy, jdbcUrl);
}

// case: dbType = mysql
{
resourceIdField.set(proxy, null);
dbTypeField.set(proxy, io.seata.sqlparser.util.JdbcConstants.MYSQL);
Assertions.assertEquals(jdbcUrl, proxy.getResourceId(), "dbType=" + dbTypeField.get(proxy));

resourceIdField.set(proxy, null);
jdbcUrlField.set(proxy, "jdbc:mysql:loadbalance://192.168.100.2:3306,192.168.100.3:3306,192.168.100.1:3306/seata");
Assertions.assertEquals("jdbc:mysql:loadbalance://192.168.100.2:3306|192.168.100.3:3306|192.168.100.1:3306/seata", proxy.getResourceId(), "dbType=" + dbTypeField.get(proxy));
jdbcUrlField.set(proxy, jdbcUrl);
}
}
}

0 comments on commit 06dae93

Please sign in to comment.