From bddd7674c0aab88b74422e48582c87ae16da1fd0 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 12 Apr 2017 16:54:38 +0200 Subject: [PATCH 1/3] DATAMONGO-1666 - Constructor creation with bulk fetching of DBRefs uses List instead of collection type. Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index eea61f492c..508ca5a3b2 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1666-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index 4a49168713..e931f07791 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1666-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1666-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 750ed23aa8..5cda02c3aa 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1666-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index 50d0a6454a..268d186ed5 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1666-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 886bcca7b6..67af779eab 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1666-SNAPSHOT ../pom.xml From d12cdfc2097b8e6d3b7379251d60cd9b8cd61b08 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 12 Apr 2017 16:53:42 +0200 Subject: [PATCH 2/3] DATAMONGO-1666 - Consider collection type in bulk DBRef fetching. We now consider the property's collection type after bulk-fetching DBRefs before returning the actual result value. The issue got only visible if bulk fetching is possible and constructor creation is used. Setting the property value on through an property accessor works fine because the property accessor checks all values for assignability and potentially converts values to their target type. That's different for constructor creation. --- .../core/convert/MappingMongoConverter.java | 14 +++-- .../DbRefMappingMongoConverterUnitTests.java | 56 +++++++++++++++---- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 785b2ea1eb..544c2f2344 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -31,6 +31,7 @@ import org.bson.conversions.Bson; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -55,7 +56,6 @@ import org.springframework.data.mapping.model.SpELExpressionEvaluator; import org.springframework.data.mapping.model.SpELExpressionParameterValueProvider; import org.springframework.data.mongodb.MongoDbFactory; -import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToBigIntegerConverter; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent; @@ -241,10 +241,9 @@ private S read(TypeInformation type, Bson bson, ObjectPath } // Retrieve persistent entity info - Document target = bson instanceof BasicDBObject ? new Document((BasicDBObject)bson) : (Document) bson; + Document target = bson instanceof BasicDBObject ? new Document((BasicDBObject) bson) : (Document) bson; - return read((MongoPersistentEntity) mappingContext.getRequiredPersistentEntity(typeToUse), target, - path); + return read((MongoPersistentEntity) mappingContext.getRequiredPersistentEntity(typeToUse), target, path); } private ParameterValueProvider getParameterProvider(MongoPersistentEntity entity, @@ -870,7 +869,7 @@ protected DBRef createDBRef(Object target, MongoPersistentProperty property) { } return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, - idMapper.convertId(id instanceof Optional ? (Optional)id : Optional.ofNullable(id)).orElse(null)); + idMapper.convertId(id instanceof Optional ? (Optional) id : Optional.ofNullable(id)).orElse(null)); }).orElseThrow(() -> new MappingException("No id property found on class " + entity.getType())); } @@ -913,7 +912,10 @@ private Object readCollectionOrArray(TypeInformation targetType, List sourceV } if (!DBRef.class.equals(rawComponentType) && isCollectionOfDbRefWhereBulkFetchIsPossible(sourceValue)) { - return bulkReadAndConvertDBRefs((List) (List) (sourceValue), componentType, path, rawComponentType); + + List objects = bulkReadAndConvertDBRefs((List) sourceValue, componentType, path, + rawComponentType); + return getPotentiallyConvertedSimpleRead(objects, targetType.getType()); } for (Object dbObjItem : sourceValue) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java index a298dd3b20..1ec87d6ee6 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java @@ -29,19 +29,18 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; -import com.mongodb.BasicDBObject; -import org.bson.BsonDocument; import org.bson.Document; import org.bson.conversions.Bson; import org.bson.types.ObjectId; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; + import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; @@ -65,7 +64,7 @@ /** * Unit tests for {@link DbRefMappingMongoConverter}. - * + * * @author Oliver Gierke * @author Thomas Darimont * @author Christoph Strobl @@ -485,7 +484,8 @@ public void shouldEagerlyResolveIdPropertyWithFieldAccess() { ClassWithLazyDbRefs result = converter.read(ClassWithLazyDbRefs.class, object); PersistentPropertyAccessor accessor = propertyEntity.getPropertyAccessor(result.dbRefToConcreteType); - MongoPersistentProperty idProperty = mappingContext.getRequiredPersistentEntity(LazyDbRefTarget.class).getIdProperty().get(); + MongoPersistentProperty idProperty = mappingContext.getRequiredPersistentEntity(LazyDbRefTarget.class) + .getIdProperty().get(); assertThat(accessor.getProperty(idProperty), is(notNullValue())); assertProxyIsResolved(result.dbRefToConcreteType, false); @@ -512,7 +512,8 @@ public void shouldNotEagerlyResolveIdPropertyWithPropertyAccess() { @Test // DATAMONGO-1076 public void shouldNotTriggerResolvingOfLazyLoadedProxyWhenFinalizeMethodIsInvoked() throws Exception { - MongoPersistentEntity entity = mappingContext.getRequiredPersistentEntity(WithObjectMethodOverrideLazyDbRefs.class); + MongoPersistentEntity entity = mappingContext + .getRequiredPersistentEntity(WithObjectMethodOverrideLazyDbRefs.class); MongoPersistentProperty property = entity.getRequiredPersistentProperty("dbRefToPlainObject"); String idValue = new ObjectId().toString(); @@ -534,8 +535,9 @@ public void shouldBulkFetchListOfReferences() { String value = "val"; MappingMongoConverter converterSpy = spy(converter); - doReturn(Arrays.asList(new Document("_id", id1).append("value", value), - new Document("_id", id2).append("value", value))).when(converterSpy).bulkReadRefs(anyListOf(DBRef.class)); + doReturn( + Arrays.asList(new Document("_id", id1).append("value", value), new Document("_id", id2).append("value", value))) + .when(converterSpy).bulkReadRefs(anyListOf(DBRef.class)); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); @@ -553,6 +555,28 @@ public void shouldBulkFetchListOfReferences() { verify(converterSpy, never()).readRef(Mockito.any(DBRef.class)); } + @Test // DATAMONGO-1666 + public void shouldBulkFetchSetOfReferencesForConstructorCreation() { + + String id1 = "1"; + String id2 = "2"; + String value = "val"; + + MappingMongoConverter converterSpy = spy(converter); + doReturn( + Arrays.asList(new Document("_id", id1).append("value", value), new Document("_id", id2).append("value", value))) + .when(converterSpy).bulkReadRefs(any()); + + Document document = new Document("dbRefToInterface", + Arrays.asList(new DBRef("lazyDbRefTarget", "1"), new DBRef("lazyDbRefTarget", "2"))); + + ClassWithDbRefSetConstructor result = converterSpy.read(ClassWithDbRefSetConstructor.class, document); + + assertThat(result.dbRefToInterface, is(instanceOf(Set.class))); + + verify(converterSpy, never()).readRef(Mockito.any(DBRef.class)); + } + @Test // DATAMONGO-1194 public void shouldFallbackToOneByOneFetchingWhenElementsInListOfReferencesPointToDifferentCollections() { @@ -561,9 +585,8 @@ public void shouldFallbackToOneByOneFetchingWhenElementsInListOfReferencesPointT String value = "val"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id1).append("value", value)) - .doReturn(new Document("_id", id2).append("value", value)).when(converterSpy) - .readRef(Mockito.any(DBRef.class)); + doReturn(new Document("_id", id1).append("value", value)).doReturn(new Document("_id", id2).append("value", value)) + .when(converterSpy).readRef(Mockito.any(DBRef.class)); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); @@ -678,6 +701,15 @@ static class ClassWithLazyDbRefs { lazy = true) LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor; } + static class ClassWithDbRefSetConstructor { + + final @org.springframework.data.mongodb.core.mapping.DBRef Set dbRefToInterface; + + public ClassWithDbRefSetConstructor(Set dbRefToInterface) { + this.dbRefToInterface = dbRefToInterface; + } + } + static class SerializableClassWithLazyDbRefs implements Serializable { private static final long serialVersionUID = 1L; @@ -785,7 +817,7 @@ public ToStringObjectMethodOverrideLazyDbRefTarget(String id, String value) { super(id, value); } - /* + /* * (non-Javadoc) * @see java.lang.Object#toString() */ From 2d824e9f5f32350ad6afa6864e4319f8d44be9f8 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 13 Apr 2017 10:22:22 +0200 Subject: [PATCH 3/3] DATAMONGO-1666 - Polishing. Some minor language level cleanups and removal of deprecated API usage. --- .../core/convert/MappingMongoConverter.java | 117 ++++++++---------- .../DbRefMappingMongoConverterUnitTests.java | 60 +++++---- 2 files changed, 83 insertions(+), 94 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 544c2f2344..e3d1870979 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -250,7 +250,7 @@ private ParameterValueProvider getParameterProvider(Mon Bson source, DefaultSpELExpressionEvaluator evaluator, ObjectPath path) { MongoDbPropertyValueProvider provider = new MongoDbPropertyValueProvider(source, evaluator, path); - PersistentEntityParameterValueProvider parameterProvider = new PersistentEntityParameterValueProvider( + PersistentEntityParameterValueProvider parameterProvider = new PersistentEntityParameterValueProvider<>( entity, provider, path.getCurrentObject()); return new ConverterAwareSpELExpressionParameterValueProvider(evaluator, conversionService, parameterProvider, @@ -273,7 +273,7 @@ private S read(final MongoPersistentEntity entity, final D DocumentAccessor documentAccessor = new DocumentAccessor(bson); // make sure id property is set before all other properties - Optional idValue = idProperty.filter(it -> documentAccessor.hasValue(it)).map(it -> { + Optional idValue = idProperty.filter(documentAccessor::hasValue).map(it -> { Optional value = getValueInternal(it, bson, evaluator, path); accessor.setProperty(it, value); @@ -285,42 +285,38 @@ private S read(final MongoPersistentEntity entity, final D idValue.isPresent() ? idProperty.map(it -> bson.get(it.getFieldName())).orElse(null) : null); // Set properties not already set in the constructor - entity.doWithProperties(new PropertyHandler() { - public void doWithPersistentProperty(MongoPersistentProperty prop) { + entity.doWithProperties((PropertyHandler) prop -> { - // we skip the id property since it was already set - if (idProperty != null && idProperty.equals(prop)) { - return; - } - - if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) { - return; - } + // we skip the id property since it was already set + if (idProperty != null && idProperty.equals(prop)) { + return; + } - accessor.setProperty(prop, getValueInternal(prop, bson, evaluator, currentPath)); + if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) { + return; } + + accessor.setProperty(prop, getValueInternal(prop, bson, evaluator, currentPath)); }); // Handle associations - entity.doWithAssociations(new AssociationHandler() { - public void doWithAssociation(Association association) { + entity.doWithAssociations((AssociationHandler) association -> { - final MongoPersistentProperty property = association.getInverse(); - Object value = documentAccessor.get(property); + final MongoPersistentProperty property = association.getInverse(); + Object value = documentAccessor.get(property); - if (value == null || entity.isConstructorArgument(property)) { - return; - } + if (value == null || entity.isConstructorArgument(property)) { + return; + } - DBRef dbref = value instanceof DBRef ? (DBRef) value : null; + DBRef dbref = value instanceof DBRef ? (DBRef) value : null; - DbRefProxyHandler handler = new DefaultDbRefProxyHandler(spELContext, mappingContext, - MappingMongoConverter.this); - DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, - MappingMongoConverter.this); + DbRefProxyHandler handler = new DefaultDbRefProxyHandler(spELContext, mappingContext, + MappingMongoConverter.this); + DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, + MappingMongoConverter.this); - accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler)); - } + accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler)); }); return result; @@ -430,31 +426,26 @@ protected void writeInternal(Object obj, final Bson bson, MongoPersistentEntity< prop -> dbObjectAccessor.computeIfAbsent(prop, () -> idMapper.convertId(accessor.getProperty(prop)))); // Write the properties - entity.doWithProperties(new PropertyHandler() { - public void doWithPersistentProperty(MongoPersistentProperty prop) { + entity.doWithProperties((PropertyHandler) prop -> { - if (idProperty.map(it -> it.equals(prop)).orElse(false) || !prop.isWritable()) { - return; - } + if (idProperty.map(it -> it.equals(prop)).orElse(false) || !prop.isWritable()) { + return; + } - accessor.getProperty(prop).ifPresent(it -> { - if (!conversions.isSimpleType(it.getClass())) { + accessor.getProperty(prop).ifPresent(it -> { + if (!conversions.isSimpleType(it.getClass())) { - writePropertyInternal(it, bson, prop); - } else { - writeSimpleInternal(it, bson, prop); - } - }); - } + writePropertyInternal(it, bson, prop); + } else { + writeSimpleInternal(it, bson, prop); + } + }); }); - entity.doWithAssociations(new AssociationHandler() { + entity.doWithAssociations((AssociationHandler) association -> { - public void doWithAssociation(Association association) { - - MongoPersistentProperty inverseProp = association.getInverse(); - accessor.getProperty(inverseProp).ifPresent(it -> writePropertyInternal(it, bson, inverseProp)); - } + MongoPersistentProperty inverseProp = association.getInverse(); + accessor.getProperty(inverseProp).ifPresent(it -> writePropertyInternal(it, bson, inverseProp)); }); } @@ -562,7 +553,7 @@ protected List createCollection(Collection collection, MongoPersisten return writeCollectionInternal(collection, Optional.of(property.getTypeInformation()), new BasicDBList()); } - List dbList = new ArrayList(); + List dbList = new ArrayList<>(collection.size()); for (Object element : collection) { @@ -624,7 +615,7 @@ protected Bson createMap(Map map, MongoPersistentProperty proper private BasicDBList writeCollectionInternal(Collection source, Optional> type, BasicDBList sink) { - Optional> componentType = type.flatMap(it -> it.getComponentType()); + Optional> componentType = type.flatMap(TypeInformation::getComponentType); for (Object element : source) { @@ -758,7 +749,7 @@ protected String potentiallyUnescapeMapKey(String source) { */ protected void addCustomTypeKeyIfNecessary(Optional> type, Object value, Bson bson) { - Optional> actualType = type.map(it -> it.getActualType()).map(it -> it.getType()); + Optional> actualType = type.map(TypeInformation::getActualType).map(TypeInformation::getType); Class reference = actualType.orElse(Object.class); Class valueType = ClassUtils.getUserClass(value.getClass()); @@ -904,7 +895,7 @@ private Object readCollectionOrArray(TypeInformation targetType, List sourceV Class rawComponentType = componentType.getType(); collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class; - Collection items = targetType.getType().isArray() ? new ArrayList() + Collection items = targetType.getType().isArray() ? new ArrayList<>(sourceValue.size()) : CollectionFactory.createCollection(collectionType, rawComponentType, sourceValue.size()); if (sourceValue.isEmpty()) { @@ -964,8 +955,8 @@ protected Map readMap(TypeInformation type, Bson bson, Object Class mapType = typeMapper.readType(bson, type).getType(); Optional> valueType = type.getMapValueType(); - Class rawKeyType = type.getComponentType().map(it -> it.getType()).orElse(null); - Class rawValueType = type.getMapValueType().map(it -> it.getType()).orElse(null); + Class rawKeyType = type.getComponentType().map(TypeInformation::getType).orElse(null); + Class rawValueType = type.getMapValueType().map(TypeInformation::getType).orElse(null); Map sourceMap = asMap(bson); Map map = CollectionFactory.createMap(mapType, rawKeyType, sourceMap.keySet().size()); @@ -1008,7 +999,7 @@ protected Map readMap(TypeInformation type, Bson bson, Object } @SuppressWarnings("unchecked") - private Map asMap(Bson bson) { + private static Map asMap(Bson bson) { if (bson instanceof Document) { return (Document) bson; @@ -1022,7 +1013,7 @@ private Map asMap(Bson bson) { String.format("Cannot read %s. as map. Given Bson must be a Document or DBObject!", bson.getClass())); } - private void addToMap(Bson bson, String key, Object value) { + private static void addToMap(Bson bson, String key, Object value) { if (bson instanceof Document) { ((Document) bson).put(key, value); @@ -1037,7 +1028,7 @@ private void addToMap(Bson bson, String key, Object value) { } @SuppressWarnings("unchecked") - private void addAllToMap(Bson bson, Map value) { + private static void addAllToMap(Bson bson, Map value) { if (bson instanceof Document) { ((Document) bson).putAll(value); @@ -1053,7 +1044,7 @@ private void addAllToMap(Bson bson, Map value) { String.format("Cannot add all to %s. Given Bson must be a Document or DBObject.", bson.getClass())); } - private void removeFromMap(Bson bson, String key) { + private static void removeFromMap(Bson bson, String key) { if (bson instanceof Document) { ((Document) bson).remove(key); @@ -1120,7 +1111,7 @@ public Object convertToMongoType(Object obj, TypeInformation typeInformation) if (obj instanceof Map) { - Map converted = new LinkedHashMap(); + Map converted = new LinkedHashMap<>(((Map)obj).size(), 1); Document result = new Document(); for (Map.Entry entry : ((Map) obj).entrySet()) { @@ -1254,7 +1245,7 @@ public Optional getPropertyValue(MongoPersistentProperty property) { return Optional .ofNullable(property.getSpelExpression()// - .map(it -> evaluator.evaluate(it))// + .map(evaluator::evaluate)// .orElseGet(() -> source.get(property)))// .map(it -> readValue(it, property.getTypeInformation(), path)); } @@ -1337,7 +1328,7 @@ private T readAndConvertDBRef(DBRef dbref, TypeInformation type, ObjectPa private void bulkReadAndConvertDBRefMapIntoTarget(TypeInformation valueType, Class rawValueType, Map sourceMap, Map targetMap) { - LinkedHashMap referenceMap = new LinkedHashMap(sourceMap); + LinkedHashMap referenceMap = new LinkedHashMap<>(sourceMap); List convertedObjects = bulkReadAndConvertDBRefs((List) new ArrayList(referenceMap.values()), valueType, ObjectPath.ROOT, rawValueType); int index = 0; @@ -1360,19 +1351,19 @@ private List bulkReadAndConvertDBRefs(List dbrefs, TypeInformation ? Collections.singletonList(readRef(dbrefs.iterator().next())) : bulkReadRefs(dbrefs); String collectionName = dbrefs.iterator().next().getCollectionName(); - List targeList = new ArrayList(dbrefs.size()); + List targeList = new ArrayList<>(dbrefs.size()); for (Document document : referencedRawDocuments) { if (document != null) { - maybeEmitEvent(new AfterLoadEvent(document, (Class) rawType, collectionName)); + maybeEmitEvent(new AfterLoadEvent<>(document, (Class) rawType, collectionName)); } final T target = (T) read(type, document, path); targeList.add(target); if (target != null) { - maybeEmitEvent(new AfterConvertEvent(document, target, collectionName)); + maybeEmitEvent(new AfterConvertEvent<>(document, target, collectionName)); } } @@ -1421,7 +1412,7 @@ private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable collectionsFound = new HashSet(); + Set collectionsFound = new HashSet<>(); for (Object dbObjItem : source) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java index 1ec87d6ee6..fd0a36113a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java @@ -24,6 +24,7 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -40,7 +41,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; - import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; @@ -95,7 +95,7 @@ public void createsSimpleDBRefCorrectly() { person.id = "foo"; DBRef dbRef = converter.toDBRef(person, null); - assertThat(dbRef.getId(), is((Object) "foo")); + assertThat(dbRef.getId(), is("foo")); assertThat(dbRef.getCollectionName(), is("person")); } @@ -127,7 +127,7 @@ public void convertDocumentWithMapDBRef() { MapDBRefVal val = new MapDBRefVal(); val.id = BigInteger.ONE; - Map mapVal = new HashMap(); + Map mapVal = new HashMap<>(); mapVal.put("test", val); mapDBRef.map = mapVal; @@ -156,7 +156,7 @@ public void createsDBRefWithClientSpecCorrectly() { person.id = "foo"; DBRef dbRef = converter.toDBRef(person, property); - assertThat(dbRef.getId(), is((Object) "foo")); + assertThat(dbRef.getId(), is("foo")); assertThat(dbRef.getCollectionName(), is("person")); } @@ -166,11 +166,11 @@ public void lazyLoadingProxyForLazyDbRefOnInterface() { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToInterface = new LinkedList(Arrays.asList(new LazyDbRefTarget("1"))); + lazyDbRefs.dbRefToInterface = new LinkedList<>(Collections.singletonList(new LazyDbRefTarget("1"))); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -187,12 +187,11 @@ public void lazyLoadingProxyForLazyDbRefOnConcreteCollection() { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteCollection = new ArrayList( - Arrays.asList(new LazyDbRefTarget(id, value))); + lazyDbRefs.dbRefToConcreteCollection = new ArrayList<>(Collections.singletonList(new LazyDbRefTarget(id, value))); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -209,7 +208,7 @@ public void lazyLoadingProxyForLazyDbRefOnConcreteType() { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); @@ -230,12 +229,11 @@ public void lazyLoadingProxyForLazyDbRefOnConcreteTypeWithPersistenceConstructor String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructor = new LazyDbRefTargetWithPeristenceConstructor((Object) id, - (Object) value); + lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructor = new LazyDbRefTargetWithPeristenceConstructor(id, value); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -252,12 +250,12 @@ public void lazyLoadingProxyForLazyDbRefOnConcreteTypeWithPersistenceConstructor String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); lazyDbRefs.dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor = new LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor( - (Object) id, (Object) value); + id, value); converterSpy.write(lazyDbRefs, document); ClassWithLazyDbRefs result = converterSpy.read(ClassWithLazyDbRefs.class, document); @@ -274,7 +272,7 @@ public void lazyLoadingProxyForSerializableLazyDbRefOnConcreteType() { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); SerializableClassWithLazyDbRefs lazyDbRefs = new SerializableClassWithLazyDbRefs(); @@ -296,7 +294,7 @@ public void lazyLoadingProxyForToStringObjectMethodOverridingDbref() { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); WithObjectMethodOverrideLazyDbRefs lazyDbRefs = new WithObjectMethodOverrideLazyDbRefs(); @@ -317,7 +315,7 @@ public void callingToStringObjectMethodOnLazyLoadingDbrefShouldNotInitializeProx String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); WithObjectMethodOverrideLazyDbRefs lazyDbRefs = new WithObjectMethodOverrideLazyDbRefs(); @@ -393,7 +391,7 @@ public void lazyLoadingProxyForEqualsAndHashcodeObjectMethodOverridingDbref() { String id = "42"; String value = "bubu"; MappingMongoConverter converterSpy = spy(converter); - doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef((DBRef) any()); + doReturn(new Document("_id", id).append("value", value)).when(converterSpy).readRef(any()); Document document = new Document(); WithObjectMethodOverrideLazyDbRefs lazyDbRefs = new WithObjectMethodOverrideLazyDbRefs(); @@ -537,11 +535,11 @@ public void shouldBulkFetchListOfReferences() { MappingMongoConverter converterSpy = spy(converter); doReturn( Arrays.asList(new Document("_id", id1).append("value", value), new Document("_id", id2).append("value", value))) - .when(converterSpy).bulkReadRefs(anyListOf(DBRef.class)); + .when(converterSpy).bulkReadRefs(anyList()); Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteCollection = new ArrayList( + lazyDbRefs.dbRefToConcreteCollection = new ArrayList<>( Arrays.asList(new LazyDbRefTarget(id1, value), new LazyDbRefTarget(id2, value))); converterSpy.write(lazyDbRefs, document); @@ -565,7 +563,7 @@ public void shouldBulkFetchSetOfReferencesForConstructorCreation() { MappingMongoConverter converterSpy = spy(converter); doReturn( Arrays.asList(new Document("_id", id1).append("value", value), new Document("_id", id2).append("value", value))) - .when(converterSpy).bulkReadRefs(any()); + .when(converterSpy).bulkReadRefs(anyList()); Document document = new Document("dbRefToInterface", Arrays.asList(new DBRef("lazyDbRefTarget", "1"), new DBRef("lazyDbRefTarget", "2"))); @@ -590,7 +588,7 @@ public void shouldFallbackToOneByOneFetchingWhenElementsInListOfReferencesPointT Document document = new Document(); ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs(); - lazyDbRefs.dbRefToConcreteCollection = new ArrayList( + lazyDbRefs.dbRefToConcreteCollection = new ArrayList<>( Arrays.asList(new LazyDbRefTarget(id1, value), new SerializableLazyDbRefTarget(id2, value))); converterSpy.write(lazyDbRefs, document); @@ -602,7 +600,7 @@ public void shouldFallbackToOneByOneFetchingWhenElementsInListOfReferencesPointT assertThat(result.dbRefToConcreteCollection.get(1).getId(), is(id2)); verify(converterSpy, times(2)).readRef(Mockito.any(DBRef.class)); - verify(converterSpy, never()).bulkReadRefs(anyListOf(DBRef.class)); + verify(converterSpy, never()).bulkReadRefs(anyList()); } @Test // DATAMONGO-1194 @@ -616,11 +614,11 @@ public void shouldBulkFetchMapOfReferences() { MappingMongoConverter converterSpy = spy(converter); doReturn(Arrays.asList(new Document("_id", val1.id), new Document("_id", val2.id))).when(converterSpy) - .bulkReadRefs(anyListOf(DBRef.class)); + .bulkReadRefs(anyList()); Document document = new Document(); MapDBRef mapDBRef = new MapDBRef(); - mapDBRef.map = new LinkedHashMap(); + mapDBRef.map = new LinkedHashMap<>(); mapDBRef.map.put("one", val1); mapDBRef.map.put("two", val2); @@ -633,7 +631,7 @@ public void shouldBulkFetchMapOfReferences() { // assertProxyIsResolved(result.map, true); assertThat(result.map.get("two").id, is(val2.id)); - verify(converterSpy, times(1)).bulkReadRefs(anyListOf(DBRef.class)); + verify(converterSpy, times(1)).bulkReadRefs(anyList()); verify(converterSpy, never()).readRef(Mockito.any(DBRef.class)); } @@ -648,11 +646,11 @@ public void shouldBulkFetchLazyMapOfReferences() { MappingMongoConverter converterSpy = spy(converter); doReturn(Arrays.asList(new Document("_id", val1.id), new Document("_id", val2.id))).when(converterSpy) - .bulkReadRefs(anyListOf(DBRef.class)); + .bulkReadRefs(anyList()); Document document = new Document(); MapDBRef mapDBRef = new MapDBRef(); - mapDBRef.lazyMap = new LinkedHashMap(); + mapDBRef.lazyMap = new LinkedHashMap<>(); mapDBRef.lazyMap.put("one", val1); mapDBRef.lazyMap.put("two", val2); @@ -665,8 +663,8 @@ public void shouldBulkFetchLazyMapOfReferences() { assertProxyIsResolved(result.lazyMap, true); assertThat(result.lazyMap.get("two").id, is(val2.id)); - verify(converterSpy, times(1)).bulkReadRefs(anyListOf(DBRef.class)); - verify(converterSpy, never()).readRef(Mockito.any(DBRef.class)); + verify(converterSpy, times(1)).bulkReadRefs(anyList()); + verify(converterSpy, never()).readRef(any()); } private Object transport(Object result) {