diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java index a1594a03..f8d19071 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java @@ -685,7 +685,7 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat /** * Sets (configures) the {@link PoolResolver} used by this {@link ClientCache} to resolve {@link Pool} objects. - * + *

* The {@link Pool} objects may be managed or un-managed depending on the {@link PoolResolver} implementation. * * @param poolResolver {@link PoolResolver} used to resolve the configured {@link Pool}. diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessor.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessor.java index 70295956..213486ef 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessor.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessor.java @@ -25,39 +25,33 @@ import org.apache.geode.cache.execute.Function; import org.apache.geode.management.internal.cli.domain.RegionInformation; import org.apache.geode.management.internal.cli.functions.GetRegionsFunction; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.springframework.beans.BeansException; import org.springframework.beans.TypeMismatchException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.data.gemfire.client.function.ListRegionsOnServerFunction; import org.springframework.data.gemfire.function.execution.GemfireOnServersFunctionTemplate; +import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.ObjectUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** - * A Spring {@link BeanFactoryPostProcessor} used to register a Client Region beans for each Region accessible to - * an Apache Geode or Pivotal GemFire DataSource. If the Region is already defined, the bean definition - * will not be overridden. + * A Spring {@link BeanPostProcessor} used to register a {@link ClientCache} {@link Region} beans + * for each {@link Region} accessible to an Apache Geode DataSource. If the {@link Region} is already + * defined as a bean, then the existing bean definition will not be overridden. * * @author David Turanski * @author John Blum * @see org.apache.geode.cache.Region * @see org.apache.geode.cache.client.ClientCache * @see org.apache.geode.cache.client.ClientRegionFactory - * @see org.apache.geode.cache.client.ClientRegionShortcut - * @see org.apache.geode.cache.execute.Function * @see org.apache.geode.management.internal.cli.functions.GetRegionsFunction - * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor - * @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory * @see org.springframework.data.gemfire.client.function.ListRegionsOnServerFunction - * @see org.springframework.data.gemfire.function.execution.GemfireOnServersFunctionTemplate - * @see ListRegionsOnServerFunction * @since 1.2.0 */ public class GemfireDataSourcePostProcessor implements BeanFactoryAware, BeanPostProcessor { @@ -79,7 +73,7 @@ public class GemfireDataSourcePostProcessor implements BeanFactoryAware, BeanPos * @see org.springframework.beans.factory.BeanFactory */ @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException { if (beanFactory instanceof ConfigurableBeanFactory) { this.beanFactory = (ConfigurableBeanFactory) beanFactory; @@ -106,7 +100,7 @@ public class GemfireDataSourcePostProcessor implements BeanFactoryAware, BeanPos * used by the client {@link Region}. * @see org.apache.geode.cache.client.ClientRegionShortcut */ - public void setClientRegionShortcut(ClientRegionShortcut clientRegionShortcut) { + public void setClientRegionShortcut(@Nullable ClientRegionShortcut clientRegionShortcut) { this.clientRegionShortcut = clientRegionShortcut; } @@ -123,13 +117,15 @@ public class GemfireDataSourcePostProcessor implements BeanFactoryAware, BeanPos } /** - * Resolves the {@link ClientRegionShortcut} used to configure and create client {@link Region Regions}. + * Resolves the {@link ClientRegionShortcut} used to create and configure client {@link Region Regions}. + *

+ * Defaults to {@link ClientRegionShortcut#PROXY}. * * @return the resolved {@link ClientRegionShortcut}. * @see org.apache.geode.cache.client.ClientRegionShortcut * @see #getClientRegionShortcut() */ - protected ClientRegionShortcut resolveClientRegionShortcut() { + protected @NonNull ClientRegionShortcut resolveClientRegionShortcut() { return getClientRegionShortcut().orElse(DEFAULT_CLIENT_REGION_SHORTCUT); } @@ -157,7 +153,7 @@ public class GemfireDataSourcePostProcessor implements BeanFactoryAware, BeanPos } - // TODO: remove this logic and delegate to o.s.d.g.config.remote.GemfireAdminOperations + // TODO: Remove this logic and delegate to o.s.d.g.config.remote.GemfireAdminOperations Iterable regionNames(ClientCache clientCache) { try { @@ -191,6 +187,7 @@ public class GemfireDataSourcePostProcessor implements BeanFactoryAware, BeanPos } } + @SuppressWarnings("rawtypes") T execute(ClientCache clientCache, Function gemfireFunction, Object... arguments) { return new GemfireOnServersFunctionTemplate(clientCache).executeAndExtract(gemfireFunction, arguments); } @@ -255,17 +252,13 @@ public class GemfireDataSourcePostProcessor implements BeanFactoryAware, BeanPos } } - public GemfireDataSourcePostProcessor using(ClientRegionShortcut clientRegionShortcut) { - + public @NonNull GemfireDataSourcePostProcessor using(ClientRegionShortcut clientRegionShortcut) { setClientRegionShortcut(clientRegionShortcut); - return this; } - public GemfireDataSourcePostProcessor using(BeanFactory beanFactory) { - + public @NonNull GemfireDataSourcePostProcessor using(@NonNull BeanFactory beanFactory) { setBeanFactory(beanFactory); - return this; } } diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java index 151e5879..58b6d8fa 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java @@ -53,10 +53,10 @@ import org.springframework.util.StringUtils; /** * Spring {@link FactoryBean} used to construct, configure and initialize a {@link Pool}. - * + *

* If a new {@link Pool} is created, its lifecycle is bound to that of this declaring {@link FactoryBean} * and indirectly, the Spring container. - * + *

* If a {@link Pool} having the configured {@link String name} already exists, then the existing {@link Pool} * will be returned as is without any modifications and its lifecycle will be unaffected by this {@link FactoryBean}. * diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperations.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperations.java index 8e07638d..0b950c29 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperations.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperations.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.config.admin; import static java.util.Arrays.stream; @@ -30,8 +29,7 @@ import org.springframework.data.gemfire.config.schema.definitions.IndexDefinitio import org.springframework.data.gemfire.config.schema.definitions.RegionDefinition; /** - * The {@link GemfireAdminOperations} interface defines a set of operations to define schema objects in a remote - * Apache Geode or Pivotal GemFire cluster. + * Interface defining a set of operations for defining schema objects in a remote Apache Geode cluster. * * @author John Blum * @see org.apache.geode.cache.DiskStore diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/repository/query/QueryPostProcessor.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/repository/query/QueryPostProcessor.java index b95e91b2..cbdbe3ec 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/repository/query/QueryPostProcessor.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/repository/query/QueryPostProcessor.java @@ -26,22 +26,23 @@ import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; /** - * The {@link QueryPostProcessor} interface defines a contract for implementations to post process - * a given {@link QUERY query} and possibly return a new or modified version of the same {@link QUERY query}. - * - * {@link QueryPostProcessor QueryPostProcessors} are useful for handling and processing {@link QUERY queries} - * derived from {@link Repository} {@link QueryMethod QueryMethods}, and give a developer the opportunity, - * via the callback, to further process the generated {@link QUERY query}. - * + * Interface defining contract for implementations to post process a given {@link QUERY query} and possibly return + * a new or modified version of the same {@link QUERY query}. + *

+ * {@link QueryPostProcessor QueryPostProcessors} are useful for processing {@link QUERY queries} derived from + * {@link Repository} {@link QueryMethod QueryMethods}, and give developers an opportunity, through this callback, + * to further process the generated {@link QUERY query}. + *

* {@link QueryPostProcessor QueryPostProcessors} can be used on both {@literal derived} {@link QUERY queries} - * and {@literal manual} {@link QUERY queries}. {@literal Manual} {@link QUERY queries} are defined as - * {@link QUERY queries} specified using SDG's {@link Query @Query} annotation or by defining a {@literal named} - * {@link QUERY query} in a module-specific {@link Properties} files - * (e.g. {@literal META-INF/gemfire-named-queries.properties}). + * as well as {@literal manual} {@link QUERY queries}. + *

+ * {@literal Manual} {@link QUERY queries} are defined as {@link QUERY queries} specified using the {@link Query @Query} + * annotation or by defining a {@literal named} {@link QUERY query} in a module-specific {@link Properties} files, + * for example: {@literal META-INF/gemfire-named-queries.properties}). * * @author John Blum * @param {@link Class type} identifying the {@link Repository Repositories} to match on during registration. - * @param {@link Class type} of the query to process. + * @param {@link Class type} of the {@literal query} to process. * @see java.lang.FunctionalInterface * @see org.springframework.core.Ordered * @see org.springframework.data.gemfire.repository.Query @@ -59,7 +60,7 @@ public interface QueryPostProcessor extends Ordered /** * Defines the {@link Integer order} of this {@link QueryPostProcessor} relative to * other {@link QueryPostProcessor QueryPostProcessors} in a sort. - * + *

* Defaults to the {@link Ordered#LOWEST_PRECEDENCE}. * * @return an {@link Integer} value specifying the order of this {@link QueryPostProcessor} relative to @@ -74,7 +75,7 @@ public interface QueryPostProcessor extends Ordered /** * Callback method invoked by the Spring Data (SD) {@link Repository} framework to allow the user to process * the given {@link QUERY query} and (possibly) return a new or modified version of the {@link QUERY query}. - * + *

* This callback is invoked for {@literal queries} generated from a SD {@link Repository} {@link QueryMethod} * signature as well as {@literal queries} specified and defined in {@link NamedQueries}, * or even using SDG's {@link Query @Query} annotation. @@ -91,7 +92,7 @@ public interface QueryPostProcessor extends Ordered /** * Callback method invoked by the Spring Data (SD) {@link Repository} framework to allow the user to process * the given {@link QUERY query} and (possibly) return a new or modified version of the {@link QUERY query}. - * + *

* This callback is invoked for {@literal queries} generated from a SD {@link Repository} {@link QueryMethod} * signature as well as {@literal queries} specified and defined in {@link NamedQueries}, * or even using SDG's {@link Query @Query} annotation. @@ -102,12 +103,12 @@ public interface QueryPostProcessor extends Ordered * @see org.springframework.data.repository.query.QueryMethod * @see #postProcess(QueryMethod, Object) */ - QUERY postProcess(@NonNull QueryMethod queryMethod, QUERY query, Object... arguments); + QUERY postProcess(QueryMethod queryMethod, QUERY query, Object... arguments); /** * Builder method used to compose this {@link QueryPostProcessor QueryPostProcessor} * with the given {@link QueryPostProcessor}. - * + *

* This {@link QueryPostProcessor} will process the query before the given {@link QueryPostProcessor} * in the processing chain. * @@ -118,6 +119,7 @@ public interface QueryPostProcessor extends Ordered * @see #processAfter(QueryPostProcessor) */ default @NonNull QueryPostProcessor processBefore(@Nullable QueryPostProcessor queryPostProcessor) { + return queryPostProcessor == null ? this : (queryMethod, query, arguments) -> queryPostProcessor.postProcess(queryMethod, this.postProcess(queryMethod, query, arguments), arguments); } @@ -125,7 +127,7 @@ public interface QueryPostProcessor extends Ordered /** * Builder method used to compose this {@link QueryPostProcessor QueryPostProcessors} * with the given {@link QueryPostProcessor}. - * + *

* This {@link QueryPostProcessor} will process the query after the given {@link QueryPostProcessor} * in the processing chain. * @@ -136,6 +138,7 @@ public interface QueryPostProcessor extends Ordered * @see #processBefore(QueryPostProcessor) */ default @NonNull QueryPostProcessor processAfter(@Nullable QueryPostProcessor queryPostProcessor) { + return queryPostProcessor == null ? this : (queryMethod, query, arguments) -> this.postProcess(queryMethod, queryPostProcessor.postProcess(queryMethod, query, arguments), arguments); } diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/search/lucene/LuceneIndexFactoryBean.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/search/lucene/LuceneIndexFactoryBean.java index 2a55b601..37f732a6 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/search/lucene/LuceneIndexFactoryBean.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/search/lucene/LuceneIndexFactoryBean.java @@ -16,13 +16,11 @@ */ package org.springframework.data.gemfire.search.lucene; -import static java.util.stream.StreamSupport.stream; -import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray; +import static org.springframework.data.gemfire.util.CollectionUtils.isEmpty; import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeCollection; import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeIterable; import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeList; import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeMap; -import static org.springframework.util.CollectionUtils.isEmpty; import java.util.Arrays; import java.util.Collections; @@ -30,6 +28,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Supplier; +import java.util.stream.StreamSupport; import org.apache.geode.cache.GemFireCache; import org.apache.geode.cache.Region; @@ -39,18 +38,19 @@ import org.apache.geode.cache.lucene.LuceneSerializer; import org.apache.geode.cache.lucene.LuceneService; import org.apache.geode.cache.lucene.LuceneServiceProvider; -import org.apache.lucene.analysis.Analyzer; - import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.data.gemfire.config.annotation.IndexConfigurer; import org.springframework.data.gemfire.support.AbstractFactoryBeanSupport; +import org.springframework.data.gemfire.util.ArrayUtils; import org.springframework.data.gemfire.util.CacheUtils; import org.springframework.data.gemfire.util.SpringExtensions; import org.springframework.util.Assert; import org.springframework.util.StringUtils; +import org.apache.lucene.analysis.Analyzer; + /** * Spring {@link FactoryBean} used to construct, configure and initialize {@link LuceneIndex Lucene Indexes} * on application domain object fields. @@ -82,11 +82,11 @@ public class LuceneIndexFactoryBean extends AbstractFactoryBeanSupport indexConfigurers = Collections.emptyList(); - private IndexConfigurer compositeIndexConfigurer = new IndexConfigurer() { + private final IndexConfigurer compositeIndexConfigurer = new IndexConfigurer() { @Override public void configure(String beanName, LuceneIndexFactoryBean bean) { - nullSafeCollection(indexConfigurers) + nullSafeCollection(LuceneIndexFactoryBean.this.indexConfigurers) .forEach(indexConfigurer -> indexConfigurer.configure(beanName, bean)); } }; @@ -143,7 +143,8 @@ public class LuceneIndexFactoryBean extends AbstractFactoryBeanSupport indexConfigurers) { - stream(nullSafeIterable(indexConfigurers).spliterator(), false) + StreamSupport.stream(nullSafeIterable(indexConfigurers).spliterator(), false) .forEach(indexConfigurer -> indexConfigurer.configure(indexName, this)); } @@ -386,6 +387,7 @@ public class LuceneIndexFactoryBean extends AbstractFactoryBeanSupport resolveRegion() { return Optional.ofNullable(getRegion()).orElseGet(() -> { @@ -515,7 +517,7 @@ public class LuceneIndexFactoryBean extends AbstractFactoryBeanSupport * This method is generally used for testing purposes only. * * @param luceneIndex {@link LuceneIndex} created by this {@link FactoryBean}. @@ -608,9 +610,7 @@ public class LuceneIndexFactoryBean extends AbstractFactoryBeanSupport getObjectType() { diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessorTest.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessorUnitTests.java similarity index 84% rename from spring-data-geode/src/test/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessorTest.java rename to spring-data-geode/src/test/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessorUnitTests.java index dac83d2e..3125ebaf 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessorTest.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/GemfireDataSourcePostProcessorUnitTests.java @@ -17,6 +17,7 @@ package org.springframework.data.gemfire.client; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.same; @@ -29,6 +30,7 @@ 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.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException; @@ -63,24 +65,18 @@ import org.springframework.data.gemfire.client.function.ListRegionsOnServerFunct import org.springframework.data.gemfire.util.RegionUtils; /** - * Unit tests for {@link GemfireDataSourcePostProcessor}. + * Unit Tests for {@link GemfireDataSourcePostProcessor}. * * @author John Blum * @see org.junit.Test + * @see org.mockito.Mock * @see org.mockito.Mockito - * @see org.apache.geode.cache.Region - * @see org.apache.geode.cache.RegionAttributes - * @see org.apache.geode.cache.client.ClientCache - * @see org.apache.geode.cache.client.ClientRegionFactory - * @see org.apache.geode.cache.execute.Function - * @see org.springframework.beans.factory.BeanFactory - * @see org.springframework.beans.factory.config.ConfigurableBeanFactory + * @see org.mockito.junit.MockitoJUnitRunner * @see org.springframework.data.gemfire.client.GemfireDataSourcePostProcessor - * @see org.springframework.data.gemfire.client.function.ListRegionsOnServerFunction * @since 1.7.0 */ @RunWith(MockitoJUnitRunner.class) -public class GemfireDataSourcePostProcessorTest { +public class GemfireDataSourcePostProcessorUnitTests { @Mock private ConfigurableBeanFactory mockBeanFactory; @@ -88,6 +84,10 @@ public class GemfireDataSourcePostProcessorTest { @Mock private ClientCache mockClientCache; + private ClientRegionShortcut getClientRegionShortcut(GemfireDataSourcePostProcessor postProcessor) { + return postProcessor.getClientRegionShortcut().orElse(null); + } + @SuppressWarnings("unchecked") private Region mockRegion(String name) { @@ -96,11 +96,11 @@ public class GemfireDataSourcePostProcessorTest { RegionAttributes mockRegionAttributes = mock(RegionAttributes.class, String.format("%s-RegionAttributes", name)); - when(mockRegion.getParentRegion()).thenReturn(null); - when(mockRegion.getFullPath()).thenReturn(RegionUtils.toRegionPath(name)); - when(mockRegion.getAttributes()).thenReturn(mockRegionAttributes); - when(mockRegionAttributes.getDataPolicy()).thenReturn(DataPolicy.PARTITION); - when(mockRegionAttributes.getScope()).thenReturn(Scope.DISTRIBUTED_ACK); + doReturn(null).when(mockRegion).getParentRegion(); + doReturn(RegionUtils.toRegionPath(name)).when(mockRegion).getFullPath(); + doReturn(mockRegionAttributes).when(mockRegion).getAttributes(); + doReturn(DataPolicy.PARTITION).when(mockRegionAttributes).getDataPolicy(); + doReturn(Scope.DISTRIBUTED_ACK).when(mockRegionAttributes).getScope(); return mockRegion; } @@ -115,7 +115,7 @@ public class GemfireDataSourcePostProcessorTest { GemfireDataSourcePostProcessor postProcessor = new GemfireDataSourcePostProcessor(); assertThat(postProcessor).isNotNull(); - assertThat(postProcessor.getClientRegionShortcut().orElse(null)).isNull(); + assertThat(postProcessor.getClientRegionShortcut()).isNotPresent(); assertThat(postProcessor.resolveClientRegionShortcut()).isEqualTo(ClientRegionShortcut.PROXY); assertThat(postProcessor.getLogger()).isNotNull(); } @@ -126,7 +126,7 @@ public class GemfireDataSourcePostProcessorTest { GemfireDataSourcePostProcessor postProcessor = new GemfireDataSourcePostProcessor(); assertThat(postProcessor).isNotNull(); - assertThat(postProcessor.getBeanFactory().orElse(null)).isNull(); + assertThat(postProcessor.getBeanFactory()).isNotPresent(); postProcessor.setBeanFactory(this.mockBeanFactory); @@ -154,14 +154,12 @@ public class GemfireDataSourcePostProcessorTest { postProcessor.setClientRegionShortcut(ClientRegionShortcut.CACHING_PROXY); - assertThat(postProcessor.getClientRegionShortcut().orElse(null)) - .isEqualTo(ClientRegionShortcut.CACHING_PROXY); + assertThat(getClientRegionShortcut(postProcessor)).isEqualTo(ClientRegionShortcut.CACHING_PROXY); assertThat(postProcessor.resolveClientRegionShortcut()).isEqualTo(ClientRegionShortcut.CACHING_PROXY); postProcessor.setClientRegionShortcut(ClientRegionShortcut.LOCAL); - assertThat(postProcessor.getClientRegionShortcut().orElse(null)) - .isEqualTo(ClientRegionShortcut.LOCAL); + assertThat(getClientRegionShortcut(postProcessor)).isEqualTo(ClientRegionShortcut.LOCAL); assertThat(postProcessor.resolveClientRegionShortcut()).isEqualTo(ClientRegionShortcut.LOCAL); postProcessor.setClientRegionShortcut(null); @@ -176,11 +174,14 @@ public class GemfireDataSourcePostProcessorTest { GemfireDataSourcePostProcessor postProcessor = spy(new GemfireDataSourcePostProcessor()); assertThat(postProcessor).isNotNull(); - assertThat(postProcessor.getBeanFactory().orElse(null)).isNull(); + assertThat(postProcessor.getBeanFactory()).isNotPresent(); assertThat(postProcessor.using(this.mockBeanFactory)).isSameAs(postProcessor); assertThat(postProcessor.getBeanFactory().orElse(null)).isSameAs(this.mockBeanFactory); + verify(postProcessor, times(1)).using(eq(this.mockBeanFactory)); verify(postProcessor, times(1)).setBeanFactory(eq(this.mockBeanFactory)); + verify(postProcessor, times(2)).getBeanFactory(); + verifyNoMoreInteractions(postProcessor); } @Test @@ -190,10 +191,10 @@ public class GemfireDataSourcePostProcessorTest { .using(ClientRegionShortcut.LOCAL_PERSISTENT); assertThat(postProcessor).isNotNull(); - assertThat(postProcessor.getClientRegionShortcut().orElse(null)).isEqualTo(ClientRegionShortcut.LOCAL_PERSISTENT); + assertThat(getClientRegionShortcut(postProcessor)).isEqualTo(ClientRegionShortcut.LOCAL_PERSISTENT); assertThat(postProcessor.resolveClientRegionShortcut()).isEqualTo(ClientRegionShortcut.LOCAL_PERSISTENT); assertThat(postProcessor.using(ClientRegionShortcut.LOCAL_OVERFLOW)).isSameAs(postProcessor); - assertThat(postProcessor.getClientRegionShortcut().orElse(null)).isEqualTo(ClientRegionShortcut.LOCAL_OVERFLOW); + assertThat(getClientRegionShortcut(postProcessor)).isEqualTo(ClientRegionShortcut.LOCAL_OVERFLOW); assertThat(postProcessor.resolveClientRegionShortcut()).isEqualTo(ClientRegionShortcut.LOCAL_OVERFLOW); verify(postProcessor, times(1)) @@ -260,7 +261,7 @@ public class GemfireDataSourcePostProcessorTest { GemfireDataSourcePostProcessor postProcessor = spy(new GemfireDataSourcePostProcessor()); doReturn(Arrays.asList(expectedRegionNames)).when(postProcessor) - .execute(isA(ClientCache.class), isA(ListRegionsOnServerFunction.class), any()); + .execute(isA(ClientCache.class), isA(ListRegionsOnServerFunction.class), any(Object[].class)); Iterable actualRegionNames = postProcessor.regionNames(this.mockClientCache); @@ -280,7 +281,7 @@ public class GemfireDataSourcePostProcessorTest { GemfireDataSourcePostProcessor postProcessor = spy(new GemfireDataSourcePostProcessor()); doThrow(new RuntimeException("FAIL")).when(postProcessor) - .execute(isA(ClientCache.class), isA(ListRegionsOnServerFunction.class), any()); + .execute(isA(ClientCache.class), isA(ListRegionsOnServerFunction.class), any(Object[].class)); doAnswer(invocation -> Arrays.stream(expectedRegionNames) @@ -288,7 +289,7 @@ public class GemfireDataSourcePostProcessorTest { .map(this::newRegionInformation) .collect(Collectors.toList()) .toArray() - ).when(postProcessor).execute(isA(ClientCache.class), isA(GetRegionsFunction.class), any()); + ).when(postProcessor).execute(isA(ClientCache.class), isA(GetRegionsFunction.class), any(Object[].class)); List actualRegionNames = StreamSupport.stream(postProcessor.regionNames(this.mockClientCache).spliterator(), false) @@ -309,15 +310,14 @@ public class GemfireDataSourcePostProcessorTest { GemfireDataSourcePostProcessor postProcessor = spy(new GemfireDataSourcePostProcessor()); doThrow(new RuntimeException("FAIL")).when(postProcessor) - .execute(any(ClientCache.class), isA(ListRegionsOnServerFunction.class), any()); + .execute(any(ClientCache.class), isA(ListRegionsOnServerFunction.class), any(Object[].class)); doReturn(null).when(postProcessor) - .execute(any(ClientCache.class), isA(GetRegionsFunction.class), any()); + .execute(any(ClientCache.class), isA(GetRegionsFunction.class), any(Object[].class)); Iterable actualRegionNames = postProcessor.regionNames(this.mockClientCache); - assertThat(actualRegionNames).isNotNull(); - assertThat(actualRegionNames).isEmpty(); + assertThat(actualRegionNames).isNotNull().isEmpty(); verify(postProcessor, times(1)) .execute(eq(this.mockClientCache), isA(ListRegionsOnServerFunction.class)); @@ -333,17 +333,16 @@ public class GemfireDataSourcePostProcessorTest { doAnswer(invocation -> { - Function function = invocation.getArgument(0); + Function function = invocation.getArgument(0); throw newIllegalArgumentException("Function [%1$s] with ID [%2$s] not registered", function.getClass().getName(), function.getId()); - }).when(postProcessor).execute(any(ClientCache.class), any(Function.class), any()); + }).when(postProcessor).execute(any(ClientCache.class), any(Function.class), any(Object[].class)); Iterable actualRegionNames = postProcessor.regionNames(this.mockClientCache); - assertThat(actualRegionNames).isNotNull(); - assertThat(actualRegionNames).isEmpty(); + assertThat(actualRegionNames).isNotNull().isEmpty(); verify(postProcessor, times(1)) .execute(eq(this.mockClientCache), isA(ListRegionsOnServerFunction.class)); @@ -385,16 +384,15 @@ public class GemfireDataSourcePostProcessorTest { } @Test - @SuppressWarnings("unchecked") public void createClientProxyRegionsIsSuccessful() { - ClientRegionFactory mockClientRegionFactory = mock(ClientRegionFactory.class); + ClientRegionFactory mockClientRegionFactory = mock(ClientRegionFactory.class); - when(this.mockClientCache.createClientRegionFactory(eq(ClientRegionShortcut.PROXY))) - .thenReturn(mockClientRegionFactory); + doReturn(mockClientRegionFactory) + .when(this.mockClientCache).createClientRegionFactory(eq(ClientRegionShortcut.PROXY)); - Region mockRegionOne = mock(Region.class, "MockGemFireRegionOne"); - Region mockRegionTwo = mock(Region.class, "MockGemFireRegionTwo"); + Region mockRegionOne = mock(Region.class, "MockGemFireRegionOne"); + Region mockRegionTwo = mock(Region.class, "MockGemFireRegionTwo"); Map> regionMap = new HashMap<>(2); @@ -425,18 +423,17 @@ public class GemfireDataSourcePostProcessorTest { } @Test - @SuppressWarnings("unchecked") public void createClientProxyRegionsWhenRegionBeanExists() { - ClientRegionFactory mockClientRegionFactory = mock(ClientRegionFactory.class); + ClientRegionFactory mockClientRegionFactory = mock(ClientRegionFactory.class); - when(this.mockClientCache.createClientRegionFactory(eq(ClientRegionShortcut.PROXY))) - .thenReturn(mockClientRegionFactory); + doReturn(mockClientRegionFactory) + .when(this.mockClientCache).createClientRegionFactory(eq(ClientRegionShortcut.PROXY)); - Region mockRegion = mock(Region.class); + Region mockRegion = mock(Region.class); - when(this.mockBeanFactory.containsBean(any(String.class))).thenReturn(true); - when(this.mockBeanFactory.getBean(eq("Example"))).thenReturn(mockRegion); + doReturn(true).when(this.mockBeanFactory).containsBean(anyString()); + doReturn(mockRegion).when(this.mockBeanFactory).getBean(eq("Example")); GemfireDataSourcePostProcessor postProcessor = new GemfireDataSourcePostProcessor(); diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperationsUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperationsUnitTests.java index 8c473c73..c79a8bfc 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperationsUnitTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/config/admin/GemfireAdminOperationsUnitTests.java @@ -13,52 +13,56 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.config.admin; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; import java.util.Arrays; import java.util.Collections; +import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; import org.apache.geode.cache.Region; import org.apache.geode.cache.query.Index; import org.springframework.data.gemfire.config.schema.SchemaObjectDefinition; -import org.springframework.data.gemfire.config.schema.SchemaObjectType; import org.springframework.data.gemfire.config.schema.definitions.IndexDefinition; import org.springframework.data.gemfire.config.schema.definitions.RegionDefinition; /** - * The GemfireAdminOperationsUnitTests class... + * Unit Tests for {@link GemfireAdminOperations}. * * @author John Blum - * @since 1.0.0 + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.springframework.data.gemfire.config.admin.GemfireAdminOperations + * @since 1.9.0 */ -@RunWith(MockitoJUnitRunner.class) public class GemfireAdminOperationsUnitTests { - @Mock private GemfireAdminOperations adminOperations; + @Before + @SuppressWarnings("deprecation") + public void setup() { + this.adminOperations = mock(GemfireAdminOperations.class, withSettings().lenient()); + } + private Index mockIndex(String name) { Index mockIndex = mock(Index.class, name); - when(mockIndex.getName()).thenReturn(name); + doReturn(name).when(mockIndex).getName(); return mockIndex; } @@ -68,225 +72,227 @@ public class GemfireAdminOperationsUnitTests { Region mockRegion = mock(Region.class, name); - when(mockRegion.getName()).thenReturn(name); + doReturn(name).when(mockRegion).getName(); return mockRegion; } - private SchemaObjectDefinition newGenericSchemaObjectDefinition(String name, SchemaObjectType type) { + private SchemaObjectDefinition newGenericSchemaObjectDefinition(String name) { return mock(SchemaObjectDefinition.class, name); } @Test public void createRegionsWithArrayCallsCreateRegion() { - doCallRealMethod().when(adminOperations).createRegions(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations) + .createRegions(any(RegionDefinition.class), any(RegionDefinition.class)); + + //doCallRealMethod().when(this.adminOperations).createRegions(any(RegionDefinition[].class)); RegionDefinition definitionOne = RegionDefinition.from(mockRegion("RegionOne")); RegionDefinition definitionTwo = RegionDefinition.from(mockRegion("RegionTwo")); - adminOperations.createRegions(definitionOne, definitionTwo); + this.adminOperations.createRegions(definitionOne, definitionTwo); - verify(adminOperations, times(1)).createRegion(eq(definitionOne)); - verify(adminOperations, times(1)).createRegion(eq(definitionTwo)); + verify(this.adminOperations, times(1)).createRegion(eq(definitionOne)); + verify(this.adminOperations, times(1)).createRegion(eq(definitionTwo)); } @Test public void createRegionsWithEmptyArray() { - doCallRealMethod().when(adminOperations).createRegions(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations).createRegions(ArgumentMatchers.any()); - adminOperations.createRegions(); + this.adminOperations.createRegions(); - verify(adminOperations, never()).createRegion(any(RegionDefinition.class)); + verify(this.adminOperations, never()).createRegion(any(RegionDefinition.class)); } @Test public void createRegionsWithNullArray() { - doCallRealMethod().when(adminOperations).createRegions(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations).createRegions(ArgumentMatchers.any()); - adminOperations.createRegions((RegionDefinition[]) null); + this.adminOperations.createRegions((RegionDefinition[]) null); - verify(adminOperations, never()).createRegion(any(RegionDefinition.class)); + verify(this.adminOperations, never()).createRegion(any(RegionDefinition.class)); } @Test @SuppressWarnings("unchecked") public void createRegionsWithIterableCallsCreateRegion() { - doCallRealMethod().when(adminOperations).createRegions(any(Iterable.class)); + doCallRealMethod().when(this.adminOperations).createRegions(any(Iterable.class)); RegionDefinition definitionOne = RegionDefinition.from(mockRegion("RegionOne")); RegionDefinition definitionTwo = RegionDefinition.from(mockRegion("RegionTwo")); - adminOperations.createRegions(Arrays.asList(definitionOne, definitionTwo)); + this.adminOperations.createRegions(Arrays.asList(definitionOne, definitionTwo)); - verify(adminOperations, times(1)).createRegion(eq(definitionOne)); - verify(adminOperations, times(1)).createRegion(eq(definitionTwo)); + verify(this.adminOperations, times(1)).createRegion(eq(definitionOne)); + verify(this.adminOperations, times(1)).createRegion(eq(definitionTwo)); } @Test @SuppressWarnings("unchecked") public void createRegionsWithEmptyIterableCallsCreateRegion() { - doCallRealMethod().when(adminOperations).createRegions(any(Iterable.class)); + doCallRealMethod().when(this.adminOperations).createRegions(any(Iterable.class)); - adminOperations.createRegions(Collections.emptyList()); + this.adminOperations.createRegions(Collections.emptyList()); - verify(adminOperations, never()).createRegion(any(RegionDefinition.class)); + verify(this.adminOperations, never()).createRegion(any(RegionDefinition.class)); } @Test - @SuppressWarnings("unchecked") public void createRegionsWithNullIterableCallsCreateRegion() { - adminOperations.createRegions((Iterable) null); + this.adminOperations.createRegions((Iterable) null); - verify(adminOperations, never()).createRegion(any(RegionDefinition.class)); + verify(this.adminOperations, never()).createRegion(any(RegionDefinition.class)); } @Test public void createLuceneIndexesWithArrayCallsCreateLuceneIndex() { - doCallRealMethod().when(adminOperations).createLuceneIndexes(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations) + .createLuceneIndexes(any(SchemaObjectDefinition.class), any(SchemaObjectDefinition.class)); - SchemaObjectDefinition definitionOne = newGenericSchemaObjectDefinition("LucenIndexOne", - SchemaObjectType.LUCENE_INDEX); + //doCallRealMethod().when(this.adminOperations).createLuceneIndexes(any(SchemaObjectDefinition[].class)); - SchemaObjectDefinition definitionTwo = newGenericSchemaObjectDefinition("LucenIndexOne", - SchemaObjectType.LUCENE_INDEX); + SchemaObjectDefinition definitionOne = newGenericSchemaObjectDefinition("LucenIndexOne"); + SchemaObjectDefinition definitionTwo = newGenericSchemaObjectDefinition("LucenIndexOne"); - adminOperations.createLuceneIndexes(definitionOne, definitionTwo); + this.adminOperations.createLuceneIndexes(definitionOne, definitionTwo); - verify(adminOperations, times(1)).createLuceneIndex(eq(definitionOne)); - verify(adminOperations, times(1)).createLuceneIndex(eq(definitionTwo)); + verify(this.adminOperations, times(1)).createLuceneIndex(eq(definitionOne)); + verify(this.adminOperations, times(1)).createLuceneIndex(eq(definitionTwo)); } @Test public void createLuceneIndexesWithEmptyArray() { - doCallRealMethod().when(adminOperations).createLuceneIndexes(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations) + .createLuceneIndexes(ArgumentMatchers.any()); - adminOperations.createLuceneIndexes(); + this.adminOperations.createLuceneIndexes(); - verify(adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); + verify(this.adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); } @Test public void createLuceneIndexesWithNullArray() { - doCallRealMethod().when(adminOperations).createLuceneIndexes(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations) + .createLuceneIndexes(ArgumentMatchers.any()); - adminOperations.createLuceneIndexes((SchemaObjectDefinition) null); + this.adminOperations.createLuceneIndexes((SchemaObjectDefinition[]) null); - verify(adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); + verify(this.adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); } @Test @SuppressWarnings("unchecked") public void createLuceneIndexesWithIterableCallsCreateLuceneIndex() { - doCallRealMethod().when(adminOperations).createLuceneIndexes(any(Iterable.class)); + doCallRealMethod().when(this.adminOperations).createLuceneIndexes(any(Iterable.class)); - SchemaObjectDefinition definitionOne = newGenericSchemaObjectDefinition("LucenIndexOne", - SchemaObjectType.LUCENE_INDEX); + SchemaObjectDefinition definitionOne = newGenericSchemaObjectDefinition("LucenIndexOne"); + SchemaObjectDefinition definitionTwo = newGenericSchemaObjectDefinition("LucenIndexOne"); - SchemaObjectDefinition definitionTwo = newGenericSchemaObjectDefinition("LucenIndexOne", - SchemaObjectType.LUCENE_INDEX); + this.adminOperations.createLuceneIndexes(Arrays.asList(definitionOne, definitionTwo)); - adminOperations.createLuceneIndexes(Arrays.asList(definitionOne, definitionTwo)); - - verify(adminOperations, times(1)).createLuceneIndex(eq(definitionOne)); - verify(adminOperations, times(1)).createLuceneIndex(eq(definitionTwo)); + verify(this.adminOperations, times(1)).createLuceneIndex(eq(definitionOne)); + verify(this.adminOperations, times(1)).createLuceneIndex(eq(definitionTwo)); } @Test @SuppressWarnings("unchecked") public void createLuceneIndexesWithEmptyIterableCallsCreateLuceneIndex() { - doCallRealMethod().when(adminOperations).createLuceneIndexes(any(Iterable.class)); + doCallRealMethod().when(this.adminOperations).createLuceneIndexes(any(Iterable.class)); - adminOperations.createLuceneIndexes(Collections.emptyList()); + this.adminOperations.createLuceneIndexes(Collections.emptyList()); - verify(adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); + verify(this.adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); } @Test - @SuppressWarnings("unchecked") public void createLuceneIndexesWithNullIterableCallsCreateLuceneIndex() { - adminOperations.createLuceneIndexes((Iterable) null); + this.adminOperations.createLuceneIndexes((Iterable) null); - verify(adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); + verify(this.adminOperations, never()).createLuceneIndex(any(SchemaObjectDefinition.class)); } @Test public void createIndexesWithArrayCallsCreateIndex() { - doCallRealMethod().when(adminOperations).createIndexes(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations) + .createIndexes(any(IndexDefinition.class), any(IndexDefinition.class)); + + //doCallRealMethod().when(this.adminOperations).createIndexes(any(IndexDefinition[].class)); IndexDefinition definitionOne = IndexDefinition.from(mockIndex("IndexOne")); IndexDefinition definitionTwo = IndexDefinition.from(mockIndex("IndexTwo")); - adminOperations.createIndexes(definitionOne, definitionTwo); + this.adminOperations.createIndexes(definitionOne, definitionTwo); - verify(adminOperations, times(1)).createIndex(eq(definitionOne)); - verify(adminOperations, times(1)).createIndex(eq(definitionTwo)); + verify(this.adminOperations, times(1)).createIndex(eq(definitionOne)); + verify(this.adminOperations, times(1)).createIndex(eq(definitionTwo)); } @Test public void createIndexesWithEmptyArray() { - doCallRealMethod().when(adminOperations).createIndexes(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations).createIndexes(ArgumentMatchers.any()); - adminOperations.createIndexes(); + this.adminOperations.createIndexes(); - verify(adminOperations, never()).createIndex(any(IndexDefinition.class)); + verify(this.adminOperations, never()).createIndex(any(IndexDefinition.class)); } @Test public void createIndexesWithNullArray() { - doCallRealMethod().when(adminOperations).createIndexes(ArgumentMatchers.any()); + doCallRealMethod().when(this.adminOperations).createIndexes(ArgumentMatchers.any()); - adminOperations.createIndexes(); + this.adminOperations.createIndexes(); - verify(adminOperations, never()).createIndex(any(IndexDefinition.class)); + verify(this.adminOperations, never()).createIndex(any(IndexDefinition.class)); } @Test @SuppressWarnings("unchecked") public void createIndexesWithIterableCallsCreateIndex() { - doCallRealMethod().when(adminOperations).createIndexes(any(Iterable.class)); + doCallRealMethod().when(this.adminOperations).createIndexes(any(Iterable.class)); IndexDefinition definitionOne = IndexDefinition.from(mockIndex("IndexOne")); IndexDefinition definitionTwo = IndexDefinition.from(mockIndex("IndexTwo")); - adminOperations.createIndexes(Arrays.asList(definitionOne, definitionTwo)); + this.adminOperations.createIndexes(Arrays.asList(definitionOne, definitionTwo)); - verify(adminOperations, times(1)).createIndex(eq(definitionOne)); - verify(adminOperations, times(1)).createIndex(eq(definitionTwo)); + verify(this.adminOperations, times(1)).createIndex(eq(definitionOne)); + verify(this.adminOperations, times(1)).createIndex(eq(definitionTwo)); } @Test @SuppressWarnings("unchecked") public void createIndexesWithEmptyIterable() { - doCallRealMethod().when(adminOperations).createIndexes(any(Iterable.class)); + doCallRealMethod().when(this.adminOperations).createIndexes(any(Iterable.class)); - adminOperations.createIndexes(Collections.emptyList()); + this.adminOperations.createIndexes(Collections.emptyList()); - verify(adminOperations, never()).createIndex(any(IndexDefinition.class)); + verify(this.adminOperations, never()).createIndex(any(IndexDefinition.class)); } @Test - @SuppressWarnings("unchecked") public void createIndexesWithNullIterable() { - adminOperations.createIndexes((Iterable) null); + this.adminOperations.createIndexes((Iterable) null); - verify(adminOperations, never()).createIndex(any(IndexDefinition.class)); + verify(this.adminOperations, never()).createIndex(any(IndexDefinition.class)); } } diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/repository/query/QueryPostProcessorUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/repository/query/QueryPostProcessorUnitTests.java index 7d0a9203..f0b4b847 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/repository/query/QueryPostProcessorUnitTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/repository/query/QueryPostProcessorUnitTests.java @@ -19,10 +19,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import org.junit.Test; import org.mockito.InOrder; @@ -52,24 +56,29 @@ public class QueryPostProcessorUnitTests { QueryPostProcessor mockQueryPostProcessorOne = mock(QueryPostProcessor.class); QueryPostProcessor mockQueryPostProcessorTwo = mock(QueryPostProcessor.class); - when(mockQueryPostProcessorOne.processAfter(any())).thenCallRealMethod(); - when(mockQueryPostProcessorOne.postProcess(any(QueryMethod.class), anyString(), any())).thenReturn(query); - when(mockQueryPostProcessorTwo.postProcess(any(QueryMethod.class), anyString(), any())).thenReturn(query); + doCallRealMethod().when(mockQueryPostProcessorOne).processAfter(any()); + doReturn(query).when(mockQueryPostProcessorOne).postProcess(any(QueryMethod.class), anyString(), anyString()); + doReturn(query).when(mockQueryPostProcessorTwo).postProcess(any(QueryMethod.class), anyString(), anyString()); QueryPostProcessor composite = mockQueryPostProcessorOne.processAfter(mockQueryPostProcessorTwo); assertThat(composite).isNotNull(); assertThat(composite).isNotSameAs(mockQueryPostProcessorOne); assertThat(composite).isNotSameAs(mockQueryPostProcessorTwo); - assertThat(composite.postProcess(mockQueryMethod, query)).isEqualTo(query); + assertThat(composite.postProcess(mockQueryMethod, query, "arg")).isEqualTo(query); InOrder inOrder = inOrder(mockQueryPostProcessorOne, mockQueryPostProcessorTwo); + inOrder.verify(mockQueryPostProcessorOne, times(1)) + .processAfter(eq(mockQueryPostProcessorTwo)); + inOrder.verify(mockQueryPostProcessorTwo, times(1)) - .postProcess(eq(mockQueryMethod), eq(query), any()); + .postProcess(eq(mockQueryMethod), eq(query), eq("arg")); inOrder.verify(mockQueryPostProcessorOne, times(1)) - .postProcess(eq(mockQueryMethod), eq(query), any()); + .postProcess(eq(mockQueryMethod), eq(query), eq("arg")); + + verifyNoMoreInteractions(mockQueryPostProcessorOne, mockQueryPostProcessorTwo); } @Test @@ -77,9 +86,12 @@ public class QueryPostProcessorUnitTests { QueryPostProcessor mockQueryPostProcessor = mock(QueryPostProcessor.class); - when(mockQueryPostProcessor.processAfter(any())).thenCallRealMethod(); + doCallRealMethod().when(mockQueryPostProcessor).processAfter(any()); assertThat(mockQueryPostProcessor.processAfter(null)).isSameAs(mockQueryPostProcessor); + + verify(mockQueryPostProcessor, times(1)).processAfter(isNull()); + verifyNoMoreInteractions(mockQueryPostProcessor); } @Test @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -89,27 +101,32 @@ public class QueryPostProcessorUnitTests { String query = "SELECT * FROM /Test"; - QueryPostProcessor mockQueryPostProcessorOne = mock(QueryPostProcessor.class); - QueryPostProcessor mockQueryPostProcessorTwo = mock(QueryPostProcessor.class); + QueryPostProcessor mockQueryPostProcessorOne = mock(QueryPostProcessor.class, "ONE"); + QueryPostProcessor mockQueryPostProcessorTwo = mock(QueryPostProcessor.class, "TWO"); - when(mockQueryPostProcessorOne.processBefore(any())).thenCallRealMethod(); - when(mockQueryPostProcessorOne.postProcess(any(QueryMethod.class), anyString(), any())).thenReturn(query); - when(mockQueryPostProcessorTwo.postProcess(any(QueryMethod.class), anyString(), any())).thenReturn(query); + doCallRealMethod().when(mockQueryPostProcessorOne).processBefore(any()); + doReturn(query).when(mockQueryPostProcessorOne).postProcess(any(QueryMethod.class), anyString(), anyString()); + doReturn(query).when(mockQueryPostProcessorTwo).postProcess(any(QueryMethod.class), anyString(), anyString()); QueryPostProcessor composite = mockQueryPostProcessorOne.processBefore(mockQueryPostProcessorTwo); assertThat(composite).isNotNull(); assertThat(composite).isNotSameAs(mockQueryPostProcessorOne); assertThat(composite).isNotSameAs(mockQueryPostProcessorTwo); - assertThat(composite.postProcess(mockQueryMethod, query)).isEqualTo(query); + assertThat(composite.postProcess(mockQueryMethod, query, "arg")).isEqualTo(query); InOrder inOrder = inOrder(mockQueryPostProcessorOne, mockQueryPostProcessorTwo); inOrder.verify(mockQueryPostProcessorOne, times(1)) - .postProcess(eq(mockQueryMethod), eq(query), any()); + .processBefore(eq(mockQueryPostProcessorTwo)); + + inOrder.verify(mockQueryPostProcessorOne, times(1)) + .postProcess(eq(mockQueryMethod), eq(query), eq("arg")); inOrder.verify(mockQueryPostProcessorTwo, times(1)) - .postProcess(eq(mockQueryMethod), eq(query), any()); + .postProcess(eq(mockQueryMethod), eq(query), eq("arg")); + + verifyNoMoreInteractions(mockQueryPostProcessorOne, mockQueryPostProcessorTwo); } @Test @@ -117,8 +134,11 @@ public class QueryPostProcessorUnitTests { QueryPostProcessor mockQueryPostProcessor = mock(QueryPostProcessor.class); - when(mockQueryPostProcessor.processBefore(any())).thenCallRealMethod(); + doCallRealMethod().when(mockQueryPostProcessor).processBefore(any()); assertThat(mockQueryPostProcessor.processBefore(null)).isSameAs(mockQueryPostProcessor); + + verify(mockQueryPostProcessor, times(1)).processBefore(isNull()); + verifyNoMoreInteractions(mockQueryPostProcessor); } }