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 d4bcae15d45 Add more assertions for AlterSQLParserRuleExecutorTest
(#33105)
d4bcae15d45 is described below
commit d4bcae15d456bd0bf59b2644dd8d7dbc2258cb89
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Oct 3 16:14:46 2024 +0800
Add more assertions for AlterSQLParserRuleExecutorTest (#33105)
---
kernel/sql-parser/distsql/handler/pom.xml | 7 +++
.../query/ShowSQLParserRuleExecutorTest.java | 34 ++++++--------
.../update/AlterSQLParserRuleExecutorTest.java | 53 +++++++++++++++-------
3 files changed, 56 insertions(+), 38 deletions(-)
diff --git a/kernel/sql-parser/distsql/handler/pom.xml
b/kernel/sql-parser/distsql/handler/pom.xml
index 83cb9c8c066..5cbae99e690 100644
--- a/kernel/sql-parser/distsql/handler/pom.xml
+++ b/kernel/sql-parser/distsql/handler/pom.xml
@@ -47,5 +47,12 @@
<artifactId>shardingsphere-sql-parser-distsql-parser</artifactId>
<version>${project.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-test-util</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/query/ShowSQLParserRuleExecutorTest.java
b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/query/ShowSQLParserRuleExecutorTest.java
index 7d737c4e046..f5c4c027a41 100644
---
a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/query/ShowSQLParserRuleExecutorTest.java
+++
b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/query/ShowSQLParserRuleExecutorTest.java
@@ -20,18 +20,18 @@ package
org.apache.shardingsphere.parser.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.parser.config.SQLParserRuleConfiguration;
import
org.apache.shardingsphere.parser.distsql.statement.queryable.ShowSQLParserRuleStatement;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
-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 static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -41,29 +41,21 @@ import static org.mockito.Mockito.when;
class ShowSQLParserRuleExecutorTest {
- private DistSQLQueryExecuteEngine engine;
-
- @BeforeEach
- void setUp() {
- engine = new DistSQLQueryExecuteEngine(new
ShowSQLParserRuleStatement(), null, mockContextManager(),
mock(DistSQLConnectionContext.class));
+ @Test
+ void assertSQLParserRule() throws SQLException {
+ DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new
ShowSQLParserRuleStatement(), 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("initialCapacity: 128,
maximumSize: 1024"));
+ assertThat(actual.get(0).getCell(2), is("initialCapacity: 2000,
maximumSize: 65535"));
}
private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
SQLParserRule rule = mock(SQLParserRule.class);
when(rule.getConfiguration()).thenReturn(new
SQLParserRuleConfiguration(new CacheOption(128, 1024L), new CacheOption(2000,
65535L)));
-
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(SQLParserRule.class)).thenReturn(Optional.of(rule));
+
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
return result;
}
-
- @Test
- void assertSQLParserRule() 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("initialCapacity: 128, maximumSize:
1024"));
- assertThat(row.getCell(2), is("initialCapacity: 2000, maximumSize:
65535"));
- }
}
diff --git
a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java
b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java
index 5461cbdc529..4d63fc54d7f 100644
---
a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java
+++
b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java
@@ -18,52 +18,71 @@
package org.apache.shardingsphere.parser.distsql.handler.update;
import
org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine;
-import
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
+import
org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
import org.apache.shardingsphere.parser.distsql.segment.CacheOptionSegment;
import
org.apache.shardingsphere.parser.distsql.statement.updatable.AlterSQLParserRuleStatement;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import
org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
+import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentMatchers;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import java.sql.SQLException;
+import java.util.Collections;
+
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AlterSQLParserRuleExecutorTest {
- private DistSQLUpdateExecuteEngine engine;
-
@Test
- void assertExecute() {
+ void assertExecute() throws SQLException {
AlterSQLParserRuleStatement sqlStatement = new
AlterSQLParserRuleStatement(new CacheOptionSegment(64, 512L), new
CacheOptionSegment(1000, 1000L));
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null,
mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ ContextManager contextManager = mockContextManager();
+ new DistSQLUpdateExecuteEngine(sqlStatement, null,
contextManager).executeUpdate();
+ MetaDataManagerPersistService metaDataManagerPersistService =
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
+ verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(
+ ArgumentMatchers.<SQLParserRuleConfiguration>argThat(x ->
assertRuleConfiguration(x, new CacheOption(64, 512L), new CacheOption(1000,
1000L))));
}
@Test
- void assertExecuteWithNullStatement() {
+ void assertExecuteWithNullStatement() throws SQLException {
AlterSQLParserRuleStatement sqlStatement = new
AlterSQLParserRuleStatement(null, null);
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null,
mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ ContextManager contextManager = mockContextManager();
+ new DistSQLUpdateExecuteEngine(sqlStatement, null,
contextManager).executeUpdate();
+ MetaDataManagerPersistService metaDataManagerPersistService =
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
+ verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(
+ ArgumentMatchers.<SQLParserRuleConfiguration>argThat(x ->
assertRuleConfiguration(x, new CacheOption(128, 1024L), new CacheOption(2000,
65535L))));
}
@Test
- void assertExecuteWithNullCacheOptionSegment() {
+ void assertExecuteWithNullCacheOptionSegment() throws SQLException {
AlterSQLParserRuleStatement sqlStatement = new
AlterSQLParserRuleStatement(new CacheOptionSegment(null, null), new
CacheOptionSegment(null, null));
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null,
mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ ContextManager contextManager = mockContextManager();
+ new DistSQLUpdateExecuteEngine(sqlStatement, null,
contextManager).executeUpdate();
+ MetaDataManagerPersistService metaDataManagerPersistService =
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
+ verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(
+ ArgumentMatchers.<SQLParserRuleConfiguration>argThat(x ->
assertRuleConfiguration(x, new CacheOption(128, 1024L), new CacheOption(2000,
65535L))));
+ }
+
+ private boolean assertRuleConfiguration(final SQLParserRuleConfiguration
actual, final CacheOption expectedParseTreeCache, final CacheOption
expectedSQLStatementCache) {
+ assertThat(actual.getParseTreeCache(),
deepEqual(expectedParseTreeCache));
+ assertThat(actual.getSqlStatementCache(),
deepEqual(expectedSQLStatementCache));
+ return true;
}
- @SuppressWarnings({"rawtypes", "unchecked"})
private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
SQLParserRule rule = mock(SQLParserRule.class);
- GlobalRuleDefinitionExecutor executor =
mock(GlobalRuleDefinitionExecutor.class);
- when(executor.getRuleClass()).thenReturn(SQLParserRule.class);
when(rule.getConfiguration()).thenReturn(new
DefaultSQLParserRuleConfigurationBuilder().build());
-
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(executor.getRuleClass())).thenReturn(rule);
+
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
return result;
}
}