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 164dd4ef5e7 Add more test cases on shardingsphere-infra-common module
(#36980)
164dd4ef5e7 is described below
commit 164dd4ef5e7eb5189a1ac016062da1c69264ad01
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Oct 31 20:50:38 2025 +0800
Add more test cases on shardingsphere-infra-common module (#36980)
* Refactor GenericSchemaBuilderTest
* Add ShardingSphereStatisticsCollectorTest
* Update CLAUDE.md
* Update CLAUDE.md
* Add StorageUnitTest
* Add StorageUnitTest
* Add StorageUnitTest
* Add ShardingSphereStatisticsFactoryTest
* Add StorageUnitNodeMapCreatorTest
* Add StorageUnitNodeMapCreatorTest
---
CLAUDE.md | 37 ++++++++
.../database/resource/unit/StorageUnit.java | 8 +-
.../ShardingSphereStatisticsCollector.java | 4 +-
.../unit/StorageUnitNodeMapCreatorTest.java | 105 +++++++++++++++++++++
.../database/resource/unit/StorageUnitTest.java | 102 ++++++++++++++++++++
.../schema/builder/GenericSchemaBuilderTest.java | 37 ++++----
.../ShardingSphereStatisticsFactoryTest.java | 46 +++++++++
.../ShardingSphereStatisticsCollectorTest.java | 74 +++++++++++++++
8 files changed, 391 insertions(+), 22 deletions(-)
diff --git a/CLAUDE.md b/CLAUDE.md
index cace06a3fba..a5cdfcf947f 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -43,6 +43,12 @@ Core concepts:
- Consider extreme performance optimization (<100ms for common operations)
- Follow CODE_OF_CONDUCT.md (clean code principles, naming, formatting)
+### Formatting
+- < 200 chars per line, no unnecessary breaks
+- Keep empty lines between methods
+- Remove empty lines within methods
+- **Comments**: No code comments - "code as documentation"
+
### Testing Standards
- 100% line and branch coverage for all new code
- No redundant test cases - each test validates unique behavior
@@ -50,6 +56,11 @@ Core concepts:
- Follow CODE_OF_CONDUCT.md (AIR principle, BCDE design, naming conventions)
- Focus on behavior testing over implementation details
+### Test Code Standards
+- **Method Naming**: Test methods start with "assert" (not "test")
+- **Assertions**: Use AssertJ style: `assertThat(actual, is(expected))`
+- **Variables**: Name test results as "actual" (not "result")
+
### Intelligent Code Standards
#### Contextual Intelligence
@@ -83,6 +94,7 @@ Core concepts:
- No functionality regression
- Spotless formatting passes
- 100% coverage for new code
+- Test code follows all formatting and style requirements
### Code Standards
- **Simplicity**: <50 lines for simple functions, <200 lines for complex
classes
@@ -153,6 +165,31 @@ public abstract class AbstractDatabaseConnector {
}
}
+### Example 4: Test Code Standards
+
+Before:
+```java
+@Test
+void testCalculateTotal() {
+ // Setup test data
+ List<Order> orders = Arrays.asList(new Order(100), new Order(200));
+ OrderService service = new OrderService();
+ double result = service.calculateTotal(orders);
+ assertEquals(300.0, result, 0.01);
+}
+```
+
+After:
+```java
+@Test
+void assertCalculateTotal() {
+ List<Order> orders = Arrays.asList(new Order(100), new Order(200));
+ OrderService service = new OrderService();
+ double actual = service.calculateTotal(orders);
+ assertThat(actual, is(300));
+}
+```
+
## Build System
Maven build commands:
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java
index 4c6a80f9f97..d832c37a7f9 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java
@@ -55,14 +55,14 @@ public final class StorageUnit {
String username = null == originUsername ? "" :
originUsername.toString();
storageType = DatabaseTypeFactory.get(url);
boolean isInstanceConnectionAvailable = new
DatabaseTypeRegistry(storageType).getDialectDatabaseMetaData().getConnectionOption().isInstanceConnectionAvailable();
- String catalog = isInstanceConnectionAvailable ?
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
storageType).parse(url, username, null).getCatalog() : null;
+ ConnectionPropertiesParser parser =
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
storageType);
+ String catalog = isInstanceConnectionAvailable ? parser.parse(url,
username, null).getCatalog() : null;
this.dataSource = isInstanceConnectionAvailable ? new
CatalogSwitchableDataSource(dataSource, catalog, url) : dataSource;
dataSourcePoolProperties = dataSourcePoolProps;
- connectionProperties = createConnectionProperties(catalog,
standardProps);
+ connectionProperties = createConnectionProperties(parser, catalog,
standardProps);
}
- private ConnectionProperties createConnectionProperties(final String
catalog, final Map<String, Object> standardProps) {
- ConnectionPropertiesParser parser =
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
storageType);
+ private ConnectionProperties createConnectionProperties(final
ConnectionPropertiesParser parser, final String catalog, final Map<String,
Object> standardProps) {
return parser.parse(standardProps.get("url").toString(),
standardProps.getOrDefault("username", "").toString(), catalog);
}
}
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/shardingsphere/ShardingSphereStatisticsCollector.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/shardingsphere/ShardingSphereStatisticsCollector.java
index 8637c81cb97..9bddb659276 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/shardingsphere/ShardingSphereStatisticsCollector.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/shardingsphere/ShardingSphereStatisticsCollector.java
@@ -49,8 +49,8 @@ public final class ShardingSphereStatisticsCollector
implements DialectDatabaseS
@Override
public Optional<Collection<Map<String, Object>>>
collectRowColumnValues(final String databaseName, final String schemaName,
final String tableName,
final ShardingSphereMetaData metaData) throws SQLException {
- Optional<ShardingSphereTableStatisticsCollector>
tableStatisticsCollector =
TypedSPILoader.findService(ShardingSphereTableStatisticsCollector.class,
- String.format("%s.%s", schemaName, tableName));
+ Optional<ShardingSphereTableStatisticsCollector>
tableStatisticsCollector = TypedSPILoader.findService(
+ ShardingSphereTableStatisticsCollector.class,
String.format("%s.%s", schemaName, tableName));
return tableStatisticsCollector.isPresent() ?
Optional.of(tableStatisticsCollector.get().collect(databaseName, schemaName,
tableName, metaData)) : Optional.empty();
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapCreatorTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapCreatorTest.java
new file mode 100644
index 00000000000..66e30d9dd10
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapCreatorTest.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.database.resource.unit;
+
+import
org.apache.shardingsphere.database.connector.core.exception.UnrecognizedDatabaseURLException;
+import
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionProperties;
+import
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionPropertiesParser;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.metadata.DialectDatabaseMetaData;
+import
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
+import
org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class StorageUnitNodeMapCreatorTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private DataSourcePoolProperties dataSourcePoolProps;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private DialectDatabaseMetaData dialectDatabaseMetaData;
+
+ @BeforeEach
+ void setUp() {
+ Map<String, Object> standardProps = new HashMap<>(2, 1F);
+ standardProps.put("url", "jdbc:mock://127.0.0.1/foo_ds");
+ standardProps.put("username", "sa");
+
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(standardProps);
+ }
+
+ @Test
+ void assertNewWithIsInstanceConnectionAvailable() {
+ try (MockedStatic<DatabaseTypedSPILoader> mockedLoader =
mockStatic(DatabaseTypedSPILoader.class)) {
+
when(dialectDatabaseMetaData.getConnectionOption().isInstanceConnectionAvailable()).thenReturn(true);
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class,
databaseType)).thenReturn(dialectDatabaseMetaData);
+ ConnectionPropertiesParser parser =
mock(ConnectionPropertiesParser.class);
+ when(parser.parse("jdbc:mock://127.0.0.1/foo_ds", "sa",
null)).thenReturn(new ConnectionProperties("127.0.0.1", 3307, null,
"foo_schema", new Properties()));
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
databaseType)).thenReturn(parser);
+ Map<String, StorageNode> actual =
StorageUnitNodeMapCreator.create(Collections.singletonMap("foo_ds",
dataSourcePoolProps));
+ assertThat(actual.size(), is(1));
+ assertTrue(actual.containsKey("foo_ds"));
+ }
+ }
+
+ @Test
+ void assertNewWithIsNotInstanceConnectionAvailable() {
+ try (MockedStatic<DatabaseTypedSPILoader> mockedLoader =
mockStatic(DatabaseTypedSPILoader.class)) {
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class,
databaseType)).thenReturn(dialectDatabaseMetaData);
+ ConnectionPropertiesParser parser =
mock(ConnectionPropertiesParser.class);
+ when(parser.parse("jdbc:mock://127.0.0.1/foo_ds", "sa",
null)).thenReturn(new ConnectionProperties("127.0.0.1", 3307, null,
"foo_schema", new Properties()));
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
databaseType)).thenReturn(parser);
+ Map<String, StorageNode> actual =
StorageUnitNodeMapCreator.create(Collections.singletonMap("foo_ds",
dataSourcePoolProps));
+ assertThat(actual.size(), is(1));
+ assertTrue(actual.containsKey("foo_ds"));
+ }
+ }
+
+ @Test
+ void assertNewWithUnrecognizedDatabaseURLException() {
+ try (MockedStatic<DatabaseTypedSPILoader> mockedLoader =
mockStatic(DatabaseTypedSPILoader.class)) {
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class,
databaseType)).thenReturn(dialectDatabaseMetaData);
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
databaseType)).thenThrow(new UnrecognizedDatabaseURLException("foo_ds",
"Invalid URL"));
+ Map<String, StorageNode> actual =
StorageUnitNodeMapCreator.create(Collections.singletonMap("foo_ds",
dataSourcePoolProps));
+ assertThat(actual.size(), is(1));
+ assertTrue(actual.containsKey("foo_ds"));
+ }
+ }
+}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitTest.java
new file mode 100644
index 00000000000..d347a4e4736
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitTest.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.database.resource.unit;
+
+import
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionProperties;
+import
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionPropertiesParser;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.metadata.DialectDatabaseMetaData;
+import
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
+import
org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import javax.sql.DataSource;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class StorageUnitTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private DataSourcePoolProperties dataSourcePoolProperties;
+
+ @Test
+ void assertNewWithoutUsernameAndIsNotInstanceConnectionAvailable() {
+ Map<String, Object> standardProperties =
Collections.singletonMap("url", "jdbc:mock://127.0.0.1/foo_ds");
+ try (MockedStatic<DatabaseTypedSPILoader> mockedLoader =
mockStatic(DatabaseTypedSPILoader.class)) {
+
when(dataSourcePoolProperties.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(standardProperties);
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class,
databaseType)).thenReturn(mock(DialectDatabaseMetaData.class,
RETURNS_DEEP_STUBS));
+ ConnectionPropertiesParser parser =
mock(ConnectionPropertiesParser.class);
+ when(parser.parse("jdbc:mock://127.0.0.1/foo_ds", "",
null)).thenReturn(new ConnectionProperties("127.0.0.1", 3307, null,
"foo_schema", new Properties()));
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
databaseType)).thenReturn(parser);
+ StorageNode storageNode = mock(StorageNode.class);
+ DataSource dataSource = mock(DataSource.class);
+ StorageUnit actual = new StorageUnit(storageNode,
dataSourcePoolProperties, dataSource);
+ assertThat(actual.getStorageNode(), is(storageNode));
+ assertThat(actual.getStorageType(), is(databaseType));
+ assertThat(actual.getDataSource(), is(dataSource));
+ assertThat(actual.getConnectionProperties().getHostname(),
is("127.0.0.1"));
+ assertThat(actual.getConnectionProperties().getPort(), is(3307));
+ assertNull(actual.getConnectionProperties().getCatalog());
+ assertThat(actual.getConnectionProperties().getSchema(),
is("foo_schema"));
+ }
+ }
+
+ @Test
+ void assertNewWithUsernameAndIsInstanceConnectionAvailable() {
+ Map<String, Object> standardProperties = new HashMap<>(2, 1F);
+ standardProperties.put("url", "jdbc:mock://127.0.0.1/foo_ds");
+ standardProperties.put("username", "sa");
+ try (MockedStatic<DatabaseTypedSPILoader> mockedLoader =
mockStatic(DatabaseTypedSPILoader.class)) {
+
when(dataSourcePoolProperties.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(standardProperties);
+ DialectDatabaseMetaData dialectDatabaseMetaData =
mock(DialectDatabaseMetaData.class, RETURNS_DEEP_STUBS);
+
when(dialectDatabaseMetaData.getConnectionOption().isInstanceConnectionAvailable()).thenReturn(true);
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class,
databaseType)).thenReturn(dialectDatabaseMetaData);
+ ConnectionPropertiesParser parser =
mock(ConnectionPropertiesParser.class);
+ when(parser.parse("jdbc:mock://127.0.0.1/foo_ds", "sa", null))
+ .thenReturn(new ConnectionProperties("127.0.0.1", 3307,
"foo_catalog", "foo_schema", new Properties()));
+ when(parser.parse("jdbc:mock://127.0.0.1/foo_ds", "sa",
"foo_catalog"))
+ .thenReturn(new ConnectionProperties("127.0.0.1", 3307,
"foo_catalog", "foo_schema", new Properties()));
+ mockedLoader.when(() ->
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class,
databaseType)).thenReturn(parser);
+ StorageUnit actual = new StorageUnit(mock(),
dataSourcePoolProperties, mock());
+ assertThat(actual.getConnectionProperties().getHostname(),
is("127.0.0.1"));
+ assertThat(actual.getConnectionProperties().getPort(), is(3307));
+ assertThat(actual.getConnectionProperties().getCatalog(),
is("foo_catalog"));
+ assertThat(actual.getConnectionProperties().getSchema(),
is("foo_schema"));
+ }
+ }
+}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
index a5c01ebdd7b..a2747c8999b 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
@@ -48,6 +48,7 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
@@ -95,22 +96,6 @@ class GenericSchemaBuilderTest {
assertTables(new ShardingSphereSchema("foo_schema",
actual.values().iterator().next().getAllTables(), Collections.emptyList()));
}
- @Test
- void assertBuildWithDifferentProtocolAndStorageTypes() throws SQLException
{
- DatabaseType differentDatabaseType =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
- Collection<String> tableNames = Collections.singleton("foo_tbl");
- Map<String, SchemaMetaData> schemaMetaDataMap =
createSchemaMetaDataMap(tableNames, material);
- when(MetaDataLoader.load(any())).thenReturn(schemaMetaDataMap);
- StorageUnit storageUnit = mock(StorageUnit.class);
- when(storageUnit.getStorageType()).thenReturn(differentDatabaseType);
- Map<String, StorageUnit> storageUnits =
Collections.singletonMap("foo_schema", storageUnit);
- ShardingSphereRule rule = mock(ShardingSphereRule.class);
- when(rule.getAttributes()).thenReturn(new
RuleAttributes(mock(TableMapperRuleAttribute.class)));
- GenericSchemaBuilderMaterial newMaterial = new
GenericSchemaBuilderMaterial(storageUnits, Collections.singleton(rule), new
ConfigurationProperties(new Properties()), "foo_schema");
- Map<String, ShardingSphereSchema> actual =
GenericSchemaBuilder.build(tableNames, databaseType, newMaterial);
- assertThat(actual.size(), is(1));
- }
-
@Test
void assertBuildWithEmptyTableNames() throws SQLException {
when(MetaDataLoader.load(any())).thenReturn(Collections.emptyMap());
@@ -133,6 +118,26 @@ class GenericSchemaBuilderTest {
assertTables(new ShardingSphereSchema("foo_schema",
actual.values().iterator().next().getAllTables(), Collections.emptyList()));
}
+ @Test
+ void assertBuildWithDifferentProtocolAndStorageTypes() throws SQLException
{
+ DatabaseType differentDatabaseType =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
+ Collection<String> tableNames = Collections.singleton("foo_tbl");
+ Map<String, SchemaMetaData> schemaMetaDataMap =
createSchemaMetaDataMap(tableNames, material);
+ when(MetaDataLoader.load(any())).thenReturn(schemaMetaDataMap);
+ StorageUnit storageUnit = mock(StorageUnit.class);
+ when(storageUnit.getStorageType()).thenReturn(differentDatabaseType);
+ Map<String, StorageUnit> storageUnits =
Collections.singletonMap("foo_schema", storageUnit);
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mock(TableMapperRuleAttribute.class)));
+ GenericSchemaBuilderMaterial newMaterial = new
GenericSchemaBuilderMaterial(storageUnits, Collections.singleton(rule), new
ConfigurationProperties(new Properties()), "foo_schema");
+ Map<String, ShardingSphereSchema> actual =
GenericSchemaBuilder.build(tableNames, databaseType, newMaterial);
+ assertThat(actual.size(), is(1));
+ ShardingSphereSchema actualSchema = actual.values().iterator().next();
+ assertTrue(actualSchema.getAllTables().isEmpty());
+ assertNull(actualSchema.getTable("foo_tbl"));
+ assertThat(actualSchema.getName(), is("foo_schema"));
+ }
+
private Map<String, SchemaMetaData> createSchemaMetaDataMap(final
Collection<String> tableNames, final GenericSchemaBuilderMaterial material) {
if (!tableNames.isEmpty() && (tableNames.contains("foo_tbl") ||
tableNames.contains("bar_tbl"))) {
Collection<TableMetaData> tableMetaDataList = tableNames.stream()
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/ShardingSphereStatisticsFactoryTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/ShardingSphereStatisticsFactoryTest.java
new file mode 100644
index 00000000000..bcf27d7bcee
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/ShardingSphereStatisticsFactoryTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.statistics.builder;
+
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingSphereStatisticsFactoryTest {
+
+ @Mock
+ private ShardingSphereMetaData metaData;
+
+ @Mock
+ private ShardingSphereStatistics statistics;
+
+ @Test
+ void assertCreateWithEmptyDatabases() {
+ when(metaData.getAllDatabases()).thenReturn(Collections.emptyList());
+ assertTrue(ShardingSphereStatisticsFactory.create(metaData,
statistics).getDatabaseStatisticsMap().isEmpty());
+ }
+}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/shardingsphere/ShardingSphereStatisticsCollectorTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/shardingsphere/ShardingSphereStatisticsCollectorTest.java
new file mode 100644
index 00000000000..111f5241f77
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/collector/shardingsphere/ShardingSphereStatisticsCollectorTest.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.infra.metadata.statistics.collector.shardingsphere;
+
+import
org.apache.shardingsphere.infra.metadata.statistics.collector.DialectDatabaseStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingSphereStatisticsCollectorTest {
+
+ private final DialectDatabaseStatisticsCollector collector = new
ShardingSphereStatisticsCollector();
+
+ @Test
+ void assertCollectRowColumnValuesWithoutAvailableCollector() throws
SQLException {
+ assertFalse(collector.collectRowColumnValues("foo_db", "foo_schema",
"foo_tbl", mock()).isPresent());
+ }
+
+ @Test
+ void assertCollectRowColumnValuesWithAvailableCollector() throws
SQLException {
+ ShardingSphereTableStatisticsCollector tableStatisticsCollector =
mock();
+ try (MockedStatic<TypedSPILoader> mockedLoader =
mockStatic(TypedSPILoader.class)) {
+ when(tableStatisticsCollector.collect(anyString(), anyString(),
anyString(),
any())).thenReturn(Collections.singleton(Collections.singletonMap("foo_db",
"foo_schema")));
+ mockedLoader.when(() ->
TypedSPILoader.findService(ShardingSphereTableStatisticsCollector.class,
"test_schema.error_table")).thenReturn(Optional.of(tableStatisticsCollector));
+ Optional<Collection<Map<String, Object>>> actual =
collector.collectRowColumnValues("foo_db", "test_schema", "error_table",
mock());
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(),
is(Collections.singleton(Collections.singletonMap("foo_db", "foo_schema"))));
+ }
+ }
+
+ @Test
+ void assertIsStatisticsTablesWithEmptySchemaTables() {
+ assertFalse(collector.isStatisticsTables(Collections.emptyMap()));
+ }
+
+ @Test
+ void assertIsStatisticsTablesWithoutStatisticsSchemaTables() {
+
assertFalse(collector.isStatisticsTables(Collections.singletonMap("foo_schema",
Collections.singletonList("foo_tbl"))));
+ }
+}