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 f611edd74c0 Add LogCaptureAssertion (#36962)
f611edd74c0 is described below
commit f611edd74c09a89f939550f6a088c5b9d51f1d4e
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Oct 29 13:59:57 2025 +0800
Add LogCaptureAssertion (#36962)
---
.../advice/MetaDataContextsFactoryAdviceTest.java | 7 +--
.../extension/log/LogCaptureAssertion.java | 62 ++++++++++++++++++++++
.../extension/log/LogCaptureExtension.java | 34 +-----------
3 files changed, 68 insertions(+), 35 deletions(-)
diff --git
a/agent/plugins/logging/type/file/src/test/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdviceTest.java
b/agent/plugins/logging/type/file/src/test/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdviceTest.java
index 4f32865924c..a274937b851 100644
---
a/agent/plugins/logging/type/file/src/test/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdviceTest.java
+++
b/agent/plugins/logging/type/file/src/test/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdviceTest.java
@@ -19,6 +19,7 @@ package
org.apache.shardingsphere.agent.plugin.logging.file.advice;
import ch.qos.logback.classic.Level;
import org.apache.shardingsphere.agent.api.advice.TargetAdviceMethod;
+import
org.apache.shardingsphere.test.infra.framework.extension.log.LogCaptureAssertion;
import
org.apache.shardingsphere.test.infra.framework.extension.log.LogCaptureExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -29,12 +30,12 @@ import static org.mockito.Mockito.mock;
class MetaDataContextsFactoryAdviceTest {
@Test
- void assertLog(final LogCaptureExtension logCaptureExtension) {
+ void assertLog(final LogCaptureAssertion logCaptureAssertion) {
MetaDataContextsFactoryAdvice advice = new
MetaDataContextsFactoryAdvice();
TargetAdviceMethod method = mock(TargetAdviceMethod.class);
advice.beforeMethod(null, method, new Object[]{}, "FIXTURE");
advice.afterMethod(null, method, new Object[]{}, null, "FIXTURE");
- logCaptureExtension.assertLogCount(1);
- logCaptureExtension.assertLogContent(0, Level.INFO, "Build meta data
contexts finished, cost {} milliseconds.", false);
+ logCaptureAssertion.assertLogCount(1);
+ logCaptureAssertion.assertLogContent(0, Level.INFO, "Build meta data
contexts finished, cost {} milliseconds.", false);
}
}
diff --git
a/test/infra/framework/src/main/java/org/apache/shardingsphere/test/infra/framework/extension/log/LogCaptureAssertion.java
b/test/infra/framework/src/main/java/org/apache/shardingsphere/test/infra/framework/extension/log/LogCaptureAssertion.java
new file mode 100644
index 00000000000..e5e7fab7a29
--- /dev/null
+++
b/test/infra/framework/src/main/java/org/apache/shardingsphere/test/infra/framework/extension/log/LogCaptureAssertion.java
@@ -0,0 +1,62 @@
+/*
+ * 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.test.infra.framework.extension.log;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import lombok.RequiredArgsConstructor;
+
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+/**
+ * Log capture assertion.
+ */
+@RequiredArgsConstructor
+public final class LogCaptureAssertion {
+
+ private final List<ILoggingEvent> loggingEvents;
+
+ /**
+ * Assert the number of captured logs.
+ *
+ * @param expectedCount expected log count
+ */
+ public void assertLogCount(final int expectedCount) {
+ assertThat(loggingEvents.size(), is(expectedCount));
+ }
+
+ /**
+ * Assert the number of captured logs with specific level and message.
+ *
+ * @param actualLogEventIndex actual log event index
+ * @param expectedLevel expected log level
+ * @param expectedMessage expected log message
+ * @param isFormatMessage format message or not
+ */
+ public void assertLogContent(final int actualLogEventIndex, final Level
expectedLevel, final String expectedMessage, final boolean isFormatMessage) {
+ assertThat(loggingEvents.get(actualLogEventIndex).getLevel(),
is(expectedLevel));
+ if (isFormatMessage) {
+
assertThat(loggingEvents.get(actualLogEventIndex).getFormattedMessage(),
is(expectedMessage));
+ } else {
+ assertThat(loggingEvents.get(actualLogEventIndex).getMessage(),
is(expectedMessage));
+ }
+ }
+}
diff --git
a/test/infra/framework/src/main/java/org/apache/shardingsphere/test/infra/framework/extension/log/LogCaptureExtension.java
b/test/infra/framework/src/main/java/org/apache/shardingsphere/test/infra/framework/extension/log/LogCaptureExtension.java
index 2feecf40ab0..d018e87af0e 100644
---
a/test/infra/framework/src/main/java/org/apache/shardingsphere/test/infra/framework/extension/log/LogCaptureExtension.java
+++
b/test/infra/framework/src/main/java/org/apache/shardingsphere/test/infra/framework/extension/log/LogCaptureExtension.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.test.infra.framework.extension.log;
-import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
@@ -28,9 +27,6 @@ import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.slf4j.LoggerFactory;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-
/**
* Log capture extension.
*/
@@ -58,37 +54,11 @@ public final class LogCaptureExtension implements
BeforeEachCallback, AfterEachC
@Override
public boolean supportsParameter(final ParameterContext parameterContext,
final ExtensionContext extensionContext) {
- return parameterContext.getParameter().getType() ==
LogCaptureExtension.class;
+ return parameterContext.getParameter().getType() ==
LogCaptureAssertion.class;
}
@Override
public Object resolveParameter(final ParameterContext parameterContext,
final ExtensionContext extensionContext) {
- return this;
- }
-
- /**
- * Assert the number of captured logs.
- *
- * @param expectedCount expected log count
- */
- public void assertLogCount(final int expectedCount) {
- assertThat(listAppender.list.size(), is(expectedCount));
- }
-
- /**
- * Assert the number of captured logs with specific level and message.
- *
- * @param actualLogEventIndex actual log event index
- * @param expectedLevel expected log level
- * @param expectedMessage expected log message
- * @param isFormatMessage format message or not
- */
- public void assertLogContent(final int actualLogEventIndex, final Level
expectedLevel, final String expectedMessage, final boolean isFormatMessage) {
- assertThat(listAppender.list.get(actualLogEventIndex).getLevel(),
is(expectedLevel));
- if (isFormatMessage) {
-
assertThat(listAppender.list.get(actualLogEventIndex).getFormattedMessage(),
is(expectedMessage));
- } else {
-
assertThat(listAppender.list.get(actualLogEventIndex).getMessage(),
is(expectedMessage));
- }
+ return new LogCaptureAssertion(listAppender.list);
}
}