This is an automated email from the ASF dual-hosted git repository.

panjuan 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 06aea68502f Refactor DatabaseTypeEngineTest (#18925)
06aea68502f is described below

commit 06aea68502fcc52c3c8b00791f06dff57293cd0a
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Jul 7 14:52:53 2022 +0800

    Refactor DatabaseTypeEngineTest (#18925)
---
 .../infra/database/type/DatabaseTypeEngine.java    |   3 +-
 .../database/type/DatabaseTypeEngineTest.java      | 131 ++++++---------------
 2 files changed, 35 insertions(+), 99 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
index d1a19d5fde2..39518b7ddc6 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
@@ -23,6 +23,7 @@ import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -109,7 +110,7 @@ public final class DatabaseTypeEngine {
         try (Connection connection = dataSource.getConnection()) {
             return getDatabaseType(connection.getMetaData().getURL());
         } catch (final SQLException ex) {
-            throw new IllegalArgumentException(ex.getMessage(), ex);
+            throw new ShardingSphereException(ex.getMessage(), ex);
         }
     }
     
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
index f03f1ea0df9..bab671c7e62 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
@@ -24,6 +24,7 @@ import 
org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import 
org.apache.shardingsphere.infra.database.type.dialect.MariaDBDatabaseType;
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
 import 
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.fixture.FixtureRuleConfiguration;
 import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Test;
@@ -46,80 +47,61 @@ import static org.mockito.Mockito.when;
 public final class DatabaseTypeEngineTest {
     
     @Test
-    public void assertGetH2DatabaseType() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("H2"));
-        Collection<DataSource> dataSources = Collections.singleton(datasource);
-        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("H2"));
+    public void assertGetProtocolTypeFromConfiguredProperties() {
+        Properties props = new Properties();
+        
props.setProperty(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));
     }
     
     @Test
-    public void assertGetMariaDBDatabaseType() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("MariaDB"));
-        Collection<DataSource> dataSources = Collections.singleton(datasource);
-        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("MariaDB"));
+    public void assertGetProtocolTypeFromDataSource() throws SQLException {
+        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("PostgreSQL"));
+        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));
     }
     
     @Test
-    public void assertGetMySQLDatabaseType() throws SQLException {
+    public void assertGetStorageType() throws SQLException {
         DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("MySQL"));
-        Collection<DataSource> dataSources = Collections.singleton(datasource);
-        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("MySQL"));
+        DatabaseConfiguration databaseConfig = new 
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("", 
datasource), Collections.singletonList(new FixtureRuleConfiguration()));
+        
assertThat(DatabaseTypeEngine.getStorageType(Collections.singletonMap("foo_db", 
databaseConfig)), instanceOf(MySQLDatabaseType.class));
     }
     
     @Test
-    public void assertGetOracleDatabaseType() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("Oracle"));
-        Collection<DataSource> dataSources = Collections.singleton(datasource);
-        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("Oracle"));
+    public void assertGetDatabaseTypeWithEmptyDataSources() {
+        
assertThat(DatabaseTypeEngine.getDatabaseType(Collections.emptyList()).getType(),
 is("MySQL"));
     }
     
     @Test
-    public void assertGetPostgreSQLDatabaseType() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("PostgreSQL"));
-        Collection<DataSource> dataSources = Collections.singleton(datasource);
-        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("PostgreSQL"));
+    public void assertGetDatabaseTypeWithDataSources() throws SQLException {
+        Collection<DataSource> dataSources = 
Arrays.asList(mockDataSource(DatabaseTypeFactory.getInstance("H2")), 
mockDataSource(DatabaseTypeFactory.getInstance("H2")));
+        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("H2"));
     }
     
-    @Test
-    public void assertGetSQL92DatabaseType() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("SQL92"));
-        Collection<DataSource> dataSources = Collections.singleton(datasource);
-        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("SQL92"));
+    @Test(expected = IllegalStateException.class)
+    public void assertGetDatabaseTypeWithDifferentDataSourceTypes() throws 
SQLException {
+        Collection<DataSource> dataSources = 
Arrays.asList(mockDataSource(DatabaseTypeFactory.getInstance("H2")), 
mockDataSource(DatabaseTypeFactory.getInstance("MySQL")));
+        DatabaseTypeEngine.getDatabaseType(dataSources);
     }
     
-    @Test
-    public void assertGetSQLServerDatabaseType() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("SQLServer"));
-        Collection<DataSource> dataSources = Collections.singleton(datasource);
-        assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(), 
is("SQLServer"));
+    @Test(expected = ShardingSphereException.class)
+    public void assertGetDatabaseTypeWhenGetConnectionError() throws 
SQLException {
+        DataSource dataSource = mock(DataSource.class);
+        when(dataSource.getConnection()).thenThrow(SQLException.class);
+        DatabaseTypeEngine.getDatabaseType(Collections.singleton(dataSource));
     }
     
     @Test
