Apply polish.

This commit is contained in:
John Blum
2018-04-03 11:58:36 -07:00
parent a9f8705013
commit 36081d35ad
4 changed files with 34 additions and 39 deletions

View File

@@ -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<Boolean> newGetter(AtomicBoolean returnValue) {
return invocation -> returnValue.get();
}
/* (non-Javadoc) */
protected static Answer<Integer> newGetter(AtomicInteger returnValue) {
return invocation -> returnValue.get();
}
/* (non-Javadoc) */
protected static Answer<Long> newGetter(AtomicLong returnValue) {
return invocation -> returnValue.get();
}
/* (non-Javadoc) */
protected static <R> Answer<R> newGetter(AtomicReference<R> returnValue) {
return invocation -> returnValue.get();
}
/* (non-Javadoc) */
protected static <R, S> Answer<S> newGetter(AtomicReference<R> returnValue, Function<R, S> converter) {
return invocation -> converter.apply(returnValue.get());
}
/* (non-Javadoc) */
protected static <R> Answer<R> newGetter(Supplier<R> returnValue) {
return invocation -> returnValue.get();
}
/* (non-Javadoc) */
protected static <R, S> Answer<S> newGetter(Supplier<R> returnValue, Function<R, S> converter) {
return invocation -> converter.apply(returnValue.get());
}
/* (non-Javadoc) */
protected static <E, C extends Collection<E>, R> Answer<R> newAdder(C collection, R returnValue) {
return invocation -> {
collection.add(invocation.getArgument(0));
@@ -99,7 +95,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <R> Answer<R> newSetter(AtomicBoolean argument, R returnValue) {
return invocation -> {
argument.set(invocation.getArgument(0));
@@ -107,7 +102,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <R> Answer<R> newSetter(AtomicBoolean argument, Boolean value, R returnValue) {
return invocation -> {
argument.set(value);
@@ -115,7 +109,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <R> Answer<R> newSetter(AtomicInteger argument, R returnValue) {
return invocation -> {
argument.set(invocation.getArgument(0));
@@ -123,7 +116,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <R> Answer<R> newSetter(AtomicInteger argument, Integer value, R returnValue) {
return invocation -> {
argument.set(value);
@@ -131,7 +123,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <R> Answer<R> newSetter(AtomicLong argument, R returnValue) {
return invocation -> {
argument.set(invocation.getArgument(0));
@@ -139,7 +130,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <R> Answer<R> newSetter(AtomicLong argument, Long value, R returnValue) {
return invocation -> {
argument.set(value);
@@ -147,7 +137,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <T, R> Answer<R> newSetter(AtomicReference<T> argument, R returnValue) {
return invocation -> {
argument.set(invocation.getArgument(0));
@@ -155,7 +144,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <T, R> Answer<R> newSetter(AtomicReference<T> argument, T value, R returnValue) {
return invocation -> {
argument.set(value);
@@ -163,7 +151,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <T, R> Answer<R> newSetter(AtomicReference<T> argument, Function<?, T> converter, R returnValue) {
return invocation -> {
argument.set(converter.apply(invocation.getArgument(0)));
@@ -171,7 +158,6 @@ public abstract class MockObjectsSupport {
};
}
/* (non-Javadoc) */
protected static <K, V, R> Answer<R> newSetter(Map<K, V> 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 <T> Answer<Void> newVoidAnswer(Consumer<InvocationOnMock> methodInvocation) {
return invocation -> {
methodInvocation.accept(invocation);

View File

@@ -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;

View File

@@ -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<? extends Annotation> getAnnotationType() {

View File

@@ -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<CacheFactory> {
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<ClientCacheFactory> {
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;
}