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/06/27 09:32:50 UTC
[shardingsphere] branch master updated: Fix issue 18446 (#18618)
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 d728bc22e16 Fix issue 18446 (#18618)
d728bc22e16 is described below
commit d728bc22e16809518523b95cb1c58da920c75aea
Author: DaneBrown <ta...@gmail.com>
AuthorDate: Mon Jun 27 17:32:43 2022 +0800
Fix issue 18446 (#18618)
* finish update unit test
* 1:remove unnecessary comment
2:use static import for Assert
3:remove unnecessary blank lines
* 1:text wrap
2:remove try catch
---
.../updatable/AlterTransactionRuleHandler.java | 1 +
.../updatable/AlterTransactionRuleHandlerTest.java | 151 +++++++++++++++++++++
2 files changed, 152 insertions(+)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
index ad174e86ccb..b05d764ac3e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
@@ -47,6 +47,7 @@ public final class AlterTransactionRuleHandler extends UpdatableRALBackendHandle
globalRules.removeIf(each -> each instanceof TransactionRule);
Map<String, ShardingSphereDatabase> databases = contextManager.getMetaDataContexts().getMetaData().getDatabases();
TransactionRule transactionRule = new TransactionRule(toBeAlteredRuleConfig, databases);
+ transactionRule.setInstanceContext(contextManager.getInstanceContext());
for (String each : transactionRule.getResources().keySet()) {
transactionRule.addResource(databases.get(each));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandlerTest.java
new file mode 100644
index 00000000000..c27e55dd581
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandlerTest.java
@@ -0,0 +1,151 @@
+/*
+ * 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.proxy.backend.text.distsql.ral.common.updatable;
+
+import com.atomikos.jdbc.AtomikosDataSourceBean;
+import org.apache.shardingsphere.distsql.parser.statement.ral.common.updatable.AlterTransactionRuleStatement;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
+import org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.metadata.persist.service.impl.GlobalRulePersistService;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
+import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
+import org.apache.shardingsphere.transaction.rule.TransactionRule;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class AlterTransactionRuleHandlerTest {
+ private ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+
+ @Before
+ public void before() {
+ GlobalRulePersistService globalRulePersistService = mock(GlobalRulePersistService.class, RETURNS_DEEP_STUBS);
+ MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS);
+ when(metaDataPersistService.getGlobalRuleService()).thenReturn(globalRulePersistService);
+ when(contextManager.getInstanceContext()).thenReturn(mock(InstanceContext.class, RETURNS_DEEP_STUBS));
+ ShardingSphereRuleMetaData shardingSphereRuleMetaData = mock(ShardingSphereRuleMetaData.class, RETURNS_DEEP_STUBS);
+ List<ShardingSphereRule> mockRules = new ArrayList<>();
+ mockRules.add(getLocalTransactionRule());
+ when(shardingSphereRuleMetaData.getRules()).thenReturn(mockRules);
+ MetaDataContexts metaDataContexts = new MetaDataContexts(
+ metaDataPersistService,
+ new ShardingSphereMetaData(
+ getDatabases(),
+ shardingSphereRuleMetaData,
+ new ConfigurationProperties(new Properties())),
+ mock(OptimizerContext.class, RETURNS_DEEP_STUBS)
+ );
+ when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
+ ProxyContext.init(contextManager);
+ }
+
+ @After
+ public void after() {
+ }
+
+ private TransactionRule getLocalTransactionRule() {
+ TransactionRule result = new TransactionRule(
+ new TransactionRuleConfiguration(
+ "LOCAL",
+ null,
+ new Properties()),
+ Collections.emptyMap());
+ result.setInstanceContext(mock(InstanceContext.class));
+ result.getResources().put(DefaultDatabase.LOGIC_NAME,
+ new ShardingSphereTransactionManagerEngine());
+ return result;
+ }
+
+ private Map<String, ShardingSphereDatabase> getDatabases() {
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ when(result.getResource().getDatabaseType()).thenReturn(new H2DatabaseType());
+ when(result.getRuleMetaData().getRules())
+ .thenReturn(Collections.emptyList());
+ when(result.getResource().getDataSources())
+ .thenReturn(Collections.singletonMap(DefaultDatabase.LOGIC_NAME,
+ mock(AtomikosDataSourceBean.class, RETURNS_DEEP_STUBS)));
+ return Collections.singletonMap("db", result);
+ }
+
+ @Test
+ public void assertUpdate() {
+ AlterTransactionRuleHandler alterTransactionRuleHandler = new AlterTransactionRuleHandler();
+ AlterTransactionRuleStatement ralStatement = mock(AlterTransactionRuleStatement.class, RETURNS_DEEP_STUBS);
+ when(ralStatement.getDefaultType()).thenReturn("LOCAL");
+ ConnectionSession connectionSession = mock(ConnectionSession.class, RETURNS_DEEP_STUBS);
+ alterTransactionRuleHandler.init(ralStatement, connectionSession);
+ alterTransactionRuleHandler.update(contextManager);
+ }
+
+ @Test
+ public void assertGetStatement() {
+ AlterTransactionRuleHandler alterTransactionRuleHandler = new AlterTransactionRuleHandler();
+ AlterTransactionRuleStatement ralStatement = mock(AlterTransactionRuleStatement.class, RETURNS_DEEP_STUBS);
+ when(ralStatement.getDefaultType()).thenReturn("LOCAL");
+ ConnectionSession connectionSession = mock(ConnectionSession.class, RETURNS_DEEP_STUBS);
+ alterTransactionRuleHandler.init(ralStatement, connectionSession);
+ AlterTransactionRuleStatement testAlterStatement = alterTransactionRuleHandler.getSqlStatement();
+ assertNotNull(testAlterStatement);
+ }
+
+ @Test
+ public void assertExecute() throws Exception {
+ AlterTransactionRuleHandler alterTransactionRuleHandler = new AlterTransactionRuleHandler();
+ AlterTransactionRuleStatement ralStatement = mock(AlterTransactionRuleStatement.class, RETURNS_DEEP_STUBS);
+ when(ralStatement.getDefaultType()).thenReturn("LOCAL");
+ ConnectionSession connectionSession = mock(ConnectionSession.class, RETURNS_DEEP_STUBS);
+ alterTransactionRuleHandler.init(ralStatement, connectionSession);
+ ResponseHeader testRespHeader = alterTransactionRuleHandler.execute();
+ assertNotNull(testRespHeader);
+ }
+
+ @Test
+ public void assertGetConnectionSession() {
+ AlterTransactionRuleHandler alterTransactionRuleHandler = new AlterTransactionRuleHandler();
+ AlterTransactionRuleStatement ralStatement = mock(AlterTransactionRuleStatement.class, RETURNS_DEEP_STUBS);
+ when(ralStatement.getDefaultType()).thenReturn("LOCAL");
+ ConnectionSession connectionSession = mock(ConnectionSession.class, RETURNS_DEEP_STUBS);
+ alterTransactionRuleHandler.init(ralStatement, connectionSession);
+ ConnectionSession session = alterTransactionRuleHandler.getConnectionSession();
+ assertNotNull(session);
+ }
+}