This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 783494a3888 Add more test cases on
YamlDataSourceConfigurationSwapperTest and YamlModeConfigurationSwapperTest
(#37070)
783494a3888 is described below
commit 783494a38880b74121d395e5d01ce82002588af5
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Nov 11 20:47:36 2025 +0800
Add more test cases on YamlDataSourceConfigurationSwapperTest and
YamlModeConfigurationSwapperTest (#37070)
* Add more test cases on YamlDataSourceConfigurationSwapperTest and
YamlModeConfigurationSwapperTest
* Add more test cases on YamlDataSourceConfigurationSwapperTest and
YamlModeConfigurationSwapperTest
---
.../mode/YamlModeConfigurationSwapperTest.java | 47 ++++++++++++++++++++-
.../YamlDataSourceConfigurationSwapperTest.java | 49 ++++++++++++++++++++++
2 files changed, 94 insertions(+), 2 deletions(-)
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/mode/YamlModeConfigurationSwapperTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/mode/YamlModeConfigurationSwapperTest.java
index 91fdc4e72f9..2848d62cd08 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/mode/YamlModeConfigurationSwapperTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/mode/YamlModeConfigurationSwapperTest.java
@@ -18,11 +18,19 @@
package org.apache.shardingsphere.infra.yaml.config.swapper.mode;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
+import
org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration;
+import
org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlPersistRepositoryConfiguration;
import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
class YamlModeConfigurationSwapperTest {
@@ -31,16 +39,51 @@ class YamlModeConfigurationSwapperTest {
private final YamlModeConfigurationSwapper swapper = new
YamlModeConfigurationSwapper();
@Test
- void assertSwapToYamlConfiguration() {
+ void assertSwapToYamlConfigurationWithNullRepository() {
YamlModeConfiguration actual = swapper.swapToYamlConfiguration(new
ModeConfiguration("TEST_TYPE", null));
assertThat(actual.getType(), is(TEST_TYPE));
}
@Test
- void assertSwapToObject() {
+ void assertSwapToYamlConfigurationWithNotNullRepository() {
+ YamlPersistRepositoryConfiguration yamlRepoConfig = new
YamlPersistRepositoryConfiguration();
+ yamlRepoConfig.setType(TEST_TYPE);
+ try (MockedStatic<TypedSPILoader> mockedLoader =
mockStatic(TypedSPILoader.class)) {
+ YamlPersistRepositoryConfigurationSwapper mockSwapper =
mock(YamlPersistRepositoryConfigurationSwapper.class);
+
when(mockSwapper.swapToYamlConfiguration(any())).thenReturn(yamlRepoConfig);
+ when(mockSwapper.getType()).thenReturn(TEST_TYPE);
+ mockedLoader.when(() ->
TypedSPILoader.getService(YamlPersistRepositoryConfigurationSwapper.class,
TEST_TYPE)).thenReturn(mockSwapper);
+ ModeConfiguration modeConfig = new ModeConfiguration(TEST_TYPE,
mock(PersistRepositoryConfiguration.class));
+ YamlModeConfiguration actual =
swapper.swapToYamlConfiguration(modeConfig);
+ assertThat(actual.getType(), is(TEST_TYPE));
+ assertThat(actual.getRepository(), is(yamlRepoConfig));
+ }
+ }
+
+ @Test
+ void assertSwapToObjectWithNullRepository() {
YamlModeConfiguration yamlConfig = new YamlModeConfiguration();
yamlConfig.setType(TEST_TYPE);
ModeConfiguration actual = swapper.swapToObject(yamlConfig);
assertThat(actual.getType(), is(TEST_TYPE));
}
+
+ @Test
+ void assertSwapToObjectWithNotNullRepository() {
+ PersistRepositoryConfiguration mockRepoConfig =
mock(PersistRepositoryConfiguration.class);
+ try (MockedStatic<TypedSPILoader> mockedLoader =
mockStatic(TypedSPILoader.class)) {
+ YamlPersistRepositoryConfigurationSwapper mockSwapper =
mock(YamlPersistRepositoryConfigurationSwapper.class);
+ when(mockSwapper.swapToObject(any())).thenReturn(mockRepoConfig);
+ when(mockSwapper.getType()).thenReturn(TEST_TYPE);
+ mockedLoader.when(() ->
TypedSPILoader.getService(YamlPersistRepositoryConfigurationSwapper.class,
TEST_TYPE)).thenReturn(mockSwapper);
+ YamlPersistRepositoryConfiguration yamlRepoConfig = new
YamlPersistRepositoryConfiguration();
+ yamlRepoConfig.setType(TEST_TYPE);
+ YamlModeConfiguration yamlConfig = new YamlModeConfiguration();
+ yamlConfig.setType(TEST_TYPE);
+ yamlConfig.setRepository(yamlRepoConfig);
+ ModeConfiguration actual = swapper.swapToObject(yamlConfig);
+ assertThat(actual.getType(), is(TEST_TYPE));
+ assertThat(actual.getRepository(), is(mockRepoConfig));
+ }
+ }
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
index ec023f497d6..614af8e18ee 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.yaml.config.swapper.resource;
import
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
+import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.test.infra.fixture.jdbc.MockedDataSource;
import org.junit.jupiter.api.Test;
@@ -28,6 +29,7 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
class YamlDataSourceConfigurationSwapperTest {
@@ -90,4 +92,51 @@ class YamlDataSourceConfigurationSwapperTest {
result.put("password", "root");
return result;
}
+
+ @Test
+ void assertGetDataSourcePoolPropertiesMap() {
+ Map<String, Map<String, Object>> dataSources = new LinkedHashMap<>(2,
1F);
+ dataSources.put("ds_1", createPropertyMap("ds_1"));
+ dataSources.put("ds_2", createPropertyMap("ds_2"));
+ YamlRootConfiguration yamlRootConfig = new YamlRootConfiguration();
+ yamlRootConfig.setDataSources(dataSources);
+ Map<String, DataSourcePoolProperties> actual =
swapper.getDataSourcePoolPropertiesMap(yamlRootConfig);
+ assertThat(actual.size(), is(2));
+ assertThat(actual.get("ds_1").getPoolClassName(),
is(MockedDataSource.class.getName()));
+ assertThat(actual.get("ds_2").getPoolClassName(),
is(MockedDataSource.class.getName()));
+ }
+
+ @Test
+ void assertSwapToDataSourcePoolPropertiesWithHikariDataSource() {
+ Map<String, Object> yamlConfig = new HashMap<>(4, 1F);
+ yamlConfig.put("dataSourceClassName",
"com.zaxxer.hikari.HikariDataSource");
+ yamlConfig.put("url", "jdbc:h2:mem:test");
+ yamlConfig.put("username", "sa");
+ yamlConfig.put("password", "");
+ DataSourcePoolProperties actual =
swapper.swapToDataSourcePoolProperties(yamlConfig);
+ assertThat(actual.getPoolClassName(),
is("com.zaxxer.hikari.HikariDataSource"));
+
assertThat(actual.getAllLocalProperties().containsKey("dataSourceClassName"),
is(false));
+ assertThat(actual.getAllLocalProperties().get("url").toString(),
is("jdbc:h2:mem:test"));
+ assertThat(actual.getAllLocalProperties().get("username").toString(),
is("sa"));
+ }
+
+ @Test
+ void assertSwapToDataSourcePoolPropertiesWithCustomPoolProps() {
+ Map<String, Object> yamlConfig = new HashMap<>(5, 1F);
+ yamlConfig.put("dataSourceClassName",
MockedDataSource.class.getName());
+ yamlConfig.put("url", "jdbc:test:memory:");
+ yamlConfig.put("username", "test");
+ yamlConfig.put("password", "test");
+ Map<String, Object> customProps = new HashMap<>(2, 1F);
+ customProps.put("customKey1", "customValue1");
+ customProps.put("customKey2", "customValue2");
+ yamlConfig.put("customPoolProps", customProps);
+ DataSourcePoolProperties actual =
swapper.swapToDataSourcePoolProperties(yamlConfig);
+ assertThat(actual.getPoolClassName(),
is(MockedDataSource.class.getName()));
+ assertThat(actual.getAllLocalProperties().get("url").toString(),
is("jdbc:test:memory:"));
+ assertThat(actual.getAllLocalProperties().get("username").toString(),
is("test"));
+
assertThat(actual.getAllLocalProperties().get("customKey1").toString(),
is("customValue1"));
+
assertThat(actual.getAllLocalProperties().get("customKey2").toString(),
is("customValue2"));
+
assertFalse(actual.getAllLocalProperties().containsKey("customPoolProps"));
+ }
}