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. 30
      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;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -470,7 +471,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
* *
* @param collection must not be {@literal null}. * @param collection must not be {@literal null}.
* @param property must not be {@literal null}. * @param property must not be {@literal null}.
*
* @return * @return
*/ */
protected DBObject createCollection(Collection<?> collection, MongoPersistentProperty property) { protected DBObject createCollection(Collection<?> collection, MongoPersistentProperty property) {
@ -721,7 +721,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Assert.notNull(targetType); Assert.notNull(targetType);
if (sourceValue.isEmpty()) { if (sourceValue.isEmpty()) {
return Collections.emptySet(); return new HashSet<Object>();
} }
Class<?> collectionType = targetType.getType(); Class<?> collectionType = targetType.getType();

30
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core.convert; package org.springframework.data.mongodb.core.convert;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -42,6 +41,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Value; 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.core.convert.converter.Converter;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.annotation.PersistenceConstructor;
@ -72,12 +73,15 @@ public class MappingMongoConverterUnitTests {
MongoMappingContext mappingContext; MongoMappingContext mappingContext;
@Mock @Mock
MongoDbFactory factory; MongoDbFactory factory;
@Mock
ApplicationContext context;
@Before @Before
public void setUp() { public void setUp() {
mappingContext = new MongoMappingContext(); mappingContext = new MongoMappingContext();
mappingContext.afterPropertiesSet(); mappingContext.setApplicationContext(context);
mappingContext.onApplicationEvent(new ContextRefreshedEvent(context));
converter = new MappingMongoConverter(factory, mappingContext); converter = new MappingMongoConverter(factory, mappingContext);
converter.afterPropertiesSet(); converter.afterPropertiesSet();
@ -181,7 +185,6 @@ public class MappingMongoConverterUnitTests {
public void writesTypeDiscriminatorIntoRootObject() { public void writesTypeDiscriminatorIntoRootObject() {
Person person = new Person(); Person person = new Person();
person.birthDate = new LocalDate();
DBObject result = new BasicDBObject(); DBObject result = new BasicDBObject();
converter.write(person, result); converter.write(person, result);
@ -302,8 +305,8 @@ public class MappingMongoConverterUnitTests {
*/ */
@Test @Test
public void writesCollectionWithInterfaceCorrectly() { public void writesCollectionWithInterfaceCorrectly() {
Person person = new Person(); Person person = new Person();
person.birthDate = new LocalDate();
person.firstname = "Oliver"; person.firstname = "Oliver";
CollectionWrapper wrapper = new CollectionWrapper(); CollectionWrapper wrapper = new CollectionWrapper();
@ -1065,6 +1068,20 @@ public class MappingMongoConverterUnitTests {
assertThat(dbRef.getRef(), is("person")); 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) { private static void assertSyntheticFieldValueOf(Object target, Object expected) {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
@ -1164,6 +1181,7 @@ public class MappingMongoConverterUnitTests {
List<Contact> contacts; List<Contact> contacts;
List<List<String>> strings; List<List<String>> strings;
List<Map<String, Locale>> listOfMaps; List<Map<String, Locale>> listOfMaps;
Set<Contact> contactsSet;
} }
static class LocaleWrapper { static class LocaleWrapper {

Loading…
Cancel
Save