You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/05/24 06:47:44 UTC
[shardingsphere] branch master updated: Refactor SelectTableExecutorTest (#17897)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 56ea05c949d Refactor SelectTableExecutorTest (#17897)
56ea05c949d is described below
commit 56ea05c949d4313e489ba4955474fef76498293a
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue May 24 14:47:38 2022 +0800
Refactor SelectTableExecutorTest (#17897)
---
.../executor/SelectTableExecutorTest.java | 72 +++++++++++-----------
1 file changed, 36 insertions(+), 36 deletions(-)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/postgresql/executor/SelectTableExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/postgresql/executor/SelectTableExecutorTest.java
index c71635ff374..c6d49be8dbe 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/postgresql/executor/SelectTableExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/postgresql/executor/SelectTableExecutorTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.proxy.backend.text.admin.postgresql.executor;
-import com.zaxxer.hikari.pool.HikariProxyResultSet;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
import org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext;
@@ -36,10 +35,8 @@ import org.apache.shardingsphere.test.mock.MockedDataSource;
import org.junit.Before;
import org.junit.Test;
-import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
@@ -59,42 +56,23 @@ import static org.mockito.Mockito.when;
public final class SelectTableExecutorTest extends ProxyContextRestorer {
- private static final ResultSet RESULT_SET = mock(HikariProxyResultSet.class);
-
@Before
public void setUp() throws IllegalAccessException, NoSuchFieldException, SQLException {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new HashMap<>(), mock(ShardingSphereRuleMetaData.class),
- mock(OptimizerContext.class), new ConfigurationProperties(new Properties()));
+ MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class),
+ new HashMap<>(), mock(ShardingSphereRuleMetaData.class), mock(OptimizerContext.class), new ConfigurationProperties(new Properties()));
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
ProxyContext.init(contextManager);
}
- private ShardingSphereDatabase getDatabaseMetaData() throws SQLException {
- return new ShardingSphereDatabase("sharding_db", new PostgreSQLDatabaseType(), new ShardingSphereResource(mockDatasourceMap()),
- mock(ShardingSphereRuleMetaData.class), Collections.singletonMap("public", new ShardingSphereSchema(Collections.singletonMap("t_order", mock(TableMetaData.class)))));
- }
-
- private Map<String, DataSource> mockDatasourceMap() throws SQLException {
- Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
- when(connection.getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/foo_ds");
- when(connection.prepareStatement(any(String.class)).executeQuery()).thenReturn(RESULT_SET);
- return Collections.singletonMap("ds_0", new MockedDataSource(connection));
- }
-
@Test
public void assertSelectSchemataExecute() throws SQLException {
- final String sql = "SELECT c.oid, n.nspname AS schemaname, c.relname AS tablename from pg_tablespace";
- Map<String, String> mockResultSetMap = new LinkedHashMap<>();
- mockResultSetMap.put("tablename", "t_order_1");
- mockResultSetMap.put("c.oid", "0000");
- mockResultSetMap.put("schemaname", "public");
- mockResultSet(mockResultSetMap);
Map<String, ShardingSphereDatabase> databaseMap = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getDatabaseMap();
- databaseMap.put("public", getDatabaseMetaData());
+ databaseMap.put("public", createDatabase());
+ String sql = "SELECT c.oid, n.nspname AS schemaname, c.relname AS tablename from pg_tablespace";
SelectTableExecutor selectSchemataExecutor = new SelectTableExecutor(sql);
selectSchemataExecutor.execute(mock(ConnectionSession.class));
- assertThat(selectSchemataExecutor.getQueryResultMetaData().getColumnCount(), is(mockResultSetMap.size()));
+ assertThat(selectSchemataExecutor.getQueryResultMetaData().getColumnCount(), is(3));
int count = 0;
while (selectSchemataExecutor.getMergedResult().next()) {
count++;
@@ -108,16 +86,38 @@ public final class SelectTableExecutorTest extends ProxyContextRestorer {
assertThat(count, is(1));
}
- private void mockResultSet(final Map<String, String> mockMap) throws SQLException {
- ResultSetMetaData metaData = mock(ResultSetMetaData.class);
- List<String> keys = new ArrayList<>(mockMap.keySet());
+ private ShardingSphereDatabase createDatabase() throws SQLException {
+ return new ShardingSphereDatabase("sharding_db", new PostgreSQLDatabaseType(), new ShardingSphereResource(Collections.singletonMap("foo_ds", new MockedDataSource(mockConnection()))),
+ mock(ShardingSphereRuleMetaData.class), Collections.singletonMap("public", new ShardingSphereSchema(Collections.singletonMap("t_order", mock(TableMetaData.class)))));
+ }
+
+ private Connection mockConnection() throws SQLException {
+ Connection result = mock(Connection.class, RETURNS_DEEP_STUBS);
+ when(result.getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/foo_ds");
+ ResultSet resultSet = mockResultSet();
+ when(result.prepareStatement(any(String.class)).executeQuery()).thenReturn(resultSet);
+ return result;
+ }
+
+ private ResultSet mockResultSet() throws SQLException {
+ ResultSet result = mock(ResultSet.class, RETURNS_DEEP_STUBS);
+ Map<String, String> expectedResultSetMap = createExpectedResultSetMap();
+ List<String> keys = new ArrayList<>(expectedResultSetMap.keySet());
for (int i = 0; i < keys.size(); i++) {
- when(metaData.getColumnName(i + 1)).thenReturn(keys.get(i));
- when(metaData.getColumnLabel(i + 1)).thenReturn(keys.get(i));
- when(RESULT_SET.getString(i + 1)).thenReturn(mockMap.get(keys.get(i)));
+ when(result.getMetaData().getColumnName(i + 1)).thenReturn(keys.get(i));
+ when(result.getMetaData().getColumnLabel(i + 1)).thenReturn(keys.get(i));
+ when(result.getString(i + 1)).thenReturn(expectedResultSetMap.get(keys.get(i)));
}
- when(RESULT_SET.next()).thenReturn(true, false);
- when(metaData.getColumnCount()).thenReturn(mockMap.size());
- when(RESULT_SET.getMetaData()).thenReturn(metaData);
+ when(result.next()).thenReturn(true, false);
+ when(result.getMetaData().getColumnCount()).thenReturn(expectedResultSetMap.size());
+ return result;
+ }
+
+ private Map<String, String> createExpectedResultSetMap() {
+ Map<String, String> result = new LinkedHashMap<>();
+ result.put("tablename", "t_order_1");
+ result.put("c.oid", "0000");
+ result.put("schemaname", "public");
+ return result;
}
}