From 21cc8e6c4ba898cbc89c60b731c88bf19d1c8132 Mon Sep 17 00:00:00 2001 From: John Blum Date: Mon, 10 Feb 2020 20:07:51 -0800 Subject: [PATCH] Add Smoke Tests for the 'spring-geode-starter-logging' module. Resolves gh-73. --- .../spring-geode-smoke-tests-logging.gradle | 15 +++ .../ApacheGeodeLoggingApplication.java | 127 ++++++++++++++++++ ...acheGeodeLoggingApplicationSmokeTests.java | 55 ++++++++ 3 files changed, 197 insertions(+) create mode 100644 spring-geode-tests/smoke-tests/logging/spring-geode-smoke-tests-logging.gradle create mode 100644 spring-geode-tests/smoke-tests/logging/src/main/java/example/app/geode/logging/ApacheGeodeLoggingApplication.java create mode 100644 spring-geode-tests/smoke-tests/logging/src/test/java/example/app/geode/logging/ApacheGeodeLoggingApplicationSmokeTests.java diff --git a/spring-geode-tests/smoke-tests/logging/spring-geode-smoke-tests-logging.gradle b/spring-geode-tests/smoke-tests/logging/spring-geode-smoke-tests-logging.gradle new file mode 100644 index 00000000..683f2b75 --- /dev/null +++ b/spring-geode-tests/smoke-tests/logging/spring-geode-smoke-tests-logging.gradle @@ -0,0 +1,15 @@ +apply plugin: 'io.spring.convention.spring-test' + +description = "Smoke Tests to assert that Spring for Apache Geode logging functions as expected." + +dependencies { + + implementation "org.assertj:assertj-core" + + // NOTE: 'spring-geode-starter-logging' must be the first entry on the application/test classpath + implementation project(":spring-geode-starter-logging") + implementation project(':spring-geode-starter') + + testImplementation project(":spring-geode-starter-test") + +} diff --git a/spring-geode-tests/smoke-tests/logging/src/main/java/example/app/geode/logging/ApacheGeodeLoggingApplication.java b/spring-geode-tests/smoke-tests/logging/src/main/java/example/app/geode/logging/ApacheGeodeLoggingApplication.java new file mode 100644 index 00000000..d69cf71d --- /dev/null +++ b/spring-geode-tests/smoke-tests/logging/src/main/java/example/app/geode/logging/ApacheGeodeLoggingApplication.java @@ -0,0 +1,127 @@ +/* + * 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 example.app.geode.logging; + +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.geode.logging.slf4j.logback.DelegatingAppender; +import org.springframework.geode.logging.slf4j.logback.StringAppender; +import org.springframework.geode.logging.slf4j.logback.support.LogbackSupport; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.LoggerContext; + +/** + * {@link SpringBootApplication Spring Boot application} used to assert and observe the log output from + * Apache Geode Logging. + * + * Use the following JVM {@link System} {@link java.util.Properties Property} to adjust {@link Logger} log level + * and log output: + * + * + * -Dlogback.root.log.level=INFO + * -Dspring.boot.data.gemfire.log.level=INFO + * + * + * Use the following JVM {@link System} {@link java.util.Properties Property} to configure a custom Logback + * log configuration file: + * + * + * -Dlogback.configurationFile=logback.xml + * + * + * @author John Blum + * @see org.slf4j.Logger + * @see org.slf4j.LoggerFactory + * @see org.springframework.boot.ApplicationRunner + * @see org.springframework.boot.SpringApplication + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.context.annotation.Bean + * @see org.springframework.geode.logging.slf4j.logback.DelegatingAppender + * @see org.springframework.geode.logging.slf4j.logback.StringAppender + * @see org.springframework.geode.logging.slf4j.logback.support.LogbackSupport + * @see ch.qos.logback.classic.LoggerContext + * @since 1.3.0 + */ +@SpringBootApplication +@SuppressWarnings({ "rawtypes", "unused" }) +public class ApacheGeodeLoggingApplication { + + private static final StringAppender stringAppender; + + static { + stringAppender = configureAndInitializeLoggingSystem(); + } + + @SuppressWarnings("unchecked") + private static StringAppender configureAndInitializeLoggingSystem() { + + LoggerContext loggerContext = LogbackSupport.requireLoggerContext(); + + LogbackSupport.suppressSpringBootLogbackInitialization(); + + ch.qos.logback.classic.Logger rootLogger = LogbackSupport.requireLogbackRootLogger(); + + LogbackSupport.removeConsoleAppender(rootLogger); + + DelegatingAppender delegatingAppender = + LogbackSupport.requireAppender(rootLogger, "delegate", DelegatingAppender.class); + + return new StringAppender.Builder() + .applyTo(delegatingAppender) + .setContext(loggerContext) + .buildAndStart(); + } + + // Customizes the configuration of SLF4J using the Logback logging provider provided in + // the 'spring-geode-starter-logging' module. + public static void main(String[] args) { + SpringApplication.run(ApacheGeodeLoggingApplication.class, args); + } + + private final Logger logger = LoggerFactory.getLogger("org.apache.geode"); + + @Bean + ApplicationRunner runner() { + + return args -> { + this.logger.info("RUNNER RAN!"); + //printLog(); + }; + } + + @Bean + Log log() { + return this::logToString; + } + + private String logToString() { + return stringAppender.getLogOutput(); + } + + private void printLog() { + System.out.printf("LOG [%s]%n", logToString()); + } + + @FunctionalInterface + interface Log { + String getContent(); + } +} diff --git a/spring-geode-tests/smoke-tests/logging/src/test/java/example/app/geode/logging/ApacheGeodeLoggingApplicationSmokeTests.java b/spring-geode-tests/smoke-tests/logging/src/test/java/example/app/geode/logging/ApacheGeodeLoggingApplicationSmokeTests.java new file mode 100644 index 00000000..1369cbe5 --- /dev/null +++ b/spring-geode-tests/smoke-tests/logging/src/test/java/example/app/geode/logging/ApacheGeodeLoggingApplicationSmokeTests.java @@ -0,0 +1,55 @@ +/* + * 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 example.app.geode.logging; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Smoke Tests for {@link ApacheGeodeLoggingApplication}. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport + * @see org.springframework.test.context.junit4.SpringRunner + * @since 1.3.0 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@SuppressWarnings("unused") +public class ApacheGeodeLoggingApplicationSmokeTests extends IntegrationTestsSupport { + + @Autowired + private ApacheGeodeLoggingApplication.Log log; + + @Test + public void springApplicationLogsContent() { + assertThat(log.getContent()).containsSequence("RUNNER RAN!"); + } + + @Test + public void apacheGeodeLogsContent() { + assertThat(log.getContent()).containsSequence("Product-Name: Apache Geode"); + } +}