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

zhaojinchao 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 6aa35a2d4c2 Refactor ShowAllVariableHandler and related test case 
(#19118)
6aa35a2d4c2 is described below

commit 6aa35a2d4c24febe63c3b7a8dccdffee4455e668
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Jul 13 23:11:55 2022 +0800

    Refactor ShowAllVariableHandler and related test case (#19118)
---
 .../ral/queryable/ShowAllVariableHandler.java      | 14 ++------
 ...erTest.java => ShowAllVariableHandlerTest.java} | 41 +++++++++++-----------
 2 files changed, 24 insertions(+), 31 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableHandler.java
index 9ee040f5719..5a611444679 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableHandler.java
@@ -29,8 +29,7 @@ import 
org.apache.shardingsphere.proxy.backend.util.SystemPropertyUtil;
 
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * Show all variable handler.
@@ -48,16 +47,9 @@ public final class ShowAllVariableHandler extends 
QueryableRALBackendHandler<Sho
     
     @Override
     protected Collection<LocalDataQueryResultRow> getRows(final ContextManager 
contextManager) {
-        return buildAllVariableRows(contextManager);
-    }
-    
-    private Collection<LocalDataQueryResultRow> buildAllVariableRows(final 
ContextManager contextManager) {
-        List<LocalDataQueryResultRow> result = new LinkedList<>();
         ConfigurationProperties props = 
contextManager.getMetaDataContexts().getMetaData().getProps();
-        ConfigurationPropertyKey.getKeyNames().forEach(each -> {
-            String propertyValue = 
props.getValue(ConfigurationPropertyKey.valueOf(each)).toString();
-            result.add(new LocalDataQueryResultRow(each.toLowerCase(), 
propertyValue));
-        });
+        Collection<LocalDataQueryResultRow> result = 
ConfigurationPropertyKey.getKeyNames().stream()
+                .map(each -> new LocalDataQueryResultRow(each.toLowerCase(), 
props.getValue(ConfigurationPropertyKey.valueOf(each)).toString())).collect(Collectors.toList());
         result.add(new LocalDataQueryResultRow(
                 VariableEnum.AGENT_PLUGINS_ENABLED.name().toLowerCase(), 
SystemPropertyUtil.getSystemProperty(VariableEnum.AGENT_PLUGINS_ENABLED.name(), 
Boolean.TRUE.toString())));
         if (getConnectionSession().getBackendConnection() instanceof 
JDBCBackendConnection) {
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableHandlerTest.java
similarity index 67%
rename from 
shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableBackendHandlerTest.java
rename to 
shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableHandlerTest.java
index e77826561ce..c5a05531fbc 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ShowAllVariableHandlerTest.java
@@ -37,39 +37,40 @@ import java.sql.SQLException;
 import java.util.List;
 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 ShowAllVariableBackendHandlerTest extends 
ProxyContextRestorer {
-    
-    private ConnectionSession connectionSession;
+public final class ShowAllVariableHandlerTest extends ProxyContextRestorer {
     
     @Before
     public void setup() {
-        ProxyContext.init(mock(ContextManager.class, RETURNS_DEEP_STUBS));
-        
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE)).thenReturn("JDBC");
-        connectionSession = new 
ConnectionSession(mock(MySQLDatabaseType.class), TransactionType.LOCAL, new 
DefaultAttributeMap());
+        ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
+        MetaDataContexts metaDataContexts = mockMetaDataContexts();
+        
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
+        ProxyContext.init(contextManager);
+    }
+    
+    private MetaDataContexts mockMetaDataContexts() {
+        MetaDataContexts result = mock(MetaDataContexts.class, 
RETURNS_DEEP_STUBS);
+        Properties props = new Properties();
+        
props.setProperty(ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE.getKey(), 
"JDBC");
+        when(result.getMetaData().getProps()).thenReturn(new 
ConfigurationProperties(props));
+        return result;
     }
     
     @Test
-    public void assertShowAllVariables() throws SQLException {
-        connectionSession.setCurrentDatabase("db");
-        ContextManager contextManager = mock(ContextManager.class);
-        ProxyContext.init(contextManager);
-        MetaDataContexts metaDataContexts = mock(MetaDataContexts.class, 
RETURNS_DEEP_STUBS);
-        
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
-        when(metaDataContexts.getMetaData().getProps()).thenReturn(new 
ConfigurationProperties(new Properties()));
-        ShowAllVariableHandler backendHandler = new ShowAllVariableHandler();
-        backendHandler.init(new ShowAllVariableStatement(), connectionSession);
-        ResponseHeader actual = backendHandler.execute();
-        assertThat(actual, instanceOf(QueryResponseHeader.class));
+    public void assertExecute() throws SQLException {
+        ConnectionSession connectionSession = new 
ConnectionSession(mock(MySQLDatabaseType.class), TransactionType.LOCAL, new 
DefaultAttributeMap());
+        connectionSession.setCurrentDatabase("foo_db");
+        ShowAllVariableHandler handler = new ShowAllVariableHandler();
+        handler.init(new ShowAllVariableStatement(), connectionSession);
+        ResponseHeader actual = handler.execute();
         assertThat(((QueryResponseHeader) actual).getQueryHeaders().size(), 
is(2));
-        backendHandler.next();
-        List<Object> rowData = backendHandler.getRowData().getData();
+        handler.next();
+        List<Object> rowData = handler.getRowData().getData();
         assertThat(rowData.get(0), is("sql_show"));
         assertThat(rowData.get(1), is(Boolean.FALSE.toString()));
     }

Reply via email to