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

zhangliang pushed a commit to branch orchestration-dev
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/orchestration-dev by this push:
     new faeb49c  assertCreateDataSourceWhenRuleConfigurationsNotEmpty added 
(#7155)
faeb49c is described below

commit faeb49c56134bc42f32b5a9455b73af2fd9fb2c7
Author: sluk3r <[email protected]>
AuthorDate: Mon Aug 31 00:50:47 2020 +0800

    assertCreateDataSourceWhenRuleConfigurationsNotEmpty added (#7155)
    
    * assertCreateDataSourceWhenRuleConfigurationsNotEmpty added
    
    * assertCreateDataSourceWithGivenDataSource added
    
    * 
assertCreateDataSourceWhenRuleConfigurationsNotEmptyWithClusterConfigurationAndMetricsConfigurationBothDefault,
 assertCreateDataSourceWithGivenDataSource and 
assertCreateDataSourceWithGivenDataSourceWithClusterConfigurationAndMetricsConfigurationBothDefault
 added
    
    * alter as suggested in terms of naming, final class and so on
    
    Co-authored-by: wangxichun <[email protected]>
---
 ...trationShardingSphereDataSourceFactoryTest.java | 104 +++++++++++++++++++++
 1 file changed, 104 insertions(+)

diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/api/OrchestrationShardingSphereDataSourceFactoryTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/api/OrchestrationShardingSphereDataSourceFactoryTest.java
new file mode 100644
index 0000000..67ad45f
--- /dev/null
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/api/OrchestrationShardingSphereDataSourceFactoryTest.java
@@ -0,0 +1,104 @@
+/*
+ * 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.driver.orchestration.api;
+
+import lombok.SneakyThrows;
+import 
org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
+import 
org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import 
org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
+import 
org.apache.shardingsphere.orchestration.repository.api.config.OrchestrationCenterConfiguration;
+import 
org.apache.shardingsphere.orchestration.repository.api.config.OrchestrationConfiguration;
+import org.junit.Test;
+
+import javax.sql.DataSource;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class OrchestrationShardingSphereDataSourceFactoryTest {
+    private static final String TABLE_TYPE = "TABLE";
+    
+    @SneakyThrows
+    @Test
+    public void assertCreateDataSourceWhenRuleConfigurationsNotEmpty() {
+        DataSource dataSource = 
OrchestrationShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(),
 Collections.singletonList(mock(RuleConfiguration.class)),
+                new Properties(), createOrchestrationConfig(), 
mock(ClusterConfiguration.class), mock(MetricsConfiguration.class));
+        assertTrue(dataSource instanceof 
OrchestrationShardingSphereDataSource);
+    }
+    
+    @SneakyThrows
+    @Test
+    public void 
assertCreateDataSourceWhenRuleConfigurationsNotEmptyWithClusterConfigurationAndMetricsConfigurationBothDefault()
 {
+        DataSource dataSource = 
OrchestrationShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(),
 Collections.singletonList(mock(RuleConfiguration.class)),
+                new Properties(), createOrchestrationConfig());
+        assertTrue(dataSource instanceof 
OrchestrationShardingSphereDataSource);
+    }
+    
+    @SneakyThrows
+    @Test
+    public void assertCreateDataSourceWithGivenDataSource() {
+        DataSource dataSource = 
OrchestrationShardingSphereDataSourceFactory.createDataSource(createDataSource(),
 Collections.singletonList(mock(RuleConfiguration.class)),
+                new Properties(), createOrchestrationConfig(), 
mock(ClusterConfiguration.class), mock(MetricsConfiguration.class));
+        assertTrue(dataSource instanceof 
OrchestrationShardingSphereDataSource);
+    }
+    
+    @SneakyThrows
+    @Test
+    public void 
assertCreateDataSourceWithGivenDataSourceWithClusterConfigurationAndMetricsConfigurationBothDefault()
 {
+        DataSource dataSource = 
OrchestrationShardingSphereDataSourceFactory.createDataSource(createDataSource(),
 Collections.singletonList(mock(RuleConfiguration.class)),
+                new Properties(), createOrchestrationConfig());
+        assertTrue(dataSource instanceof 
OrchestrationShardingSphereDataSource);
+    }
+    
+    private Map<String, DataSource> createDataSourceMap() {
+        Map<String, DataSource> result = new HashMap<>();
+        result.put("dataSourceMapKey", createDataSource());
+        return result;
+    }
+    
+    @SneakyThrows
+    private DataSource createDataSource() {
+        DataSource result = mock(DataSource.class);
+        Connection connection = mock(Connection.class);
+        DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
+        when(connection.getMetaData()).thenReturn(databaseMetaData);
+        
when(databaseMetaData.getURL()).thenReturn("jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT%2B8");
+        ResultSet resultSet = mock(ResultSet.class);
+        when(databaseMetaData.getTables(null, null, null, new 
String[]{TABLE_TYPE})).thenReturn(resultSet);
+        when(result.getConnection()).thenReturn(connection);
+        return result;
+    }
+    
+    private OrchestrationConfiguration createOrchestrationConfig() {
+        OrchestrationConfiguration result = 
mock(OrchestrationConfiguration.class);
+        OrchestrationCenterConfiguration orchestrationCenterConfiguration = 
mock(OrchestrationCenterConfiguration.class);
+        
when(result.getRegistryCenterConfiguration()).thenReturn(orchestrationCenterConfiguration);
+        
when(orchestrationCenterConfiguration.getType()).thenReturn("REG_TEST");
+        return result;
+    }
+}

Reply via email to