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 c7df20215e4 Improve DatabaseTypeEngineTest with more generic approach (#35325) c7df20215e4 is described below commit c7df20215e4f41989656ff3b4bad9a844e3a860a Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Mon May 5 14:41:42 2025 +0800 Improve DatabaseTypeEngineTest with more generic approach (#35325) - Remove specific database type imports (MySQL, PostgreSQL, H2) - Use TypedSPILoader to get database types dynamically - Update assertions to use is() instead of instanceOf() - Simplify test cases by removing repeated code --- .../infra/database/DatabaseTypeEngineTest.java | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java index 0d822a345ee..3fe7377de2f 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngineTest.java @@ -22,8 +22,6 @@ import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDa import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; -import org.apache.shardingsphere.infra.database.postgresql.type.PostgreSQLDatabaseType; import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException; import org.apache.shardingsphere.infra.fixture.FixtureRuleConfiguration; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; @@ -38,7 +36,6 @@ import java.sql.SQLException; import java.util.Collections; import java.util.Properties; -import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -50,23 +47,26 @@ class DatabaseTypeEngineTest { @Test void assertGetProtocolTypeFromConfiguredProperties() { + DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL"); Properties props = PropertiesBuilder.build(new Property(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "MySQL")); DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), Collections.singleton(new FixtureRuleConfiguration())); - assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(props)), instanceOf(MySQLDatabaseType.class)); - assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(props)), instanceOf(MySQLDatabaseType.class)); + assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(props)), is(databaseType)); + assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(props)), is(databaseType)); } @Test void assertGetProtocolTypeFromDataSource() throws SQLException { - DataSource datasource = mockDataSource(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); + DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"); + DataSource datasource = mockDataSource(databaseType); DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_ds", datasource), Collections.singleton(new FixtureRuleConfiguration())); - assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(new Properties())), instanceOf(PostgreSQLDatabaseType.class)); - assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(new Properties())), instanceOf(PostgreSQLDatabaseType.class)); + assertThat(DatabaseTypeEngine.getProtocolType(databaseConfig, new ConfigurationProperties(new Properties())), is(databaseType)); + assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(new Properties())), is(databaseType)); } @Test void assertGetStorageType() throws SQLException { - assertThat(DatabaseTypeEngine.getStorageType(mockDataSource(TypedSPILoader.getService(DatabaseType.class, "H2"))).getType(), is("H2")); + DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "H2"); + assertThat(DatabaseTypeEngine.getStorageType(mockDataSource(databaseType)), is(databaseType)); } @Test @@ -78,7 +78,8 @@ class DatabaseTypeEngineTest { @Test void assertGetDefaultStorageTypeWithEmptyDataSources() { - assertThat(DatabaseTypeEngine.getDefaultStorageType().getType(), is("MySQL")); + DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL"); + assertThat(DatabaseTypeEngine.getDefaultStorageType(), is(databaseType)); } private DataSource mockDataSource(final DatabaseType databaseType) throws SQLException {