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 7e625a0c861 refactor ShowBroadcastTableRuleExecutorTest (#33106)
7e625a0c861 is described below

commit 7e625a0c86158a00f3859a847a457fe952a0a3e7
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Oct 3 16:49:12 2024 +0800

    refactor ShowBroadcastTableRuleExecutorTest (#33106)
---
 .../query/ShowBroadcastTableRuleExecutorTest.java  | 30 ++++-------
 .../query/ShowSQLTranslatorRuleExecutorTest.java   | 42 ++++++---------
 .../query/ShowTransactionRuleExecutorTest.java     | 59 ++++++++--------------
 3 files changed, 47 insertions(+), 84 deletions(-)

diff --git 
a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java
 
b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java
index 4c69c8b0fc8..c7d99639a6d 100644
--- 
a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java
+++ 
b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java
@@ -24,15 +24,14 @@ import 
org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext
 import 
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
 import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import java.sql.SQLException;
-import java.util.Collection;
+import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Iterator;
-import java.util.Optional;
+import java.util.List;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -42,11 +41,12 @@ import static org.mockito.Mockito.when;
 
 class ShowBroadcastTableRuleExecutorTest {
     
-    private DistSQLQueryExecuteEngine engine;
-    
-    @BeforeEach
-    void setUp() {
-        engine = new 
DistSQLQueryExecuteEngine(mock(ShowBroadcastTableRulesStatement.class), 
"foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
+    @Test
+    void assertGetRowData() throws SQLException {
+        DistSQLQueryExecuteEngine engine = new 
DistSQLQueryExecuteEngine(mock(ShowBroadcastTableRulesStatement.class), 
"foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
+        engine.executeQuery();
+        List<LocalDataQueryResultRow> actual = new 
ArrayList<>(engine.getRows());
+        assertThat(actual.get(0).getCell(1), is("t_address"));
     }
     
     private ContextManager mockContextManager() {
@@ -54,20 +54,10 @@ class ShowBroadcastTableRuleExecutorTest {
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
         when(result.getDatabase("foo_db")).thenReturn(database);
         BroadcastRule rule = mockBroadcastRule();
-        
when(database.getRuleMetaData().findSingleRule(BroadcastRule.class)).thenReturn(Optional.of(rule));
+        when(database.getRuleMetaData()).thenReturn(new 
RuleMetaData(Collections.singleton(rule)));
         return result;
     }
     
-    @Test
-    void assertGetRowData() throws SQLException {
-        engine.executeQuery();
-        Collection<LocalDataQueryResultRow> actual = engine.getRows();
-        assertThat(actual.size(), is(1));
-        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
-        LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is("t_address"));
-    }
-    
     private BroadcastRule mockBroadcastRule() {
         BroadcastRule result = mock(BroadcastRule.class);
         BroadcastRuleConfiguration config = 
mock(BroadcastRuleConfiguration.class);
diff --git 
a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/query/ShowSQLTranslatorRuleExecutorTest.java
 
b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/query/ShowSQLTranslatorRuleExecutorTest.java
index 86d12b60587..d4d2e2bb91a 100644
--- 
a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/query/ShowSQLTranslatorRuleExecutorTest.java
+++ 
b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/query/ShowSQLTranslatorRuleExecutorTest.java
@@ -20,17 +20,17 @@ package 
org.apache.shardingsphere.sqltranslator.distsql.handler.query;
 import 
org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext;
 import 
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
 import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import 
org.apache.shardingsphere.sqltranslator.config.SQLTranslatorRuleConfiguration;
 import 
org.apache.shardingsphere.sqltranslator.distsql.statement.queryable.ShowSQLTranslatorRuleStatement;
 import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import java.sql.SQLException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Optional;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -41,34 +41,22 @@ import static org.mockito.Mockito.when;
 
 class ShowSQLTranslatorRuleExecutorTest {
     
-    private DistSQLQueryExecuteEngine engine;
-    
-    @BeforeEach
-    void setUp() {
-        engine = new DistSQLQueryExecuteEngine(new 
ShowSQLTranslatorRuleStatement(), null, mockContextManager(), 
mock(DistSQLConnectionContext.class));
+    @Test
+    void assertExecute() throws SQLException {
+        DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new 
ShowSQLTranslatorRuleStatement(), null, mockContextManager(), 
mock(DistSQLConnectionContext.class));
+        engine.executeQuery();
+        List<LocalDataQueryResultRow> actual = new 
ArrayList<>(engine.getRows());
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get(0).getCell(1), is("NATIVE"));
+        assertThat(actual.get(0).getCell(2), is(""));
+        assertThat(actual.get(0).getCell(3), is("true"));
     }
     
     private ContextManager mockContextManager() {
         ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
         SQLTranslatorRule rule = mock(SQLTranslatorRule.class);
-        
when(rule.getConfiguration()).thenReturn(createSQLTranslatorRuleConfiguration());
-        
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(SQLTranslatorRule.class)).thenReturn(Optional.of(rule));
+        when(rule.getConfiguration()).thenReturn(new 
SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true));
+        
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new
 RuleMetaData(Collections.singleton(rule)));
         return result;
     }
-    
-    private SQLTranslatorRuleConfiguration 
createSQLTranslatorRuleConfiguration() {
-        return new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), 
true);
-    }
-    
-    @Test
-    void assertExecute() throws SQLException {
-        engine.executeQuery();
-        Collection<LocalDataQueryResultRow> actual = engine.getRows();
-        assertThat(actual.size(), is(1));
-        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
-        LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is("NATIVE"));
-        assertThat(row.getCell(2), is(""));
-        assertThat(row.getCell(3), is("true"));
-    }
 }
