diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java index fd5c87878..ee20619ac 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeoSpatialIndexed.java @@ -29,7 +29,7 @@ import java.lang.annotation.Target; * @author Christoph Strobl * @author Mark Paluch */ -@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE }) @Retention(RetentionPolicy.RUNTIME) public @interface GeoSpatialIndexed { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Field.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Field.java index 33d1eada8..19ffb6ebd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Field.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Field.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2015 the original author or authors. + * Copyright 2011-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. @@ -16,8 +16,10 @@ package org.springframework.data.mongodb.core.mapping; import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** * Annotation to define custom metadata for document fields. @@ -26,6 +28,7 @@ import java.lang.annotation.RetentionPolicy; */ @Documented @Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE }) public @interface Field { /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Meta.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Meta.java index a24503b5c..d864e6814 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Meta.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Meta.java @@ -29,7 +29,7 @@ import org.springframework.data.annotation.QueryAnnotation; * @since 1.6 */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) @Documented @QueryAnnotation public @interface Meta { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Query.java index 8870ef3a6..97fb25ff1 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Query.java @@ -33,7 +33,7 @@ import org.springframework.data.annotation.QueryAnnotation; * @author Mark Paluch */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) @Documented @QueryAnnotation public @interface Query { 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 ecfa60e04..97554b558 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 @@ -18,7 +18,7 @@ package org.springframework.data.mongodb.core.index; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import static org.springframework.data.mongodb.test.util.IsBsonObject.isBsonObject; +import static org.springframework.data.mongodb.test.util.IsBsonObject.*; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -76,7 +76,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void indexPathOnLevelZeroIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(IndexOnLevelZero.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + IndexOnLevelZero.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection("indexedProperty", "Zero", indexDefinitions.get(0)); @@ -112,7 +113,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void resolvesIndexPathNameForNamedPropertiesCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(IndexOnLevelOneWithExplicitlyNamedField.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + IndexOnLevelOneWithExplicitlyNamedField.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection("customZero.customFieldName", "indexOnLevelOneWithExplicitlyNamedField", @@ -125,11 +127,12 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void resolvesIndexDefinitionCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(IndexOnLevelZero.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + IndexOnLevelZero.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); - assertThat(indexDefinition.getIndexOptions(), equalTo(new BasicDBObjectBuilder().add("name", "indexedProperty") - .get())); + assertThat(indexDefinition.getIndexOptions(), + equalTo(new BasicDBObjectBuilder().add("name", "indexedProperty").get())); } /** @@ -138,7 +141,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void resolvesIndexDefinitionOptionsCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(WithOptionsOnIndexedProperty.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + WithOptionsOnIndexedProperty.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); assertThat(indexDefinition.getIndexOptions(), @@ -152,7 +156,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void resolvesIndexCollectionNameCorrectlyWhenDefinedInAnnotation() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(WithOptionsOnIndexedProperty.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + WithOptionsOnIndexedProperty.class); assertThat(indexDefinitions.get(0).getCollection(), equalTo("CollectionOverride")); } @@ -166,8 +171,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertThat(indexDefinitions, hasSize(1)); assertThat(indexDefinitions.get(0).getCollection(), equalTo("withDbRef")); - assertThat(indexDefinitions.get(0).getIndexKeys(), equalTo(new BasicDBObjectBuilder().add("indexedDbRef", 1) - .get())); + assertThat(indexDefinitions.get(0).getIndexKeys(), + equalTo(new BasicDBObjectBuilder().add("indexedDbRef", 1).get())); } /** @@ -176,7 +181,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void resolvesIndexOnDbrefWhenDefinedOnNestedElement() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(WrapperOfWithDbRef.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + WrapperOfWithDbRef.class); assertThat(indexDefinitions, hasSize(1)); assertThat(indexDefinitions.get(0).getCollection(), equalTo("wrapperOfWithDbRef")); @@ -198,7 +204,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertThat(indexDefinitions.get(0).getIndexOptions(), equalTo(new BasicDBObjectBuilder().add("name", "_name").get())); } - + /** * @see DATAMONGO-1373 */ @@ -216,7 +222,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertThat(indexDefinitionHolder.getIndexOptions(), isBsonObject().containing("sparse", true).containing("unique", true).containing("name", "my_index_name")); } - + /** * @see DATAMONGO-1373 */ @@ -254,8 +260,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { static class WithOptionsOnIndexedProperty { @Indexed(background = true, collection = "CollectionOverride", direction = IndexDirection.DESCENDING, - dropDups = true, expireAfterSeconds = 10, sparse = true, unique = true)// - String indexedProperty; + dropDups = true, expireAfterSeconds = 10, sparse = true, unique = true) // + String indexedProperty; } @Document @@ -277,8 +283,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Document static class WithDbRef { - @Indexed// - @DBRef// + @Indexed // + @DBRef // NoIndex indexedDbRef; } @@ -286,7 +292,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { static class NoIndex { @Id String id; } - + @Document static class IndexedDocumentWithComposedAnnotations { @@ -294,37 +300,36 @@ public class MongoPersistentEntityIndexResolverUnitTests { @CustomIndexedAnnotation String fieldWithDifferentIndexName; @ComposedIndexedAnnotation String fieldWithMyIndexName; } - + @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD }) @ComposedIndexedAnnotation(indexName = "different_name", beUnique = false) static @interface CustomIndexedAnnotation { } - + @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE }) @Indexed static @interface ComposedIndexedAnnotation { - + @AliasFor(annotation = Indexed.class, attribute = "unique") boolean beUnique() default true; - + @AliasFor(annotation = Indexed.class, attribute = "sparse") boolean beSparse() default true; - + @AliasFor(annotation = Indexed.class, attribute = "name") String indexName() default "my_index_name"; } - + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @org.springframework.data.mongodb.core.mapping.Field static @interface ComposedFieldAnnotation { - + @AliasFor(annotation = org.springframework.data.mongodb.core.mapping.Field.class, attribute = "value") String name() default "_id"; } - } @Target({ ElementType.FIELD }) @@ -352,7 +357,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void geoSpatialIndexPathOnLevelZeroIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(GeoSpatialIndexOnLevelZero.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + GeoSpatialIndexOnLevelZero.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection("geoIndexedProperty", "Zero", indexDefinitions.get(0)); @@ -364,7 +370,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void geoSpatialIndexPathOnLevelOneIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(GeoSpatialIndexOnLevelOne.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + GeoSpatialIndexOnLevelOne.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection("zero.geoIndexedProperty", "One", indexDefinitions.get(0)); @@ -376,7 +383,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void depplyNestedGeoSpatialIndexPathIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(GeoSpatialIndexOnLevelTwo.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + GeoSpatialIndexOnLevelTwo.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection("one.zero.geoIndexedProperty", "Two", indexDefinitions.get(0)); @@ -388,30 +396,29 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void resolvesIndexDefinitionOptionsCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(WithOptionsOnGeoSpatialIndexProperty.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + WithOptionsOnGeoSpatialIndexProperty.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); - assertThat( - indexDefinition.getIndexOptions(), - equalTo(new BasicDBObjectBuilder().add("name", "location").add("min", 1).add("max", 100).add("bits", 2).get())); + assertThat(indexDefinition.getIndexOptions(), equalTo( + new BasicDBObjectBuilder().add("name", "location").add("min", 1).add("max", 100).add("bits", 2).get())); } - + /** * @see DATAMONGO-1373 */ @Test public void resolvesComposedAnnotationIndexDefinitionOptionsCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(GeoSpatialIndexedDocumentWithComposedAnnotation.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + GeoSpatialIndexedDocumentWithComposedAnnotation.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); - assertThat( - indexDefinition.getIndexKeys(), + assertThat(indexDefinition.getIndexKeys(), isBsonObject().containing("location", "geoHaystack").containing("What light?", 1)); - assertThat( - indexDefinition.getIndexOptions(), + assertThat(indexDefinition.getIndexOptions(), isBsonObject().containing("name", "my_geo_index_name").containing("bucketSize", 2.0)); } @@ -434,31 +441,31 @@ public class MongoPersistentEntityIndexResolverUnitTests { static class WithOptionsOnGeoSpatialIndexProperty { @GeoSpatialIndexed(collection = "CollectionOverride", bits = 2, max = 100, min = 1, - type = GeoSpatialIndexType.GEO_2D)// - Point location; + type = GeoSpatialIndexType.GEO_2D) // + Point location; } - + @Document(collection = "WithComposedAnnotation") static class GeoSpatialIndexedDocumentWithComposedAnnotation { - @ComposedGeoSpatialIndexed// + @ComposedGeoSpatialIndexed // Point location; } - + @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD }) @GeoSpatialIndexed @interface ComposedGeoSpatialIndexed { - + @AliasFor(annotation = GeoSpatialIndexed.class, attribute = "name") String indexName() default "my_geo_index_name"; - + @AliasFor(annotation = GeoSpatialIndexed.class, attribute = "additionalField") - String theAdditionalFieldINeedToDefine() default "What light?"; - + String theAdditionalFieldINeedToDefine() default "What light?"; + @AliasFor(annotation = GeoSpatialIndexed.class, attribute = "bucketSize") - double size() default 2; - + double size() default 2; + @AliasFor(annotation = GeoSpatialIndexed.class, attribute = "type") GeoSpatialIndexType indexType() default GeoSpatialIndexType.GEO_HAYSTACK; } @@ -478,7 +485,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void compoundIndexPathOnLevelZeroIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(CompoundIndexOnLevelZero.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + CompoundIndexOnLevelZero.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection(new String[] { "foo", "bar" }, "CompoundIndexOnLevelZero", indexDefinitions.get(0)); @@ -490,12 +498,14 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void compoundIndexOptionsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(CompoundIndexOnLevelZero.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + CompoundIndexOnLevelZero.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); assertThat(indexDefinition.getIndexOptions(), equalTo(new BasicDBObjectBuilder().add("name", "compound_index") .add("unique", true).add("dropDups", true).add("sparse", true).add("background", true).get())); - assertThat(indexDefinition.getIndexKeys(), equalTo(new BasicDBObjectBuilder().add("foo", 1).add("bar", -1).get())); + assertThat(indexDefinition.getIndexKeys(), + equalTo(new BasicDBObjectBuilder().add("foo", 1).add("bar", -1).get())); } /** @@ -504,12 +514,14 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void compoundIndexOnSuperClassResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(IndexDefinedOnSuperClass.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + IndexDefinedOnSuperClass.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); assertThat(indexDefinition.getIndexOptions(), equalTo(new BasicDBObjectBuilder().add("name", "compound_index") .add("unique", true).add("dropDups", true).add("sparse", true).add("background", true).get())); - assertThat(indexDefinition.getIndexKeys(), equalTo(new BasicDBObjectBuilder().add("foo", 1).add("bar", -1).get())); + assertThat(indexDefinition.getIndexKeys(), + equalTo(new BasicDBObjectBuilder().add("foo", 1).add("bar", -1).get())); } /** @@ -518,14 +530,14 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void compoundIndexDoesNotSpecifyNameWhenUsingGenerateName() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(ComountIndexWithAutogeneratedName.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + ComountIndexWithAutogeneratedName.class); IndexDefinition indexDefinition = indexDefinitions.get(0).getIndexDefinition(); - assertThat( - indexDefinition.getIndexOptions(), - equalTo(new BasicDBObjectBuilder().add("unique", true).add("dropDups", true).add("sparse", true) - .add("background", true).get())); - assertThat(indexDefinition.getIndexKeys(), equalTo(new BasicDBObjectBuilder().add("foo", 1).add("bar", -1).get())); + assertThat(indexDefinition.getIndexOptions(), equalTo(new BasicDBObjectBuilder().add("unique", true) + .add("dropDups", true).add("sparse", true).add("background", true).get())); + assertThat(indexDefinition.getIndexKeys(), + equalTo(new BasicDBObjectBuilder().add("foo", 1).add("bar", -1).get())); } /** @@ -534,7 +546,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void compoundIndexPathOnLevelOneIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(CompoundIndexOnLevelOne.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + CompoundIndexOnLevelOne.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection(new String[] { "zero.foo", "zero.bar" }, "CompoundIndexOnLevelOne", @@ -547,7 +560,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void emptyCompoundIndexPathOnLevelOneIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(CompoundIndexOnLevelOneWithEmptyIndexDefinition.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + CompoundIndexOnLevelOneWithEmptyIndexDefinition.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection(new String[] { "zero" }, "CompoundIndexOnLevelZeroWithEmptyIndexDef", @@ -560,24 +574,26 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void singleCompoundIndexPathOnLevelZeroIsResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(SingleCompoundIndex.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + SingleCompoundIndex.class); assertThat(indexDefinitions, hasSize(1)); assertIndexPathAndCollection(new String[] { "foo", "bar" }, "CompoundIndexOnLevelZero", indexDefinitions.get(0)); } - + /** * @see DATAMONGO-1373 */ @Test public void singleCompoundIndexUsingComposedAnnotationsOnTypeResolvedCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(CompoundIndexDocumentWithComposedAnnotation.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + CompoundIndexDocumentWithComposedAnnotation.class); assertThat(indexDefinitions, hasSize(1)); assertThat(indexDefinitions.get(0).getIndexKeys(), isBsonObject().containing("foo", 1).containing("bar", -1)); - assertThat(indexDefinitions.get(0).getIndexOptions(), isBsonObject().containing("name", "my_compound_index_name"). - containing("unique", true).containing("background", true)); + assertThat(indexDefinitions.get(0).getIndexOptions(), isBsonObject().containing("name", "my_compound_index_name") + .containing("unique", true).containing("background", true)); } @Document(collection = "CompoundIndexOnLevelOne") @@ -597,8 +613,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { dropDups = true, sparse = true, unique = true) }) static class CompoundIndexOnLevelZero {} - @CompoundIndexes({ @CompoundIndex(name = "compound_index", background = true, dropDups = true, sparse = true, - unique = true) }) + @CompoundIndexes({ + @CompoundIndex(name = "compound_index", background = true, dropDups = true, sparse = true, unique = true) }) static class CompoundIndexOnLevelZeroWithEmptyIndexDef {} @Document(collection = "CompoundIndexOnLevelZero") @@ -616,33 +632,33 @@ public class MongoPersistentEntityIndexResolverUnitTests { static class ComountIndexWithAutogeneratedName { } - + @Document(collection = "WithComposedAnnotation") @ComposedCompoundIndex static class CompoundIndexDocumentWithComposedAnnotation { } - + @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) @CompoundIndex @interface ComposedCompoundIndex { - + @AliasFor(annotation = CompoundIndex.class, attribute = "def") String fields() default "{'foo': 1, 'bar': -1}"; - + @AliasFor(annotation = CompoundIndex.class, attribute = "background") boolean inBackground() default true; - + @AliasFor(annotation = CompoundIndex.class, attribute = "name") String indexName() default "my_compound_index_name"; - + @AliasFor(annotation = CompoundIndex.class, attribute = "useGeneratedName") boolean useGeneratedName() default false; - + @AliasFor(annotation = CompoundIndex.class, attribute = "unique") boolean isUnique() default true; - + } } @@ -655,7 +671,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldResolveSingleFieldTextIndexCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(TextIndexOnSinglePropertyInRoot.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + TextIndexOnSinglePropertyInRoot.class); assertThat(indexDefinitions.size(), equalTo(1)); assertIndexPathAndCollection("bar", "textIndexOnSinglePropertyInRoot", indexDefinitions.get(0)); } @@ -666,7 +683,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldResolveMultiFieldTextIndexCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(TextIndexOnMutiplePropertiesInRoot.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + TextIndexOnMutiplePropertiesInRoot.class); assertThat(indexDefinitions.size(), equalTo(1)); assertIndexPathAndCollection(new String[] { "foo", "bar" }, "textIndexOnMutiplePropertiesInRoot", indexDefinitions.get(0)); @@ -678,7 +696,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldResolveTextIndexOnElementCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(TextIndexOnNestedRoot.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + TextIndexOnNestedRoot.class); assertThat(indexDefinitions.size(), equalTo(1)); assertIndexPathAndCollection(new String[] { "nested.foo" }, "textIndexOnNestedRoot", indexDefinitions.get(0)); } @@ -689,7 +708,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldResolveTextIndexOnElementWithWeightCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(TextIndexOnNestedWithWeightRoot.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + TextIndexOnNestedWithWeightRoot.class); assertThat(indexDefinitions.size(), equalTo(1)); assertIndexPathAndCollection(new String[] { "nested.foo" }, "textIndexOnNestedWithWeightRoot", indexDefinitions.get(0)); @@ -704,7 +724,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldResolveTextIndexOnElementWithMostSpecificWeightCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(TextIndexOnNestedWithMostSpecificValueRoot.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + TextIndexOnNestedWithMostSpecificValueRoot.class); assertThat(indexDefinitions.size(), equalTo(1)); assertIndexPathAndCollection(new String[] { "nested.foo", "nested.bar" }, "textIndexOnNestedWithMostSpecificValueRoot", indexDefinitions.get(0)); @@ -720,7 +741,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldSetDefaultLanguageCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithDefaultLanguage.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithDefaultLanguage.class); assertThat(indexDefinitions.get(0).getIndexOptions().get("default_language"), is((Object) "spanish")); } @@ -730,7 +752,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldResolveTextIndexLanguageOverrideCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithLanguageOverride.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithLanguageOverride.class); assertThat(indexDefinitions.get(0).getIndexOptions().get("language_override"), is((Object) "lang")); } @@ -740,7 +763,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldIgnoreTextIndexLanguageOverrideOnNestedElements() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithLanguageOverrideOnNestedElement.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithLanguageOverrideOnNestedElement.class); assertThat(indexDefinitions.get(0).getIndexOptions().get("language_override"), is(nullValue())); } @@ -750,7 +774,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldNotCreateIndexDefinitionWhenOnlyLanguageButNoTextIndexPresent() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithNoTextIndexPropertyButReservedFieldLanguage.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithNoTextIndexPropertyButReservedFieldLanguage.class); assertThat(indexDefinitions, is(empty())); } @@ -760,7 +785,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldNotCreateIndexDefinitionWhenOnlyAnnotatedLanguageButNoTextIndexPresent() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithNoTextIndexPropertyButReservedFieldLanguageAnnotated.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithNoTextIndexPropertyButReservedFieldLanguageAnnotated.class); assertThat(indexDefinitions, is(empty())); } @@ -770,18 +796,20 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldPreferExplicitlyAnnotatedLanguageProperty() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithOverlappingLanguageProps.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithOverlappingLanguageProps.class); assertThat(indexDefinitions.get(0).getIndexOptions().get("language_override"), is((Object) "lang")); } - + /** * @see DATAMONGO-1373 */ @Test public void shouldResolveComposedAnnotationCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(TextIndexedDocumentWithComposedAnnotation.class); - + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + TextIndexedDocumentWithComposedAnnotation.class); + DBObject weights = DBObjectTestUtils.getAsDBObject(indexDefinitions.get(0).getIndexOptions(), "weights"); assertThat(weights, isBsonObject().containing("foo", 99f)); } @@ -870,23 +898,22 @@ public class MongoPersistentEntityIndexResolverUnitTests { String language; @Language String lang; } - + @Document static class TextIndexedDocumentWithComposedAnnotation { @ComposedTextIndexedAnnotation String foo; String lang; } - + @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE }) @TextIndexed static @interface ComposedTextIndexedAnnotation { - + @AliasFor(annotation = TextIndexed.class, attribute = "weight") float heavyweight() default 99f; } - } public static class MixedIndexResolutionTests { @@ -932,7 +959,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldNotRunIntoStackOverflow() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(CycleStartingInBetween.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + CycleStartingInBetween.class); assertThat(indexDefinitions, hasSize(1)); } @@ -965,7 +993,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void indexBeResolvedCorrectlyWhenPropertiesOfDifferentTypesAreNamedEqually() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(NoCycleButIdenticallyNamedProperties.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + NoCycleButIdenticallyNamedProperties.class); assertIndexPathAndCollection("foo", "noCycleButIdenticallyNamedProperties", indexDefinitions.get(0)); assertIndexPathAndCollection("reference.foo", "noCycleButIdenticallyNamedProperties", indexDefinitions.get(1)); assertIndexPathAndCollection("reference.deep.foo", "noCycleButIdenticallyNamedProperties", @@ -979,7 +1008,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldNotDetectCycleInSimilarlyNamedProperties() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(SimilarityHolingBean.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + SimilarityHolingBean.class); assertIndexPathAndCollection("norm", "similarityHolingBean", indexDefinitions.get(0)); assertThat(indexDefinitions, hasSize(1)); } @@ -990,7 +1020,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldDetectSelfCycleViaCollectionTypeCorrectly() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(SelfCyclingViaCollectionType.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + SelfCyclingViaCollectionType.class); assertThat(indexDefinitions, empty()); } @@ -1000,7 +1031,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldNotDetectCycleWhenTypeIsUsedMoreThanOnce() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(MultipleObjectsOfSameType.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + MultipleObjectsOfSameType.class); assertThat(indexDefinitions, empty()); } @@ -1032,7 +1064,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldUsePathIndexAsIndexNameForDocumentsHavingNamedNestedCompoundIndexFixedOnCollection() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithNestedDocumentHavingNamedCompoundIndex.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithNestedDocumentHavingNamedCompoundIndex.class); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("propertyOfTypeHavingNamedCompoundIndex.c_index")); } @@ -1043,7 +1076,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldUseIndexNameForNestedTypesWithNamedCompoundIndexDefinition() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithNestedTypeHavingNamedCompoundIndex.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithNestedTypeHavingNamedCompoundIndex.class); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("propertyOfTypeHavingNamedCompoundIndex.c_index")); } @@ -1054,7 +1088,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldUsePathIndexAsIndexNameForDocumentsHavingNamedNestedIndexFixedOnCollection() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithNestedDocumentHavingNamedIndex.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithNestedDocumentHavingNamedIndex.class); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("propertyOfTypeHavingNamedIndex.property_index")); } @@ -1065,7 +1100,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldUseIndexNameForNestedTypesWithNamedIndexDefinition() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithNestedTypeHavingNamedIndex.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithNestedTypeHavingNamedIndex.class); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("propertyOfTypeHavingNamedIndex.property_index")); } @@ -1076,7 +1112,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldUseIndexNameOnRootLevel() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(DocumentWithNamedIndex.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + DocumentWithNamedIndex.class); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("property_index")); } @@ -1086,7 +1123,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnRoot() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLetters.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + MultiplePropertiesOfSameTypeWithMatchingStartLetters.class); assertThat(indexDefinitions, hasSize(2)); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("name.component")); @@ -1099,7 +1137,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty.class); assertThat(indexDefinitions, hasSize(2)); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("component.nameLast")); @@ -1112,7 +1151,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldOnlyConsiderEntitiesAsPotentialCycleCandidates() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths.class); assertThat(indexDefinitions, hasSize(2)); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("path1.foo")); @@ -1127,7 +1167,8 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Test public void shouldConsiderGenericTypeArgumentsOfCollectionElements() { - List indexDefinitions = prepareMappingContextAndResolveIndexForType(EntityWithGenericTypeWrapperAsElement.class); + List indexDefinitions = prepareMappingContextAndResolveIndexForType( + EntityWithGenericTypeWrapperAsElement.class); assertThat(indexDefinitions, hasSize(1)); assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), @@ -1338,7 +1379,6 @@ public class MongoPersistentEntityIndexResolverUnitTests { private static void assertIndexPathAndCollection(String expectedPath, String expectedCollection, IndexDefinitionHolder holder) { - assertIndexPathAndCollection(new String[] { expectedPath }, expectedCollection, holder); } @@ -1348,7 +1388,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { for (String expectedPath : expectedPaths) { assertThat(holder.getIndexDefinition().getIndexKeys().containsField(expectedPath), equalTo(true)); } + assertThat(holder.getCollection(), equalTo(expectedCollection)); } - } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java index 458b0fd4b..e5f8c8725 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java @@ -257,23 +257,15 @@ public class BasicMongoPersistentEntityUnitTests { } @Document(collection = "contacts") - class Contact { + class Contact {} - } - - class Person extends Contact { - - } + class Person extends Contact {} @Document(collection = "#{35}") - class Company { - - } + class Company {} @Document(collection = "#{myBean.collectionName}") - class DynamicallyMapped { - - } + class DynamicallyMapped {} class CollectionProvider { String collectionName; @@ -284,23 +276,15 @@ public class BasicMongoPersistentEntityUnitTests { } @Document(language = "spanish") - static class DocumentWithLanguage { + static class DocumentWithLanguage {} - } - - static class AnyDocument { - - } + static class AnyDocument {} @CustomDocumentAnnotation - static class DocumentWithCustomAnnotation { - - } + static class DocumentWithCustomAnnotation {} @ComposedDocumentAnnotation - static class DocumentWithComposedAnnotation { - - } + static class DocumentWithComposedAnnotation {} @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java index e2c64260c..5f325e7aa 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java @@ -190,27 +190,25 @@ public class BasicMongoPersistentPropertyUnitTests { "id"); assertThat(property.isIdProperty(), is(true)); } - + /** * @see DATAMONGO-1373 */ @Test public void shouldConsiderComposedAnnotationsForIdField() { - MongoPersistentProperty property = getPropertyFor(DocumentWithComposedAnnotations.class, - "myId"); + MongoPersistentProperty property = getPropertyFor(DocumentWithComposedAnnotations.class, "myId"); assertThat(property.isIdProperty(), is(true)); assertThat(property.getFieldName(), is("_id")); } - + /** * @see DATAMONGO-1373 */ @Test public void shouldConsiderComposedAnnotationsForFields() { - MongoPersistentProperty property = getPropertyFor(DocumentWithComposedAnnotations.class, - "myField"); + MongoPersistentProperty property = getPropertyFor(DocumentWithComposedAnnotations.class, "myField"); assertThat(property.getFieldName(), is("myField")); } @@ -282,13 +280,13 @@ public class BasicMongoPersistentPropertyUnitTests { @Id @org.springframework.data.mongodb.core.mapping.Field("id") String id; } - + static class DocumentWithComposedAnnotations { @ComposedIdAnnotation @ComposedFieldAnnotation String myId; @ComposedFieldAnnotation(name = "myField") String myField; } - + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @org.springframework.data.mongodb.core.mapping.Field @@ -297,10 +295,10 @@ public class BasicMongoPersistentPropertyUnitTests { @AliasFor(annotation = org.springframework.data.mongodb.core.mapping.Field.class, attribute = "value") String name() default "_id"; } - + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Id - static @interface ComposedIdAnnotation { } - + static @interface ComposedIdAnnotation { + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java index 011333c24..c788e4524 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-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. @@ -40,6 +40,7 @@ import com.mongodb.MongoClient; /** * @author Christoph Strobl * @author Oliver Gierke + * @author Mark Paluch */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @@ -130,7 +131,7 @@ public class ComplexIdRepositoryIntegrationTests { assertThat(loaded, is(Matchers. iterableWithSize(1))); assertThat(loaded, contains(userWithId)); } - + /** * @see DATAMONGO-1373 */ @@ -141,7 +142,7 @@ public class ComplexIdRepositoryIntegrationTests { assertThat(repo.getUserUsingComposedAnnotationByComplexId(id), is(userWithId)); } - + /** * @see DATAMONGO-1373 */ @@ -152,5 +153,5 @@ public class ComplexIdRepositoryIntegrationTests { assertThat(repo.findUsersUsingComposedMetaAnnotationByUserIds(Arrays.asList(id)), hasSize(0)); } - + } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexIdRepository.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexIdRepository.java index dca1abab1..33aec8198 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexIdRepository.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexIdRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-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. @@ -28,6 +28,7 @@ import org.springframework.data.repository.CrudRepository; /** * @author Christoph Strobl + * @author Mark Paluch */ public interface UserWithComplexIdRepository extends CrudRepository { @@ -36,31 +37,30 @@ public interface UserWithComplexIdRepository extends CrudRepository findUsersUsingComposedMetaAnnotationByUserIds(Collection ids); - - + @Retention(RetentionPolicy.RUNTIME) - @Target({ ElementType.METHOD }) - @Document + @Target({ ElementType.METHOD }) + @Document @Query - @interface ComposedQueryAnnotation { - - @AliasFor(annotation = Query.class, attribute = "value") - String myQuery() default "{'_id': ?0}"; - } - + @interface ComposedQueryAnnotation { + + @AliasFor(annotation = Query.class, attribute = "value") + String myQuery() default "{'_id': ?0}"; + } + @Retention(RetentionPolicy.RUNTIME) - @Target({ ElementType.METHOD }) - @Meta - @interface ComposedMetaAnnotation { - - @AliasFor(annotation = Meta.class, attribute = "maxScanDocuments") - long scanDocuments() default 1; - } + @Target({ ElementType.METHOD }) + @Meta + @interface ComposedMetaAnnotation { + + @AliasFor(annotation = Meta.class, attribute = "maxScanDocuments") + long scanDocuments() default 1; + } }