-    public void assertGetDatabaseTypeWithEmptyDataSources() {
-        
assertThat(DatabaseTypeEngine.getDatabaseType(Collections.emptyList()).getType(),
 is("MySQL"));
+    public void assertGetDatabaseTypeWithRecognizedURL() {
+        
assertThat(DatabaseTypeEngine.getDatabaseType("jdbc:mysql://localhost:3306/test").getType(),
 is("MySQL"));
     }
     
     @Test
-    public void assertGetDatabaseTypeFromSameDataSources() throws SQLException 
{
-        DataSource datasource1 = 
mockDataSource(DatabaseTypeFactory.getInstance("MySQL"));
-        DataSource datasource2 = 
mockDataSource(DatabaseTypeFactory.getInstance("MySQL"));
-        Collection<DataSource> sameDataSources = Arrays.asList(datasource1, 
datasource2);
-        
assertThat(DatabaseTypeEngine.getDatabaseType(sameDataSources).getType(), 
is("MySQL"));
-    }
-    
-    @Test(expected = IllegalStateException.class)
-    public void assertGetDatabaseTypeFromDifferentDataSources() throws 
SQLException {
-        DataSource datasource1 = 
mockDataSource(DatabaseTypeFactory.getInstance("H2"));
-        DataSource datasource2 = 
mockDataSource(DatabaseTypeFactory.getInstance("Oracle"));
-        Collection<DataSource> differentDataSources = 
Arrays.asList(datasource1, datasource2);
-        DatabaseTypeEngine.getDatabaseType(differentDataSources);
-    }
-    
-    @Test(expected = IllegalArgumentException.class)
-    public void assertCantGetConnectionFromDataSource() throws SQLException {
-        DataSource mockDataSource = mock(DataSource.class);
-        when(mockDataSource.getConnection()).thenThrow(SQLException.class);
-        
DatabaseTypeEngine.getDatabaseType(Collections.singleton(mockDataSource));
+    public void assertGetDatabaseTypeWithUnrecognizedURL() {
+        
assertThat(DatabaseTypeEngine.getDatabaseType("jdbc:sqlite:test").getType(), 
is("SQL92"));
     }
     
     private DataSource mockDataSource(final DatabaseType databaseType) throws 
SQLException {
@@ -132,38 +114,15 @@ public final class DatabaseTypeEngineTest {
         switch (databaseType.getType()) {
             case "H2":
                 return 
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL";
-            case "MariaDB":
-                return "jdbc:mariadb://localhost:3306/test";
             case "MySQL":
                 return "jdbc:mysql://localhost:3306/test";
             case "PostgreSQL":
                 return "jdbc:postgresql://localhost:5432/test";
-            case "SQLServer":
-                return 
"jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=test";
-            case "Oracle":
-                return "jdbc:oracle:oci:@127.0.0.1/test";
-            case "SQL92":
-                return "jdbc:jtds:sqlserver://127.0.0.1;DatabaseName=test";
             default:
                 throw new IllegalStateException("Unexpected value: " + 
databaseType.getType());
         }
     }
     
-    @Test
-    public void assertGetDatabaseTypeByStandardURL() {
-        
assertThat(DatabaseTypeEngine.getDatabaseType("jdbc:mysql://localhost:3306/test").getType(),
 is("MySQL"));
-    }
-    
-    @Test
-    public void assertGetDatabaseTypeByURLAlias() {
-        
assertThat(DatabaseTypeEngine.getDatabaseType("jdbc:mysqlx://localhost:3306/test").getType(),
 is("MySQL"));
-    }
-    
-    @Test
-    public void assertGetDatabaseTypeSQL92() {
-        
assertThat(DatabaseTypeEngine.getDatabaseType("jdbc:sqlite:test").getType(), 
is("SQL92"));
-    }
-    
     @Test
     public void assertGetTrunkDatabaseTypeWithTrunkDatabaseType() {
         assertThat(DatabaseTypeEngine.getTrunkDatabaseType("MySQL").getType(), 
is("MySQL"));
@@ -184,30 +143,6 @@ public final class DatabaseTypeEngineTest {
         assertThat(DatabaseTypeEngine.getTrunkDatabaseTypeName(new 
MariaDBDatabaseType()), is("MySQL"));
     }
     
-    @Test
-    public void assertGetProtocolTypeFromConfiguredProperties() {
-        Properties props = new Properties();
-        
props.setProperty(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));
-    }
-    
-    @Test
-    public void assertGetProtocolTypeFromDataSource() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("PostgreSQL"));
-        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));
-    }
-    
-    @Test
-    public void assertGetStorageType() throws SQLException {
-        DataSource datasource = 
mockDataSource(DatabaseTypeFactory.getInstance("MySQL"));
-        DatabaseConfiguration databaseConfig = new 
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("", 
datasource), Collections.singletonList(new FixtureRuleConfiguration()));
-        
assertThat(DatabaseTypeEngine.getStorageType(Collections.singletonMap("foo_db", 
databaseConfig)), instanceOf(MySQLDatabaseType.class));
-    }
-    
     @Test
     public void assertGetDefaultSchemaName() {
         DatabaseType schemaSupportDatabaseType = 
DatabaseTypeFactory.getInstance("openGauss");

Reply via email to