Skip to content

Commit

Permalink
Add Smoke Tests for the 'spring-geode-starter-logging' module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxblum committed Feb 12, 2020
1 parent 3a347ba commit 21cc8e6
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
@@ -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")

}
@@ -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:
*
* <code>
* -Dlogback.root.log.level=INFO
* -Dspring.boot.data.gemfire.log.level=INFO
* </code>
*
* Use the following JVM {@link System} {@link java.util.Properties Property} to configure a custom Logback
* log configuration file:
*
* <code>
* -Dlogback.configurationFile=logback.xml
* </code>
*
* @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();
}
}
@@ -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");
}
}

0 comments on commit 21cc8e6

Please sign in to comment.