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 3bc7876d9..6dde39e2f 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 @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -470,7 +471,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App * * @param collection must not be {@literal null}. * @param property must not be {@literal null}. - * * @return */ protected DBObject createCollection(Collection collection, MongoPersistentProperty property) { @@ -721,7 +721,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Assert.notNull(targetType); if (sourceValue.isEmpty()) { - return Collections.emptySet(); + return new HashSet(); } Class collectionType = targetType.getType(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java index e9ce13c0b..fff8bf0dc 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright 2011-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mongodb.core.convert; import static org.hamcrest.Matchers.*; @@ -42,6 +41,8 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.core.convert.converter.Converter; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.PersistenceConstructor; @@ -72,12 +73,15 @@ public class MappingMongoConverterUnitTests { MongoMappingContext mappingContext; @Mock MongoDbFactory factory; + @Mock + ApplicationContext context; @Before public void setUp() { mappingContext = new MongoMappingContext(); - mappingContext.afterPropertiesSet(); + mappingContext.setApplicationContext(context); + mappingContext.onApplicationEvent(new ContextRefreshedEvent(context)); converter = new MappingMongoConverter(factory, mappingContext); converter.afterPropertiesSet(); @@ -181,7 +185,6 @@ public class MappingMongoConverterUnitTests { public void writesTypeDiscriminatorIntoRootObject() { Person person = new Person(); - person.birthDate = new LocalDate(); DBObject result = new BasicDBObject(); converter.write(person, result); @@ -302,8 +305,8 @@ public class MappingMongoConverterUnitTests { */ @Test public void writesCollectionWithInterfaceCorrectly() { + Person person = new Person(); - person.birthDate = new LocalDate(); person.firstname = "Oliver"; CollectionWrapper wrapper = new CollectionWrapper(); @@ -1065,6 +1068,20 @@ public class MappingMongoConverterUnitTests { assertThat(dbRef.getRef(), is("person")); } + /** + * @see DATAMONGO-458 + */ + @Test + public void readEmptyCollectionIsModifiable() { + + DBObject dbObject = new BasicDBObject("contactsSet", new BasicDBList()); + CollectionWrapper wrapper = converter.read(CollectionWrapper.class, dbObject); + + assertThat(wrapper.contactsSet, is(notNullValue())); + wrapper.contactsSet.add(new Contact() { + }); + } + private static void assertSyntheticFieldValueOf(Object target, Object expected) { for (int i = 0; i < 10; i++) { @@ -1164,6 +1181,7 @@ public class MappingMongoConverterUnitTests { List contacts; List> strings; List> listOfMaps; + Set contactsSet; } static class LocaleWrapper {