From 6b1fcbc65762b8b82586c220357663a17697d28e Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 21 Jun 2018 17:08:04 -0700 Subject: [PATCH] Add Integration Tests support for configuring, bootstrapping and properly closing a Spring ApplicationContext. --- ...icationContextIntegrationTestsSupport.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java new file mode 100644 index 0000000..f9485fd --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java @@ -0,0 +1,76 @@ +/* + * Copyright 2018 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 + * + * 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.springframework.data.gemfire.tests.integration; + +import java.util.Optional; + +import org.junit.AfterClass; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * The {@link SpringApplicationContextIntegrationTestsSupport} class is an extension of {@link IntegrationTestsSupport} + * for writing Integration Tests involving a Spring {@link ApplicationContext}. + * + * This class contains functionality common to all Integration Tests involving a Spring {@link ApplicationContext} + * and can be extended to create, acquire a reference and close the {@link ApplicationContext} + * on test class completion, properly. + * + * @author John Blum + * @see org.springframework.context.ApplicationContext + * @see org.springframework.context.ConfigurableApplicationContext + * @see org.springframework.context.annotation.AnnotationConfigApplicationContext + * @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport + * @since 1.0.0 + */ +@SuppressWarnings("unused") +public abstract class SpringApplicationContextIntegrationTestsSupport extends IntegrationTestsSupport { + + private volatile ConfigurableApplicationContext applicationContext; + + @AfterClass + public void closeApplicationContext() { + Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close); + } + + protected ConfigurableApplicationContext newApplicationContext(Class... annotatedClasses) { + + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + + applicationContext.register(annotatedClasses); + applicationContext.registerShutdownHook(); + applicationContext.refresh(); + + this.applicationContext = applicationContext; + + return applicationContext; + } + + @SuppressWarnings("unchecked") + protected T getApplicationContext() { + return (T) this.applicationContext; + } + + protected T getBean(Class requiredType) { + return getApplicationContext().getBean(requiredType); + } + + protected T getBean(String beanName, Class requiredType) { + return getApplicationContext().getBean(beanName, requiredType); + } +}