Browse Source

DATAMONGO-458 - Read empty collections are modifiable now.

So far we've read empty collections and populated the property value of the Java object being created with Collections.emptySet(). This returns an unmodifiable Set so that further modifications fail with an UnsupportedOperationException. We now simply use new HashSet<Object>().
pull/6/head
Oliver Gierke 14 years ago
parent
commit
b3a891c69b
  1. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 28
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

@ -20,6 +20,7 @@ import java.util.ArrayList; @@ -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 @@ -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 @@ -721,7 +721,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Assert.notNull(targetType);
if (sourceValue.isEmpty()) {
return Collections.emptySet();
return new HashSet<Object>();
}
Class<?> collectionType = targetType.getType();

28
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* 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.
@ -13,7 +13,6 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -1164,6 +1181,7 @@ public class MappingMongoConverterUnitTests {
List<Contact> contacts;
List<List<String>> strings;
List<Map<String, Locale>> listOfMaps;
Set<Contact> contactsSet;
}
static class LocaleWrapper {

Loading…
Cancel
Save