diff --git 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
index 9981ec1ebe0..a7d585a9b27 100644
--- 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
+++ 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
@@ -20,6 +20,7 @@ package 
org.apache.shardingsphere.transaction.distsql.handler.query;
 import 
org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext;
 import 
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
 import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
@@ -30,10 +31,9 @@ import 
org.apache.shardingsphere.transaction.rule.TransactionRule;
 import org.junit.jupiter.api.Test;
 
 import java.sql.SQLException;
-import java.util.Collection;
+import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Iterator;
-import java.util.Optional;
+import java.util.List;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -45,53 +45,38 @@ import static org.mockito.Mockito.when;
 
 class ShowTransactionRuleExecutorTest {
     
-    private DistSQLQueryExecuteEngine engine;
-    
-    private DistSQLQueryExecuteEngine setUp(final ContextManager 
contextManager) {
-        return new DistSQLQueryExecuteEngine(new 
ShowTransactionRuleStatement(), null, contextManager, 
mock(DistSQLConnectionContext.class));
-    }
-    
-    private ContextManager mockContextManager(final TransactionRule rule) {
-        ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
-        
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(TransactionRule.class)).thenReturn(Optional.of(rule));
-        return result;
-    }
-    
     @Test
-    void assertExecuteWithXA() throws SQLException {
-        TransactionRule rule = new 
TransactionRule(createTransactionRuleConfiguration(TransactionType.XA.name(), 
"Atomikos",
-                PropertiesBuilder.build(new Property("host", "127.0.0.1"), new 
Property("databaseName", "jbossts"))), Collections.emptyMap());
+    void assertExecuteQueryWithXA() throws SQLException {
+        TransactionRule rule = new TransactionRule(new 
TransactionRuleConfiguration(TransactionType.XA.name(),
+                "Atomikos", PropertiesBuilder.build(new Property("host", 
"127.0.0.1"), new Property("databaseName", "jbossts"))), 
Collections.emptyMap());
         ContextManager contextManager = mockContextManager(rule);
-        engine = setUp(contextManager);
+        DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new 
ShowTransactionRuleStatement(), null, contextManager, 
mock(DistSQLConnectionContext.class));
         engine.executeQuery();
-        Collection<LocalDataQueryResultRow> actual = engine.getRows();
+        List<LocalDataQueryResultRow> actual = new 
ArrayList<>(engine.getRows());
         assertThat(actual.size(), is(1));
-        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
-        LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is(TransactionType.XA.name()));
-        assertThat(row.getCell(2), is("Atomikos"));
-        String props = (String) row.getCell(3);
+        assertThat(actual.get(0).getCell(1), is(TransactionType.XA.name()));
+        assertThat(actual.get(0).getCell(2), is("Atomikos"));
+        String props = (String) actual.get(0).getCell(3);
         assertTrue(props.contains("\"databaseName\":\"jbossts\""));
         assertTrue(props.contains("\"host\":\"127.0.0.1\""));
     }
     
     @Test
-    void assertExecuteWithLocal() throws SQLException {
-        TransactionRule rule = new 
TransactionRule(createTransactionRuleConfiguration(TransactionType.LOCAL.name(),
-                null, new Properties()), Collections.emptyMap());
+    void assertExecuteQueryWithLocal() throws SQLException {
+        TransactionRule rule = new TransactionRule(new 
TransactionRuleConfiguration(TransactionType.LOCAL.name(), null, new 
Properties()), Collections.emptyMap());
         ContextManager contextManager = mockContextManager(rule);
-        engine = setUp(contextManager);
+        DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new 
ShowTransactionRuleStatement(), null, contextManager, 
mock(DistSQLConnectionContext.class));
         engine.executeQuery();
-        Collection<LocalDataQueryResultRow> actual = engine.getRows();
+        List<LocalDataQueryResultRow> actual = new 
ArrayList<>(engine.getRows());
         assertThat(actual.size(), is(1));
-        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
-        LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is(TransactionType.LOCAL.name()));
-        assertThat(row.getCell(2), is(""));
-        assertThat(row.getCell(3), is(""));
+        assertThat(actual.get(0).getCell(1), is(TransactionType.LOCAL.name()));
+        assertThat(actual.get(0).getCell(2), is(""));
+        assertThat(actual.get(0).getCell(3), is(""));
     }
     
-    private TransactionRuleConfiguration 
createTransactionRuleConfiguration(final String defaultType, final String 
providerType, final Properties props) {
-        return new TransactionRuleConfiguration(defaultType, providerType, 
props);
+    private ContextManager mockContextManager(final TransactionRule rule) {
+        ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+        
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new
 RuleMetaData(Collections.singleton(rule)));
+        return result;
     }
 }

Reply via email to