From cb0a0e02ed9f22cf2938fb1d7b710b66a005e1fd Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 28 Jun 2019 16:17:09 -0700 Subject: [PATCH] Add testing support for logging and capturing log events. Adds a custom, SLF4J, Logback Appender named 'TestAppender' to listen for and record log events (ILoggingEvent) in a Stack. --- .../logging/slf4j/logback/TestAppender.java | 76 +++++++++++++++++++ .../slf4j/logback/TestAppenderUnitTests.java | 71 +++++++++++++++++ .../src/test/resources/logback.xml | 8 +- 3 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppender.java create mode 100644 spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppenderUnitTests.java diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppender.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppender.java new file mode 100644 index 0000000..adc35e0 --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppender.java @@ -0,0 +1,76 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.data.gemfire.tests.logging.slf4j.logback; + +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; + +import java.util.Optional; +import java.util.Stack; +import java.util.concurrent.atomic.AtomicReference; + +import org.springframework.util.StringUtils; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.Appender; +import ch.qos.logback.core.AppenderBase; + +/** + * The {@link TestAppender} class is a SLF4J, Logback {@link Appender} implementation used for testing purposes. + * + * @author John Blum + * @see java.util.Stack + * @see ch.qos.logback.core.Appender + * @see ch.qos.logback.core.AppenderBase + * @since 0.0.5.RELEASE + */ +@SuppressWarnings("unused") +public class TestAppender extends AppenderBase implements Appender { + + private static final AtomicReference INSTANCE = new AtomicReference<>(null); + + private static final Stack logMessages = new Stack<>(); + + public static TestAppender getInstance() { + + return Optional.ofNullable(INSTANCE.get()) + .orElseThrow(() -> newIllegalStateException("[%s] was not properly configured", + TestAppender.class.getName())); + } + + public TestAppender() { + INSTANCE.compareAndSet(null, this); + } + + @Override + protected void append(ILoggingEvent event) { + + Optional.ofNullable(event) + .map(ILoggingEvent::getFormattedMessage) + .filter(StringUtils::hasText) + .ifPresent(logMessages::push); + } + + public String lastLogMessage() { + + synchronized (logMessages) { + return logMessages.empty() ? null : logMessages.pop(); + } + } + + public void clear() { + logMessages.clear(); + } +} diff --git a/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppenderUnitTests.java b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppenderUnitTests.java new file mode 100644 index 0000000..b2b4819 --- /dev/null +++ b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/logging/slf4j/logback/TestAppenderUnitTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.data.gemfire.tests.logging.slf4j.logback; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Unit Tests for {@link TestAppender}. + * + * @author John Blum + * @see org.junit.Test + * @see org.slf4j.Logger + * @see org.springframework.data.gemfire.tests.logging.slf4j.logback.TestAppender + * @since 0.0.5.RELEASE + */ +public class TestAppenderUnitTests { + + private static final Logger logger = LoggerFactory.getLogger(TestAppenderUnitTests.class); + + @Test + public void logEventsAppendedCorrectly() { + + TestAppender testAppender = TestAppender.getInstance(); + + assertThat(testAppender).isNotNull(); + assertThat(testAppender.lastLogMessage()).isNull(); + + LoggableObject object = new LoggableObject(); + + object.logAnError(); + object.logAnInfoMessage(); + object.logAWarning(); + + assertThat(testAppender.lastLogMessage()).isEqualTo("WARN TEST"); + assertThat(testAppender.lastLogMessage()).isEqualTo("ERROR TEST"); + assertThat(testAppender.lastLogMessage()).isNull(); + } + + static class LoggableObject { + + public void logAnError() { + logger.error("ERROR TEST"); + } + + public void logAnInfoMessage() { + logger.info("INFO TEST"); + } + + public void logAWarning() { + logger.warn("WARN TEST"); + } + } +} diff --git a/spring-data-geode-test/src/test/resources/logback.xml b/spring-data-geode-test/src/test/resources/logback.xml index 4d64c87..df0621b 100644 --- a/spring-data-geode-test/src/test/resources/logback.xml +++ b/spring-data-geode-test/src/test/resources/logback.xml @@ -11,7 +11,7 @@ - + TEST - %m%n @@ -21,14 +21,10 @@ - + - - - -