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 2024/09/26 16:53:00 UTC
(shardingsphere) branch master updated: Add test cases on LoggingUtils (#33017)
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 66657f4313e Add test cases on LoggingUtils (#33017)
66657f4313e is described below
commit 66657f4313eacb08681ff066e40365669a34db79
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Sep 27 00:52:53 2024 +0800
Add test cases on LoggingUtils (#33017)
---
.../logging/rule/builder/LoggingRuleBuilder.java | 2 +-
.../shardingsphere/logging/util/LoggingUtils.java | 32 +++-----
.../logging/util/LoggingUtilsTest.java | 89 ++++++++++++++++++++++
3 files changed, 102 insertions(+), 21 deletions(-)
diff --git a/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/rule/builder/LoggingRuleBuilder.java b/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/rule/builder/LoggingRuleBuilder.java
index 210be34c71d..90ad7422abf 100644
--- a/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/rule/builder/LoggingRuleBuilder.java
+++ b/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/rule/builder/LoggingRuleBuilder.java
@@ -34,7 +34,7 @@ public final class LoggingRuleBuilder implements GlobalRuleBuilder<LoggingRuleCo
@Override
public LoggingRule build(final LoggingRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
- LoggingUtils.syncLoggingConfig(ruleConfig, props);
+ LoggingUtils.syncLoggingRuleConfiguration(ruleConfig, props);
return new LoggingRule(ruleConfig);
}
diff --git a/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/util/LoggingUtils.java b/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/util/LoggingUtils.java
index 440283a7bc1..a18362f22ec 100644
--- a/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/util/LoggingUtils.java
+++ b/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/util/LoggingUtils.java
@@ -36,42 +36,34 @@ import java.util.Properties;
public final class LoggingUtils {
/**
- * Get ShardingSphere-SQL logger.
+ * Get ShardingSphere logger.
*
- * @param globalRuleMetaData ShardingSphere global rule metaData
- * @return ShardingSphere-SQL logger
+ * @param globalRuleMetaData ShardingSphere rule metaData
+ * @return ShardingSphere logger
*/
public static Optional<ShardingSphereLogger> getSQLLogger(final RuleMetaData globalRuleMetaData) {
return globalRuleMetaData.findSingleRule(LoggingRule.class).isPresent() ? getSQLLogger(globalRuleMetaData.getSingleRule(LoggingRule.class).getConfiguration()) : Optional.empty();
}
- /**
- * Get ShardingSphere-SQL logger.
- *
- * @param loggingRuleConfig logging global rule configuration
- * @return ShardingSphere-SQL logger
- */
- public static Optional<ShardingSphereLogger> getSQLLogger(final LoggingRuleConfiguration loggingRuleConfig) {
- return loggingRuleConfig.getLoggers().stream()
- .filter(each -> LoggingConstants.SQL_LOG_TOPIC.equalsIgnoreCase(each.getLoggerName())).findFirst();
+ private static Optional<ShardingSphereLogger> getSQLLogger(final LoggingRuleConfiguration ruleConfig) {
+ return ruleConfig.getLoggers().stream().filter(each -> LoggingConstants.SQL_LOG_TOPIC.equalsIgnoreCase(each.getLoggerName())).findFirst();
}
/**
* Synchronize the log-related configuration in logging rule and props.
* Use the configuration in the logging rule first.
*
- * @param loggingRuleConfig logging global rule configuration
+ * @param ruleConfig logging rule configuration
* @param props configuration properties
*/
- public static void syncLoggingConfig(final LoggingRuleConfiguration loggingRuleConfig, final ConfigurationProperties props) {
- getSQLLogger(loggingRuleConfig).ifPresent(option -> {
- Properties loggerProps = option.getProps();
- syncPropsToLoggingRule(loggerProps, props);
- syncLoggingRuleToProps(loggerProps, props);
+ public static void syncLoggingRuleConfiguration(final LoggingRuleConfiguration ruleConfig, final ConfigurationProperties props) {
+ getSQLLogger(ruleConfig).ifPresent(optional -> {
+ syncPropertiesToRule(optional.getProps(), props);
+ syncRuleToProperties(optional.getProps(), props);
});
}
- private static void syncPropsToLoggingRule(final Properties loggerProps, final ConfigurationProperties props) {
+ private static void syncPropertiesToRule(final Properties loggerProps, final ConfigurationProperties props) {
if (!loggerProps.containsKey(LoggingConstants.SQL_LOG_ENABLE) && props.getProps().containsKey(LoggingConstants.SQL_SHOW)) {
loggerProps.setProperty(LoggingConstants.SQL_LOG_ENABLE, props.getProps().get(LoggingConstants.SQL_SHOW).toString());
}
@@ -80,7 +72,7 @@ public final class LoggingUtils {
}
}
- private static void syncLoggingRuleToProps(final Properties loggerProps, final ConfigurationProperties props) {
+ private static void syncRuleToProperties(final Properties loggerProps, final ConfigurationProperties props) {
if (loggerProps.containsKey(LoggingConstants.SQL_LOG_ENABLE)) {
props.getProps().setProperty(LoggingConstants.SQL_SHOW, loggerProps.get(LoggingConstants.SQL_LOG_ENABLE).toString());
}
diff --git a/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/util/LoggingUtilsTest.java b/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/util/LoggingUtilsTest.java
new file mode 100644
index 00000000000..f4faf68b65c
--- /dev/null
+++ b/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/util/LoggingUtilsTest.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.logging.util;
+
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.logging.config.LoggingRuleConfiguration;
+import org.apache.shardingsphere.logging.constant.LoggingConstants;
+import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
+import org.apache.shardingsphere.logging.rule.LoggingRule;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class LoggingUtilsTest {
+
+ @Test
+ void assertGetSQLLoggerWhenLoggingRuleAbsent() {
+ assertFalse(LoggingUtils.getSQLLogger(mock(RuleMetaData.class)).isPresent());
+ }
+
+ @Test
+ void assertGetSQLLoggerWhenLoggingRulePresent() {
+ RuleMetaData ruleMetaData = mock(RuleMetaData.class);
+ LoggingRule loggingRule = new LoggingRule(new LoggingRuleConfiguration(
+ Collections.singleton(new ShardingSphereLogger(LoggingConstants.SQL_LOG_TOPIC, null, false, null)), Collections.emptyList()));
+ when(ruleMetaData.findSingleRule(LoggingRule.class)).thenReturn(Optional.of(loggingRule));
+ when(ruleMetaData.getSingleRule(LoggingRule.class)).thenReturn(loggingRule);
+ assertTrue(LoggingUtils.getSQLLogger(ruleMetaData).isPresent());
+ }
+
+ @Test
+ void assertSyncLoggingRuleConfigurationWhenSyncPropertiesToRule() {
+ ShardingSphereLogger logger = new ShardingSphereLogger(LoggingConstants.SQL_LOG_TOPIC, null, false, null);
+ ConfigurationProperties props = new ConfigurationProperties(PropertiesBuilder.build(
+ new Property(LoggingConstants.SQL_SHOW, Boolean.TRUE.toString()), new Property(LoggingConstants.SQL_SIMPLE, Boolean.TRUE.toString())));
+ LoggingUtils.syncLoggingRuleConfiguration(new LoggingRuleConfiguration(Collections.singleton(logger), Collections.emptyList()), props);
+ assertThat(logger.getProps().getProperty(LoggingConstants.SQL_LOG_ENABLE), is(Boolean.TRUE.toString()));
+ assertThat(logger.getProps().getProperty(LoggingConstants.SQL_LOG_SIMPLE), is(Boolean.TRUE.toString()));
+ }
+
+ @Test
+ void assertSyncLoggingRuleConfigurationWhenSyncRuleToProperties() {
+ ShardingSphereLogger logger = new ShardingSphereLogger(LoggingConstants.SQL_LOG_TOPIC, null, false, null);
+ logger.getProps().setProperty(LoggingConstants.SQL_LOG_ENABLE, Boolean.TRUE.toString());
+ logger.getProps().setProperty(LoggingConstants.SQL_LOG_SIMPLE, Boolean.TRUE.toString());
+ ConfigurationProperties props = new ConfigurationProperties(new Properties());
+ LoggingUtils.syncLoggingRuleConfiguration(new LoggingRuleConfiguration(Collections.singleton(logger), Collections.emptyList()), props);
+ assertThat(props.getProps().getProperty(LoggingConstants.SQL_SHOW), is(Boolean.TRUE.toString()));
+ assertThat(props.getProps().getProperty(LoggingConstants.SQL_SIMPLE), is(Boolean.TRUE.toString()));
+ }
+
+ @Test
+ void assertSyncLoggingRuleConfigurationWhenSyncNothing() {
+ ShardingSphereLogger logger = new ShardingSphereLogger(LoggingConstants.SQL_LOG_TOPIC, null, false, null);
+ ConfigurationProperties props = new ConfigurationProperties(new Properties());
+ LoggingUtils.syncLoggingRuleConfiguration(new LoggingRuleConfiguration(Collections.singleton(logger), Collections.emptyList()), props);
+ assertFalse(logger.getProps().containsKey(LoggingConstants.SQL_LOG_ENABLE));
+ assertFalse(logger.getProps().containsKey(LoggingConstants.SQL_LOG_SIMPLE));
+ assertFalse(props.getProps().containsKey(LoggingConstants.SQL_SHOW));
+ assertFalse(props.getProps().containsKey(LoggingConstants.SQL_SIMPLE));
+ }
+}