Browse Source

Fix to get rid of problem using Arrays.asList(Object[]). Now creates new LinkedList to preserve order of original array.

pull/1/head
Jon Brisbin 15 years ago committed by J. Brisbin
parent
commit
034164794c
  1. 69
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java
  2. 14
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/index/CompoundIndex.java
  3. 2
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/index/Indexed.java
  4. 44
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/CustomCollectionWithIndex.java
  5. 45
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/DetectedCollectionWithIndex.java
  6. 39
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java

69
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/convert/MappingMongoConverter.java

@ -27,8 +27,10 @@ import java.util.Collection; @@ -27,8 +27,10 @@ import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
@ -89,28 +91,28 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext @@ -89,28 +91,28 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
/**
* Creates a new {@link MappingMongoConverter} with the given {@link MappingContext}.
*
*
* @param mappingContext
*/
public MappingMongoConverter(MappingContext mappingContext) {
this.mappingContext = mappingContext;
this.conversionService.removeConvertible(Object.class, String.class);
}
/**
* Add custom {@link Converter} or {@link ConverterFactory} instances to be used that will take presidence over
* metadata driven conversion between of objects to/from DBObject
*
* @param converters
*/
public void setConverters(List<Converter<?, ?>> converters) {
if (null != converters) {
for (Converter<?, ?> c : converters) {
registerConverter(c);
conversionService.addConverter(c);
}
}
}
/**
* Add custom {@link Converter} or {@link ConverterFactory} instances to be used that will take presidence over
* metadata driven conversion between of objects to/from DBObject
*
* @param converters
*/
public void setConverters(List<Converter<?, ?>> converters) {
if (null != converters) {
for (Converter<?, ?> c : converters) {
registerConverter(c);
conversionService.addConverter(c);
}
}
}
/**
* Inspects the given {@link Converter} for the types it can convert and registers the pair for custom type conversion
@ -379,17 +381,17 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext @@ -379,17 +381,17 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
private void initializeConverters() {
if (!conversionService.canConvert(ObjectId.class, String.class)) {
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
}
if (!conversionService.canConvert(String.class, ObjectId.class)) {
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
}
if (!conversionService.canConvert(ObjectId.class, BigInteger.class)) {
conversionService.addConverter(ObjectIdToBigIntegerConverter.INSTANCE);
}
if (!conversionService.canConvert(BigInteger.class, ObjectId.class)) {
conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE);
}
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
}
if (!conversionService.canConvert(String.class, ObjectId.class)) {
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
}
if (!conversionService.canConvert(ObjectId.class, BigInteger.class)) {
conversionService.addConverter(ObjectIdToBigIntegerConverter.INSTANCE);
}
if (!conversionService.canConvert(BigInteger.class, ObjectId.class)) {
conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE);
}
MappingBeanHelper.setConversionService(conversionService);
}
@ -405,7 +407,10 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext @@ -405,7 +407,10 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
BasicDBList dbList = new BasicDBList();
Collection<?> coll;
if (type.isArray()) {
coll = Arrays.asList((Object[]) obj);
coll = new ArrayList<Object>();
for (Object o : (Object[]) obj) {
((List) coll).add(o);
}
} else {
coll = (Collection<?>) obj;
}
@ -562,7 +567,11 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext @@ -562,7 +567,11 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
items[i] = dbObjItem;
}
}
return Arrays.asList(items);
List<Object> itemsToReturn = new LinkedList<Object>();
for (Object obj : items) {
itemsToReturn.add(obj);
}
return itemsToReturn;
}
Class<?> toType = findTypeToBeUsed((DBObject) dbObj);
@ -602,7 +611,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext @@ -602,7 +611,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
}
public void afterPropertiesSet() {
initializeConverters();
initializeConverters();
}
protected class PersistentPropertyWrapper {

14
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/index/CompoundIndex.java

@ -28,18 +28,18 @@ import java.lang.annotation.Target; @@ -28,18 +28,18 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface CompoundIndex {
String def();
String def();
IndexDirection direction() default IndexDirection.ASCENDING;
IndexDirection direction() default IndexDirection.ASCENDING;
boolean unique() default false;
boolean unique() default false;
boolean sparse() default false;
boolean sparse() default false;
boolean dropDups() default true;
boolean dropDups() default false;
String name() default "";
String name() default "";
String collection() default "";
String collection() default "";
}

2
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/index/Indexed.java

@ -34,7 +34,7 @@ public @interface Indexed { @@ -34,7 +34,7 @@ public @interface Indexed {
boolean sparse() default false;
boolean dropDups() default true;
boolean dropDups() default false;
String name() default "";

44
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/CustomCollectionWithIndex.java

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb.mapping;
import org.springframework.data.annotation.Id;
import org.springframework.data.document.mongodb.index.Indexed;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document(collection = "foobar")
public class CustomCollectionWithIndex {
@Id
private String id;
@Indexed
private String name;
public CustomCollectionWithIndex(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

45
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/DetectedCollectionWithIndex.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb.mapping;
import org.springframework.data.annotation.Id;
import org.springframework.data.document.mongodb.index.Indexed;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document
public class DetectedCollectionWithIndex {
@Id
private String id;
@Indexed
private String name;
public DetectedCollectionWithIndex(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

39
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java

@ -25,13 +25,18 @@ import java.util.List; @@ -25,13 +25,18 @@ import java.util.List;
import java.util.Map;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.document.mongodb.CollectionCallback;
import org.springframework.data.document.mongodb.MongoDbUtils;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
@ -44,6 +49,7 @@ public class MappingTests { @@ -44,6 +49,7 @@ public class MappingTests {
private static final Log LOGGER = LogFactory.getLog(MongoDbUtils.class);
private final String[] collectionsToDrop = new String[]{
"foobar",
"person",
"personmapproperty",
"personpojo",
@ -223,4 +229,37 @@ public class MappingTests { @@ -223,4 +229,37 @@ public class MappingTests {
assertThat(result.size(), is(1));
}
@Test
public void testIndexesCreatedInRightCollection() {
CustomCollectionWithIndex ccwi = new CustomCollectionWithIndex("test");
template.insert(ccwi);
assertTrue(template.execute("foobar", new CollectionCallback<Boolean>() {
public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException {
List<DBObject> indexes = collection.getIndexInfo();
for (DBObject dbo : indexes) {
if ("name_1".equals(dbo.get("name"))) {
return true;
}
}
return false;
}
}));
DetectedCollectionWithIndex dcwi = new DetectedCollectionWithIndex("test");
template.insert(dcwi);
assertTrue(template.execute(DetectedCollectionWithIndex.class.getSimpleName().toLowerCase(), new CollectionCallback<Boolean>() {
public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException {
List<DBObject> indexes = collection.getIndexInfo();
for (DBObject dbo : indexes) {
if ("name_1".equals(dbo.get("name"))) {
return true;
}
}
return false;
}
}));
}
}

Loading…
Cancel
Save