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 7ef32db8df0 Add test case to cover line 64 and 80 in
DatabaseTypeEngine (#36936)
7ef32db8df0 is described below
commit 7ef32db8df04754c49f5d894f1da557f742b6620
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Oct 24 22:14:10 2025 +0800
Add test case to cover line 64 and 80 in DatabaseTypeEngine (#36936)
* Add test case to cover line 64 and 80 in DatabaseTypeEngine
- Add assertGetProtocolTypeWithEmptyDataSources test method
- Covers getDefaultStorageType() branch when dataSources.isEmpty() is true
- Ensures complete line coverage for getProtocolType methods
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Update DatabaseTypeEngineTest
* Refactor DatabaseTypeEngine
* Refactor DatabaseTypeEngine
---------
Co-authored-by: Claude <[email protected]>
---
.../infra/database/DatabaseTypeEngine.java | 38 ++++++++++++----------
.../infra/database/DatabaseTypeEngineTest.java | 8 +++++
2 files changed, 29 insertions(+), 17 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
index bd88462fdac..aed941918ca 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
@@ -71,13 +71,13 @@ public final class DatabaseTypeEngine {
* @param props configuration properties
* @return protocol type
*/
- public static DatabaseType getProtocolType(final Map<String, ? extends
DatabaseConfiguration> databaseConfigs, final ConfigurationProperties props) {
+ public static DatabaseType getProtocolType(final Map<String,
DatabaseConfiguration> databaseConfigs, final ConfigurationProperties props) {
Optional<DatabaseType> configuredDatabaseType =
findConfiguredDatabaseType(props);
if (configuredDatabaseType.isPresent()) {
return configuredDatabaseType.get();
}
- Map<String, DataSource> dataSources = getDataSources(databaseConfigs);
- return dataSources.isEmpty() ? getDefaultStorageType() :
getStorageType(dataSources.values().iterator().next());
+ Collection<DataSource> dataSources =
getDataSources(databaseConfigs).values();
+ return dataSources.isEmpty() ? getDefaultStorageType() :
getStorageType(dataSources.iterator().next());
}
private static Optional<DatabaseType> findConfiguredDatabaseType(final
ConfigurationProperties props) {
@@ -85,9 +85,9 @@ public final class DatabaseTypeEngine {
return null == configuredDatabaseType ? Optional.empty() :
Optional.of(configuredDatabaseType.getTrunkDatabaseType().orElse(configuredDatabaseType));
}
- private static Map<String, DataSource> getDataSources(final Map<String, ?
extends DatabaseConfiguration> databaseConfigs) {
+ private static Map<String, DataSource> getDataSources(final Map<String,
DatabaseConfiguration> databaseConfigs) {
Map<String, DataSource> result = new LinkedHashMap<>();
- for (Entry<String, ? extends DatabaseConfiguration> entry :
databaseConfigs.entrySet()) {
+ for (Entry<String, DatabaseConfiguration> entry :
databaseConfigs.entrySet()) {
result.putAll(getDataSources(entry.getValue()));
}
return result;
@@ -113,21 +113,25 @@ public final class DatabaseTypeEngine {
try (Connection connection = dataSource.getConnection()) {
return DatabaseTypeFactory.get(connection.getMetaData().getURL());
} catch (final SQLFeatureNotSupportedException
sqlFeatureNotSupportedException) {
- try (Connection connection = dataSource.getConnection()) {
- Class<?> hiveConnectionClass =
Class.forName("org.apache.hive.jdbc.HiveConnection");
- if (connection.isWrapperFor(hiveConnectionClass)) {
- Object hiveConnection =
connection.unwrap(hiveConnectionClass);
- String connectedUrl = (String)
hiveConnectionClass.getMethod("getConnectedUrl").invoke(hiveConnection);
- return DatabaseTypeFactory.get(connectedUrl);
- }
- throw new SQLWrapperException(sqlFeatureNotSupportedException);
- } catch (final SQLException ex) {
- throw new SQLWrapperException(ex);
- } catch (final ClassNotFoundException | NoSuchMethodException |
InvocationTargetException | IllegalAccessException ex) {
- throw new SQLWrapperException(new SQLException(ex));
+ return getStorageType(dataSource, sqlFeatureNotSupportedException);
+ } catch (final SQLException ex) {
+ throw new SQLWrapperException(ex);
+ }
+ }
+
+ private static DatabaseType getStorageType(final DataSource dataSource,
final SQLFeatureNotSupportedException sqlFeatureNotSupportedException) {
+ try (Connection connection = dataSource.getConnection()) {
+ Class<?> hiveConnectionClass =
Class.forName("org.apache.hive.jdbc.HiveConnection");
+ if (connection.isWrapperFor(hiveConnectionClass)) {
+ Object hiveConnection = connection.unwrap(hiveConnectionClass);
+ String connectedUrl = (String)
hiveConnectionClass.getMethod("getConnectedUrl").invoke(hiveConnection);
+ return DatabaseTypeFactory.get(connectedUrl);
}
+ throw new SQLWrapperException(sqlFeatureNotSupportedException);
} catch (final SQLException ex) {
throw new SQLWrapperException(ex);
+ } catch (final ClassNotFoundException | NoSuchMethodException |
InvocationTargetException | IllegalAccessException ex) {
+ throw new SQLWrapperException(new SQLException(ex));
}
}
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 8521a454f19..c80f455ccd0 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
@@ -63,6 +63,14 @@ class DatabaseTypeEngineTest {
assertThat(DatabaseTypeEngine.getProtocolType(Collections.singletonMap("foo_db",
databaseConfig), new ConfigurationProperties(new Properties())),
is(databaseType));
}
+ @Test
+ void assertGetProtocolTypeWithEmptyDataSources() {
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "MySQL");
+ DatabaseConfiguration databaseConfig = new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.singleton(new FixtureRuleConfiguration()));
+ 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 {
DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "H2");