You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/07/02 03:47:10 UTC
[shardingsphere] branch master updated: Add test case for ShardingSphereMetaData.dropDatabase (#18762)
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 e0f17198e5e Add test case for ShardingSphereMetaData.dropDatabase (#18762)
e0f17198e5e is described below
commit e0f17198e5e2ddd714d19d33d4cee60b5a9b5c4e
Author: Ziyuan Han <ha...@outlook.com>
AuthorDate: Sat Jul 2 11:47:04 2022 +0800
Add test case for ShardingSphereMetaData.dropDatabase (#18762)
* Add test case for ShardingSphereMetaData.dropDatabase
---
.../infra/metadata/ShardingSphereMetaDataTest.java | 85 ++++++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java
new file mode 100644
index 00000000000..53021c2662b
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.infra.metadata;
+
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResource;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ShardingSphereMetaDataTest {
+ @Mock
+ private ShardingSphereRuleMetaData globalRuleMetaData;
+
+ @Mock
+ private ResourceHeldRule<?> shardingRuleMetaDataResourceHeldRule;
+
+ @Mock
+ private ResourceHeldRule<?> globalRuleMedataResourceHeldRule;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ShardingSphereDatabase shardingSphereDatabase;
+
+ @Mock
+ private ShardingSphereResource shardingSphereResource;
+
+ @Mock
+ private DataSource mockedDataSource;
+
+ private ShardingSphereMetaData shardingSphereMetaData;
+
+ @Before
+ public void setUp() throws SQLException {
+ when(shardingSphereDatabase.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
+ when(shardingSphereDatabase.getResource()).thenReturn(shardingSphereResource);
+ when(shardingSphereDatabase.getResource().getDataSources()).thenReturn(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockedDataSource));
+ when(shardingSphereDatabase.getRuleMetaData().findRules(ResourceHeldRule.class)).thenReturn(Collections.singletonList(shardingRuleMetaDataResourceHeldRule));
+ when(globalRuleMetaData.findRules(ResourceHeldRule.class)).thenReturn(Collections.singletonList(globalRuleMedataResourceHeldRule));
+ shardingSphereMetaData = new ShardingSphereMetaData(new LinkedHashMap<>(), globalRuleMetaData, new ConfigurationProperties(new Properties()));
+ }
+
+ @Test
+ public void assertDropDatabase() {
+ shardingSphereMetaData.getDatabases().put(DefaultDatabase.LOGIC_NAME, shardingSphereDatabase);
+ shardingSphereMetaData.dropDatabase(DefaultDatabase.LOGIC_NAME);
+ Assert.assertThat(shardingSphereMetaData.getDatabases(), is(Collections.emptyMap()));
+ verify(shardingSphereResource).close(mockedDataSource);
+ verify(shardingRuleMetaDataResourceHeldRule).closeStaleResource(DefaultDatabase.LOGIC_NAME);
+ verify(globalRuleMedataResourceHeldRule).closeStaleResource(DefaultDatabase.LOGIC_NAME);
+ }
+}