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

totalo 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 c4271b3  Fix tests failed in proxy-backend (#15241)
c4271b3 is described below

commit c4271b329ef77801e9fb680fc9f2fea4457f80a7
Author: 吴伟杰 <[email protected]>
AuthorDate: Fri Feb 4 22:12:11 2022 +0800

    Fix tests failed in proxy-backend (#15241)
---
 .../distsql/ral/SetVariableBackendHandlerTest.java | 24 +++++++++++++++-------
 .../ral/ShowVariableBackendHandlerTest.java        | 21 ++++++++++++++++++-
 2 files changed, 37 insertions(+), 8 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java
index 12da33f..eca5f83 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/SetVariableBackendHandlerTest.java
@@ -20,6 +20,7 @@ package 
org.apache.shardingsphere.proxy.backend.text.distsql.ral;
 import io.netty.util.DefaultAttributeMap;
 import 
org.apache.shardingsphere.distsql.parser.statement.ral.common.set.SetVariableStatement;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
 import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
 import 
org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext;
@@ -39,10 +40,10 @@ import 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.enums.Var
 import 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.exception.UnsupportedVariableException;
 import org.apache.shardingsphere.proxy.backend.util.SystemPropertyUtil;
 import org.apache.shardingsphere.transaction.core.TransactionType;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-import java.lang.reflect.Field;
 import java.sql.SQLException;
 import java.util.Collections;
 import java.util.HashMap;
@@ -60,17 +61,21 @@ public final class SetVariableBackendHandlerTest {
     
     private static final String SCHEMA_PATTERN = "schema_%s";
     
-    private final ConnectionSession connectionSession = new 
ConnectionSession(TransactionType.LOCAL, new DefaultAttributeMap());
+    private ContextManager contextManagerBefore;
+    
+    private ConnectionSession connectionSession;
     
     @Before
-    public void setUp() throws NoSuchFieldException, IllegalAccessException {
-        Field contextManagerField = 
ProxyContext.getInstance().getClass().getDeclaredField("contextManager");
-        contextManagerField.setAccessible(true);
+    public void setUp() {
+        Properties properties = new Properties();
+        
properties.setProperty(ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE.getKey(),
 ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE.getDefaultValue());
         MetaDataContexts metaDataContexts = new 
MetaDataContexts(mock(MetaDataPersistService.class), getMetaDataMap(), 
-                mock(ShardingSphereRuleMetaData.class), 
mock(ExecutorEngine.class), mock(OptimizerContext.class), new 
ConfigurationProperties(new Properties()));
+                mock(ShardingSphereRuleMetaData.class), 
mock(ExecutorEngine.class), mock(OptimizerContext.class), new 
ConfigurationProperties(properties));
+        contextManagerBefore = ProxyContext.getInstance().getContextManager();
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
+        ProxyContext.getInstance().init(contextManager);
         
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
-        contextManagerField.set(ProxyContext.getInstance(), contextManager);
+        connectionSession = new ConnectionSession(TransactionType.LOCAL, new 
DefaultAttributeMap());
     }
     
     private Map<String, ShardingSphereMetaData> getMetaDataMap() {
@@ -151,4 +156,9 @@ public final class SetVariableBackendHandlerTest {
         assertThat(actual, instanceOf(UpdateResponseHeader.class));
         
assertThat(SystemPropertyUtil.getSystemProperty(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
 Boolean.FALSE.toString()), is(Boolean.FALSE.toString()));
     }
+    
+    @After
+    public void tearDown() {
+        ProxyContext.getInstance().init(contextManagerBefore);
+    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java
index 12cc535..e193e6a 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/ShowVariableBackendHandlerTest.java
@@ -20,6 +20,7 @@ package 
org.apache.shardingsphere.proxy.backend.text.distsql.ral;
 import io.netty.util.DefaultAttributeMap;
 import 
org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowVariableStatement;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -30,6 +31,8 @@ import 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.ShowDistS
 import 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.enums.VariableEnum;
 import 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.exception.UnsupportedVariableException;
 import org.apache.shardingsphere.transaction.core.TransactionType;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.sql.SQLException;
@@ -39,12 +42,23 @@ import java.util.Properties;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public final class ShowVariableBackendHandlerTest {
     
-    private final ConnectionSession connectionSession = new 
ConnectionSession(TransactionType.LOCAL, new DefaultAttributeMap());
+    private ContextManager contextManagerBefore;
+    
+    private ConnectionSession connectionSession;
+    
+    @Before
+    public void setup() {
+        contextManagerBefore = ProxyContext.getInstance().getContextManager();
+        ProxyContext.getInstance().init(mock(ContextManager.class, 
RETURNS_DEEP_STUBS));
+        
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps().getValue(ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE)).thenReturn("JDBC");
+        connectionSession = new ConnectionSession(TransactionType.LOCAL, new 
DefaultAttributeMap());
+    }
     
     @Test
     public void assertShowTransactionType() throws SQLException {
@@ -107,4 +121,9 @@ public final class ShowVariableBackendHandlerTest {
         Collection<Object> rowData = backendHandler.getRowData();
         assertThat(rowData.iterator().next(), is("true"));
     }
+    
+    @After
+    public void tearDown() {
+        ProxyContext.getInstance().init(contextManagerBefore);
+    }
 }

Reply via email to