From 36081d35ad33babb284c63c55905f38a1c537eaa Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 3 Apr 2018 11:58:36 -0700 Subject: [PATCH] Apply polish. --- .../tests/mock/MockObjectsSupport.java | 29 +++++-------------- .../annotation/EnableGemFireMockObjects.java | 5 ++-- .../GemFireMockObjectsConfiguration.java | 12 ++++---- .../GemFireMockObjectsBeanPostProcessor.java | 27 +++++++++++------ 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/tests/mock/MockObjectsSupport.java b/src/main/java/org/springframework/data/gemfire/tests/mock/MockObjectsSupport.java index 86bab5c..e8b49ee 100644 --- a/src/main/java/org/springframework/data/gemfire/tests/mock/MockObjectsSupport.java +++ b/src/main/java/org/springframework/data/gemfire/tests/mock/MockObjectsSupport.java @@ -40,7 +40,7 @@ import org.springframework.util.StringUtils; * @see org.mockito.stubbing.Answer * @since 0.0.1 */ -@SuppressWarnings("unused") +@SuppressWarnings("all") public abstract class MockObjectsSupport { private static final AtomicLong mockObjectIdentifier = new AtomicLong(0L); @@ -52,46 +52,42 @@ public abstract class MockObjectsSupport { } public static String mockObjectIdentifier(String mockObjectName) { - return String.format("%s%d", Optional.ofNullable(mockObjectName).filter(StringUtils::hasText) - .orElse(DEFAULT_MOCK_OBJECT_NAME), mockObjectIdentifier.incrementAndGet()); + + String resolvedMockObjectName = Optional.ofNullable(mockObjectName) + .filter(StringUtils::hasText) + .orElse(DEFAULT_MOCK_OBJECT_NAME); + + return String.format("%s%d", resolvedMockObjectName, mockObjectIdentifier.incrementAndGet()); } - /* (non-Javadoc) */ protected static Answer newGetter(AtomicBoolean returnValue) { return invocation -> returnValue.get(); } - /* (non-Javadoc) */ protected static Answer newGetter(AtomicInteger returnValue) { return invocation -> returnValue.get(); } - /* (non-Javadoc) */ protected static Answer newGetter(AtomicLong returnValue) { return invocation -> returnValue.get(); } - /* (non-Javadoc) */ protected static Answer newGetter(AtomicReference returnValue) { return invocation -> returnValue.get(); } - /* (non-Javadoc) */ protected static Answer newGetter(AtomicReference returnValue, Function converter) { return invocation -> converter.apply(returnValue.get()); } - /* (non-Javadoc) */ protected static Answer newGetter(Supplier returnValue) { return invocation -> returnValue.get(); } - /* (non-Javadoc) */ protected static Answer newGetter(Supplier returnValue, Function converter) { return invocation -> converter.apply(returnValue.get()); } - /* (non-Javadoc) */ protected static , R> Answer newAdder(C collection, R returnValue) { return invocation -> { collection.add(invocation.getArgument(0)); @@ -99,7 +95,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicBoolean argument, R returnValue) { return invocation -> { argument.set(invocation.getArgument(0)); @@ -107,7 +102,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicBoolean argument, Boolean value, R returnValue) { return invocation -> { argument.set(value); @@ -115,7 +109,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicInteger argument, R returnValue) { return invocation -> { argument.set(invocation.getArgument(0)); @@ -123,7 +116,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicInteger argument, Integer value, R returnValue) { return invocation -> { argument.set(value); @@ -131,7 +123,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicLong argument, R returnValue) { return invocation -> { argument.set(invocation.getArgument(0)); @@ -139,7 +130,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicLong argument, Long value, R returnValue) { return invocation -> { argument.set(value); @@ -147,7 +137,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicReference argument, R returnValue) { return invocation -> { argument.set(invocation.getArgument(0)); @@ -155,7 +144,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicReference argument, T value, R returnValue) { return invocation -> { argument.set(value); @@ -163,7 +151,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(AtomicReference argument, Function converter, R returnValue) { return invocation -> { argument.set(converter.apply(invocation.getArgument(0))); @@ -171,7 +158,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newSetter(Map argument, R returnValue) { return invocation -> { argument.put(invocation.getArgument(0), invocation.getArgument(1)); @@ -179,7 +165,6 @@ public abstract class MockObjectsSupport { }; } - /* (non-Javadoc) */ protected static Answer newVoidAnswer(Consumer methodInvocation) { return invocation -> { methodInvocation.accept(invocation); diff --git a/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/EnableGemFireMockObjects.java b/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/EnableGemFireMockObjects.java index 6756e47..8d6cf42 100644 --- a/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/EnableGemFireMockObjects.java +++ b/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/EnableGemFireMockObjects.java @@ -47,11 +47,12 @@ import org.springframework.context.annotation.Import; public @interface EnableGemFireMockObjects { /** - * Determines whether the mock {@link GemFireCache} created for Unit Tests is a Singleton. + * Configures whether the mock {@link GemFireCache} created for Unit Testing is a Singleton. * * Defaults to {@literal false}. * - * @return a boolean value indicating whether the mock {@link GemFireCache} created for Unit Tests is a Singleton. + * @return a boolean value indicating whether the mock {@link GemFireCache} created for Unit Testing + * is a Singleton. */ boolean useSingletonCache() default false; diff --git a/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/GemFireMockObjectsConfiguration.java b/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/GemFireMockObjectsConfiguration.java index ac23bea..0fb2b77 100644 --- a/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/GemFireMockObjectsConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/tests/mock/annotation/GemFireMockObjectsConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.data.gemfire.tests.mock.annotation; import java.lang.annotation.Annotation; +import java.util.Optional; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; @@ -54,12 +55,11 @@ public class GemFireMockObjectsConfiguration implements ImportAware { @Override public void setImportMetadata(AnnotationMetadata importingClassMetadata) { - if (isAnnotationPresent(importingClassMetadata)) { - - AnnotationAttributes enableGemFireMockingAttributes = getAnnotationAttributes(importingClassMetadata); - - this.useSingletonCache = enableGemFireMockingAttributes.getBoolean("useSingletonCache"); - } + Optional.of(importingClassMetadata) + .filter(this::isAnnotationPresent) + .map(this::getAnnotationAttributes) + .ifPresent(enableGemFireMockObjectsAttributes -> + this.useSingletonCache = enableGemFireMockObjectsAttributes.getBoolean("useSingletonCache")); } private Class getAnnotationType() { diff --git a/src/main/java/org/springframework/data/gemfire/tests/mock/config/GemFireMockObjectsBeanPostProcessor.java b/src/main/java/org/springframework/data/gemfire/tests/mock/config/GemFireMockObjectsBeanPostProcessor.java index 98882b8..600d6a1 100644 --- a/src/main/java/org/springframework/data/gemfire/tests/mock/config/GemFireMockObjectsBeanPostProcessor.java +++ b/src/main/java/org/springframework/data/gemfire/tests/mock/config/GemFireMockObjectsBeanPostProcessor.java @@ -50,6 +50,7 @@ import org.springframework.lang.Nullable; * @see org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport * @since 0.0.1 */ +@SuppressWarnings("all") public class GemFireMockObjectsBeanPostProcessor implements BeanPostProcessor { private static final boolean DEFAULT_USE_SINGLETON_CACHE = false; @@ -76,7 +77,7 @@ public class GemFireMockObjectsBeanPostProcessor implements BeanPostProcessor { @Nullable @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - return (isGemFireProperties(bean, beanName) ? set((Properties) bean) + return (isGemFireProperties(bean, beanName) ? set((Properties) bean) : (bean instanceof CacheFactoryBean ? spyOnCacheFactoryBean((CacheFactoryBean) bean, this.useSingletonCache) : (bean instanceof PoolFactoryBean ? mockThePoolFactoryBean((PoolFactoryBean) bean) : bean))); @@ -96,7 +97,7 @@ public class GemFireMockObjectsBeanPostProcessor implements BeanPostProcessor { } private boolean isGemFireProperties(Object bean, String beanName) { - return (bean instanceof Properties && GEMFIRE_PROPERTIES_BEAN_NAME.equals(beanName)); + return bean instanceof Properties && GEMFIRE_PROPERTIES_BEAN_NAME.equals(beanName); } private Object set(Properties gemfireProperties) { @@ -106,9 +107,9 @@ public class GemFireMockObjectsBeanPostProcessor implements BeanPostProcessor { private Object spyOnCacheFactoryBean(CacheFactoryBean bean, boolean useSingletonCache) { - return (bean instanceof ClientCacheFactoryBean + return bean instanceof ClientCacheFactoryBean ? SpyingClientCacheFactoryInitializer.spyOn((ClientCacheFactoryBean) bean, useSingletonCache) - : SpyingCacheFactoryInitializer.spyOn(bean, useSingletonCache)); + : SpyingCacheFactoryInitializer.spyOn(bean, useSingletonCache); } private Object mockThePoolFactoryBean(PoolFactoryBean bean) { @@ -118,7 +119,7 @@ public class GemFireMockObjectsBeanPostProcessor implements BeanPostProcessor { protected static class SpyingCacheFactoryInitializer implements CacheFactoryBean.CacheFactoryInitializer { - public static CacheFactoryBean spyOn(CacheFactoryBean cacheFactoryBean, boolean useSingletonCache) { + protected static CacheFactoryBean spyOn(CacheFactoryBean cacheFactoryBean, boolean useSingletonCache) { cacheFactoryBean.setCacheFactoryInitializer(new SpyingCacheFactoryInitializer(useSingletonCache)); return cacheFactoryBean; } @@ -129,16 +130,20 @@ public class GemFireMockObjectsBeanPostProcessor implements BeanPostProcessor { this.useSingletonCache = useSingletonCache; } + protected boolean isUsingSingletonCache() { + return this.useSingletonCache; + } + @Override public CacheFactory initialize(CacheFactory cacheFactory) { - return GemFireMockObjectsSupport.spyOn(cacheFactory, useSingletonCache); + return GemFireMockObjectsSupport.spyOn(cacheFactory, isUsingSingletonCache()); } } protected static class SpyingClientCacheFactoryInitializer implements CacheFactoryBean.CacheFactoryInitializer { - public static ClientCacheFactoryBean spyOn(ClientCacheFactoryBean clientCacheFactoryBean, + protected static ClientCacheFactoryBean spyOn(ClientCacheFactoryBean clientCacheFactoryBean, boolean useSingletonCache) { clientCacheFactoryBean.setCacheFactoryInitializer( @@ -153,15 +158,19 @@ public class GemFireMockObjectsBeanPostProcessor implements BeanPostProcessor { this.useSingletonCache = useSingletonCache; } + protected boolean isUsingSingletonCache() { + return this.useSingletonCache; + } + @Override public ClientCacheFactory initialize(ClientCacheFactory clientCacheFactory) { - return GemFireMockObjectsSupport.spyOn(clientCacheFactory, this.useSingletonCache); + return GemFireMockObjectsSupport.spyOn(clientCacheFactory, isUsingSingletonCache()); } } protected static class MockingPoolFactoryInitializer implements PoolFactoryBean.PoolFactoryInitializer { - public static PoolFactoryBean mock(PoolFactoryBean poolFactoryBean) { + protected static PoolFactoryBean mock(PoolFactoryBean poolFactoryBean) { poolFactoryBean.setPoolFactoryInitializer(new MockingPoolFactoryInitializer()); return poolFactoryBean; }