You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by do...@apache.org on 2020/08/22 06:49:29 UTC

[shardingsphere] branch master updated: Add ShardingSphereSchemaTest (#6990)

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

dongzonglei 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 a411e03  Add ShardingSphereSchemaTest (#6990)
a411e03 is described below

commit a411e0317a845dd7cb10ab0851aa270e0562a19e
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Aug 22 14:49:10 2020 +0800

    Add ShardingSphereSchemaTest (#6990)
    
    * Refactor ShardingSphereSchema's constructor
    
    * Move CachedDatabaseMetaDataTest
    
    * Add ShardingSphereSchemaTest
---
 .../context/schema/ShardingSphereSchema.java       | 18 ++-----
 .../runtime/CachedDatabaseMetaDataTest.java        |  0
 .../context/schema/ShardingSphereSchemaTest.java   | 58 ++++++++++++++++++++++
 .../context/schema/fixture/ClosableDataSource.java | 23 +++++++++
 4 files changed, 86 insertions(+), 13 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java
index 074276d..66c4cdd 100644
--- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java
+++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.kernel.context.schema;
 
 import lombok.Getter;
+import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -25,32 +26,23 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import javax.sql.DataSource;
 import java.lang.reflect.Method;
 import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
 import java.util.Map;
 
 /**
  * ShardingSphere schema.
  */
+@RequiredArgsConstructor
 @Getter
 public final class ShardingSphereSchema {
     
-    private final Collection<RuleConfiguration> configurations = new LinkedList<>();
+    private final Collection<RuleConfiguration> configurations;
     
-    private final Collection<ShardingSphereRule> rules = new LinkedList<>();
+    private final Collection<ShardingSphereRule> rules;
     
-    private final Map<String, DataSource> dataSources = new LinkedHashMap<>();
+    private final Map<String, DataSource> dataSources;
     
     private final ShardingSphereMetaData metaData;
     
-    public ShardingSphereSchema(final Collection<RuleConfiguration> configurations, final Collection<ShardingSphereRule> rules,
-                                final Map<String, DataSource> dataSourceMap, final ShardingSphereMetaData shardingSphereMetaData) {
-        this.configurations.addAll(configurations);
-        this.rules.addAll(rules);
-        dataSources.putAll(dataSourceMap);
-        metaData = shardingSphereMetaData;
-    }
-    
     /**
      * Close data sources.
      * @param dataSources data sources
diff --git a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-facade/src/test/java/org/apache/shardingsphere/kernel/context/runtime/CachedDatabaseMetaDataTest.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/runtime/CachedDatabaseMetaDataTest.java
similarity index 100%
rename from shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-facade/src/test/java/org/apache/shardingsphere/kernel/context/runtime/CachedDatabaseMetaDataTest.java
rename to shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/runtime/CachedDatabaseMetaDataTest.java
diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchemaTest.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchemaTest.java
new file mode 100644
index 0000000..32ca92c
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchemaTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.kernel.context.schema;
+
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.kernel.context.schema.fixture.ClosableDataSource;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.sql.DataSource;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ShardingSphereSchemaTest {
+    
+    @Mock
+    private ClosableDataSource dataSource0;
+    
+    @Mock
+    private ClosableDataSource dataSource1;
+    
+    @Test
+    public void assertCloseDataSources() throws Exception {
+        new ShardingSphereSchema(Collections.emptyList(), Collections.emptyList(), createDataSources(), mock(ShardingSphereMetaData.class)).closeDataSources(Collections.singleton("ds_0"));
+        verify(dataSource0).close();
+        verify(dataSource1, times(0)).close();
+    }
+    
+    private Map<String, DataSource> createDataSources() {
+        Map<String, DataSource> result = new HashMap<>(2, 1);
+        result.put("ds_0", dataSource0);
+        result.put("ds_1", dataSource1);
+        return result;
+    }
+}
diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/schema/fixture/ClosableDataSource.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/schema/fixture/ClosableDataSource.java
new file mode 100644
index 0000000..3449c4f
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/test/java/org/apache/shardingsphere/kernel/context/schema/fixture/ClosableDataSource.java
@@ -0,0 +1,23 @@
+/*
+ * 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.kernel.context.schema.fixture;
+
+import javax.sql.DataSource;
+
+public interface ClosableDataSource extends DataSource, AutoCloseable {
+}