From b5ea0eccd2e8041a2742e54ae58a987d682fcead Mon Sep 17 00:00:00 2001 From: Jordi Llach Date: Mon, 16 Nov 2015 19:48:33 +0100 Subject: [PATCH] DATAMONGO-1163 - Allow usage of @Indexed as meta-annotation. @Indexed can now be used as meta-annotation so that user annotations can be annotated with it and the index creation facilities still pick up the configuration information. Original pull request: #325. --- .../data/mongodb/core/index/Indexed.java | 3 +- .../core/index/IndexingIntegrationTests.java | 35 ++++++++++++------- ...ersistentEntityIndexResolverUnitTests.java | 29 +++++++++++++++ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java index 31eb1ef17..a32262242 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java @@ -29,8 +29,9 @@ import java.lang.annotation.Target; * @author Johno Crawford * @author Thomas Darimont * @author Christoph Strobl + * @author Jordi Llach */ -@Target(ElementType.FIELD) +@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Indexed { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java index 5ac8c4c57..7444ed79f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java @@ -15,10 +15,16 @@ */ package org.springframework.data.mongodb.core.index; +import com.mongodb.DBCollection; +import com.mongodb.DBObject; +import com.mongodb.MongoException; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - import org.junit.After; +import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -30,10 +36,6 @@ import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; -import com.mongodb.MongoException; - /** * Integration tests for index handling. * @@ -45,26 +47,33 @@ import com.mongodb.MongoException; public class IndexingIntegrationTests { @Autowired MongoOperations operations; - - @After + + @After public void tearDown() { - operations.dropCollection(IndexedPerson.class); + operations.dropCollection(IndexedPerson.class); } - - /** + + /** * @see DATADOC-237 + * @see DATAMONGO-1163 */ @Test public void createsIndexWithFieldName() { - operations.save(new IndexedPerson()); assertThat(hasIndex("_firstname", IndexedPerson.class), is(true)); + assertThat(hasIndex("_lastname", IndexedPerson.class), is(true)); } + @Target({ElementType.FIELD}) + @Retention(RetentionPolicy.RUNTIME) + @Indexed + @interface IndexedFieldAnnotation {} + @Document class IndexedPerson { @Field("_firstname") @Indexed String firstname; + @Field("_lastname") @IndexedFieldAnnotation String lastname; } /** @@ -87,4 +96,4 @@ public class IndexingIntegrationTests { } }); } -} +} \ No newline at end of file diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index 3a0fb5727..53273fd1a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -19,6 +19,10 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.util.Collections; import java.util.List; @@ -177,6 +181,19 @@ public class MongoPersistentEntityIndexResolverUnitTests { equalTo(new BasicDBObjectBuilder().add("nested.indexedDbRef", 1).get())); } + /** + * @see DATAMONGO-1163 + */ + @Test + public void resolveIndexDefinitionInMetaAnnotatedFields() { + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + IndexOnMetaAnnotatedField.class); + assertThat(indexDefinitions, hasSize(1)); + assertThat(indexDefinitions.get(0).getCollection(), equalTo("indexOnMetaAnnotatedField")); + assertThat(indexDefinitions.get(0).getIndexOptions(), + equalTo(new BasicDBObjectBuilder().add("name", "_name").get())); + } + @Document(collection = "Zero") static class IndexOnLevelZero { @Indexed String indexedProperty; @@ -231,6 +248,18 @@ public class MongoPersistentEntityIndexResolverUnitTests { } + @Target({ ElementType.FIELD }) + @Retention(RetentionPolicy.RUNTIME) + @Indexed + @interface IndexedFieldAnnotation { + + } + + @Document + static class IndexOnMetaAnnotatedField { + @Field("_name") @IndexedFieldAnnotation String lastname; + } + /** * Test resolution of {@link GeoSpatialIndexed}. *