From 103cb930ba8dee874945c0e51ff76c2c8687ebf0 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 18 Mar 2021 18:54:47 -0700 Subject: [PATCH] Refactor codebase. Apply Spring @NonNull & @Nullable annotations to the API. --- ...icationContextIntegrationTestsSupport.java | 51 +++++++++++++++---- ...ootApplicationIntegrationTestsSupport.java | 12 +++-- 2 files changed, 48 insertions(+), 15 deletions(-) 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 index 7b26bce..d7e5110 100644 --- 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 @@ -15,14 +15,20 @@ */ package org.springframework.data.gemfire.tests.integration; +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; + import java.util.Optional; import org.junit.After; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.gemfire.util.ArrayUtils; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; /** * The {@link SpringApplicationContextIntegrationTestsSupport} class is an extension of {@link IntegrationTestsSupport} @@ -34,21 +40,22 @@ import org.springframework.data.gemfire.util.ArrayUtils; * * @author John Blum * @see org.springframework.context.ApplicationContext + * @see org.springframework.context.ApplicationEventPublisher + * @see org.springframework.context.ApplicationEventPublisherAware * @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 { +public abstract class SpringApplicationContextIntegrationTestsSupport extends IntegrationTestsSupport + implements ApplicationEventPublisherAware { private volatile ConfigurableApplicationContext applicationContext; @After public void closeApplicationContext() { - - Optional.ofNullable(this.applicationContext) - .ifPresent(ConfigurableApplicationContext::close); + getOptionalApplicationContext().ifPresent(ConfigurableApplicationContext::close); } protected ConfigurableApplicationContext newApplicationContext(Class... annotatedClasses) { @@ -63,14 +70,16 @@ public abstract class SpringApplicationContextIntegrationTestsSupport extends In return setApplicationContext(applicationContext); } - protected ConfigurableApplicationContext processBeforeRefresh(ConfigurableApplicationContext applicationContext) { + protected @NonNull ConfigurableApplicationContext processBeforeRefresh( + @NonNull ConfigurableApplicationContext applicationContext) { - TestContextCacheLifecycleListenerAdapter.getInstance().setApplicationEventPublisher(applicationContext); + setApplicationEventPublisher(applicationContext); return applicationContext; } - protected T setApplicationContext(T applicationContext) { + protected @Nullable T setApplicationContext( + @Nullable T applicationContext) { this.applicationContext = applicationContext; @@ -78,15 +87,37 @@ public abstract class SpringApplicationContextIntegrationTestsSupport extends In } @SuppressWarnings("unchecked") - protected T getApplicationContext() { + protected @Nullable T getApplicationContext() { return (T) this.applicationContext; } + protected Optional getOptionalApplicationContext() { + return Optional.ofNullable(getApplicationContext()); + } + + /** + * @inheritDoc + */ + @Override + public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { + + if (applicationEventPublisher != null) { + TestContextCacheLifecycleListenerAdapter.getInstance() + .setApplicationEventPublisher(applicationEventPublisher); + } + } + protected T getBean(Class requiredType) { - return getApplicationContext().getBean(requiredType); + + return getOptionalApplicationContext() + .map(applicationContext -> applicationContext.getBean(requiredType)) + .orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized")); } protected T getBean(String beanName, Class requiredType) { - return getApplicationContext().getBean(beanName, requiredType); + + return getOptionalApplicationContext() + .map(applicationContext -> applicationContext.getBean(beanName, requiredType)) + .orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized")); } } diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringBootApplicationIntegrationTestsSupport.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringBootApplicationIntegrationTestsSupport.java index 125f708..0f095c1 100644 --- a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringBootApplicationIntegrationTestsSupport.java +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringBootApplicationIntegrationTestsSupport.java @@ -19,6 +19,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.data.gemfire.util.ArrayUtils; +import org.springframework.lang.NonNull; /** * The {@link SpringBootApplicationIntegrationTestsSupport} class is an extension of Spring Test @@ -48,7 +50,7 @@ public abstract class SpringBootApplicationIntegrationTestsSupport } @Override - protected ConfigurableApplicationContext newApplicationContext(Class... annotatedClasses) { + protected @NonNull ConfigurableApplicationContext newApplicationContext(Class... annotatedClasses) { return setApplicationContext(processBeforeRun(processBeforeBuild( newSpringApplicationBuilder(annotatedClasses) @@ -58,15 +60,15 @@ public abstract class SpringBootApplicationIntegrationTestsSupport .run(getArguments())); } - protected SpringApplicationBuilder newSpringApplicationBuilder(Class... annotatedClasses) { - return new SpringApplicationBuilder(annotatedClasses); + protected @NonNull SpringApplicationBuilder newSpringApplicationBuilder(Class... annotatedClasses) { + return new SpringApplicationBuilder(ArrayUtils.nullSafeArray(annotatedClasses, Class.class)); } - protected SpringApplicationBuilder processBeforeBuild(SpringApplicationBuilder springApplicationBuilder) { + protected @NonNull SpringApplicationBuilder processBeforeBuild(@NonNull SpringApplicationBuilder springApplicationBuilder) { return springApplicationBuilder; } - protected SpringApplication processBeforeRun(SpringApplication springApplication) { + protected @NonNull SpringApplication processBeforeRun(@NonNull SpringApplication springApplication) { return springApplication; } }