From 249e64586537d9be9331971b9e3b3d864b6cf694 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 8 Mar 2017 15:21:05 -0800 Subject: [PATCH] SGF-402 - Add SDG Annotation configuration support for Lucene Integration. --- .../EntityDefinedRegionsConfiguration.java | 5 + .../config/annotation/IndexConfiguration.java | 150 ++++++++++++------ .../mapping/annotation/LuceneIndexed.java | 74 +++++++++ .../EnableIndexesConfigurationUnitTests.java | 92 +++++++++-- .../test/entities/PartitionRegionEntity.java | 5 + 5 files changed, 266 insertions(+), 60 deletions(-) create mode 100644 src/main/java/org/springframework/data/gemfire/mapping/annotation/LuceneIndexed.java diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java index 6fb7736a..cd1a1e98 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java @@ -205,6 +205,11 @@ public class EntityDefinedRegionsConfiguration return importingClassMetadata.hasAnnotation(annotationName); } + /* (non-Javadoc) */ + protected AnnotationAttributes getAnnotationAttributes(Annotation annotation) { + return AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(annotation)); + } + /* (non-Javadoc) */ protected AnnotationAttributes getAnnotationAttributes(AnnotationMetadata importingClassMetadata) { return getAnnotationAttributes(importingClassMetadata, getAnnotationTypeName()); diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/IndexConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/IndexConfiguration.java index 0f385297..b17f5846 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/IndexConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/IndexConfiguration.java @@ -18,8 +18,11 @@ package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; +import java.util.Optional; import org.apache.geode.cache.Region; +import org.apache.geode.cache.lucene.LuceneIndex; +import org.apache.geode.cache.query.Index; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.annotation.AnnotationAttributes; @@ -32,13 +35,15 @@ import org.springframework.data.gemfire.config.xml.GemfireConstants; import org.springframework.data.gemfire.mapping.GemfirePersistentEntity; import org.springframework.data.gemfire.mapping.GemfirePersistentProperty; import org.springframework.data.gemfire.mapping.annotation.Indexed; +import org.springframework.data.gemfire.mapping.annotation.LuceneIndexed; +import org.springframework.data.gemfire.search.lucene.LuceneIndexFactoryBean; import org.springframework.data.mapping.PropertyHandler; import org.springframework.util.StringUtils; /** * The {@link IndexConfiguration} class is a Spring {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar} * and extension of {@link EntityDefinedRegionsConfiguration} used in the {@link EnableIndexes} annotation - * to dynamically create GemFire/Geode {@link org.apache.geode.cache.Region} Indexes based on + * to dynamically create GemFire/Geode {@link org.apache.geode.cache.Region} {@link Index Indexes} based on * {@link GemfirePersistentEntity} {@link GemfirePersistentProperty} annotations. * * @author John Blum @@ -48,11 +53,13 @@ import org.springframework.util.StringUtils; * @see org.springframework.data.annotation.Id * @see org.springframework.data.gemfire.IndexFactoryBean * @see org.springframework.data.gemfire.IndexType + * @see org.springframework.data.gemfire.search.lucene.LuceneIndexFactoryBean * @see org.springframework.data.gemfire.config.annotation.EntityDefinedRegionsConfiguration * @see org.springframework.data.gemfire.mapping.GemfirePersistentEntity * @see org.springframework.data.gemfire.mapping.GemfirePersistentProperty - * @see Indexed + * @see org.springframework.data.gemfire.mapping.annotation.Indexed * @see org.apache.geode.cache.Region + * @see org.apache.geode.cache.lucene.LuceneIndex * @see org.apache.geode.cache.query.Index * @since 1.9.0 */ @@ -104,32 +111,27 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration { */ @Override protected GemfirePersistentEntity postProcess(AnnotationMetadata importingClassMetadata, - final BeanDefinitionRegistry registry, GemfirePersistentEntity persistentEntity) { + BeanDefinitionRegistry registry, GemfirePersistentEntity persistentEntity) { - final GemfirePersistentEntity localPersistentEntity = + GemfirePersistentEntity localPersistentEntity = super.postProcess(importingClassMetadata, registry, persistentEntity); if (isAnnotationPresent(importingClassMetadata, getEnableIndexesAnnotationTypeName())) { - final AnnotationAttributes enableIndexesAttributes = + AnnotationAttributes enableIndexesAttributes = getAnnotationAttributes(importingClassMetadata, getEnableIndexesAnnotationTypeName()); - localPersistentEntity.doWithProperties(new PropertyHandler() { - @Override - public void doWithPersistentProperty(GemfirePersistentProperty persistentProperty) { - Id idAnnotation = persistentProperty.findAnnotation(Id.class); + localPersistentEntity.doWithProperties((PropertyHandler) persistentProperty -> { + Optional.ofNullable(persistentProperty.findAnnotation(Id.class)).ifPresent(idAnnotation -> + registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity, persistentProperty, + IndexType.KEY, idAnnotation, registry)); - if (idAnnotation != null) { - registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity, persistentProperty, - IndexType.KEY, idAnnotation, registry); - } + Optional.ofNullable(persistentProperty.findAnnotation(Indexed.class)).ifPresent(indexedAnnotation -> + registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity, persistentProperty, + indexedAnnotation.type(), indexedAnnotation, registry)); - Indexed indexedAnnotation = persistentProperty.findAnnotation(Indexed.class); - - if (indexedAnnotation != null) { - registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity, persistentProperty, - indexedAnnotation.type(), indexedAnnotation, registry); - } - } + Optional.ofNullable(persistentProperty.findAnnotation(LuceneIndexed.class)).ifPresent( + luceneIndexAnnotation -> registerLuceneIndexBeanDefinition(enableIndexesAttributes, + localPersistentEntity, persistentProperty, luceneIndexAnnotation, registry)); }); } @@ -155,38 +157,87 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration { * @see org.springframework.data.gemfire.mapping.GemfirePersistentEntity * @see org.springframework.data.gemfire.mapping.GemfirePersistentProperty */ - @SuppressWarnings("unused") protected void registerIndexBeanDefinition(AnnotationAttributes enableIndexesAttributes, GemfirePersistentEntity persistentEntity, GemfirePersistentProperty persistentProperty, IndexType indexType, Annotation indexAnnotation, BeanDefinitionRegistry registry) { - if (indexAnnotation != null) { - AnnotationAttributes indexAnnotationAttributes = AnnotationAttributes.fromMap( - AnnotationUtils.getAnnotationAttributes(indexAnnotation)); + Optional.ofNullable(indexAnnotation).ifPresent(localIndexAnnotation -> { + AnnotationAttributes indexedAttributes = getAnnotationAttributes(localIndexAnnotation); BeanDefinitionBuilder indexFactoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition(IndexFactoryBean.class); - String indexName = resolveName(persistentEntity, persistentProperty, - indexAnnotationAttributes, indexType); + String indexName = resolveName(persistentEntity, persistentProperty, indexedAttributes, indexType); indexFactoryBeanBuilder.addPropertyReference("cache", GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME); indexFactoryBeanBuilder.addPropertyValue("define", resolveDefine(enableIndexesAttributes)); indexFactoryBeanBuilder.addPropertyValue("expression", - resolveExpression(persistentEntity, persistentProperty, indexAnnotationAttributes)); + resolveExpression(persistentEntity, persistentProperty, indexedAttributes)); indexFactoryBeanBuilder.addPropertyValue("from", - resolveFrom(persistentEntity, persistentProperty, indexAnnotationAttributes)); + resolveFrom(persistentEntity, persistentProperty, indexedAttributes)); indexFactoryBeanBuilder.addPropertyValue("name", indexName); + /* + indexFactoryBeanBuilder.addPropertyValue("override", + resolveOverride(persistentEntity, persistentProperty, indexedAttributes)); + */ + indexFactoryBeanBuilder.addPropertyValue("type", - resolveType(persistentEntity, persistentProperty, indexAnnotationAttributes, indexType).toString()); + resolveType(persistentEntity, persistentProperty, indexedAttributes, indexType).toString()); registry.registerBeanDefinition(indexName, indexFactoryBeanBuilder.getBeanDefinition()); - } + }); + } + + /** + * Registers a {@link LuceneIndex} for the {@link GemfirePersistentProperty} on the {@link GemfirePersistentEntity} + * using the {@link Annotation} meta-data to define the Index. + * + * @param enableIndexesAttributes {@link AnnotationAttributes} containing meta-data + * for the {@link EnableIndexes} annotation. + * @param persistentEntity {@link GemfirePersistentEntity} containing the {@link GemfirePersistentProperty} + * to be indexed. + * @param persistentProperty {@link GemfirePersistentProperty} for which the {@link LuceneIndex} will be created. + * @param luceneIndexAnnotation {@link LuceneIndexed} {@link Annotation}. + * @param registry {@link BeanDefinitionRegistry} used to register the {@link LuceneIndex} bean definition. + * @see java.lang.annotation.Annotation + * @see org.springframework.beans.factory.support.BeanDefinitionBuilder + * @see org.springframework.beans.factory.support.BeanDefinitionRegistry + * @see org.springframework.data.gemfire.mapping.GemfirePersistentEntity + * @see org.springframework.data.gemfire.mapping.GemfirePersistentProperty + * @see org.springframework.data.gemfire.mapping.annotation.LuceneIndexed + */ + @SuppressWarnings("unused") + protected void registerLuceneIndexBeanDefinition(AnnotationAttributes enableIndexesAttributes, + GemfirePersistentEntity persistentEntity, GemfirePersistentProperty persistentProperty, + Annotation luceneIndexAnnotation, BeanDefinitionRegistry registry) { + + Optional.ofNullable(luceneIndexAnnotation).ifPresent(localLuceneIndexAnnotation -> { + AnnotationAttributes luceneIndexAttributes = + AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(localLuceneIndexAnnotation)); + + BeanDefinitionBuilder luceneIndexFactoryBeanBuilder = + BeanDefinitionBuilder.genericBeanDefinition(LuceneIndexFactoryBean.class); + + String indexName = luceneIndexAttributes.getString("name"); + + boolean destroy = (luceneIndexAttributes.containsKey("destroy") + && luceneIndexAttributes.getBoolean("destroy")); + + luceneIndexFactoryBeanBuilder.addPropertyValue("destroy", destroy); + + luceneIndexFactoryBeanBuilder.addPropertyValue("fields", persistentProperty.getName()); + + luceneIndexFactoryBeanBuilder.addPropertyValue("indexName", indexName); + + luceneIndexFactoryBeanBuilder.addPropertyValue("regionPath", persistentEntity.getRegionName()); + + registry.registerBeanDefinition(indexName, luceneIndexFactoryBeanBuilder.getBeanDefinition()); + }); } /* (non-Javadoc) */ @@ -198,10 +249,10 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration { /* (non-Javadoc) */ @SuppressWarnings("unused") private String resolveExpression(GemfirePersistentEntity persistentEntity, - GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes) { + GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes) { - String expression = (indexAnnotationAttributes.containsKey("expression") - ? indexAnnotationAttributes.getString("expression") : null); + String expression = (indexedAttributes.containsKey("expression") + ? indexedAttributes.getString("expression") : null); return (StringUtils.hasText(expression) ? expression : persistentProperty.getName()); } @@ -209,21 +260,20 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration { /* (non-Javadoc) */ @SuppressWarnings("unused") private String resolveFrom(GemfirePersistentEntity persistentEntity, - GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes) { + GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes) { - String from = (indexAnnotationAttributes.containsKey("from") - ? indexAnnotationAttributes.getString("from") : null); + String from = (indexedAttributes.containsKey("from") + ? indexedAttributes.getString("from") : null); return (StringUtils.hasText(from) ? from : persistentEntity.getRegionName()); } /* (non-Javadoc) */ private String resolveName(GemfirePersistentEntity persistentEntity, - GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes, - IndexType indexType) { + GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes, IndexType indexType) { - String indexName = (indexAnnotationAttributes.containsKey("name") - ? indexAnnotationAttributes.getString("name") : null); + String indexName = (indexedAttributes.containsKey("name") + ? indexedAttributes.getString("name") : null); return (StringUtils.hasText(indexName) ? indexName : generateIndexName(persistentEntity, persistentProperty, indexType)); @@ -238,15 +288,25 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration { StringUtils.capitalize(indexType.name().toLowerCase())); } + /* (non-Javadoc) */ + /* + @SuppressWarnings("unused") + private boolean resolveOverride(GemfirePersistentEntity persistentEntity, + GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes) { + + return (indexedAttributes.containsKey("override") + && indexedAttributes.getBoolean("override")); + } + */ + /* (non-Javadoc) */ @SuppressWarnings("unused") private IndexType resolveType(GemfirePersistentEntity persistentEntity, - GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes, - IndexType indexType) { + GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes, IndexType indexType) { - IndexType resolvedIndexType = (indexAnnotationAttributes.containsKey("type") - ? indexAnnotationAttributes.getEnum("type") : null); + IndexType resolvedIndexType = (indexedAttributes.containsKey("type") + ? indexedAttributes.getEnum("type") : null); - return (resolvedIndexType != null ? resolvedIndexType : indexType); + return Optional.ofNullable(resolvedIndexType).orElse(indexType); } } diff --git a/src/main/java/org/springframework/data/gemfire/mapping/annotation/LuceneIndexed.java b/src/main/java/org/springframework/data/gemfire/mapping/annotation/LuceneIndexed.java new file mode 100644 index 00000000..15320ab5 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/mapping/annotation/LuceneIndexed.java @@ -0,0 +1,74 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.springframework.data.gemfire.mapping.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.apache.geode.cache.lucene.LuceneIndex; +import org.springframework.core.annotation.AliasFor; +import org.springframework.data.gemfire.mapping.GemfirePersistentEntity; +import org.springframework.data.gemfire.mapping.GemfirePersistentProperty; + +/** + * The {@link LuceneIndexed} annotation is used to index a {@link GemfirePersistentEntity} + * {@link GemfirePersistentProperty}, creating a GemFire/Geode {@link LuceneIndex} + * on a GemFire/Geode {@link org.apache.geode.cache.Region}. + * + * @author John Blum + * @see org.springframework.core.annotation.AliasFor + * @see org.springframework.data.gemfire.IndexType + * @see org.apache.geode.cache.Region + * @see org.apache.geode.cache.lucene.LuceneIndex + * @since 1.1.0 + */ +@Target({ ElementType.FIELD, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Documented +@SuppressWarnings("unused") +public @interface LuceneIndexed { + + /** + * {@link String Name} of the {@link LuceneIndex}. + * + * @return a {@link String} containing the name of the {@link LuceneIndex} + */ + @AliasFor(attribute = "name") + String value() default ""; + + /** + * {@link String Name} of the {@link LuceneIndex}. + * + * @return a {@link String} containing the name of the {@link LuceneIndex} + */ + @AliasFor(attribute = "value") + String name() default ""; + + /** + * Determine whether the {@link LuceneIndex} should be destroy when the application shutsdown. + * + * Default is {@literal false}. + */ + boolean destroy() default false; + +} diff --git a/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexesConfigurationUnitTests.java b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexesConfigurationUnitTests.java index f96318c0..be018836 100644 --- a/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexesConfigurationUnitTests.java +++ b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexesConfigurationUnitTests.java @@ -20,19 +20,25 @@ package org.springframework.data.gemfire.config.annotation; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.Map; +import java.util.Optional; import org.apache.geode.cache.Cache; import org.apache.geode.cache.RegionAttributes; import org.apache.geode.cache.RegionFactory; import org.apache.geode.cache.RegionShortcut; +import org.apache.geode.cache.lucene.LuceneIndex; +import org.apache.geode.cache.lucene.LuceneService; import org.apache.geode.cache.query.Index; import org.apache.geode.cache.query.QueryService; import org.junit.After; import org.junit.Test; +import org.mockito.Matchers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.springframework.context.ConfigurableApplicationContext; @@ -65,9 +71,7 @@ public class EnableIndexesConfigurationUnitTests { @After public void tearDown() { - if (applicationContext != null) { - applicationContext.close(); - } + Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close); } /* (non-Javadoc) */ @@ -79,6 +83,15 @@ public class EnableIndexesConfigurationUnitTests { assertThat(index.getType()).isEqualTo(indexType.getGemfireIndexType()); } + /* (non-Javadoc) */ + protected void assertLuceneIndex(LuceneIndex index, String name, String regionPath, String... fields) { + assertThat(index).isNotNull(); + assertThat(index.getName()).isEqualTo(name); + assertThat(index.getRegionPath()).isEqualTo(regionPath); + assertThat(index.getFieldNames()).contains(fields); + assertThat(index.getFieldNames()).hasSize(fields.length); + } + /* (non-Javadoc) */ protected ConfigurableApplicationContext newApplicationContext(Class... annotatedClasses) { ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(annotatedClasses); @@ -87,17 +100,28 @@ public class EnableIndexesConfigurationUnitTests { } @Test - public void pesistentEntityIndexesCreatedSuccessfully() { + public void persistentEntityIndexesCreatedSuccessfully() { applicationContext = newApplicationContext(IndexedPersistentEntityConfiguration.class); + assertLuceneIndexes(applicationContext); + assertOqlIndexes(applicationContext); + } + + private void assertLuceneIndexes(ConfigurableApplicationContext applicationContext) { + LuceneIndex luceneIndex = applicationContext.getBean("TitleLuceneIdx", LuceneIndex.class); + + assertLuceneIndex(luceneIndex, "TitleLuceneIdx", "Customers", "title"); + } + + protected void assertOqlIndexes(ConfigurableApplicationContext applicationContext) { Index customersIdIdx = applicationContext.getBean("CustomersIdKeyIdx", Index.class); assertIndex(customersIdIdx, "CustomersIdKeyIdx", "id", "Customers", IndexType.KEY); Index customersFirstNameIdx = applicationContext.getBean("CustomersFirstNameFunctionalIdx", Index.class); - assertIndex(customersFirstNameIdx, "CustomersFirstNameFunctionalIdx", "first_name", "/LoyalCustomers", - IndexType.FUNCTIONAL); + assertIndex(customersFirstNameIdx, "CustomersFirstNameFunctionalIdx", "first_name", + "/LoyalCustomers", IndexType.FUNCTIONAL); Index lastNameIdx = applicationContext.getBean("LastNameIdx", Index.class); @@ -116,20 +140,18 @@ public class EnableIndexesConfigurationUnitTests { @Configuration @SuppressWarnings("unused") - static class CacheConfiguration { + static class GemFireConfiguration { @Bean @SuppressWarnings("unchecked") Cache gemfireCache() throws Exception { - Cache mockCache = mock(Cache.class); + return mockQueryService(mockRegionFactory(mock(Cache.class))); + } + + Cache mockQueryService(Cache mockCache) throws Exception { QueryService mockQueryService = mock(QueryService.class); - RegionFactory mockRegionFactory = mock(RegionFactory.class); when(mockCache.getQueryService()).thenReturn(mockQueryService); - when(mockCache.createRegionFactory()).thenReturn(mockRegionFactory); - when(mockCache.createRegionFactory(any(RegionAttributes.class))).thenReturn(mockRegionFactory); - when(mockCache.createRegionFactory(any(RegionShortcut.class))).thenReturn(mockRegionFactory); - when(mockCache.createRegionFactory(anyString())).thenReturn(mockRegionFactory); when(mockQueryService.createHashIndex(anyString(), anyString(), anyString())) .thenAnswer(new HashIndexAnswer()); @@ -142,6 +164,46 @@ public class EnableIndexesConfigurationUnitTests { return mockCache; } + + @SuppressWarnings("unchecked") + Cache mockRegionFactory(Cache mockCache) { + RegionFactory mockRegionFactory = mock(RegionFactory.class); + + when(mockCache.createRegionFactory()).thenReturn(mockRegionFactory); + when(mockCache.createRegionFactory(any(RegionAttributes.class))).thenReturn(mockRegionFactory); + when(mockCache.createRegionFactory(any(RegionShortcut.class))).thenReturn(mockRegionFactory); + when(mockCache.createRegionFactory(anyString())).thenReturn(mockRegionFactory); + + return mockCache; + } + + @Bean + LuceneService luceneService() { + LuceneService mockLuceneService = mock(LuceneService.class); + + doAnswer(invocation -> { + LuceneIndex mockLuceneIndex = mock(LuceneIndex.class); + + String indexName = invocation.getArgumentAt(0, String.class); + String regionPath = invocation.getArgumentAt(1, String.class); + + when(mockLuceneIndex.getName()).thenReturn(indexName); + when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath); + when(mockLuceneIndex.getFieldNames()).thenReturn(resolveFieldNames(invocation)); + when(mockLuceneService.getIndex(eq(indexName), eq(regionPath))).thenReturn(mockLuceneIndex); + + return mockLuceneIndex; + }).when(mockLuceneService).createIndex(anyString(), anyString(), Matchers.anyVararg()); + + return mockLuceneService; + } + + @SuppressWarnings("all") + String[] resolveFieldNames(InvocationOnMock invocation) { + String[] fieldNames = new String[invocation.getArguments().length - 2]; + System.arraycopy(invocation.getArguments(), 2, fieldNames, 0, fieldNames.length); + return fieldNames; + } } static abstract class AbstractIndexAnswer implements Answer { @@ -195,7 +257,7 @@ public class EnableIndexesConfigurationUnitTests { excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { ClientRegionEntity.class, CollocatedPartitionRegionEntity.class, GenericRegionEntity.class, LocalRegionEntity.class, ReplicateRegionEntity.class })) - static class IndexedPersistentEntityConfiguration extends CacheConfiguration { + static class IndexedPersistentEntityConfiguration extends GemFireConfiguration { } @@ -203,7 +265,7 @@ public class EnableIndexesConfigurationUnitTests { excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { ClientRegionEntity.class, CollocatedPartitionRegionEntity.class, GenericRegionEntity.class, LocalRegionEntity.class, ReplicateRegionEntity.class })) - static class NoIndexesCreatedForIndexedPersistentEntityConfiguration extends CacheConfiguration { + static class NoIndexesCreatedForIndexedPersistentEntityConfiguration extends GemFireConfiguration { } } diff --git a/src/test/java/org/springframework/data/gemfire/config/annotation/test/entities/PartitionRegionEntity.java b/src/test/java/org/springframework/data/gemfire/config/annotation/test/entities/PartitionRegionEntity.java index 92cb0ab2..8cecc0c7 100644 --- a/src/test/java/org/springframework/data/gemfire/config/annotation/test/entities/PartitionRegionEntity.java +++ b/src/test/java/org/springframework/data/gemfire/config/annotation/test/entities/PartitionRegionEntity.java @@ -20,6 +20,7 @@ package org.springframework.data.gemfire.config.annotation.test.entities; import org.springframework.data.annotation.Id; import org.springframework.data.gemfire.IndexType; import org.springframework.data.gemfire.mapping.annotation.Indexed; +import org.springframework.data.gemfire.mapping.annotation.LuceneIndexed; import org.springframework.data.gemfire.mapping.annotation.PartitionRegion; /** @@ -35,6 +36,7 @@ import org.springframework.data.gemfire.mapping.annotation.PartitionRegion; @PartitionRegion.FixedPartition(name = "two", numBuckets = 21) } ) +@SuppressWarnings("unused") public class PartitionRegionEntity { @Id @@ -46,4 +48,7 @@ public class PartitionRegionEntity { @Indexed(name = "LastNameIdx") private String lastName; + @LuceneIndexed("TitleLuceneIdx") + private String title; + }