DATAGEODE-175 - Move away from Spring Data Commons deprecations.
This commit is contained in:
@@ -30,6 +30,7 @@ import org.apache.geode.NoSystemException;
|
||||
import org.apache.geode.SystemConnectException;
|
||||
import org.apache.geode.SystemIsRunningException;
|
||||
import org.apache.geode.UnmodifiableException;
|
||||
import org.apache.geode.admin.AdminException;
|
||||
import org.apache.geode.cache.CacheException;
|
||||
import org.apache.geode.cache.CacheExistsException;
|
||||
import org.apache.geode.cache.CacheLoaderException;
|
||||
@@ -76,24 +77,24 @@ import org.springframework.dao.TypeMismatchDataAccessException;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Helper class featuring methods for GemFire Cache or Region handling.
|
||||
* Abstract utility class featuring methods for Apache Geode / Pivotal GemFire Cache or Region handling.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author John Blum
|
||||
*/
|
||||
public abstract class GemfireCacheUtils {
|
||||
|
||||
private static Class<?> CQ_EXCEPTION_CLASS;
|
||||
|
||||
static {
|
||||
|
||||
Class<?> type = null;
|
||||
|
||||
try {
|
||||
type = ClassUtils.resolveClassName("org.apache.geode.cache.query.CqInvalidException",
|
||||
GemfireCacheUtils.class.getClassLoader());
|
||||
|
||||
}
|
||||
catch (IllegalArgumentException ignore) {
|
||||
}
|
||||
catch (IllegalArgumentException ignore) { }
|
||||
|
||||
CQ_EXCEPTION_CLASS = type;
|
||||
}
|
||||
@@ -103,149 +104,209 @@ public abstract class GemfireCacheUtils {
|
||||
* Converts the given (unchecked) Gemfire exception to an appropriate one from the
|
||||
* <code>org.springframework.dao</code> hierarchy.
|
||||
*
|
||||
* @param ex Gemfire unchecked exception
|
||||
* @param cause Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static DataAccessException convertGemfireAccessException(GemFireException ex) {
|
||||
if (ex instanceof CacheException) {
|
||||
if (ex instanceof CacheExistsException) {
|
||||
return new DataIntegrityViolationException(ex.getMessage(), ex);
|
||||
public static DataAccessException convertGemfireAccessException(GemFireException cause) {
|
||||
|
||||
if (cause instanceof CacheException) {
|
||||
if (cause instanceof CacheExistsException) {
|
||||
return new DataIntegrityViolationException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof CommitConflictException) {
|
||||
return new DataIntegrityViolationException(ex.getMessage(), ex);
|
||||
if (cause instanceof CommitConflictException) {
|
||||
return new DataIntegrityViolationException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof CommitIncompleteException) {
|
||||
return new DataIntegrityViolationException(ex.getMessage(), ex);
|
||||
if (cause instanceof CommitIncompleteException) {
|
||||
return new DataIntegrityViolationException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof EntryExistsException) {
|
||||
return new DuplicateKeyException(ex.getMessage(), ex);
|
||||
if (cause instanceof EntryExistsException) {
|
||||
return new DuplicateKeyException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof EntryNotFoundException) {
|
||||
return new DataRetrievalFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof EntryNotFoundException) {
|
||||
return new DataRetrievalFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof RegionExistsException) {
|
||||
return new DataIntegrityViolationException(ex.getMessage(), ex);
|
||||
if (cause instanceof RegionExistsException) {
|
||||
return new DataIntegrityViolationException(cause.getMessage(), cause);
|
||||
}
|
||||
}
|
||||
if (ex instanceof CacheRuntimeException) {
|
||||
if (ex instanceof CacheXmlException) {
|
||||
return new GemfireSystemException(ex);
|
||||
|
||||
if (cause instanceof CacheRuntimeException) {
|
||||
if (cause instanceof CacheXmlException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof CancelException) {
|
||||
if (cause instanceof CancelException) {
|
||||
// all cancellations go wrapped by this exception
|
||||
return new GemfireCancellationException((CancelException) ex);
|
||||
return new GemfireCancellationException((CancelException) cause);
|
||||
}
|
||||
if (ex instanceof CqClosedException) {
|
||||
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
|
||||
if (cause instanceof CqClosedException) {
|
||||
return new InvalidDataAccessApiUsageException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof DiskAccessException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof DiskAccessException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof EntryDestroyedException) {
|
||||
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
|
||||
if (cause instanceof EntryDestroyedException) {
|
||||
return new InvalidDataAccessApiUsageException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof FailedSynchronizationException) {
|
||||
return new PessimisticLockingFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof FailedSynchronizationException) {
|
||||
return new PessimisticLockingFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof IndexMaintenanceException) {
|
||||
return new GemfireIndexException((IndexMaintenanceException) ex);
|
||||
if (cause instanceof IndexMaintenanceException) {
|
||||
return new GemfireIndexException((IndexMaintenanceException) cause);
|
||||
}
|
||||
if (ex instanceof OperationAbortedException) {
|
||||
if (cause instanceof OperationAbortedException) {
|
||||
// treat user exceptions first
|
||||
if (ex instanceof CacheLoaderException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof CacheLoaderException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof CacheWriterException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof CacheWriterException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
// the rest are treated as resource failures
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof PartitionedRegionDistributionException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof PartitionedRegionDistributionException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof PartitionedRegionStorageException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof PartitionedRegionStorageException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof QueryExecutionTimeoutException) {
|
||||
return new GemfireQueryException((QueryExecutionTimeoutException) ex);
|
||||
if (cause instanceof QueryExecutionTimeoutException) {
|
||||
return new GemfireQueryException((QueryExecutionTimeoutException) cause);
|
||||
}
|
||||
if (ex instanceof RegionDestroyedException) {
|
||||
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
|
||||
if (cause instanceof RegionDestroyedException) {
|
||||
return new InvalidDataAccessResourceUsageException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof org.apache.geode.admin.RegionNotFoundException) {
|
||||
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
|
||||
if (cause instanceof org.apache.geode.admin.RegionNotFoundException) {
|
||||
return new InvalidDataAccessResourceUsageException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof ResourceException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof ResourceException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof RoleException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof RoleException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof StatisticsDisabledException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof StatisticsDisabledException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof SynchronizationCommitConflictException) {
|
||||
return new PessimisticLockingFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof SynchronizationCommitConflictException) {
|
||||
return new PessimisticLockingFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
}
|
||||
if (ex instanceof CopyException) {
|
||||
return new GemfireSystemException(ex);
|
||||
|
||||
if (cause instanceof CopyException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof FunctionException) {
|
||||
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
|
||||
if (cause instanceof FunctionException) {
|
||||
return new InvalidDataAccessApiUsageException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof GemFireCacheException) {
|
||||
return convertGemfireAccessException(((GemFireCacheException) ex).getCacheException());
|
||||
if (cause instanceof GemFireCacheException) {
|
||||
return convertGemfireAccessException(((GemFireCacheException) cause).getCacheException());
|
||||
}
|
||||
if (ex instanceof GemFireConfigException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof GemFireConfigException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof GemFireIOException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof GemFireIOException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof GemFireSecurityException) {
|
||||
return new PermissionDeniedDataAccessException(ex.getMessage(), ex);
|
||||
if (cause instanceof GemFireSecurityException) {
|
||||
return new PermissionDeniedDataAccessException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof IncompatibleSystemException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof IncompatibleSystemException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof InternalGemFireException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof InternalGemFireException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof InvalidValueException) {
|
||||
return new TypeMismatchDataAccessException(ex.getMessage(), ex);
|
||||
if (cause instanceof InvalidValueException) {
|
||||
return new TypeMismatchDataAccessException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof LeaseExpiredException) {
|
||||
return new PessimisticLockingFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof LeaseExpiredException) {
|
||||
return new PessimisticLockingFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof NoSystemException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof NoSystemException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof org.apache.geode.admin.RuntimeAdminException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof org.apache.geode.admin.RuntimeAdminException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof ServerConnectivityException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof ServerConnectivityException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof SystemConnectException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
if (cause instanceof SystemConnectException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
if (ex instanceof SystemIsRunningException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof SystemIsRunningException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
if (ex instanceof UnmodifiableException) {
|
||||
return new GemfireSystemException(ex);
|
||||
if (cause instanceof UnmodifiableException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
|
||||
// for exceptions that had their parent changed in 6.5
|
||||
DataAccessException dae = convertQueryExceptions(ex);
|
||||
if (dae != null)
|
||||
return dae;
|
||||
DataAccessException dataAccessException = convertQueryExceptions(cause);
|
||||
|
||||
// fall back
|
||||
return new GemfireSystemException(ex);
|
||||
if (dataAccessException != null) {
|
||||
return dataAccessException;
|
||||
}
|
||||
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given (checked) Gemfire exception to an appropriate one from the
|
||||
* <code>org.springframework.dao</code> hierarchy.
|
||||
*
|
||||
* @param cause Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static DataAccessException convertGemfireAccessException(GemFireCheckedException cause) {
|
||||
|
||||
if (cause instanceof AdminException) {
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
|
||||
if (cause instanceof QueryException) {
|
||||
return new GemfireQueryException((QueryException) cause);
|
||||
}
|
||||
|
||||
if (cause instanceof VersionException) {
|
||||
return new DataAccessResourceFailureException(cause.getMessage(), cause);
|
||||
}
|
||||
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given (unchecked) Gemfire exception to an appropriate one from the
|
||||
* <code>org.springframework.dao</code> hierarchy. This method exists to handle backwards compatibility
|
||||
* for exceptions that had their parents changed in GemFire 6.5.
|
||||
*
|
||||
* @param cause Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
public static DataAccessException convertGemfireAccessException(IndexInvalidException cause) {
|
||||
return new GemfireIndexException(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given (unchecked) Gemfire exception to an appropriate one from the
|
||||
* <code>org.springframework.dao</code> hierarchy. This method exists to handle backwards compatibility
|
||||
* for exceptions that had their parents changed in GemFire 6.5.
|
||||
*
|
||||
* @param cause Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
public static DataAccessException convertGemfireAccessException(QueryInvalidException cause) {
|
||||
return new GemfireQueryException(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Package protected method for detecting CqInvalidException which has been removed in GemFire 6.5 GA.
|
||||
*/
|
||||
static boolean isCqInvalidException(RuntimeException cause) {
|
||||
return CQ_EXCEPTION_CLASS != null && CQ_EXCEPTION_CLASS.isInstance(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,76 +314,21 @@ public abstract class GemfireCacheUtils {
|
||||
* parent changed. This method exists to 'fool' the compiler type checks
|
||||
* by loosening the type so the code compiles on both 6.5 (pre and current) branches.
|
||||
*/
|
||||
static DataAccessException convertQueryExceptions(RuntimeException ex) {
|
||||
if (ex instanceof IndexInvalidException) {
|
||||
return convertGemfireAccessException((IndexInvalidException) ex);
|
||||
}
|
||||
if (ex instanceof QueryInvalidException) {
|
||||
return convertGemfireAccessException((QueryInvalidException) ex);
|
||||
static DataAccessException convertQueryExceptions(RuntimeException cause) {
|
||||
|
||||
if (cause instanceof IndexInvalidException) {
|
||||
return convertGemfireAccessException((IndexInvalidException) cause);
|
||||
}
|
||||
|
||||
if (isCqInvalidException(ex)) {
|
||||
return convertCqInvalidException(ex);
|
||||
if (cause instanceof QueryInvalidException) {
|
||||
return convertGemfireAccessException((QueryInvalidException) cause);
|
||||
}
|
||||
|
||||
// fall back
|
||||
return new GemfireSystemException(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given (checked) Gemfire exception to an appropriate one from the
|
||||
* <code>org.springframework.dao</code> hierarchy.
|
||||
*
|
||||
* @param ex Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static DataAccessException convertGemfireAccessException(GemFireCheckedException ex) {
|
||||
// query exceptions
|
||||
if (ex instanceof QueryException) {
|
||||
return new GemfireQueryException((QueryException) ex);
|
||||
if (isCqInvalidException(cause)) {
|
||||
return convertCqInvalidException(cause);
|
||||
}
|
||||
// version exception
|
||||
if (ex instanceof VersionException) {
|
||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||
}
|
||||
// admin exception
|
||||
if (ex instanceof org.apache.geode.admin.AdminException) {
|
||||
return new GemfireSystemException(ex);
|
||||
}
|
||||
// fall back
|
||||
return new GemfireSystemException(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given (unchecked) Gemfire exception to an appropriate one from the
|
||||
* <code>org.springframework.dao</code> hierarchy. This method exists to handle backwards compatibility
|
||||
* for exceptions that had their parents changed in GemFire 6.5.
|
||||
*
|
||||
* @param ex Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
public static DataAccessException convertGemfireAccessException(IndexInvalidException ex) {
|
||||
return new GemfireIndexException(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given (unchecked) Gemfire exception to an appropriate one from the
|
||||
* <code>org.springframework.dao</code> hierarchy. This method exists to handle backwards compatibility
|
||||
* for exceptions that had their parents changed in GemFire 6.5.
|
||||
*
|
||||
* @param ex Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
public static DataAccessException convertGemfireAccessException(QueryInvalidException ex) {
|
||||
return new GemfireQueryException(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Package protected method for detecting CqInvalidException which has been removed in GemFire 6.5 GA.
|
||||
*/
|
||||
static boolean isCqInvalidException(RuntimeException ex) {
|
||||
return (CQ_EXCEPTION_CLASS != null && CQ_EXCEPTION_CLASS.isAssignableFrom(ex.getClass()));
|
||||
return new GemfireSystemException(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -330,11 +336,11 @@ public abstract class GemfireCacheUtils {
|
||||
* <code>org.springframework.dao</code> hierarchy. This method exists to handle backwards compatibility
|
||||
* for exceptions that have been removed in 6.5.
|
||||
*
|
||||
* @param ex Gemfire unchecked exception
|
||||
* @param cause Gemfire unchecked exception
|
||||
* @return new the corresponding DataAccessException instance
|
||||
*/
|
||||
static DataAccessException convertCqInvalidException(RuntimeException ex) {
|
||||
return new GemfireQueryException(ex);
|
||||
static DataAccessException convertCqInvalidException(RuntimeException cause) {
|
||||
return new GemfireQueryException(cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,19 +24,25 @@ import org.springframework.util.ClassUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* {@link GemfireUtils} is an abstract utility class encapsulating common functionality to access features
|
||||
* and capabilities of GemFire based on version and other configuration meta-data.
|
||||
=======
|
||||
* {@link GemfireUtils} is an abstract utility class encapsulating common functionality for accessing features
|
||||
* and capabilities of Apache Geode or Pivotal GemFire based on version as well as other configuration meta-data.
|
||||
>>>>>>> 284e248b... SGF-826 - Move away from Spring Data Commons deprecations.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.apache.geode.cache.CacheFactory
|
||||
* @see org.apache.geode.cache.Region
|
||||
* @see org.apache.geode.internal.GemFireVersion
|
||||
* @see org.springframework.data.gemfire.config.support.GemfireFeature
|
||||
* @see org.springframework.data.gemfire.util.RegionUtils
|
||||
* @since 1.3.3
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class GemfireUtils extends RegionUtils {
|
||||
|
||||
public final static String APACHE_GEODE_NAME = "Aache Geode";
|
||||
public final static String APACHE_GEODE_NAME = "Apache Geode";
|
||||
public final static String GEMFIRE_NAME = apacheGeodeProductName();
|
||||
public final static String GEMFIRE_VERSION = apacheGeodeVersion();
|
||||
public final static String UNKNOWN = "unknown";
|
||||
@@ -144,8 +150,6 @@ public abstract class GemfireUtils extends RegionUtils {
|
||||
}
|
||||
|
||||
public static void main(final String... args) {
|
||||
System.out.printf("GemFire Product Name (%1$s) Version (%2$s)%n", GEMFIRE_NAME, GEMFIRE_VERSION);
|
||||
//System.out.printf("Is GemFire Version 6.5 of Above? %1$s%n", isGemfireVersion65OrAbove());
|
||||
//System.out.printf("Is GemFire Version 7.0 of Above? %1$s%n", isGemfireVersion7OrAbove());
|
||||
System.out.printf("Product Name [%1$s] Version [%2$s]%n", GEMFIRE_NAME, GEMFIRE_VERSION);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -39,6 +38,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.data.gemfire.util.CacheUtils;
|
||||
import org.springframework.data.gemfire.util.CollectionUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -95,6 +95,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @see org.springframework.context.ConfigurableApplicationContext
|
||||
*/
|
||||
public static synchronized ConfigurableApplicationContext getApplicationContext() {
|
||||
|
||||
Assert.state(applicationContext != null,
|
||||
"A Spring ApplicationContext was not configured and initialized properly");
|
||||
|
||||
@@ -111,7 +112,8 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @see java.lang.ClassLoader
|
||||
*/
|
||||
public static void setBeanClassLoader(ClassLoader beanClassLoader) {
|
||||
if (applicationContext == null || !applicationContext.isActive()) {
|
||||
|
||||
if (isApplicationContextInitializable()) {
|
||||
beanClassLoaderReference.set(beanClassLoader);
|
||||
}
|
||||
else {
|
||||
@@ -132,6 +134,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @see org.springframework.context.event.ContextRefreshedEvent
|
||||
*/
|
||||
protected static void notifyOnExistingContextRefreshedEvent(ApplicationListener<ContextRefreshedEvent> listener) {
|
||||
|
||||
synchronized (applicationEventNotifier) {
|
||||
if (contextRefreshedEvent != null) {
|
||||
listener.onApplicationEvent(contextRefreshedEvent);
|
||||
@@ -156,6 +159,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* #addApplicationListener(org.springframework.context.ApplicationListener)
|
||||
*/
|
||||
public static <T extends ApplicationListener<ContextRefreshedEvent>> T register(T listener) {
|
||||
|
||||
synchronized (applicationEventNotifier) {
|
||||
applicationEventNotifier.addApplicationListener(listener);
|
||||
notifyOnExistingContextRefreshedEvent(listener);
|
||||
@@ -173,7 +177,9 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @see #unregister(Class)
|
||||
*/
|
||||
public static boolean register(Class<?> annotatedClass) {
|
||||
|
||||
Assert.notNull(annotatedClass, "The Spring annotated class to register must not be null");
|
||||
|
||||
return registeredAnnotatedClasses.add(annotatedClass);
|
||||
}
|
||||
|
||||
@@ -192,6 +198,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* #removeApplicationListener(org.springframework.context.ApplicationListener)
|
||||
*/
|
||||
public static <T extends ApplicationListener<ContextRefreshedEvent>> T unregister(T listener) {
|
||||
|
||||
synchronized (applicationEventNotifier) {
|
||||
applicationEventNotifier.removeApplicationListener(listener);
|
||||
}
|
||||
@@ -222,11 +229,11 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
return LogFactory.getLog(getClass());
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
private boolean isConfigurable(Collection<Class<?>> annotatedClasses, String[] basePackages,
|
||||
String[] contextConfigLocations) {
|
||||
String[] contextConfigLocations) {
|
||||
|
||||
return !(CollectionUtils.isEmpty(annotatedClasses) && ObjectUtils.isEmpty(basePackages)
|
||||
return !(CollectionUtils.isEmpty(annotatedClasses)
|
||||
&& ObjectUtils.isEmpty(basePackages)
|
||||
&& ObjectUtils.isEmpty(contextConfigLocations));
|
||||
}
|
||||
|
||||
@@ -255,21 +262,24 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @see org.springframework.context.support.ClassPathXmlApplicationContext
|
||||
*/
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages, String[] configLocations) {
|
||||
Assert.isTrue(isConfigurable(registeredAnnotatedClasses, basePackages, configLocations),
|
||||
"'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified in order to"
|
||||
+ " construct and configure an instance of the ConfigurableApplicationContext");
|
||||
|
||||
Class<?>[] annotatedClasses = registeredAnnotatedClasses.toArray(
|
||||
new Class<?>[registeredAnnotatedClasses.size()]);
|
||||
String message = "'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified"
|
||||
+ " in order to construct and configure an instance of the ConfigurableApplicationContext";
|
||||
|
||||
return scanBasePackages(registerAnnotatedClasses(createApplicationContext(configLocations),
|
||||
annotatedClasses), basePackages);
|
||||
Assert.isTrue(isConfigurable(registeredAnnotatedClasses, basePackages, configLocations), message);
|
||||
|
||||
Class<?>[] annotatedClasses = registeredAnnotatedClasses.toArray(new Class<?>[0]);
|
||||
|
||||
ConfigurableApplicationContext applicationContext = createApplicationContext(configLocations);
|
||||
|
||||
return scanBasePackages(registerAnnotatedClasses(applicationContext, annotatedClasses), basePackages);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) - used for testing purposes only */
|
||||
ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? new AnnotationConfigApplicationContext()
|
||||
: new ClassPathXmlApplicationContext(configLocations, false));
|
||||
|
||||
return ObjectUtils.isEmpty(configLocations)
|
||||
? new AnnotationConfigApplicationContext()
|
||||
: new ClassPathXmlApplicationContext(configLocations, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -284,6 +294,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @throws java.lang.IllegalArgumentException if the ApplicationContext reference is null!
|
||||
*/
|
||||
protected ConfigurableApplicationContext initApplicationContext(ConfigurableApplicationContext applicationContext) {
|
||||
|
||||
Assert.notNull(applicationContext, "ConfigurableApplicationContext must not be null");
|
||||
|
||||
applicationContext.addApplicationListener(this);
|
||||
@@ -302,6 +313,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @throws java.lang.IllegalArgumentException if the ApplicationContext reference is null!
|
||||
*/
|
||||
protected ConfigurableApplicationContext refreshApplicationContext(ConfigurableApplicationContext applicationContext) {
|
||||
|
||||
Assert.notNull(applicationContext, "ConfigurableApplicationContext must not be null");
|
||||
|
||||
applicationContext.refresh();
|
||||
@@ -323,11 +335,15 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
ConfigurableApplicationContext registerAnnotatedClasses(ConfigurableApplicationContext applicationContext,
|
||||
Class<?>[] annotatedClasses) {
|
||||
|
||||
if (applicationContext instanceof AnnotationConfigApplicationContext
|
||||
&& !ObjectUtils.isEmpty(annotatedClasses)) {
|
||||
return applicationContext instanceof AnnotationConfigApplicationContext && !ObjectUtils.isEmpty(annotatedClasses)
|
||||
? doRegister(applicationContext, annotatedClasses)
|
||||
: applicationContext;
|
||||
}
|
||||
|
||||
((AnnotationConfigApplicationContext) applicationContext).register(annotatedClasses);
|
||||
}
|
||||
ConfigurableApplicationContext doRegister(ConfigurableApplicationContext applicationContext,
|
||||
Class<?>[] annotatedClasses) {
|
||||
|
||||
((AnnotationConfigApplicationContext) applicationContext).register(annotatedClasses);
|
||||
|
||||
return applicationContext;
|
||||
}
|
||||
@@ -345,11 +361,14 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
ConfigurableApplicationContext scanBasePackages(ConfigurableApplicationContext applicationContext,
|
||||
String[] basePackages) {
|
||||
|
||||
if (applicationContext instanceof AnnotationConfigApplicationContext
|
||||
&& !ObjectUtils.isEmpty(basePackages)) {
|
||||
return applicationContext instanceof AnnotationConfigApplicationContext && !ObjectUtils.isEmpty(basePackages)
|
||||
? doScan(applicationContext, basePackages)
|
||||
: applicationContext;
|
||||
}
|
||||
|
||||
((AnnotationConfigApplicationContext) applicationContext).scan(basePackages);
|
||||
}
|
||||
ConfigurableApplicationContext doScan(ConfigurableApplicationContext applicationContext, String[] basePackages) {
|
||||
|
||||
((AnnotationConfigApplicationContext) applicationContext).scan(basePackages);
|
||||
|
||||
return applicationContext;
|
||||
}
|
||||
@@ -363,6 +382,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @see java.lang.ClassLoader
|
||||
*/
|
||||
ConfigurableApplicationContext setClassLoader(ConfigurableApplicationContext applicationContext) {
|
||||
|
||||
ClassLoader beanClassLoader = beanClassLoaderReference.get();
|
||||
|
||||
if (applicationContext instanceof DefaultResourceLoader && beanClassLoader != null) {
|
||||
@@ -372,25 +392,33 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public void init(Properties parameters) {
|
||||
init(CacheUtils.getCache(), parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a Spring ApplicationContext with the given parameters specified with a GemFire <initializer>
|
||||
* block in cache.xml.
|
||||
* Initializes a Spring {@link ApplicationContext} with the given parameters specified with an Apache Geode
|
||||
* or Pivotal GemFire <initializer> block in {@literal cache.xml}.
|
||||
*
|
||||
* @param parameters a Properties object containing the configuration parameters and settings defined in the
|
||||
* GemFire cache.xml <initializer> block for the declared SpringContextBootstrappingInitializer
|
||||
* GemFire Declarable object.
|
||||
* @throws org.springframework.context.ApplicationContextException if the Spring ApplicationContext could not be
|
||||
* successfully created, configured and initialized.
|
||||
* @param parameters {@link Properties} object containing the configuration parameters and settings defined in the
|
||||
* Apache Geode/Pivotal GemFire {@literal cache.xml} <initializer> block for the declared
|
||||
* {@link SpringContextBootstrappingInitializer} Apache Geode/Pivotal GemFire {@link Declarable} object.
|
||||
* @param cache reference to the peer {@link Cache}.
|
||||
* @throws org.springframework.context.ApplicationContextException if the Spring {@link ApplicationContext}
|
||||
* could not be successfully constructed, configured and initialized.
|
||||
* @see #createApplicationContext(String[], String[])
|
||||
* @see #initApplicationContext(org.springframework.context.ConfigurableApplicationContext)
|
||||
* @see #refreshApplicationContext(org.springframework.context.ConfigurableApplicationContext)
|
||||
* @see java.util.Properties
|
||||
*/
|
||||
@Override
|
||||
public void init(Properties parameters) {
|
||||
public void init(Cache cache, Properties parameters) {
|
||||
|
||||
try {
|
||||
synchronized (SpringContextBootstrappingInitializer.class) {
|
||||
if (applicationContext == null || !applicationContext.isActive()) {
|
||||
if (isApplicationContextInitializable()) {
|
||||
|
||||
String basePackages = parameters.getProperty(BASE_PACKAGES_PARAMETER);
|
||||
String contextConfigLocations = parameters.getProperty(CONTEXT_CONFIG_LOCATIONS_PARAMETER);
|
||||
|
||||
@@ -419,6 +447,10 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isApplicationContextInitializable() {
|
||||
return applicationContext == null || !applicationContext.isActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* Null-safe operation used to get the ID of the Spring ApplicationContext.
|
||||
*
|
||||
@@ -427,7 +459,10 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
* @see org.springframework.context.ApplicationContext#getId()
|
||||
*/
|
||||
String nullSafeGetApplicationContextId(ApplicationContext applicationContext) {
|
||||
return (applicationContext != null ? applicationContext.getId() : null);
|
||||
|
||||
return applicationContext != null
|
||||
? applicationContext.getId()
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -448,6 +483,7 @@ public class SpringContextBootstrappingInitializer implements Declarable, Applic
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationContextEvent event) {
|
||||
|
||||
if (event instanceof ContextRefreshedEvent) {
|
||||
synchronized (applicationEventNotifier) {
|
||||
contextRefreshedEvent = (ContextRefreshedEvent) event;
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
@@ -51,7 +52,7 @@ public abstract class SpringUtils {
|
||||
|
||||
Collections.addAll(dependsOnList, nullSafeArray(bean.getDependsOn(), String.class));
|
||||
dependsOnList.addAll(Arrays.asList(nullSafeArray(beanNames, String.class)));
|
||||
bean.setDependsOn(dependsOnList.toArray(new String[dependsOnList.size()]));
|
||||
bean.setDependsOn(dependsOnList.toArray(new String[0]));
|
||||
|
||||
return bean;
|
||||
}
|
||||
@@ -59,9 +60,9 @@ public abstract class SpringUtils {
|
||||
public static Optional<Object> getPropertyValue(BeanDefinition beanDefinition, String propertyName) {
|
||||
|
||||
return Optional.ofNullable(beanDefinition)
|
||||
.map(it -> it.getPropertyValues())
|
||||
.map(BeanDefinition::getPropertyValues)
|
||||
.map(propertyValues -> propertyValues.getPropertyValue(propertyName))
|
||||
.map(propertyValue -> propertyValue.getValue());
|
||||
.map(PropertyValue::getValue);
|
||||
}
|
||||
|
||||
public static BeanDefinition setPropertyReference(BeanDefinition beanDefinition,
|
||||
|
||||
@@ -80,7 +80,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
if (personRepository.count() == 0) {
|
||||
|
||||
if (this.personRepository.count() == 0) {
|
||||
sourDoe = personRepository.save(sourDoe);
|
||||
sandyHandy = personRepository.save(sandyHandy);
|
||||
jonDoe = personRepository.save(jonDoe);
|
||||
@@ -91,10 +92,11 @@ public class PersonRepositoryIntegrationTests {
|
||||
cookieDoe = personRepository.save(cookieDoe);
|
||||
}
|
||||
|
||||
assertThat(personRepository.count()).isEqualTo(8L);
|
||||
assertThat(this.personRepository.count()).isEqualTo(8L);
|
||||
}
|
||||
|
||||
protected <T> List<T> asList(Iterable<T> iterable) {
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
for (T element : iterable) {
|
||||
@@ -117,12 +119,13 @@ public class PersonRepositoryIntegrationTests {
|
||||
}
|
||||
|
||||
protected Sort newSort(Sort.Order... orders) {
|
||||
return new Sort(orders);
|
||||
return Sort.by(orders);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllPeopleSorted() {
|
||||
Iterable<Person> people = personRepository.findAll(newSort(newSortOrder("firstname")));
|
||||
|
||||
Iterable<Person> people = this.personRepository.findAll(newSort(newSortOrder("firstname")));
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
|
||||
@@ -135,7 +138,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findDistinctPeopleOrderedByLastnameDescendingFirstnameAscending() {
|
||||
List<Person> actualPeople = personRepository.findDistinctPeopleByOrderByLastnameDesc(
|
||||
|
||||
List<Person> actualPeople = this.personRepository.findDistinctPeopleByOrderByLastnameDesc(
|
||||
newSort(newSortOrder("firstname")));
|
||||
|
||||
assertThat(actualPeople).isNotNull();
|
||||
@@ -146,7 +150,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findDistinctPeopleByLastnameUnordered() {
|
||||
List<Person> actualPeople = personRepository.findDistinctByLastname("Handy", null);
|
||||
|
||||
List<Person> actualPeople = this.personRepository.findDistinctByLastname("Handy", null);
|
||||
|
||||
assertThat(actualPeople).isNotNull();
|
||||
assertThat(actualPeople.size()).isEqualTo(2);
|
||||
@@ -155,7 +160,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findDistinctPeopleByFirstOrLastNameWithSort() {
|
||||
Collection<Person> people = personRepository.findDistinctByFirstnameOrLastname("Cookie", "Pigg",
|
||||
|
||||
Collection<Person> people = this.personRepository.findDistinctByFirstnameOrLastname("Cookie", "Pigg",
|
||||
newSort(newSortOrder("lastname", Sort.Direction.DESC), newSortOrder("firstname", Sort.Direction.ASC)));
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
@@ -172,7 +178,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findPersonByFirstAndLastNameIgnoringCase() {
|
||||
Collection<Person> people = personRepository.findByFirstnameIgnoreCaseAndLastnameIgnoreCase("jON", "doE");
|
||||
|
||||
Collection<Person> people = this.personRepository.findByFirstnameIgnoreCaseAndLastnameIgnoreCase("jON", "doE");
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
assertThat(people.size()).isEqualTo(1);
|
||||
@@ -181,7 +188,8 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findByFirstAndLastNameAllIgnoringCase() {
|
||||
Collection<Person> people = personRepository.findByFirstnameAndLastnameAllIgnoringCase("IMa", "PIGg");
|
||||
|
||||
Collection<Person> people = this.personRepository.findByFirstnameAndLastnameAllIgnoringCase("IMa", "PIGg");
|
||||
|
||||
assertThat(people).isNotNull();
|
||||
assertThat(people.size()).isEqualTo(1);
|
||||
@@ -195,6 +203,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
public static class GemFireConfiguration {
|
||||
|
||||
Properties gemfireProperties() {
|
||||
|
||||
Properties gemfireProperties = new Properties();
|
||||
|
||||
gemfireProperties.setProperty("name", applicationName());
|
||||
@@ -215,6 +224,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Bean
|
||||
CacheFactoryBean gemfireCache() {
|
||||
|
||||
CacheFactoryBean gemfireCache = new CacheFactoryBean();
|
||||
|
||||
gemfireCache.setClose(true);
|
||||
@@ -225,6 +235,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
|
||||
@Bean(name = "simple")
|
||||
LocalRegionFactoryBean simpleRegion(Cache gemfireCache, RegionAttributes<Long, Person> simpleRegionAttributes) {
|
||||
|
||||
LocalRegionFactoryBean<Long, Person> simpleRegion = new LocalRegionFactoryBean<Long, Person>();
|
||||
|
||||
simpleRegion.setAttributes(simpleRegionAttributes);
|
||||
@@ -238,6 +249,7 @@ public class PersonRepositoryIntegrationTests {
|
||||
@Bean
|
||||
@SuppressWarnings("unchecked")
|
||||
RegionAttributesFactoryBean simpleRegionAttributes() {
|
||||
|
||||
RegionAttributesFactoryBean simpleRegionAttributes = new RegionAttributesFactoryBean();
|
||||
|
||||
simpleRegionAttributes.setKeyConstraint(Long.class);
|
||||
|
||||
@@ -37,9 +37,11 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.LocalRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.support.ReflectionEntityInformation;
|
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@@ -58,7 +60,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
@SuppressWarnings("unused")
|
||||
public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning";
|
||||
static final String GEMFIRE_LOG_LEVEL = "warning";
|
||||
|
||||
@Autowired
|
||||
private GemfireTemplate template;
|
||||
@@ -71,33 +73,45 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
private SimpleGemfireRepository<Person, Long> repository;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("all")
|
||||
public void setUp() {
|
||||
people.clear();
|
||||
regionClearListener = new RegionClearListener();
|
||||
people.getAttributesMutator().addCacheListener(regionClearListener);
|
||||
EntityInformation<Person, Long> information = new ReflectionEntityInformation<>(Person.class);
|
||||
repository = new SimpleGemfireRepository<>(template, information);
|
||||
|
||||
this.people.clear();
|
||||
this.regionClearListener = new RegionClearListener();
|
||||
this.people.getAttributesMutator().addCacheListener(this.regionClearListener);
|
||||
|
||||
GemfireMappingContext mappingContext = new GemfireMappingContext();
|
||||
|
||||
GemfirePersistentEntity<Person> personEntity =
|
||||
(GemfirePersistentEntity<Person>) mappingContext.getPersistentEntity(Person.class);
|
||||
|
||||
EntityInformation<Person, Long> information = new PersistentEntityInformation<>(personEntity);
|
||||
|
||||
this.repository = new SimpleGemfireRepository<>(this.template, information);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteAllFiresClearEvent() {
|
||||
assertThat(regionClearListener.eventFired).isFalse();
|
||||
repository.deleteAll();
|
||||
assertThat(regionClearListener.eventFired).isTrue();
|
||||
|
||||
assertThat(this.regionClearListener.eventFired).isFalse();
|
||||
|
||||
this.repository.deleteAll();
|
||||
|
||||
assertThat(this.regionClearListener.eventFired).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllWithIds() {
|
||||
|
||||
Person dave = new Person(1L, "Dave", "Matthews");
|
||||
Person carter = new Person(2L, "Carter", "Beauford");
|
||||
Person leroi = new Person(3L, "Leroi", "Moore");
|
||||
|
||||
template.put(dave.getId(), dave);
|
||||
template.put(carter.getId(), carter);
|
||||
template.put(leroi.getId(), leroi);
|
||||
this.template.put(dave.getId(), dave);
|
||||
this.template.put(carter.getId(), carter);
|
||||
this.template.put(leroi.getId(), leroi);
|
||||
|
||||
Collection<Person> result = repository.findAllById(Arrays.asList(carter.getId(), leroi.getId()));
|
||||
Collection<Person> result = this.repository.findAllById(Arrays.asList(carter.getId(), leroi.getId()));
|
||||
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.size()).isEqualTo(2);
|
||||
@@ -106,7 +120,8 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findAllWithIdsReturnsNoMatches() {
|
||||
Collection<Person> results = repository.findAllById(Arrays.asList(1L, 2L));
|
||||
|
||||
Collection<Person> results = this.repository.findAllById(Arrays.asList(1L, 2L));
|
||||
|
||||
assertThat(results).isNotNull();
|
||||
assertThat(results).isEmpty();
|
||||
@@ -114,14 +129,15 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void findAllWithIdsReturnsPartialMatches() {
|
||||
|
||||
Person kurt = new Person(1L, "Kurt", "Cobain");
|
||||
Person eddie = new Person(2L, "Eddie", "Veddar");
|
||||
Person michael = new Person(3L, "Michael", "Jackson");
|
||||
|
||||
template.put(kurt.getId(), kurt);
|
||||
template.put(eddie.getId(), eddie);
|
||||
this.template.put(kurt.getId(), kurt);
|
||||
this.template.put(eddie.getId(), eddie);
|
||||
|
||||
Collection<Person> results = repository.findAllById(Arrays.asList(0L, 1L, 2L, 4L));
|
||||
Collection<Person> results = this.repository.findAllById(Arrays.asList(0L, 1L, 2L, 4L));
|
||||
|
||||
assertThat(results).isNotNull();
|
||||
assertThat(results).hasSize(2);
|
||||
@@ -130,12 +146,13 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryRegion() throws Exception {
|
||||
public void queryRegion() {
|
||||
|
||||
Person oliverGierke = new Person(1L, "Oliver", "Gierke");
|
||||
|
||||
assertThat(template.put(oliverGierke.getId(), oliverGierke)).isNull();
|
||||
assertThat(this.template.put(oliverGierke.getId(), oliverGierke)).isNull();
|
||||
|
||||
SelectResults<Person> people = template.find("SELECT * FROM /People p WHERE p.firstname = $1",
|
||||
SelectResults<Person> people = this.template.find("SELECT * FROM /People p WHERE p.firstname = $1",
|
||||
oliverGierke.getFirstname());
|
||||
|
||||
assertThat(people.size()).isEqualTo(1);
|
||||
@@ -144,34 +161,36 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void saveAndDeleteEntity() {
|
||||
|
||||
Person oliverGierke = new Person(1L, "Oliver", "Gierke");
|
||||
|
||||
assertThat(repository.save(oliverGierke)).isEqualTo(oliverGierke);
|
||||
assertThat(repository.count()).isEqualTo(1L);
|
||||
assertThat(repository.findById(oliverGierke.getId()).orElse(null)).isEqualTo(oliverGierke);
|
||||
assertThat(repository.findAll()).isEqualTo(Collections.singletonList(oliverGierke));
|
||||
assertThat(this.repository.save(oliverGierke)).isEqualTo(oliverGierke);
|
||||
assertThat(this.repository.count()).isEqualTo(1L);
|
||||
assertThat(this.repository.findById(oliverGierke.getId()).orElse(null)).isEqualTo(oliverGierke);
|
||||
assertThat(this.repository.findAll()).isEqualTo(Collections.singletonList(oliverGierke));
|
||||
|
||||
repository.delete(oliverGierke);
|
||||
this.repository.delete(oliverGierke);
|
||||
|
||||
assertThat(repository.count()).isEqualTo(0L);
|
||||
assertThat(repository.findById(oliverGierke.getId()).orElse(null)).isNull();
|
||||
assertThat(repository.findAll()).isEmpty();
|
||||
assertThat(this.repository.count()).isEqualTo(0L);
|
||||
assertThat(this.repository.findById(oliverGierke.getId()).orElse(null)).isNull();
|
||||
assertThat(this.repository.findAll()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveEntities() {
|
||||
assertThat(template.getRegion()).isEmpty();
|
||||
|
||||
assertThat(this.template.getRegion()).isEmpty();
|
||||
|
||||
Person johnBlum = new Person(1L, "John", "Blum");
|
||||
Person jonBloom = new Person(2L, "Jon", "Bloom");
|
||||
Person juanBlume = new Person(3L, "Juan", "Blume");
|
||||
|
||||
repository.saveAll(Arrays.asList(johnBlum, jonBloom, juanBlume));
|
||||
this.repository.saveAll(Arrays.asList(johnBlum, jonBloom, juanBlume));
|
||||
|
||||
assertThat(template.getRegion().size()).isEqualTo(3);
|
||||
assertThat(template.<Long, Person>get(johnBlum.getId())).isEqualTo(johnBlum);
|
||||
assertThat(template.<Long, Person>get(jonBloom.getId())).isEqualTo(jonBloom);
|
||||
assertThat(template.<Long, Person>get(juanBlume.getId())).isEqualTo(juanBlume);
|
||||
assertThat(this.template.getRegion().size()).isEqualTo(3);
|
||||
assertThat((Person) this.template.get(johnBlum.getId())).isEqualTo(johnBlum);
|
||||
assertThat((Person) this.template.get(jonBloom.getId())).isEqualTo(jonBloom);
|
||||
assertThat((Person) this.template.get(juanBlume.getId())).isEqualTo(juanBlume);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@@ -180,16 +199,17 @@ public class SimpleGemfireRepositoryIntegrationTests {
|
||||
volatile boolean eventFired;
|
||||
|
||||
@Override
|
||||
public void afterRegionClear(RegionEvent ev) {
|
||||
eventFired = true;
|
||||
public void afterRegionClear(RegionEvent event) {
|
||||
this.eventFired = true;
|
||||
}
|
||||
}
|
||||
|
||||
@PeerCacheApplication(name = "SimpleGemfireRepositoryIntegrationTests", logLevel = DEFAULT_GEMFIRE_LOG_LEVEL)
|
||||
@PeerCacheApplication(name = "SimpleGemfireRepositoryIntegrationTests", logLevel = GEMFIRE_LOG_LEVEL)
|
||||
static class SimpleGemfireRepositoryConfiguration {
|
||||
|
||||
@Bean(name = "People")
|
||||
LocalRegionFactoryBean<Object, Object> peopleRegion(GemFireCache gemfireCache) {
|
||||
|
||||
LocalRegionFactoryBean<Object, Object> peopleRegion = new LocalRegionFactoryBean<>();
|
||||
|
||||
peopleRegion.setCache(gemfireCache);
|
||||
|
||||
@@ -35,9 +35,12 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.repository.GemfireRepository;
|
||||
import org.springframework.data.gemfire.repository.sample.Customer;
|
||||
import org.springframework.data.repository.core.support.ReflectionEntityInformation;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
@@ -73,22 +76,26 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
|
||||
private Region customers;
|
||||
|
||||
static Customer createCustomer(String firstName, String lastName) {
|
||||
|
||||
Customer customer = new SerializableCustomer(firstName, lastName);
|
||||
|
||||
customer.setId(ID_SEQUENCE.incrementAndGet());
|
||||
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
assertNotNull("The 'Customers' GemFire Cache Region was not properly configured and initialized!", customers);
|
||||
assertEquals("Customers", customers.getName());
|
||||
assertEquals("/Customers", customers.getFullPath());
|
||||
assertTrue(customers.isEmpty());
|
||||
|
||||
assertNotNull("The 'Customers' Cache Region was not properly configured and initialized!", this.customers);
|
||||
assertEquals("Customers", this.customers.getName());
|
||||
assertEquals("/Customers", this.customers.getFullPath());
|
||||
assertTrue(this.customers.isEmpty());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
customers.clear();
|
||||
this.customers.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -100,24 +107,24 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
|
||||
expectedCustomers.add(createCustomer("Pie", "Doe"));
|
||||
expectedCustomers.add(createCustomer("Cookie", "Doe"));
|
||||
|
||||
customerService.saveAll(expectedCustomers);
|
||||
this.customerService.saveAll(expectedCustomers);
|
||||
|
||||
assertFalse(customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), customers.size());
|
||||
assertFalse(this.customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), this.customers.size());
|
||||
|
||||
try {
|
||||
customerService.removeAllCausingTransactionRollback();
|
||||
this.customerService.removeAllCausingTransactionRollback();
|
||||
}
|
||||
catch (RuntimeException ignore) {
|
||||
// the RuntimeException should cause the Cache Transaction to rollback and avoid the Region modification!
|
||||
}
|
||||
|
||||
assertFalse(customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), customers.size());
|
||||
assertFalse(this.customers.isEmpty());
|
||||
assertEquals(expectedCustomers.size(), this.customers.size());
|
||||
|
||||
customerService.removeAll();
|
||||
this.customerService.removeAll();
|
||||
|
||||
assertTrue(customers.isEmpty());
|
||||
assertTrue(this.customers.isEmpty());
|
||||
}
|
||||
|
||||
public static class SerializableCustomer extends Customer implements Serializable {
|
||||
@@ -141,34 +148,52 @@ public class SimpleGemfireRepositoryTransactionalIntegrationTest {
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@Autowired
|
||||
@SuppressWarnings("all")
|
||||
public CustomerService(GemfireTemplate customersTemplate, PlatformTransactionManager transactionManager) {
|
||||
customerRepository = new SimpleGemfireRepository<Customer, Long>(customersTemplate,
|
||||
new ReflectionEntityInformation<Customer, Long>(Customer.class));
|
||||
|
||||
transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
GemfireMappingContext mappingContext = new GemfireMappingContext();
|
||||
|
||||
GemfirePersistentEntity<Customer> customerEntity =
|
||||
(GemfirePersistentEntity<Customer>) mappingContext.getPersistentEntity(Customer.class);
|
||||
|
||||
EntityInformation<Customer, Long> entityInformation = new PersistentEntityInformation<>(customerEntity);
|
||||
|
||||
this.customerRepository = new SimpleGemfireRepository<Customer, Long>(customersTemplate, entityInformation);
|
||||
this.transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
}
|
||||
|
||||
void saveAll(final Iterable<Customer> customers) {
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
|
||||
customerRepository.saveAll(customers);
|
||||
|
||||
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
CustomerService.this.customerRepository.saveAll(customers);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void removeAllCausingTransactionRollback() {
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
|
||||
|
||||
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
|
||||
removeAll();
|
||||
|
||||
throw new IllegalStateException("'removeAll' operation not permitted");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void removeAll() {
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override protected void doInTransactionWithoutResult(final TransactionStatus status) {
|
||||
customerRepository.deleteAll();
|
||||
|
||||
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
CustomerService.this.customerRepository.deleteAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -52,6 +51,7 @@ import org.springframework.data.gemfire.repository.sample.User;
|
||||
import org.springframework.data.gemfire.support.sample.TestUserDao;
|
||||
import org.springframework.data.gemfire.support.sample.TestUserService;
|
||||
import org.springframework.data.gemfire.test.support.DataSourceAdapter;
|
||||
import org.springframework.data.gemfire.util.SpringUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -77,7 +77,7 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
private static final Object MUTEX_LOCK = new Object();
|
||||
|
||||
protected static final String GEMFIRE_LOCATORS = "localhost[11235]";
|
||||
protected static final String GEMFIRE_LOG_LEVEL = "warning";
|
||||
protected static final String GEMFIRE_LOG_LEVEL = "error";
|
||||
protected static final String GEMFIRE_JMX_MANAGER = "true";
|
||||
protected static final String GEMFIRE_JMX_MANAGER_PORT = "1199";
|
||||
protected static final String GEMFIRE_JMX_MANAGER_START = "true";
|
||||
@@ -87,56 +87,52 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
setupBeforeCacheCreate();
|
||||
}
|
||||
|
||||
private void setupBeforeCacheCreate() {
|
||||
try {
|
||||
long timeout = (System.currentTimeMillis() + CACHE_CLOSE_TIMEOUT);
|
||||
|
||||
long timeout = System.currentTimeMillis() + CACHE_CLOSE_TIMEOUT;
|
||||
|
||||
while (CacheFactory.getAnyInstance() != null && System.currentTimeMillis() < timeout) {
|
||||
synchronized (MUTEX_LOCK) {
|
||||
try {
|
||||
System.out.printf("Waiting in setup...%n");
|
||||
MUTEX_LOCK.wait(500l);
|
||||
}
|
||||
catch (InterruptedException ignore) {
|
||||
MUTEX_LOCK.wait(500L);
|
||||
}
|
||||
catch (InterruptedException ignore) { }
|
||||
}
|
||||
}
|
||||
|
||||
fail(String.format("The Cache instance was not properly closed in the allotted timeout of %1$d seconds!%n",
|
||||
(CACHE_CLOSE_TIMEOUT / 1000)));
|
||||
}
|
||||
catch (CacheClosedException ignore) {
|
||||
fail(String.format("The Cache instance was not properly closed in the allotted timeout of %d seconds%n",
|
||||
TimeUnit.MILLISECONDS.toSeconds(CACHE_CLOSE_TIMEOUT)));
|
||||
}
|
||||
catch (CacheClosedException ignore) { }
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
SpringContextBootstrappingInitializer.getApplicationContext().close();
|
||||
UserDataStoreCacheLoader.INSTANCE.set(null);
|
||||
tearDownCache();
|
||||
}
|
||||
|
||||
private void tearDownCache() {
|
||||
|
||||
try {
|
||||
|
||||
Cache cache = CacheFactory.getAnyInstance();
|
||||
|
||||
if (cache != null) {
|
||||
System.out.printf("Closing Cache...%n");
|
||||
|
||||
cache.close();
|
||||
|
||||
// Now, wait for the GemFire Hog to shutdown, OIY!
|
||||
// Now, wait for the Apache Geode or Pivotal GemFire Hog to shutdown, OIY!
|
||||
synchronized (MUTEX_LOCK) {
|
||||
while (!cache.isClosed()) {
|
||||
try {
|
||||
System.out.printf("Waiting in tearDown...");
|
||||
MUTEX_LOCK.wait(500l);
|
||||
|
||||
SpringUtils.safeRunOperation(() -> {
|
||||
while (!cache.isClosed()) {
|
||||
MUTEX_LOCK.wait(500L);
|
||||
}
|
||||
catch (InterruptedException ignore) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
MUTEX_LOCK.notifyAll();
|
||||
}
|
||||
@@ -149,13 +145,12 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
protected void doSpringContextBootstrappingInitializationTest(final String cacheXmlFile) {
|
||||
protected void doSpringContextBootstrappingInitializationTest(String cacheXmlFile) {
|
||||
|
||||
Cache gemfireCache = new CacheFactory()
|
||||
.set("name", GEMFIRE_NAME)
|
||||
.set("mcast-port", GEMFIRE_MCAST_PORT)
|
||||
.set("log-level", GEMFIRE_LOG_LEVEL)
|
||||
.set("cache-xml-file", cacheXmlFile)
|
||||
//.set("locators", GEMFIRE_LOCATORS)
|
||||
//.set("start-locator", GEMFIRE_LOCATORS)
|
||||
//.set("jmx-manager", GEMFIRE_JMX_MANAGER)
|
||||
//.set("jmx-manager-port", GEMFIRE_JMX_MANAGER_PORT)
|
||||
@@ -173,12 +168,13 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
assertNotNull(gemfireCache.getRegion("/TestRegion"));
|
||||
assertNotNull(gemfireCache.getRegion("/Users"));
|
||||
|
||||
ConfigurableApplicationContext applicationContext = SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
ConfigurableApplicationContext applicationContext =
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
|
||||
assertNotNull(applicationContext);
|
||||
assertTrue(applicationContext.containsBean(GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
|
||||
assertTrue(applicationContext.containsBean("TestRegion"));
|
||||
assertFalse(applicationContext.containsBean("Users")); // Region 'Users' is defined in GemFire cache.xml
|
||||
assertFalse(applicationContext.containsBean("Users")); // Region 'Users' is defined in Pivotal GemFire cache.xml
|
||||
assertTrue(applicationContext.containsBean("userDataSource"));
|
||||
assertTrue(applicationContext.containsBean("userDao"));
|
||||
assertTrue(applicationContext.containsBean("userService"));
|
||||
@@ -190,7 +186,7 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
assertSame(userDataSource, userDao.getDataSource());
|
||||
assertSame(userDao, userService.getUserDao());
|
||||
|
||||
// NOTE a GemFire declared component initialized by Spring!
|
||||
// NOTE Pivotal GemFire declared component initialized by Spring!
|
||||
UserDataStoreCacheLoader usersCacheLoader = UserDataStoreCacheLoader.getInstance();
|
||||
|
||||
assertSame(userDataSource, usersCacheLoader.getDataSource());
|
||||
@@ -210,9 +206,10 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testSpringContextBootstrappingInitializerUsingAnnotatedClasses() {
|
||||
|
||||
SpringContextBootstrappingInitializer.register(TestAppConfig.class);
|
||||
|
||||
new SpringContextBootstrappingInitializer().init(new Properties());
|
||||
new SpringContextBootstrappingInitializer().init(null, new Properties());
|
||||
|
||||
ConfigurableApplicationContext applicationContext = SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
|
||||
@@ -231,7 +228,8 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testSpringContextBootstrappingInitializerUsingContextConfigLocations() {
|
||||
doSpringContextBootstrappingInitializationTest("cache-with-spring-context-bootstrap-initializer.xml");
|
||||
doSpringContextBootstrappingInitializationTest(
|
||||
"cache-with-spring-context-bootstrap-initializer.xml");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -248,14 +246,14 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
public static final class TestDataSource extends DataSourceAdapter {
|
||||
}
|
||||
public static final class TestDataSource extends DataSourceAdapter { }
|
||||
|
||||
public static final class UserDataStoreCacheLoader extends LazyWiringDeclarableSupport implements CacheLoader<String, User> {
|
||||
public static final class UserDataStoreCacheLoader extends LazyWiringDeclarableSupport
|
||||
implements CacheLoader<String, User> {
|
||||
|
||||
private static final AtomicReference<UserDataStoreCacheLoader> INSTANCE = new AtomicReference<UserDataStoreCacheLoader>();
|
||||
private static final AtomicReference<UserDataStoreCacheLoader> INSTANCE = new AtomicReference<>();
|
||||
|
||||
private static final Map<String, User> USER_DATA = new ConcurrentHashMap<String, User>(3);
|
||||
private static final Map<String, User> USER_DATA = new ConcurrentHashMap<>(3);
|
||||
|
||||
static {
|
||||
USER_DATA.put("jblum", new User("jblum"));
|
||||
@@ -279,10 +277,12 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
|
||||
protected static User createUser(String username, Boolean active, Calendar since, String email) {
|
||||
|
||||
User user = new User(username);
|
||||
user.setActive(active);
|
||||
user.setEmail(email);
|
||||
user.setSince(since);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -291,32 +291,35 @@ public class SpringContextBootstrappingInitializerIntegrationTest {
|
||||
}
|
||||
|
||||
public UserDataStoreCacheLoader() {
|
||||
Assert.state(INSTANCE.compareAndSet(null, this), String.format("An instance of %1$s was already created!",
|
||||
getClass().getName()));
|
||||
Assert.state(INSTANCE.compareAndSet(null, this),
|
||||
String.format("An instance of %1$s was already created!", getClass().getName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void assertInitialized() {
|
||||
|
||||
super.assertInitialized();
|
||||
Assert.state(userDataSource != null, String.format(
|
||||
"The 'User' Data Source was not properly configured and initialized for use in (%1$s!)",
|
||||
|
||||
Assert.state(this.userDataSource != null,
|
||||
String.format("The 'User' Data Source was not properly configured and initialized for use in (%s)",
|
||||
getClass().getName()));
|
||||
}
|
||||
|
||||
protected DataSource getDataSource() {
|
||||
return userDataSource;
|
||||
return this.userDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
userDataSource = null;
|
||||
this.userDataSource = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User load(final LoaderHelper<String, User> helper) throws CacheLoaderException {
|
||||
public User load(LoaderHelper<String, User> helper) throws CacheLoaderException {
|
||||
|
||||
assertInitialized();
|
||||
|
||||
return USER_DATA.get(helper.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,22 +16,39 @@
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.isA;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.geode.cache.Cache;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Matchers;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -64,11 +81,27 @@ import org.springframework.util.ObjectUtils;
|
||||
@SuppressWarnings("unused")
|
||||
public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
private static Properties createParameters(String parameter, String value) {
|
||||
|
||||
Properties parameters = new Properties();
|
||||
|
||||
parameters.setProperty(parameter, value);
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private static Properties createParameters(Properties parameters, String parameter, String value) {
|
||||
|
||||
parameters.setProperty(parameter, value);
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private Cache mockCache = mock(Cache.class);
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
SpringContextBootstrappingInitializer.applicationContext = null;
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent = null;
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(null);
|
||||
@@ -76,21 +109,11 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
SpringContextBootstrappingInitializer.unregister(TestAppConfigTwo.class);
|
||||
}
|
||||
|
||||
protected static Properties createParameters(final String parameter, final String value) {
|
||||
Properties parameters = new Properties();
|
||||
parameters.setProperty(parameter, value);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
protected static Properties createParameters(final Properties parameters, final String parameter, final String value) {
|
||||
parameters.setProperty(parameter, value);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInitializedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testGetApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testGetApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
|
||||
|
||||
@@ -98,25 +121,36 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getUninitializedApplicationContext() {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage("A Spring ApplicationContext was not configured and initialized properly");
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
try {
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("A Spring ApplicationContext was not configured and initialized properly"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoader() {
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.applicationContext, is(nullValue()));
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoaderWhenApplicationContextIsInactive() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(false);
|
||||
|
||||
@@ -126,114 +160,161 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, times(1)).isActive();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void setBeanClassLoaderWithCurrentThreadContextClassLoaderWhenApplicationContextIsActive() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(true);
|
||||
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("A Spring ApplicationContext has already been initialized");
|
||||
|
||||
try {
|
||||
SpringContextBootstrappingInitializer.applicationContext = mockApplicationContext;
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("A Spring ApplicationContext has already been initialized"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
finally {
|
||||
verify(mockApplicationContext, times(1)).isActive();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void createApplicationContextWhenAnnotatedClassesBasePackagesAndConfigLocationsAreUnspecified() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified"
|
||||
+ " in order to construct and configure an instance of the ConfigurableApplicationContext");
|
||||
|
||||
new SpringContextBootstrappingInitializer().createApplicationContext(null, null);
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().createApplicationContext(null, null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("'AnnotatedClasses', 'basePackages' or 'configLocations' must be specified"
|
||||
+ " in order to construct and configure an instance of the ConfigurableApplicationContext"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAnnotationApplicationContextWithAnnotatedClasses() {
|
||||
final AnnotationConfigApplicationContext mockAnnotationApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockAnnotationApplicationContext");
|
||||
public void createAnnotationBasedApplicationContextWithAnnotatedClasses() {
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
AnnotationConfigApplicationContext mockAnnotationApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockAnnotationApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockXmlApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockXmlApplicationContext");
|
||||
|
||||
Class<?>[] annotatedClasses = { TestAppConfigOne.class, TestAppConfigTwo.class };
|
||||
|
||||
SpringContextBootstrappingInitializer.register(annotatedClasses[0]);
|
||||
SpringContextBootstrappingInitializer.register(annotatedClasses[1]);
|
||||
Arrays.stream(annotatedClasses).forEach(SpringContextBootstrappingInitializer::register);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext);
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
|
||||
return ObjectUtils.isEmpty(configLocations)
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(null, null);
|
||||
/*
|
||||
doAnswer(invocationOnMock ->
|
||||
ObjectUtils.isEmpty(invocationOnMock.getArgument(0))
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext
|
||||
).when(initializer).createApplicationContext(any(String[].class));
|
||||
*/
|
||||
|
||||
assertThat(actualApplicationContext,
|
||||
is(sameInstance((ConfigurableApplicationContext) mockAnnotationApplicationContext)));
|
||||
doReturn(mockAnnotationApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
verify(mockAnnotationApplicationContext, times(1)).register(annotatedClasses[0], annotatedClasses[1]);
|
||||
ConfigurableApplicationContext actualApplicationContext =
|
||||
initializer.createApplicationContext(null, null);
|
||||
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockAnnotationApplicationContext)));
|
||||
|
||||
verify(initializer, times(1))
|
||||
.doRegister(eq(mockAnnotationApplicationContext), eq(annotatedClasses));
|
||||
|
||||
verifyZeroInteractions(mockXmlApplicationContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAnnotationApplicationContextWithBasePackages() {
|
||||
final AnnotationConfigApplicationContext mockAnnotationApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockAnnotationApplicationContext");
|
||||
public void createAnnotationBasedApplicationContextWithBasePackages() {
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
AnnotationConfigApplicationContext mockAnnotationApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class,"MockAnnotationApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockXmlApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockXmlApplicationContext");
|
||||
|
||||
String[] basePackages = { "org.example.app" };
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext);
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
|
||||
return ObjectUtils.isEmpty(configLocations)
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(basePackages, null);
|
||||
doReturn(mockAnnotationApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
assertThat(actualApplicationContext,
|
||||
is(sameInstance((ConfigurableApplicationContext) mockAnnotationApplicationContext)));
|
||||
ConfigurableApplicationContext actualApplicationContext =
|
||||
initializer.createApplicationContext(basePackages, null);
|
||||
|
||||
verify(mockAnnotationApplicationContext, times(1)).scan(eq(basePackages[0]));
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockAnnotationApplicationContext)));
|
||||
|
||||
verify(initializer, times(1))
|
||||
.scanBasePackages(eq(mockAnnotationApplicationContext), eq(basePackages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createXmlApplicationContext() {
|
||||
final ConfigurableApplicationContext mockAnnotationApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockAnnotationApplicationContext");
|
||||
public void createXmlBasedApplicationContext() {
|
||||
|
||||
final ConfigurableApplicationContext mockXmlApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockXmlApplicationContext");
|
||||
ConfigurableApplicationContext mockAnnotationApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockAnnotationApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockXmlApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockXmlApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
return (ObjectUtils.isEmpty(configLocations) ? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext);
|
||||
|
||||
@Override
|
||||
ConfigurableApplicationContext createApplicationContext(final String[] configLocations) {
|
||||
|
||||
return ObjectUtils.isEmpty(configLocations)
|
||||
? mockAnnotationApplicationContext
|
||||
: mockXmlApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
ConfigurableApplicationContext actualApplicationContext = initializer.createApplicationContext(null,
|
||||
new String[] { "/path/to/application/context.xml" });
|
||||
ConfigurableApplicationContext actualApplicationContext =
|
||||
initializer.createApplicationContext(null, new String[] { "/path/to/application/context.xml" });
|
||||
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockXmlApplicationContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initApplicationContext() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AbstractApplicationContext mockApplicationContext =
|
||||
mock(AbstractApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
@@ -246,19 +327,28 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, times(1)).setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void initApplicationContextWithNull() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("ConfigurableApplicationContext must not be null");
|
||||
|
||||
new SpringContextBootstrappingInitializer().initApplicationContext(null);
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().initApplicationContext(null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("ConfigurableApplicationContext must not be null"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void refreshApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().refreshApplicationContext(mockApplicationContext),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
@@ -266,100 +356,150 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, times(1)).refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void refreshApplicationContextWithNull() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("ConfigurableApplicationContext must not be null");
|
||||
|
||||
new SpringContextBootstrappingInitializer().refreshApplicationContext(null);
|
||||
try {
|
||||
new SpringContextBootstrappingInitializer().refreshApplicationContext(null);
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
|
||||
assertThat(expected.getMessage(),
|
||||
containsString("ConfigurableApplicationContext must not be null"));
|
||||
|
||||
assertThat(expected.getCause(), is(nullValue(Throwable.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAnnotatedClasses() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class,"MockApplicationContext");
|
||||
|
||||
Class<?>[] annotatedClasses = { TestAppConfigOne.class, TestAppConfigTwo.class };
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer()
|
||||
.registerAnnotatedClasses(mockApplicationContext, annotatedClasses),
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
assertThat(initializer.registerAnnotatedClasses(mockApplicationContext, annotatedClasses),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).register(annotatedClasses);
|
||||
verify(initializer, times(1))
|
||||
.doRegister(eq(mockApplicationContext), eq(annotatedClasses));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAnnotatedClassesWithEmptyAnnotatedClassesArray() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext,
|
||||
new Class<?>[0]),
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
assertThat(initializer.registerAnnotatedClasses(mockApplicationContext, new Class<?>[0]),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, never()).register(any(Class[].class));
|
||||
verify(initializer, never()).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAnnotatedClassesWithNonAnnotationBasedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().registerAnnotatedClasses(mockApplicationContext,
|
||||
new Class<?>[] { TestAppConfigOne.class }), is(sameInstance(mockApplicationContext)));
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
assertThat(initializer.registerAnnotatedClasses(mockApplicationContext, new Class<?>[] { TestAppConfigOne.class }),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(initializer, never()).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scanBasePackages() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
String[] basePackages = { "org.example.app", "org.example.plugins" };
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, basePackages),
|
||||
assertThat(initializer.scanBasePackages(mockApplicationContext, basePackages),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).scan(basePackages);
|
||||
verify(initializer, times(1)).doScan(eq(mockApplicationContext), eq(basePackages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scanBasePackagesWithEmptyBasePackagesArray() {
|
||||
AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext, null),
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
assertThat(initializer.scanBasePackages(mockApplicationContext, null),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, never()).scan(any(String[].class));
|
||||
verify(initializer, never()).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scanBasePackagesWithNonAnnotationBasedApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().scanBasePackages(mockApplicationContext,
|
||||
new String[] { "org.example.app" }), is(sameInstance(mockApplicationContext)));
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer());
|
||||
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
|
||||
assertThat(initializer.scanBasePackages(mockApplicationContext, new String[] { "org.example.app" }),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(initializer, never()).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setClassLoader() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AbstractApplicationContext mockApplicationContext =
|
||||
mock(AbstractApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
assertThat(new SpringContextBootstrappingInitializer().setClassLoader(mockApplicationContext),
|
||||
is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
verify(mockApplicationContext, times(1))
|
||||
.setClassLoader(eq(Thread.currentThread().getContextClassLoader()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setClassLoaderWithNonSettableClassLoaderApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
@@ -369,8 +509,9 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void setClassLoaderWithNullClassLoader() {
|
||||
AbstractApplicationContext mockApplicationContext = mock(AbstractApplicationContext.class,
|
||||
"MockApplicationContext");
|
||||
|
||||
AbstractApplicationContext mockApplicationContext =
|
||||
mock(AbstractApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
SpringContextBootstrappingInitializer.setBeanClassLoader(null);
|
||||
|
||||
@@ -380,13 +521,6 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
verify(mockApplicationContext, never()).setClassLoader(any(ClassLoader.class));
|
||||
}
|
||||
|
||||
private Class<?>[] annotatedClasses(final Class<?>... annotatedClasses) {
|
||||
return argThat(argument -> {
|
||||
assertThat(argument instanceof Class<?>[], is(true));
|
||||
return Arrays.equals(annotatedClasses, (Class<?>[]) argument);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullSafeGetApplicationContextIdWithNullReference() {
|
||||
assertThat(new SpringContextBootstrappingInitializer().nullSafeGetApplicationContextId(null), is(nullValue()));
|
||||
@@ -394,6 +528,7 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void nullSafeGetApplicationContextIdWithNonNullReference() {
|
||||
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class, "MockApplicationContext");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("123");
|
||||
@@ -404,14 +539,13 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void testInitWithAnnotatedClasses() {
|
||||
final AnnotationConfigApplicationContext mockApplicationContext = mock(AnnotationConfigApplicationContext.class,
|
||||
"testInitWithAnnotatedClasses");
|
||||
|
||||
AnnotationConfigApplicationContext mockApplicationContext =
|
||||
mock(AnnotationConfigApplicationContext.class, "testInitWithAnnotatedClasses");
|
||||
|
||||
doNothing().when(mockApplicationContext).addApplicationListener(any(ApplicationListener.class));
|
||||
doNothing().when(mockApplicationContext).registerShutdownHook();
|
||||
doNothing().when(mockApplicationContext).refresh();
|
||||
doNothing().when(mockApplicationContext).register(Matchers.<Class<?>[]>anyVararg());
|
||||
//doNothing().when(mockApplicationContext).register(annotatedClasses(TestAppConfigOne.class, TestAppConfigTwo.class));
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWithAnnotatedClasses");
|
||||
when(mockApplicationContext.isRunning()).thenReturn(true);
|
||||
@@ -421,28 +555,33 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
SpringContextBootstrappingInitializer.register(TestAppConfigOne.class);
|
||||
SpringContextBootstrappingInitializer.register(TestAppConfigTwo.class);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override protected ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
SpringContextBootstrappingInitializer initializer = spy(new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
initializer.init(createParameters("test", "test"));
|
||||
doReturn(mockApplicationContext)
|
||||
.when(initializer).doRegister(any(ConfigurableApplicationContext.class), any(Class[].class));
|
||||
|
||||
initializer.init(this.mockCache, createParameters("test", "test"));
|
||||
|
||||
verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
verify(mockApplicationContext, times(1)).registerShutdownHook();
|
||||
verify(mockApplicationContext, times(1)).register(TestAppConfigOne.class, TestAppConfigTwo.class);
|
||||
//verify(mockApplicationContext, times(1)).register(annotatedClasses(TestAppConfigOne.class, TestAppConfigTwo.class));
|
||||
//verify(mockApplicationContext, times(1)).register(Matchers.<Class<?>[]>anyVararg());
|
||||
verify(mockApplicationContext, never()).scan(any(String[].class));
|
||||
verify(initializer, never()).doScan(any(ConfigurableApplicationContext.class), any(String[].class));
|
||||
verify(initializer, times(1)).doRegister(eq(mockApplicationContext), eq(new Class[] {
|
||||
TestAppConfigOne.class, TestAppConfigTwo.class }));
|
||||
|
||||
assertEquals(mockApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitWithExistingApplicationContext() {
|
||||
ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWithExistingApplicationContext");
|
||||
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testInitWithExistingApplicationContext");
|
||||
|
||||
when(mockApplicationContext.isActive()).thenReturn(true);
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWithExistingApplicationContext");
|
||||
@@ -453,7 +592,7 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer();
|
||||
|
||||
initializer.init(createParameters("test", "test"));
|
||||
initializer.init(this.mockCache, createParameters("test", "test"));
|
||||
|
||||
verify(mockApplicationContext, never()).addApplicationListener(any(SpringContextBootstrappingInitializer.class));
|
||||
verify(mockApplicationContext, never()).registerShutdownHook();
|
||||
@@ -464,24 +603,29 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void testInitWhenApplicationContextIsNull() {
|
||||
|
||||
assertNull(SpringContextBootstrappingInitializer.applicationContext);
|
||||
|
||||
final ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWhenApplicationContextIsNull");
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testInitWhenApplicationContextIsNull");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWhenApplicationContextIsNull");
|
||||
when(mockApplicationContext.isRunning()).thenReturn(true);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
|
||||
final String[] configLocations) {
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"/path/to/spring/application/context.xml"));
|
||||
Properties parameters = createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"/path/to/spring/application/context.xml");
|
||||
|
||||
initializer.init(this.mockCache, parameters);
|
||||
|
||||
verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
verify(mockApplicationContext, times(1)).registerShutdownHook();
|
||||
@@ -492,8 +636,9 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
|
||||
@Test
|
||||
public void testInitWhenApplicationContextIsInactive() {
|
||||
ConfigurableApplicationContext mockInactiveApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWhenApplicationContextIsInactive.Inactive");
|
||||
|
||||
ConfigurableApplicationContext mockInactiveApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class, "testInitWhenApplicationContextIsInactive.Inactive");
|
||||
|
||||
when(mockInactiveApplicationContext.isActive()).thenReturn(false);
|
||||
|
||||
@@ -508,14 +653,16 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
when(mockNewApplicationContext.isRunning()).thenReturn(true);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
|
||||
final String[] configLocations) {
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
return mockNewApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
initializer.init(this.mockCache, createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
"org.example.app"));
|
||||
|
||||
verify(mockNewApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
@@ -525,51 +672,68 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
assertSame(mockNewApplicationContext, SpringContextBootstrappingInitializer.getApplicationContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitWhenBasePackagesAndContextConfigLocationsParametersAreUnspecified() throws Throwable {
|
||||
@Test(expected = ApplicationContextException.class)
|
||||
public void testInitWhenBasePackagesAndContextConfigLocationsParametersAreUnspecified() {
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.applicationContext, is(nullValue()));
|
||||
|
||||
expectedException.expect(ApplicationContextException.class);
|
||||
expectedException.expectCause(isA(IllegalArgumentException.class));
|
||||
expectedException.expectMessage(containsString("Failed to bootstrap the Spring ApplicationContext"));
|
||||
try {
|
||||
|
||||
new SpringContextBootstrappingInitializer().init(createParameters(createParameters(
|
||||
Properties parameters = createParameters(createParameters(
|
||||
SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER, ""),
|
||||
SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER, " "));
|
||||
SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER, " ");
|
||||
|
||||
new SpringContextBootstrappingInitializer().init(this.mockCache, parameters);
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
|
||||
assertThat(expected.getMessage(), containsString("Failed to bootstrap the Spring ApplicationContext"));
|
||||
assertThat(expected.getCause(), is(instanceOf(IllegalArgumentException.class)));
|
||||
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testInitWhenApplicationContextIsNotRunning() {
|
||||
assertNull(SpringContextBootstrappingInitializer.applicationContext);
|
||||
|
||||
final ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
|
||||
"testInitWhenApplicationContextIsNotRunning");
|
||||
ConfigurableApplicationContext mockApplicationContext =
|
||||
mock(ConfigurableApplicationContext.class,"testInitWhenApplicationContextIsNotRunning");
|
||||
|
||||
when(mockApplicationContext.getId()).thenReturn("testInitWhenApplicationContextIsNotRunning");
|
||||
when(mockApplicationContext.isRunning()).thenReturn(false);
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(final String[] basePackages,
|
||||
final String[] configLocations) {
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
"org.example.app, org.example.plugins"));
|
||||
|
||||
Properties parameters = createParameters(SpringContextBootstrappingInitializer.BASE_PACKAGES_PARAMETER,
|
||||
"org.example.app, org.example.plugins");
|
||||
|
||||
initializer.init(this.mockCache, parameters);
|
||||
|
||||
SpringContextBootstrappingInitializer.getApplicationContext();
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
|
||||
assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext"));
|
||||
assertTrue(expected.getCause() instanceof IllegalStateException);
|
||||
assertEquals("The Spring ApplicationContext (testInitWhenApplicationContextIsNotRunning) failed to be properly initialized with the context config files ([]) or base packages ([org.example.app, org.example.plugins])!",
|
||||
expected.getCause().getMessage());
|
||||
|
||||
throw (IllegalStateException) expected.getCause();
|
||||
}
|
||||
finally {
|
||||
|
||||
verify(mockApplicationContext, times(1)).addApplicationListener(same(initializer));
|
||||
verify(mockApplicationContext, times(1)).registerShutdownHook();
|
||||
verify(mockApplicationContext, times(1)).refresh();
|
||||
@@ -578,16 +742,21 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testInitLogsErrors() throws Throwable {
|
||||
final Log mockLog = mock(Log.class, "testInitLogsErrors.MockLog");
|
||||
|
||||
Log mockLog = mock(Log.class, "testInitLogsErrors.MockLog");
|
||||
|
||||
SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer() {
|
||||
@Override protected Log initLogger() {
|
||||
|
||||
@Override
|
||||
protected Log initLogger() {
|
||||
return mockLog;
|
||||
}
|
||||
|
||||
@Override protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
@Override
|
||||
protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
|
||||
String[] configLocations) {
|
||||
|
||||
throw new IllegalStateException("TEST");
|
||||
@@ -595,50 +764,60 @@ public class SpringContextBootstrappingInitializerTest {
|
||||
};
|
||||
|
||||
try {
|
||||
initializer.init(createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"classpath/to/spring/application/context.xml"));
|
||||
|
||||
Properties parameters = createParameters(SpringContextBootstrappingInitializer.CONTEXT_CONFIG_LOCATIONS_PARAMETER,
|
||||
"classpath/to/spring/application/context.xml");
|
||||
|
||||
initializer.init(this.mockCache, parameters);
|
||||
}
|
||||
catch (ApplicationContextException expected) {
|
||||
|
||||
assertTrue(expected.getMessage().contains("Failed to bootstrap the Spring ApplicationContext"));
|
||||
assertTrue(expected.getCause() instanceof IllegalStateException);
|
||||
assertEquals("TEST", expected.getCause().getMessage());
|
||||
|
||||
throw expected.getCause();
|
||||
}
|
||||
finally {
|
||||
verify(mockLog, times(1)).error(eq("Failed to bootstrap the Spring ApplicationContext"),
|
||||
any(RuntimeException.class));
|
||||
verify(mockLog, times(1))
|
||||
.error(eq("Failed to bootstrap the Spring ApplicationContext"), any(RuntimeException.class));
|
||||
}
|
||||
}
|
||||
|
||||
protected static void assertNotified(TestApplicationListener listener, ApplicationContextEvent expectedEvent) {
|
||||
|
||||
assertThat(listener, is(notNullValue()));
|
||||
assertThat(listener.isNotified(), is(true));
|
||||
assertThat(listener.getActualEvent(), is(sameInstance(expectedEvent)));
|
||||
}
|
||||
|
||||
protected static void assertUnnotified(TestApplicationListener listener) {
|
||||
|
||||
assertThat(listener, is(notNullValue()));
|
||||
assertThat(listener.isNotified(), is(false));
|
||||
assertThat(listener.getActualEvent(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void onContextClosedApplicationEvent() {
|
||||
TestApplicationListener testApplicationListener = new TestApplicationListener(
|
||||
"testOnContextClosedApplicationEvent");
|
||||
|
||||
TestApplicationListener testApplicationListener =
|
||||
new TestApplicationListener("testOnContextClosedApplicationEvent");
|
||||
|
||||
try {
|
||||
|
||||
testApplicationListener = SpringContextBootstrappingInitializer.register(testApplicationListener);
|
||||
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent = mock(ContextRefreshedEvent.class,
|
||||
"MockContextRefreshedEvent");
|
||||
SpringContextBootstrappingInitializer.contextRefreshedEvent =
|
||||
mock(ContextRefreshedEvent.class,"MockContextRefreshedEvent");
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, isA(ContextRefreshedEvent.class));
|
||||
|
||||
new SpringContextBootstrappingInitializer().onApplicationEvent(mock(ContextClosedEvent.class,
|
||||
"MockContextClosedEvent"));
|
||||
new SpringContextBootstrappingInitializer()
|
||||
.onApplicationEvent(mock(ContextClosedEvent.class,"MockContextClosedEvent"));
|
||||
|
||||
assertThat(SpringContextBootstrappingInitializer.contextRefreshedEvent, is(nullValue()));
|
||||
assertUnnotified(testApplicationListener);
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire.support;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -51,18 +50,21 @@ public class SpringServerLauncherCacheProviderIntegrationTest {
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
System.clearProperty(gemfireName());
|
||||
SpringContextBootstrappingInitializer.getApplicationContext().close();
|
||||
GemfireUtils.closeClientCache();
|
||||
}
|
||||
|
||||
String gemfireName() {
|
||||
return (GemfireUtils.GEMFIRE_PREFIX + GemfireUtils.NAME_PROPERTY_NAME);
|
||||
return GemfireUtils.GEMFIRE_PREFIX + GemfireUtils.NAME_PROPERTY_NAME;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createCacheWithSpring() {
|
||||
|
||||
String springXmlLocation = getClass().getSimpleName() + "-context.xml";
|
||||
|
||||
ServerLauncher.Builder builder = new ServerLauncher.Builder();
|
||||
|
||||
builder.setSpringXmlLocation(springXmlLocation);
|
||||
@@ -70,6 +72,7 @@ public class SpringServerLauncherCacheProviderIntegrationTest {
|
||||
builder.setDisableDefaultServer(true);
|
||||
|
||||
ServerLauncher launcher = builder.build();
|
||||
|
||||
ServerState state = launcher.start();
|
||||
|
||||
assertThat(state.getStatus(), is(equalTo(Status.ONLINE)));
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE cache PUBLIC "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN"
|
||||
"http://www.gemstone.com/dtd/cache7_0.dtd">
|
||||
<cache>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<cache xmlns="http://geode.apache.org/schema/cache"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd"
|
||||
version="1.0">
|
||||
|
||||
<region name="Users" refid="REPLICATE">
|
||||
<region-attributes initial-capacity="101" load-factor="0.85">
|
||||
<key-constraint>java.lang.String</key-constraint>
|
||||
@@ -11,6 +13,7 @@
|
||||
</cache-loader>
|
||||
</region-attributes>
|
||||
</region>
|
||||
|
||||
<initializer>
|
||||
<class-name>org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer</class-name>
|
||||
<parameter name="basePackages">
|
||||
@@ -19,4 +22,5 @@
|
||||
</string>
|
||||
</parameter>
|
||||
</initializer>
|
||||
|
||||
</cache>
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE cache PUBLIC "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN"
|
||||
"http://www.gemstone.com/dtd/cache7_0.dtd">
|
||||
<cache>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<cache xmlns="http://geode.apache.org/schema/cache"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd"
|
||||
version="1.0">
|
||||
|
||||
<region name="Users" refid="REPLICATE">
|
||||
<region-attributes initial-capacity="101" load-factor="0.85">
|
||||
<key-constraint>java.lang.String</key-constraint>
|
||||
<value-constraint>org.springframework.data.gemfire.repository.sample.User</value-constraint>
|
||||
<cache-loader>
|
||||
<class-name>org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerIntegrationTest$UserDataStoreCacheLoader</class-name>
|
||||
<class-name>
|
||||
org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerIntegrationTest$UserDataStoreCacheLoader
|
||||
</class-name>
|
||||
</cache-loader>
|
||||
</region-attributes>
|
||||
</region>
|
||||
|
||||
<initializer>
|
||||
<class-name>org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer</class-name>
|
||||
<parameter name="contextConfigLocations">
|
||||
@@ -21,4 +26,5 @@
|
||||
</string>
|
||||
</parameter>
|
||||
</initializer>
|
||||
|
||||
</cache>
|
||||
|
||||
Reference in New Issue
Block a user