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 e09d67b8d44 Refactor ShardingSphereDatabasesFactoryTest (#36985)
e09d67b8d44 is described below
commit e09d67b8d44f1d514833601ff54f0ceffe50e9aa
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Nov 1 20:50:05 2025 +0800
Refactor ShardingSphereDatabasesFactoryTest (#36985)
* Refactor ShardingSphereVersion
* Refactor ShardingSphereDatabasesFactoryTest
* Add test coverage for first create method in
ShardingSphereDatabasesFactory
- Add assertCreateDatabasesWithSchemas test method with mixed storage units
- Cover empty storage units branch (line 64) and non-empty storage units
branch
- Test schemas parameter handling and boundary conditions
- Ensure full coverage of create method with schemas parameter (line 53)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
---------
Co-authored-by: Claude <[email protected]>
---
.../ShardingSphereDatabasesFactoryTest.java | 51 ++++++++++++++++++----
1 file changed, 42 insertions(+), 9 deletions(-)
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactoryTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactoryTest.java
index 02e0859992d..1b5a9cff2c4 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactoryTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactoryTest.java
@@ -20,28 +20,61 @@ package org.apache.shardingsphere.infra.metadata.database;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import
org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.test.infra.fixture.jdbc.MockedDataSource;
import org.junit.jupiter.api.Test;
+import java.sql.Connection;
import java.sql.SQLException;
-import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
-import java.util.List;
+import java.util.LinkedHashMap;
+import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
class ShardingSphereDatabasesFactoryTest {
@Test
- void assertCreateDatabases() throws SQLException {
- DatabaseConfiguration databaseConfig = new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.emptyList());
- List<ShardingSphereDatabase> actual = new
ArrayList<>(ShardingSphereDatabasesFactory.create(
- Collections.singletonMap("foo_db", databaseConfig), new
ConfigurationProperties(new Properties()),
mock(ComputeNodeInstanceContext.class)));
- assertThat(actual.get(0).getName(), is("foo_db"));
-
assertTrue(actual.get(0).getResourceMetaData().getStorageUnits().isEmpty());
+ void assertCreateDatabasesWithSchemas() throws SQLException {
+ Map<String, DatabaseConfiguration> databaseConfigs = new
LinkedHashMap<>(2, 1F);
+ databaseConfigs.put("empty_db", new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.emptyList()));
+ Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+
when(connection.getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1/foo_ds");
+ databaseConfigs.put("foo_db", new
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds", new
MockedDataSource(connection)), Collections.emptyList()));
+ Map<String, Collection<ShardingSphereSchema>> schemas = new
LinkedHashMap<>(2, 1F);
+ schemas.put("empty_db", Collections.singleton(new
ShardingSphereSchema("empty_schema")));
+ schemas.put("foo_db", Collections.singleton(new
ShardingSphereSchema("foo_schema")));
+ Collection<ShardingSphereDatabase> actual =
ShardingSphereDatabasesFactory.create(databaseConfigs, schemas, new
ConfigurationProperties(new Properties()), mock());
+ assertThat(actual.size(), is(2));
+ assertTrue(actual.stream().anyMatch(each ->
"empty_db".equals(each.getName())));
+ assertTrue(actual.stream().anyMatch(each ->
"foo_db".equals(each.getName())));
+ }
+
+ @Test
+ void assertCreateDatabasesWithoutSchemas() throws SQLException {
+ Map<String, DatabaseConfiguration> databaseConfigs = new
LinkedHashMap<>(3, 1F);
+ Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+
when(connection.getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1/foo_ds");
+ MockedDataSource mockedDataSource = new MockedDataSource(connection);
+ databaseConfigs.put("foo_db", new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.emptyList()));
+ databaseConfigs.put("bar_db", new
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds",
mockedDataSource), Collections.emptyList()));
+ databaseConfigs.put("sys", new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.emptyList()));
+ databaseConfigs.put("shardingsphere", new
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds",
mockedDataSource), Collections.emptyList()));
+ Collection<ShardingSphereDatabase> actual =
ShardingSphereDatabasesFactory.create(databaseConfigs, new
ConfigurationProperties(new Properties()), mock());
+ assertThat(actual.size(), is(7));
+ assertTrue(actual.stream().anyMatch(each ->
"foo_db".equals(each.getName())));
+ assertTrue(actual.stream().anyMatch(each ->
"bar_db".equals(each.getName())));
+ assertTrue(actual.stream().anyMatch(each ->
"information_schema".equals(each.getName())));
+ assertTrue(actual.stream().anyMatch(each ->
"performance_schema".equals(each.getName())));
+ assertTrue(actual.stream().anyMatch(each ->
"sys".equals(each.getName())));
+ assertTrue(actual.stream().anyMatch(each ->
"mysql".equals(each.getName())));
+ assertTrue(actual.stream().anyMatch(each ->
"shardingsphere".equals(each.getName())));
}
}