You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/05/01 07:38:11 UTC
[shardingsphere] branch master updated: Refactor AlterSQLParserRuleHandler and AlterTrafficRuleHandler (#17248)
This is an automated email from the ASF dual-hosted git repository.
zhonghongsheng 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 01a9c9e069d Refactor AlterSQLParserRuleHandler and AlterTrafficRuleHandler (#17248)
01a9c9e069d is described below
commit 01a9c9e069db5ab7a82d30b2cb06f2ace6053c90
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun May 1 15:38:03 2022 +0800
Refactor AlterSQLParserRuleHandler and AlterTrafficRuleHandler (#17248)
* Refactor AlterSQLParserRuleHandler
* Refactor AlterTrafficRuleHandler
* Refactor AlterTrafficRuleHandler
* Refactor TrafficRule
* Fix test cases
---
.../shardingsphere/traffic/rule/TrafficRule.java | 2 +-
.../updatable/AlterSQLParserRuleHandler.java | 49 ++++++++++------------
.../common/updatable/AlterTrafficRuleHandler.java | 21 ++++++----
.../updatable/AlterTransactionRuleHandler.java | 4 --
.../common/updatable/DropTrafficRuleHandler.java | 44 +++++++++----------
.../updatable/AlterSQLParserRuleHandlerTest.java | 48 ++++++++++-----------
6 files changed, 78 insertions(+), 90 deletions(-)
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
index 7a9435f519e..070d785bfe2 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
@@ -53,9 +53,9 @@ import java.util.Properties;
/**
* Traffic rule.
*/
+@Getter
public final class TrafficRule implements GlobalRule {
- @Getter
private final Collection<TrafficStrategyRule> strategyRules;
public TrafficRule(final TrafficRuleConfiguration config) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandler.java
index 49b935f26e8..7b15cae4fbb 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandler.java
@@ -21,7 +21,6 @@ import org.apache.shardingsphere.distsql.parser.segment.CacheOptionSegment;
import org.apache.shardingsphere.distsql.parser.statement.ral.common.updatable.AlterSQLParserRuleStatement;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
import org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
@@ -39,44 +38,38 @@ public final class AlterSQLParserRuleHandler extends UpdatableRALBackendHandler<
@Override
protected void update(final ContextManager contextManager, final AlterSQLParserRuleStatement sqlStatement) {
- updateSQLParserRule();
+ Optional<SQLParserRuleConfiguration> currentConfig = findCurrentConfiguration();
+ SQLParserRuleConfiguration toBeAlteredRuleConfig = createSQLParserRuleConfiguration(currentConfig.orElseGet(() -> new DefaultSQLParserRuleConfigurationBuilder().build()));
+ Collection<RuleConfiguration> globalRuleConfigs = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData().getConfigurations();
+ globalRuleConfigs.removeIf(each -> each instanceof SQLParserRuleConfiguration);
+ globalRuleConfigs.add(toBeAlteredRuleConfig);
+ persistNewRuleConfigurations(globalRuleConfigs);
}
- private void updateSQLParserRule() {
- MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
- Collection<RuleConfiguration> globalRuleConfigurations = metaDataContexts.getGlobalRuleMetaData().getConfigurations();
- RuleConfiguration currentRuleConfiguration = globalRuleConfigurations.stream().filter(configuration -> configuration instanceof SQLParserRuleConfiguration).findFirst().orElse(null);
- SQLParserRuleConfiguration toBeAlteredRuleConfiguration = getToBeAlteredRuleConfig(currentRuleConfiguration);
- globalRuleConfigurations.removeIf(configuration -> configuration instanceof SQLParserRuleConfiguration);
- globalRuleConfigurations.add(toBeAlteredRuleConfiguration);
- Optional<MetaDataPersistService> metaDataPersistService = metaDataContexts.getMetaDataPersistService();
- if (metaDataPersistService.isPresent() && null != metaDataPersistService.get().getGlobalRuleService()) {
- metaDataPersistService.get().getGlobalRuleService().persist(globalRuleConfigurations, true);
- }
- }
-
- private SQLParserRuleConfiguration getToBeAlteredRuleConfig(final RuleConfiguration currentRuleConfiguration) {
- if (null == currentRuleConfiguration) {
- return buildWithCurrentRuleConfiguration(new DefaultSQLParserRuleConfigurationBuilder().build());
- }
- return buildWithCurrentRuleConfiguration((SQLParserRuleConfiguration) currentRuleConfiguration);
+ private Optional<SQLParserRuleConfiguration> findCurrentConfiguration() {
+ return ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData().findRuleConfiguration(SQLParserRuleConfiguration.class).stream().findAny();
}
- private SQLParserRuleConfiguration buildWithCurrentRuleConfiguration(final SQLParserRuleConfiguration ruleConfiguration) {
+ private SQLParserRuleConfiguration createSQLParserRuleConfiguration(final SQLParserRuleConfiguration currentConfig) {
SQLParserRuleConfiguration result = new SQLParserRuleConfiguration();
- result.setSqlCommentParseEnabled(null == sqlStatement.getSqlCommentParseEnable() ? ruleConfiguration.isSqlCommentParseEnabled()
- : sqlStatement.getSqlCommentParseEnable());
- result.setParseTreeCache(null == sqlStatement.getParseTreeCache() ? ruleConfiguration.getParseTreeCache()
- : buildCacheOption(ruleConfiguration.getParseTreeCache(), sqlStatement.getParseTreeCache()));
- result.setSqlStatementCache(null == sqlStatement.getSqlStatementCache() ? ruleConfiguration.getSqlStatementCache()
- : buildCacheOption(ruleConfiguration.getSqlStatementCache(), sqlStatement.getSqlStatementCache()));
+ result.setSqlCommentParseEnabled(null == sqlStatement.getSqlCommentParseEnable() ? currentConfig.isSqlCommentParseEnabled() : sqlStatement.getSqlCommentParseEnable());
+ result.setParseTreeCache(null == sqlStatement.getParseTreeCache() ? currentConfig.getParseTreeCache() : createCacheOption(currentConfig.getParseTreeCache(), sqlStatement.getParseTreeCache()));
+ result.setSqlStatementCache(null == sqlStatement.getSqlStatementCache() ? currentConfig.getSqlStatementCache()
+ : createCacheOption(currentConfig.getSqlStatementCache(), sqlStatement.getSqlStatementCache()));
return result;
}
- private CacheOption buildCacheOption(final CacheOption cacheOption, final CacheOptionSegment segment) {
+ private CacheOption createCacheOption(final CacheOption cacheOption, final CacheOptionSegment segment) {
int initialCapacity = null == segment.getInitialCapacity() ? cacheOption.getInitialCapacity() : segment.getInitialCapacity();
long maximumSize = null == segment.getMaximumSize() ? cacheOption.getMaximumSize() : segment.getMaximumSize();
int concurrencyLevel = null == segment.getConcurrencyLevel() ? cacheOption.getConcurrencyLevel() : segment.getConcurrencyLevel();
return new CacheOption(initialCapacity, maximumSize, concurrencyLevel);
}
+
+ private void persistNewRuleConfigurations(final Collection<RuleConfiguration> globalRuleConfigs) {
+ Optional<MetaDataPersistService> metaDataPersistService = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaDataPersistService();
+ if (metaDataPersistService.isPresent() && null != metaDataPersistService.get().getGlobalRuleService()) {
+ metaDataPersistService.get().getGlobalRuleService().persist(globalRuleConfigs, true);
+ }
+ }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandler.java
index d9dfc4406f4..d1e3775e626 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandler.java
@@ -46,12 +46,15 @@ public final class AlterTrafficRuleHandler extends UpdatableRALBackendHandler<Al
@Override
protected void update(final ContextManager contextManager, final AlterTrafficRuleStatement sqlStatement) throws DistSQLException {
- Optional<TrafficRuleConfiguration> currentConfig = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData()
- .findRuleConfiguration(TrafficRuleConfiguration.class).stream().findAny();
+ Optional<TrafficRuleConfiguration> currentConfig = findCurrentConfiguration();
DistSQLException.predictionThrow(currentConfig.isPresent(), () -> new RequiredRuleMissedException("Traffic"));
check(sqlStatement, currentConfig.get());
TrafficRuleConfiguration toBeAlteredConfig = TrafficRuleConverter.convert(sqlStatement.getSegments());
- updateToRepository(toBeAlteredConfig, currentConfig.get());
+ persistNewRuleConfigurations(toBeAlteredConfig, currentConfig.get());
+ }
+
+ private Optional<TrafficRuleConfiguration> findCurrentConfiguration() {
+ return ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData().findRuleConfiguration(TrafficRuleConfiguration.class).stream().findAny();
}
private void check(final AlterTrafficRuleStatement sqlStatement, final TrafficRuleConfiguration currentConfig) throws DistSQLException {
@@ -64,18 +67,18 @@ public final class AlterTrafficRuleHandler extends UpdatableRALBackendHandler<Al
private Collection<String> getInvalidAlgorithmNames() {
Collection<String> result = new LinkedList<>();
- sqlStatement.getSegments().forEach(each -> {
+ for (TrafficRuleSegment each : sqlStatement.getSegments()) {
if (!TrafficAlgorithmFactory.contains(each.getAlgorithm().getName())) {
result.add(each.getAlgorithm().getName());
}
if (null != each.getLoadBalancer() && !TrafficLoadBalanceAlgorithmFactory.contains(each.getLoadBalancer().getName())) {
result.add(each.getLoadBalancer().getName());
}
- });
+ }
return result;
}
- private void updateToRepository(final TrafficRuleConfiguration toBeAlteredConfig, final TrafficRuleConfiguration currentConfig) {
+ private void persistNewRuleConfigurations(final TrafficRuleConfiguration toBeAlteredConfig, final TrafficRuleConfiguration currentConfig) {
Collection<String> toBeAlteredConfigNames = toBeAlteredConfig.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getName).collect(Collectors.toSet());
currentConfig.getTrafficStrategies().removeIf(each -> toBeAlteredConfigNames.contains(each.getName()));
currentConfig.getTrafficStrategies().addAll(toBeAlteredConfig.getTrafficStrategies());
@@ -87,8 +90,8 @@ public final class AlterTrafficRuleHandler extends UpdatableRALBackendHandler<Al
metaDataPersistService.ifPresent(optional -> optional.getGlobalRuleService().persist(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), true));
}
- private Collection<String> getUnusedLoadBalancer(final TrafficRuleConfiguration config) {
- Collection<String> currentlyInUse = config.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getLoadBalancerName).collect(Collectors.toSet());
- return config.getLoadBalancers().keySet().stream().filter(each -> !currentlyInUse.contains(each)).collect(Collectors.toSet());
+ private Collection<String> getUnusedLoadBalancer(final TrafficRuleConfiguration currentConfig) {
+ Collection<String> currentlyInUse = currentConfig.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getLoadBalancerName).collect(Collectors.toSet());
+ return currentConfig.getLoadBalancers().keySet().stream().filter(each -> !currentlyInUse.contains(each)).collect(Collectors.toSet());
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
index aa4aa0cf963..cd769eb0b0f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
@@ -40,10 +40,6 @@ public final class AlterTransactionRuleHandler extends UpdatableRALBackendHandle
@Override
protected void update(final ContextManager contextManager, final AlterTransactionRuleStatement sqlStatement) {
- updateTransactionRule();
- }
-
- private void updateTransactionRule() {
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
ShardingSphereRuleMetaData globalRuleMetaData = metaDataContexts.getGlobalRuleMetaData();
Collection<ShardingSphereRule> globalRules = globalRuleMetaData.getRules();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/DropTrafficRuleHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/DropTrafficRuleHandler.java
index 9803a26a3f5..63ca636beca 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/DropTrafficRuleHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/DropTrafficRuleHandler.java
@@ -40,40 +40,40 @@ public final class DropTrafficRuleHandler extends UpdatableRALBackendHandler<Dro
@Override
protected void update(final ContextManager contextManager, final DropTrafficRuleStatement sqlStatement) throws DistSQLException {
- Optional<TrafficRuleConfiguration> configuration = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData()
+ Optional<TrafficRuleConfiguration> config = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData()
.findRuleConfiguration(TrafficRuleConfiguration.class).stream().findAny();
- check(sqlStatement, configuration);
- if (configuration.isPresent()) {
- configuration.get().getTrafficStrategies().removeIf(each -> sqlStatement.getRuleNames().contains(each.getName()));
- getUnusedAlgorithm(configuration.get()).forEach(each -> configuration.get().getTrafficAlgorithms().remove(each));
- getUnusedLoadBalancer(configuration.get()).forEach(each -> configuration.get().getLoadBalancers().remove(each));
- updateToRepository(configuration.get());
- }
- }
-
- private void check(final DropTrafficRuleStatement sqlStatement, final Optional<TrafficRuleConfiguration> config) throws DistSQLException {
if (!sqlStatement.isContainsIfExistClause()) {
DistSQLException.predictionThrow(config.isPresent(), () -> new RequiredRuleMissedException("Traffic"));
- Set<String> currentTrafficStrategyNames = config.get().getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getName).collect(Collectors.toSet());
- Set<String> notExistRuleNames = sqlStatement.getRuleNames().stream().filter(each -> !currentTrafficStrategyNames.contains(each)).collect(Collectors.toSet());
- DistSQLException.predictionThrow(notExistRuleNames.isEmpty(), () -> new RequiredRuleMissedException("Traffic"));
+ checkTrafficRuleConfiguration(sqlStatement, config.get());
}
+ if (config.isPresent()) {
+ config.get().getTrafficStrategies().removeIf(each -> sqlStatement.getRuleNames().contains(each.getName()));
+ getUnusedAlgorithm(config.get()).forEach(each -> config.get().getTrafficAlgorithms().remove(each));
+ getUnusedLoadBalancer(config.get()).forEach(each -> config.get().getLoadBalancers().remove(each));
+ updateToRepository(config.get());
+ }
+ }
+
+ private void checkTrafficRuleConfiguration(final DropTrafficRuleStatement sqlStatement, final TrafficRuleConfiguration config) throws DistSQLException {
+ Set<String> currentTrafficStrategyNames = config.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getName).collect(Collectors.toSet());
+ Set<String> notExistRuleNames = sqlStatement.getRuleNames().stream().filter(each -> !currentTrafficStrategyNames.contains(each)).collect(Collectors.toSet());
+ DistSQLException.predictionThrow(notExistRuleNames.isEmpty(), () -> new RequiredRuleMissedException("Traffic"));
}
- private Collection<String> getUnusedAlgorithm(final TrafficRuleConfiguration configuration) {
- Collection<String> currentlyInUse = configuration.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getAlgorithmName).collect(Collectors.toSet());
- return configuration.getTrafficAlgorithms().keySet().stream().filter(each -> !currentlyInUse.contains(each)).collect(Collectors.toSet());
+ private Collection<String> getUnusedAlgorithm(final TrafficRuleConfiguration config) {
+ Collection<String> currentlyInUse = config.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getAlgorithmName).collect(Collectors.toSet());
+ return config.getTrafficAlgorithms().keySet().stream().filter(each -> !currentlyInUse.contains(each)).collect(Collectors.toSet());
}
- private Collection<String> getUnusedLoadBalancer(final TrafficRuleConfiguration configuration) {
- Collection<String> currentlyInUse = configuration.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getLoadBalancerName).collect(Collectors.toSet());
- return configuration.getLoadBalancers().keySet().stream().filter(each -> !currentlyInUse.contains(each)).collect(Collectors.toSet());
+ private Collection<String> getUnusedLoadBalancer(final TrafficRuleConfiguration config) {
+ Collection<String> currentlyInUse = config.getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getLoadBalancerName).collect(Collectors.toSet());
+ return config.getLoadBalancers().keySet().stream().filter(each -> !currentlyInUse.contains(each)).collect(Collectors.toSet());
}
- private void updateToRepository(final TrafficRuleConfiguration currentConfiguration) {
+ private void updateToRepository(final TrafficRuleConfiguration config) {
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
Optional<MetaDataPersistService> metaDataPersistService = metaDataContexts.getMetaDataPersistService();
- getUnusedLoadBalancer(currentConfiguration).forEach(each -> currentConfiguration.getLoadBalancers().remove(each));
+ getUnusedLoadBalancer(config).forEach(each -> config.getLoadBalancers().remove(each));
metaDataPersistService.ifPresent(optional -> optional.getGlobalRuleService().persist(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), true));
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandlerTest.java
index 6004205242c..cbbfc7ce0c0 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterSQLParserRuleHandlerTest.java
@@ -30,10 +30,10 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.sql.SQLException;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -46,41 +46,37 @@ public final class AlterSQLParserRuleHandlerTest {
@Test
public void assertExecuteWithoutCurrentRuleConfiguration() throws SQLException {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+ when(contextManager.getMetaDataContexts().getGlobalRuleMetaData().findRuleConfiguration(SQLParserRuleConfiguration.class)).thenReturn(Collections.emptyList());
when(contextManager.getMetaDataContexts().getGlobalRuleMetaData().getConfigurations()).thenReturn(new LinkedList<>());
ProxyContext.getInstance().init(contextManager);
new AlterSQLParserRuleHandler().initStatement(getSQLStatement()).execute();
- Collection<RuleConfiguration> globalRuleConfigurations = contextManager.getMetaDataContexts().getGlobalRuleMetaData().getConfigurations();
- RuleConfiguration ruleConfiguration = globalRuleConfigurations.stream().filter(configuration -> configuration instanceof SQLParserRuleConfiguration).findAny().orElse(null);
- assertNotNull(ruleConfiguration);
- SQLParserRuleConfiguration sqlParserRuleConfiguration = (SQLParserRuleConfiguration) ruleConfiguration;
- assertTrue(sqlParserRuleConfiguration.isSqlCommentParseEnabled());
- assertThat(sqlParserRuleConfiguration.getSqlStatementCache().getInitialCapacity(), is(1000));
- assertThat(sqlParserRuleConfiguration.getSqlStatementCache().getMaximumSize(), is(1000L));
- assertThat(sqlParserRuleConfiguration.getSqlStatementCache().getConcurrencyLevel(), is(3));
- assertThat(sqlParserRuleConfiguration.getParseTreeCache().getInitialCapacity(), is(64));
- assertThat(sqlParserRuleConfiguration.getParseTreeCache().getMaximumSize(), is(512L));
- assertThat(sqlParserRuleConfiguration.getParseTreeCache().getConcurrencyLevel(), is(3));
+ SQLParserRuleConfiguration actual = (SQLParserRuleConfiguration) contextManager.getMetaDataContexts().getGlobalRuleMetaData().getConfigurations().iterator().next();
+ assertTrue(actual.isSqlCommentParseEnabled());
+ assertThat(actual.getSqlStatementCache().getInitialCapacity(), is(1000));
+ assertThat(actual.getSqlStatementCache().getMaximumSize(), is(1000L));
+ assertThat(actual.getSqlStatementCache().getConcurrencyLevel(), is(3));
+ assertThat(actual.getParseTreeCache().getInitialCapacity(), is(64));
+ assertThat(actual.getParseTreeCache().getMaximumSize(), is(512L));
+ assertThat(actual.getParseTreeCache().getConcurrencyLevel(), is(3));
}
@Test
public void assertExecuteWithDefaultRuleConfiguration() throws SQLException {
- Collection<RuleConfiguration> globalRuleConfiguration = new LinkedList<>();
- globalRuleConfiguration.add(new DefaultSQLParserRuleConfigurationBuilder().build());
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- when(contextManager.getMetaDataContexts().getGlobalRuleMetaData().getConfigurations()).thenReturn(globalRuleConfiguration);
+ SQLParserRuleConfiguration sqlParserRuleConfig = new DefaultSQLParserRuleConfigurationBuilder().build();
+ Collection<RuleConfiguration> globalRuleConfigs = new LinkedList<>(Collections.singleton(sqlParserRuleConfig));
+ when(contextManager.getMetaDataContexts().getGlobalRuleMetaData().findRuleConfiguration(SQLParserRuleConfiguration.class)).thenReturn(Collections.singleton(sqlParserRuleConfig));
+ when(contextManager.getMetaDataContexts().getGlobalRuleMetaData().getConfigurations()).thenReturn(globalRuleConfigs);
ProxyContext.getInstance().init(contextManager);
new AlterSQLParserRuleHandler().initStatement(getSQLStatement()).execute();
- Collection<RuleConfiguration> globalRuleConfigurations = contextManager.getMetaDataContexts().getGlobalRuleMetaData().getConfigurations();
- RuleConfiguration ruleConfiguration = globalRuleConfigurations.stream().filter(configuration -> configuration instanceof SQLParserRuleConfiguration).findAny().orElse(null);
- assertNotNull(ruleConfiguration);
- SQLParserRuleConfiguration sqlParserRuleConfiguration = (SQLParserRuleConfiguration) ruleConfiguration;
- assertTrue(sqlParserRuleConfiguration.isSqlCommentParseEnabled());
- assertThat(sqlParserRuleConfiguration.getSqlStatementCache().getInitialCapacity(), is(1000));
- assertThat(sqlParserRuleConfiguration.getSqlStatementCache().getMaximumSize(), is(1000L));
- assertThat(sqlParserRuleConfiguration.getSqlStatementCache().getConcurrencyLevel(), is(3));
- assertThat(sqlParserRuleConfiguration.getParseTreeCache().getInitialCapacity(), is(64));
- assertThat(sqlParserRuleConfiguration.getParseTreeCache().getMaximumSize(), is(512L));
- assertThat(sqlParserRuleConfiguration.getParseTreeCache().getConcurrencyLevel(), is(3));
+ SQLParserRuleConfiguration actual = (SQLParserRuleConfiguration) contextManager.getMetaDataContexts().getGlobalRuleMetaData().getConfigurations().iterator().next();
+ assertTrue(actual.isSqlCommentParseEnabled());
+ assertThat(actual.getSqlStatementCache().getInitialCapacity(), is(1000));
+ assertThat(actual.getSqlStatementCache().getMaximumSize(), is(1000L));
+ assertThat(actual.getSqlStatementCache().getConcurrencyLevel(), is(3));
+ assertThat(actual.getParseTreeCache().getInitialCapacity(), is(64));
+ assertThat(actual.getParseTreeCache().getMaximumSize(), is(512L));
+ assertThat(actual.getParseTreeCache().getConcurrencyLevel(), is(3));
}
private AlterSQLParserRuleStatement getSQLStatement() {