From 9d0c8ecdc3acb835f1e911cefae63bc42f4ded3f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sun, 13 Mar 2016 16:32:17 +0000 Subject: [PATCH] DATAMONGO-1245 - Polishing. Adapt to API changes in Spring Data Commons. Related tickets: DATACMNS-810. Original pull request: #341. --- .../data/mongodb/core/MongoTemplate.java | 19 --- .../core/convert/MongoExampleMapper.java | 61 ++++--- .../support/SimpleMongoRepository.java | 26 +-- .../convert/MongoExampleMapperUnitTests.java | 161 ++++++++++-------- .../core/convert/QueryMapperUnitTests.java | 34 ++-- .../ContactRepositoryIntegrationTests.java | 39 +---- .../support/SimpleMongoRepositoryTests.java | 29 ++-- .../data/mongodb/test/util/IsBsonObject.java | 7 +- 8 files changed, 174 insertions(+), 202 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index ec56a16f0..e7419a06f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -636,25 +636,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return doFindOne(collectionName, new BasicDBObject(idKey, id), null, entityClass); } - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.core.MongoOperations#findByExample(java.lang.Object) - */ - public List findByExample(S sample) { - return findByExample(Example.of(sample)); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.core.MongoOperations#findByExample(org.springframework.data.domain.Example) - */ - @SuppressWarnings("unchecked") - public List findByExample(Example sample) { - - Assert.notNull(sample, "Sample object must not be null!"); - return (List) find(new Query(new Criteria().alike(sample)), sample.getResultType()); - } - public GeoResults geoNear(NearQuery near, Class entityClass) { return geoNear(near, entityClass, determineCollectionName(entityClass)); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java index 7463921fe..184439071 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java @@ -17,8 +17,8 @@ package org.springframework.data.mongodb.core.convert; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -27,20 +27,20 @@ import java.util.Set; import java.util.Stack; import java.util.regex.Pattern; -import org.bson.BasicBSONObject; import org.springframework.data.domain.Example; -import org.springframework.data.domain.ExampleSpec; -import org.springframework.data.domain.ExampleSpec.NullHandler; -import org.springframework.data.domain.ExampleSpec.PropertyValueTransformer; -import org.springframework.data.domain.ExampleSpec.StringMatcher; +import org.springframework.data.domain.ExampleMatcher.NullHandler; +import org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer; +import org.springframework.data.domain.ExampleMatcher.StringMatcher; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.query.MongoRegexCreator; import org.springframework.data.mongodb.core.query.SerializationUtils; -import org.springframework.data.repository.core.support.ExampleSpecAccessor; +import org.springframework.data.repository.core.support.ExampleMatcherAccessor; import org.springframework.data.repository.query.parser.Part.Type; +import org.springframework.data.util.TypeInformation; +import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -74,11 +74,13 @@ public class MongoExampleMapper { * Returns the given {@link Example} as {@link DBObject} holding matching values extracted from * {@link Example#getProbe()}. * - * @param example + * @param example must not be {@literal null}. * @return - * @since 1.8 */ public DBObject getMappedExample(Example example) { + + Assert.notNull(example, "Example must not be null!"); + return getMappedExample(example, mappingContext.getPersistentEntity(example.getProbeType())); } @@ -86,31 +88,45 @@ public class MongoExampleMapper { * Returns the given {@link Example} as {@link DBObject} holding matching values extracted from * {@link Example#getProbe()}. * - * @param example - * @param entity + * @param example must not be {@literal null}. + * @param entity must not be {@literal null}. * @return - * @since 1.8 */ + @SuppressWarnings({ "unchecked", "rawtypes" }) public DBObject getMappedExample(Example example, MongoPersistentEntity entity) { + Assert.notNull(example, "Example must not be null!"); + Assert.notNull(entity, "MongoPersistentEntity must not be null!"); + DBObject reference = (DBObject) converter.convertToMongoType(example.getProbe()); if (entity.hasIdProperty() && entity.getIdentifierAccessor(example.getProbe()).getIdentifier() == null) { reference.removeField(entity.getIdProperty().getFieldName()); } - ExampleSpecAccessor exampleSpecAccessor = new ExampleSpecAccessor(example.getExampleSpec()); + ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); - applyPropertySpecs("", reference, example.getProbeType(), exampleSpecAccessor); + applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); - if (exampleSpecAccessor.isTyped()) { - this.converter.getTypeMapper().writeTypeRestrictions(reference, (Set) Collections.singleton(example.getResultType())); - } + this.converter.getTypeMapper().writeTypeRestrictions(reference, getTypesToMatch(example)); - return ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, exampleSpecAccessor.getNullHandler()) ? reference + return ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new BasicDBObject(SerializationUtils.flattenMap(reference)); } + private Set> getTypesToMatch(Example example) { + + Set> types = new HashSet>(); + + for (TypeInformation reference : mappingContext.getManagedTypes()) { + if (example.getProbeType().isAssignableFrom(reference.getType())) { + types.add(reference.getType()); + } + } + + return types; + } + private String getMappedPropertyPath(String path, Class probeType) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(probeType); @@ -159,7 +175,7 @@ public class MongoExampleMapper { } private void applyPropertySpecs(String path, DBObject source, Class probeType, - ExampleSpecAccessor exampleSpecAccessor) { + ExampleMatcherAccessor exampleSpecAccessor) { if (!(source instanceof BasicDBObject)) { return; @@ -172,12 +188,12 @@ public class MongoExampleMapper { Map.Entry entry = iter.next(); String propertyPath = StringUtils.hasText(path) ? path + "." + entry.getKey() : entry.getKey(); String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType); - - if(isEmptyIdProperty(entry)) { + + if (isEmptyIdProperty(entry)) { iter.remove(); continue; } - + if (exampleSpecAccessor.isIgnoredPath(propertyPath) || exampleSpecAccessor.isIgnoredPath(mappedPropertyPath)) { iter.remove(); continue; @@ -243,5 +259,4 @@ public class MongoExampleMapper { dbo.put("$options", "i"); } } - } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java index fcca93e66..21af693ba 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java @@ -30,7 +30,6 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -import org.springframework.data.domain.TypedExampleSpec; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; @@ -273,12 +272,14 @@ public class SimpleMongoRepository implements MongoR Query q = new Query(new Criteria().alike(example)).with(pageable); - long count = mongoOperations.count(q, getResultType(example), entityInformation.getCollectionName()); + long count = mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()); + if (count == 0) { return new PageImpl(Collections. emptyList()); } - return new PageImpl(mongoOperations.find(q, getResultType(example), entityInformation.getCollectionName()), pageable, - count); + + return new PageImpl(mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()), + pageable, count); } /* @@ -296,7 +297,7 @@ public class SimpleMongoRepository implements MongoR q.with(sort); } - return mongoOperations.find(q, getResultType(example), entityInformation.getCollectionName()); + return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()); } /* @@ -318,7 +319,7 @@ public class SimpleMongoRepository implements MongoR Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); - return mongoOperations.findOne(q, getResultType(example), entityInformation.getCollectionName()); + return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName()); } /* @@ -331,7 +332,7 @@ public class SimpleMongoRepository implements MongoR Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); - return mongoOperations.count(q, getResultType(example), entityInformation.getCollectionName()); + return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()); } /* @@ -344,16 +345,7 @@ public class SimpleMongoRepository implements MongoR Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); - return mongoOperations.exists(q, getResultType(example), entityInformation.getCollectionName()); - } - - private Class getResultType(Example example) { - - if (example.getExampleSpec() instanceof TypedExampleSpec) { - return example.getResultType(); - } - - return (Class) entityInformation.getJavaType(); + return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName()); } private List findAll(Query query) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java index ba01bacaa..7f09c500e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java @@ -18,6 +18,7 @@ package org.springframework.data.mongodb.core.convert; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.springframework.data.domain.Example.*; +import static org.springframework.data.domain.ExampleMatcher.*; import static org.springframework.data.mongodb.core.DBObjectTestUtils.*; import static org.springframework.data.mongodb.test.util.IsBsonObject.*; @@ -25,6 +26,7 @@ import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; +import org.bson.BSONObject; import org.hamcrest.core.Is; import org.junit.Before; import org.junit.Test; @@ -33,9 +35,8 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.annotation.Id; import org.springframework.data.domain.Example; -import org.springframework.data.domain.ExampleSpec; -import org.springframework.data.domain.ExampleSpec.GenericPropertyMatcher; -import org.springframework.data.domain.ExampleSpec.StringMatcher; +import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers; +import org.springframework.data.domain.ExampleMatcher.StringMatcher; import org.springframework.data.geo.Point; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.convert.QueryMapperUnitTests.ClassWithGeoTypes; @@ -44,9 +45,10 @@ import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; +import org.springframework.data.mongodb.test.util.IsBsonObject; +import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; -import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DBObject; /** @@ -82,9 +84,9 @@ public class MongoExampleMapperUnitTests { FlatDocument probe = new FlatDocument(); probe.id = "steelheart"; - DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().containing("_id", "steelheart"); - assertThat(dbo, is(new BasicDBObjectBuilder().add("_id", "steelheart").get())); + assertThat(mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -98,10 +100,12 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.intValue = 100; - DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("_id", "steelheart").// + containing("stringValue", "firefight").// + containing("intValue", 100); - assertThat(dbo, is(new BasicDBObjectBuilder().add("_id", "steelheart").add("stringValue", "firefight") - .add("intValue", 100).get())); + assertThat(mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -114,9 +118,11 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.intValue = 100; - DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue", "firefight").// + containing("intValue", 100); - assertThat(dbo, is(new BasicDBObjectBuilder().add("stringValue", "firefight").add("intValue", 100).get())); + assertThat(mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -128,9 +134,13 @@ public class MongoExampleMapperUnitTests { FlatDocument probe = new FlatDocument(); probe.listOfString = Arrays.asList("Prof", "Tia", "David"); - DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)); + BasicDBList list = new BasicDBList(); + list.addAll(Arrays.asList("Prof", "Tia", "David")); + + IsBsonObject expected = isBsonObject().// + containing("listOfString", list); - assertThat(dbo, is(new BasicDBObjectBuilder().add("listOfString", Arrays.asList("Prof", "Tia", "David")).get())); + assertThat(mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -142,9 +152,9 @@ public class MongoExampleMapperUnitTests { FlatDocument probe = new FlatDocument(); probe.customNamedField = "Mitosis"; - DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().containing("custom_field_name", "Mitosis"); - assertThat(dbo, is(new BasicDBObjectBuilder().add("custom_field_name", "Mitosis").get())); + assertThat(mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -157,8 +167,7 @@ public class MongoExampleMapperUnitTests { probe.flatDoc = new FlatDocument(); probe.flatDoc.stringValue = "conflux"; - DBObject dbo = mapper.getMappedExample(of(probe, ExampleSpec.typed(WrapperDocument.class)), - context.getPersistentEntity(WrapperDocument.class)); + DBObject dbo = mapper.getMappedExample(Example.of(probe), context.getPersistentEntity(WrapperDocument.class)); assertThat(dbo, isBsonObject().containing("_class", new BasicDBObject("$in", new String[] { probe.getClass().getName() }))); @@ -174,9 +183,9 @@ public class MongoExampleMapperUnitTests { probe.flatDoc = new FlatDocument(); probe.flatDoc.stringValue = "conflux"; - DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(WrapperDocument.class)); + IsBsonObject expected = isBsonObject().containing("flatDoc\\.stringValue", "conflux"); - assertThat(dbo, is(new BasicDBObjectBuilder().add("flatDoc.stringValue", "conflux").get())); + assertThat(mapper.getMappedExample(of(probe), context.getPersistentEntity(WrapperDocument.class)), is(expected)); } /** @@ -189,11 +198,10 @@ public class MongoExampleMapperUnitTests { probe.flatDoc = new FlatDocument(); probe.flatDoc.stringValue = "conflux"; - Example example = Example.of(probe, ExampleSpec.untyped().withIncludeNullValues()); + Example example = Example.of(probe, matching().withIncludeNullValues()); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(WrapperDocument.class)); - - assertThat(dbo, isBsonObject().containing("flatDoc.stringValue", "conflux")); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(WrapperDocument.class)), // + isBsonObject().containing("flatDoc.stringValue", "conflux")); } /** @@ -206,12 +214,13 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.intValue = 100; - Example example = Example.of(probe, ExampleSpec.untyped().withStringMatcher(StringMatcher.STARTING)); + Example example = Example.of(probe, matching().withStringMatcher(StringMatcher.STARTING)); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue.$regex", "^firefight").// + containing("intValue", 100); - assertThat(dbo, is(new BasicDBObjectBuilder().add("stringValue", new BasicDBObject("$regex", "^firefight")) - .add("intValue", 100).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -224,12 +233,13 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "fire.ight"; probe.intValue = 100; - Example example = Example.of(probe, ExampleSpec.untyped().withStringMatcherStarting()); + Example example = Example.of(probe, matching().withStringMatcher(StringMatcher.STARTING)); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue.$regex", "^" + Pattern.quote("fire.ight")).// + containing("intValue", 100); - assertThat(dbo, is(new BasicDBObjectBuilder() - .add("stringValue", new BasicDBObject("$regex", "^" + Pattern.quote("fire.ight"))).add("intValue", 100).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -242,12 +252,13 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.intValue = 100; - Example example = Example.of(probe, ExampleSpec.untyped().withStringMatcher(StringMatcher.ENDING)); + Example example = Example.of(probe, matching().withStringMatcher(StringMatcher.ENDING)); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue.$regex", "firefight$").// + containing("intValue", 100); - assertThat(dbo, is(new BasicDBObjectBuilder().add("stringValue", new BasicDBObject("$regex", "firefight$")) - .add("intValue", 100).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -260,12 +271,13 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.customNamedField = "^(cat|dog).*shelter\\d?"; - Example example = Example.of(probe, ExampleSpec.untyped().withStringMatcher(StringMatcher.REGEX)); + Example example = Example.of(probe, matching().withStringMatcher(StringMatcher.REGEX)); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue.$regex", "firefight").// + containing("custom_field_name.$regex", "^(cat|dog).*shelter\\d?"); - assertThat(dbo, is(new BasicDBObjectBuilder().add("stringValue", new BasicDBObject("$regex", "firefight")) - .add("custom_field_name", new BasicDBObject("$regex", "^(cat|dog).*shelter\\d?")).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -278,15 +290,13 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.intValue = 100; - Example example = Example.of(probe, - ExampleSpec.untyped().withStringMatcher(StringMatcher.ENDING).withIgnoreCase()); + Example example = Example.of(probe, matching().withStringMatcher(StringMatcher.ENDING).withIgnoreCase()); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue", new BasicDBObject("$regex", "firefight$").append("$options", "i")).// + containing("intValue", 100); - assertThat(dbo, - is(new BasicDBObjectBuilder() - .add("stringValue", new BasicDBObjectBuilder().add("$regex", "firefight$").add("$options", "i").get()) - .add("intValue", 100).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -299,15 +309,13 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.intValue = 100; - Example example = Example.of(probe, ExampleSpec.untyped().withIgnoreCase()); + Example example = Example.of(probe, matching().withIgnoreCase()); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue", new BasicDBObject("$regex", Pattern.quote("firefight")).append("$options", "i")).// + containing("intValue", 100); - assertThat(dbo, - is(new BasicDBObjectBuilder() - .add("stringValue", - new BasicDBObjectBuilder().add("$regex", Pattern.quote("firefight")).add("$options", "i").get()) - .add("intValue", 100).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -339,7 +347,7 @@ public class MongoExampleMapperUnitTests { DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(FlatDocument.class)); - assertThat(dbo, is(new BasicDBObjectBuilder().add("stringValue", "steelheart").get())); + assertThat(dbo, isBsonObject().containing("stringValue", "steelheart")); } /** @@ -368,11 +376,13 @@ public class MongoExampleMapperUnitTests { probe.intValue = 10; probe.stringValue = "string"; - Example example = Example.of(probe, ExampleSpec.untyped().withIgnorePaths("customNamedField")); + Example example = Example.of(probe, matching().withIgnorePaths("customNamedField")); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue", "string").// + containing("intValue", 10); - assertThat(dbo, is(new BasicDBObjectBuilder().add("stringValue", "string").add("intValue", 10).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -386,11 +396,13 @@ public class MongoExampleMapperUnitTests { probe.intValue = 10; probe.stringValue = "string"; - Example example = Example.of(probe, ExampleSpec.untyped().withIgnorePaths("stringValue")); + Example example = Example.of(probe, matching().withIgnorePaths("stringValue")); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("custom_field_name", "foo").// + containing("intValue", 10); - assertThat(dbo, is(new BasicDBObjectBuilder().add("custom_field_name", "foo").add("intValue", 10).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -405,12 +417,13 @@ public class MongoExampleMapperUnitTests { probe.flatDoc.intValue = 10; probe.flatDoc.stringValue = "string"; - Example example = Example.of(probe, ExampleSpec.untyped().withIgnorePaths("flatDoc.stringValue")); + Example example = Example.of(probe, matching().withIgnorePaths("flatDoc.stringValue")); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(WrapperDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("flatDoc\\.custom_field_name", "foo").// + containing("flatDoc\\.intValue", 10); - assertThat(dbo, - is(new BasicDBObjectBuilder().add("flatDoc.custom_field_name", "foo").add("flatDoc.intValue", 10).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(WrapperDocument.class)), is(expected)); } /** @@ -425,12 +438,13 @@ public class MongoExampleMapperUnitTests { probe.flatDoc.intValue = 10; probe.flatDoc.stringValue = "string"; - Example example = Example.of(probe, ExampleSpec.untyped().withIgnorePaths("flatDoc.customNamedField")); + Example example = Example.of(probe, matching().withIgnorePaths("flatDoc.customNamedField")); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(WrapperDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("flatDoc\\.stringValue", "string").// + containing("flatDoc\\.intValue", 10); - assertThat(dbo, - is(new BasicDBObjectBuilder().add("flatDoc.stringValue", "string").add("flatDoc.intValue", 10).get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(WrapperDocument.class)), is(expected)); } /** @@ -443,13 +457,13 @@ public class MongoExampleMapperUnitTests { probe.stringValue = "firefight"; probe.customNamedField = "steelheart"; - Example example = Example.of(probe, - ExampleSpec.untyped().withMatcher("stringValue", new GenericPropertyMatcher().contains())); + Example example = Example.of(probe, matching().withMatcher("stringValue", GenericPropertyMatchers.contains())); - DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)); + IsBsonObject expected = isBsonObject().// + containing("stringValue.$regex", ".*firefight.*").// + containing("custom_field_name", "steelheart"); - assertThat(dbo, is(new BasicDBObjectBuilder().add("stringValue", new BasicDBObject("$regex", ".*firefight.*")) - .add("custom_field_name", "steelheart").get())); + assertThat(mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class)), is(expected)); } /** @@ -495,5 +509,4 @@ public class MongoExampleMapperUnitTests { @Id String id; String value; } - } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java index 80d9b98b1..bc6b8272a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java @@ -240,8 +240,8 @@ public class QueryMapperUnitTests { ObjectId accidentallyAnObjectId = new ObjectId(); - Query query = Query.query(Criteria.where("id").is("id_value").and("publishers") - .ne(accidentallyAnObjectId.toString())); + Query query = Query + .query(Criteria.where("id").is("id_value").and("publishers").ne(accidentallyAnObjectId.toString())); DBObject dbObject = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(UserEntity.class)); assertThat(dbObject.get("publishers"), is(instanceOf(DBObject.class))); @@ -275,8 +275,8 @@ public class QueryMapperUnitTests { public void translatesPropertyReferenceCorrectly() { Query query = query(where("field").is(new CustomizedField())); - DBObject result = mapper - .getMappedObject(query.getQueryObject(), context.getPersistentEntity(CustomizedField.class)); + DBObject result = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(CustomizedField.class)); assertThat(result.containsField("foo"), is(true)); assertThat(result.keySet().size(), is(1)); @@ -286,8 +286,8 @@ public class QueryMapperUnitTests { public void translatesNestedPropertyReferenceCorrectly() { Query query = query(where("field.field").is(new CustomizedField())); - DBObject result = mapper - .getMappedObject(query.getQueryObject(), context.getPersistentEntity(CustomizedField.class)); + DBObject result = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(CustomizedField.class)); assertThat(result.containsField("foo.foo"), is(true)); assertThat(result.keySet().size(), is(1)); @@ -297,8 +297,8 @@ public class QueryMapperUnitTests { public void returnsOriginalKeyIfNoPropertyReference() { Query query = query(where("bar").is(new CustomizedField())); - DBObject result = mapper - .getMappedObject(query.getQueryObject(), context.getPersistentEntity(CustomizedField.class)); + DBObject result = mapper.getMappedObject(query.getQueryObject(), + context.getPersistentEntity(CustomizedField.class)); assertThat(result.containsField("bar"), is(true)); assertThat(result.keySet().size(), is(1)); @@ -643,8 +643,8 @@ public class QueryMapperUnitTests { Query query = new Query().with(new Sort("textScore")); - DBObject dbo = mapper - .getMappedSort(query.getSortObject(), context.getPersistentEntity(WithTextScoreProperty.class)); + DBObject dbo = mapper.getMappedSort(query.getSortObject(), + context.getPersistentEntity(WithTextScoreProperty.class)); assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("score", new BasicDBObject("$meta", "textScore")).get())); } @@ -657,8 +657,8 @@ public class QueryMapperUnitTests { Query query = new Query().with(new Sort("id")); - DBObject dbo = mapper - .getMappedSort(query.getSortObject(), context.getPersistentEntity(WithTextScoreProperty.class)); + DBObject dbo = mapper.getMappedSort(query.getSortObject(), + context.getPersistentEntity(WithTextScoreProperty.class)); assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("_id", 1).get())); } @@ -768,8 +768,8 @@ public class QueryMapperUnitTests { @Test public void withinShouldUseGeoJsonPolygonWhenMappingPolygonOn2DSphereIndex() { - Query query = query(where("geoJsonPoint").within( - new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0)))); + Query query = query(where("geoJsonPoint") + .within(new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0)))); DBObject dbo = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(ClassWithGeoTypes.class)); @@ -782,8 +782,8 @@ public class QueryMapperUnitTests { @Test public void intersectsShouldUseGeoJsonRepresentationCorrectly() { - Query query = query(where("geoJsonPoint").intersects( - new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0)))); + Query query = query(where("geoJsonPoint") + .intersects(new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0)))); DBObject dbo = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(ClassWithGeoTypes.class)); @@ -835,7 +835,7 @@ public class QueryMapperUnitTests { DBObject dbo = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class)); - assertThat(dbo, is(new BasicDBObjectBuilder().add("embedded._id", "conflux").get())); + assertThat(dbo, isBsonObject().containing("embedded\\._id", "conflux")); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java index 224b85a53..34722d709 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java @@ -23,7 +23,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; -import org.springframework.data.domain.ExampleSpec; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -37,8 +36,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration("config/MongoNamespaceIntegrationTests-context.xml") public class ContactRepositoryIntegrationTests { - @Autowired - ContactRepository repository; + @Autowired ContactRepository repository; @Before public void setUp() throws Exception { @@ -54,45 +52,14 @@ public class ContactRepositoryIntegrationTests { assertTrue(repository.findOne(result.getId().toString()) instanceof Person); } - /** - * @see DATAMONGO-1245 - */ - @Test - public void findsContactByUntypedExample() { - - Person person = new Person("Oliver", "Gierke"); - Contact result = repository.save(person); - - Example example = Example.of(person, ExampleSpec.untyped()); - - assertThat(repository.findOne(example), instanceOf(Person.class)); - } - /** * @see DATAMONGO-1245 */ @Test public void findsContactByTypedExample() { - Person person = new Person("Oliver", "Gierke"); - Contact result = repository.save(person); - - Example example = Example.of(person, ExampleSpec.typed(Person.class)); - - assertThat(repository.findOne(example), instanceOf(Person.class)); - } - - /** - * @see DATAMONGO-1245 - */ - @Test - public void findsNoContactByExample() { - - Person person = new Person("Oliver", "Gierke"); - Contact result = repository.save(person); - - Example example = Example.of(person, ExampleSpec.typed(Contact.class)); + Person person = repository.save(new Person("Oliver", "Gierke")); - assertThat(repository.findOne(example), is(nullValue())); + assertThat(repository.findOne(Example.of(person)), instanceOf(Person.class)); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java index 1d4b4460a..d7221de3f 100755 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java @@ -17,6 +17,7 @@ package org.springframework.data.mongodb.repository.support; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import static org.springframework.data.domain.ExampleMatcher.*; import java.util.ArrayList; import java.util.Arrays; @@ -32,8 +33,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; -import org.springframework.data.domain.ExampleSpec; -import org.springframework.data.domain.ExampleSpec.StringMatcher; +import org.springframework.data.domain.ExampleMatcher.StringMatcher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.geo.Point; @@ -262,7 +262,7 @@ public class SimpleMongoRepositoryTests { sample.setAddress(new Address(null, null, "Washington")); trimDomainType(sample, "id", "createdAt", "email"); - Example example = Example.of(sample, ExampleSpec.typed(Person.class).withIncludeNullValues()); + Example example = Example.of(sample, matching().withIncludeNullValues()); List result = repository.findAll(example); assertThat(result, empty()); @@ -281,7 +281,7 @@ public class SimpleMongoRepositoryTests { sample.setAddress(dave.getAddress()); trimDomainType(sample, "id", "createdAt", "email"); - Example example = Example.of(sample, ExampleSpec.untyped().withIncludeNullValues()); + Example example = Example.of(sample, matching().withIncludeNullValues()); List result = repository.findAll(example); assertThat(result, hasItem(dave)); @@ -298,7 +298,7 @@ public class SimpleMongoRepositoryTests { sample.setLastname("Mat"); trimDomainType(sample, "id", "createdAt", "email"); - Example example = Example.of(sample, ExampleSpec.untyped().withStringMatcher(StringMatcher.STARTING)); + Example example = Example.of(sample, matching().withStringMatcher(StringMatcher.STARTING)); List result = repository.findAll(example); assertThat(result, hasItems(dave, oliver)); @@ -379,14 +379,20 @@ public class SimpleMongoRepositoryTests { @Test public void findAllByExampleShouldProcessInheritanceCorrectly() { - PersonExtended sample = new PersonExtended() {}; + PersonExtended reference = new PersonExtended(); + reference.setLastname("Matthews"); + + repository.save(reference); + + PersonExtended sample = new PersonExtended(); sample.setLastname("Matthews"); + trimDomainType(sample, "id", "createdAt", "email"); List result = repository.findAll(Example.of(sample)); - assertThat(result, containsInAnyOrder(dave, oliver)); - assertThat(result, hasSize(2)); + assertThat(result, hasSize(1)); + assertThat(result, hasItem(reference)); } /** @@ -436,11 +442,6 @@ public class SimpleMongoRepositoryTests { assertThat(result, is(equalTo(2L))); } - @Document(collection = "customizedPerson") - static class PersonExtended extends Person { - - } - private void assertThatAllReferencePersonsWereStoredCorrectly(Map references, List saved) { for (Person person : saved) { @@ -489,4 +490,6 @@ public class SimpleMongoRepositoryTests { } } + @Document + static class PersonExtended extends Person {} } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/IsBsonObject.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/IsBsonObject.java index 0657dd131..d42cec284 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/IsBsonObject.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/IsBsonObject.java @@ -151,9 +151,10 @@ public class IsBsonObject extends TypeSafeMatcher { Object getValue(BSONObject source, String path) { - String[] fragments = path.split("\\."); + String[] fragments = path.split("(? it = Arrays.asList(fragments).iterator(); @@ -161,7 +162,7 @@ public class IsBsonObject extends TypeSafeMatcher { Object current = source; while (it.hasNext()) { - String key = it.next(); + String key = it.next().replace("\\.", "."); if (!(current instanceof BSONObject) && !key.startsWith("[")) { return null;