JIRA: DATADOC-70: Trying to fix problem with indexes being wiped out in the tests, trying to make sure indexes get created properly. Now tests do not wipe out database automatically.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,3 +9,4 @@ target
|
||||
.project
|
||||
.classpath
|
||||
src/ant/.ant-targets-upload-dist.xml
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
@@ -45,7 +45,7 @@ import org.w3c.dom.Element;
|
||||
public class MongoMappingConverterParser extends AbstractBeanDefinitionParser {
|
||||
|
||||
static final String MAPPING_CONTEXT = "mappingContext";
|
||||
private static final String MAPPING_CONFIGURATION_HELPER = "mappingConfigurationHelper";
|
||||
private static final String INDEX_HELPER = "indexCreationHelper";
|
||||
private static final String TEMPLATE = "mongoTemplate";
|
||||
private static final String BASE_PACKAGE = "base-package";
|
||||
|
||||
@@ -85,13 +85,13 @@ public class MongoMappingConverterParser extends AbstractBeanDefinitionParser {
|
||||
converterBuilder.addPropertyReference("mongo", StringUtils.hasText(mongoRef) ? mongoRef : "mongo");
|
||||
|
||||
try {
|
||||
registry.getBeanDefinition(MAPPING_CONFIGURATION_HELPER);
|
||||
registry.getBeanDefinition(INDEX_HELPER);
|
||||
} catch (NoSuchBeanDefinitionException ignored) {
|
||||
String templateRef = element.getAttribute("mongo-template-ref");
|
||||
BeanDefinitionBuilder mappingConfigHelperBuilder = BeanDefinitionBuilder.genericBeanDefinition(MongoPersistentEntityIndexCreator.class);
|
||||
mappingConfigHelperBuilder.addConstructorArgValue(new RuntimeBeanReference(ctxRef));
|
||||
mappingConfigHelperBuilder.addConstructorArgValue(new RuntimeBeanReference(StringUtils.hasText(templateRef) ? templateRef : TEMPLATE));
|
||||
registry.registerBeanDefinition(MAPPING_CONFIGURATION_HELPER, mappingConfigHelperBuilder.getBeanDefinition());
|
||||
BeanDefinitionBuilder indexHelperBuilder = BeanDefinitionBuilder.genericBeanDefinition(MongoPersistentEntityIndexCreator.class);
|
||||
indexHelperBuilder.addConstructorArgValue(new RuntimeBeanReference(ctxRef));
|
||||
indexHelperBuilder.addConstructorArgValue(new RuntimeBeanReference(StringUtils.hasText(templateRef) ? templateRef : TEMPLATE));
|
||||
registry.registerBeanDefinition(INDEX_HELPER, indexHelperBuilder.getBeanDefinition());
|
||||
}
|
||||
|
||||
return converterBuilder.getBeanDefinition();
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.data.document.mongodb.mapping;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -50,12 +48,10 @@ import org.springframework.util.Assert;
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MongoPersistentEntityIndexCreator implements ApplicationListener<MappingContextEvent>{
|
||||
public class MongoPersistentEntityIndexCreator implements ApplicationListener<MappingContextEvent> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class);
|
||||
|
||||
private Map<String, CompoundIndex> compoundIndexes = new HashMap<String, CompoundIndex>();
|
||||
private Map<String, Indexed> fieldIndexes = new HashMap<String, Indexed>();
|
||||
private Set<Class<?>> classesSeen = Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>());
|
||||
|
||||
private final MongoTemplate mongoTemplate;
|
||||
@@ -79,7 +75,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
|
||||
}
|
||||
|
||||
protected void checkForIndexes(MongoPersistentEntity<?> entity) {
|
||||
Class<?> type = entity.getType();
|
||||
final Class<?> type = entity.getType();
|
||||
if (!classesSeen.contains(type)) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Analyzing class " + type + " for index information.");
|
||||
@@ -102,13 +98,10 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
|
||||
if ("".equals(indexColl)) {
|
||||
indexColl = type.getSimpleName().toLowerCase();
|
||||
}
|
||||
if (!compoundIndexes.containsKey(indexColl)) {
|
||||
ensureIndex(indexColl, index.name(), index.def(), index.direction(), index.unique(), index.dropDups(), index.sparse());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Created compound index " + index);
|
||||
}
|
||||
compoundIndexes.put(indexColl, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,17 +110,18 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
|
||||
Field field = persistentProperty.getField();
|
||||
if (field.isAnnotationPresent(Indexed.class)) {
|
||||
Indexed index = field.getAnnotation(Indexed.class);
|
||||
String name = index.name();
|
||||
if ("".equals(name)) {
|
||||
name = field.getName();
|
||||
}
|
||||
String collection = index.collection();
|
||||
if ("".equals(collection)) {
|
||||
collection = field.getName();
|
||||
collection = type.getSimpleName().toLowerCase();
|
||||
}
|
||||
if (!fieldIndexes.containsKey(collection)) {
|
||||
ensureIndex(collection, index.name(), null, index.direction(), index.unique(), index.dropDups(), index.sparse());
|
||||
ensureIndex(collection, name, null, index.direction(), index.unique(), index.dropDups(), index.sparse());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Created property index " + index);
|
||||
}
|
||||
fieldIndexes.put(collection, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -135,6 +129,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
|
||||
classesSeen.add(type);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected void ensureIndex(String collection,
|
||||
@@ -154,9 +149,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
|
||||
defObj.put(name, (direction == IndexDirection.ASCENDING ? 1 : -1));
|
||||
}
|
||||
DBObject opts = new BasicDBObject();
|
||||
if (!"".equals(name)) {
|
||||
opts.put("name", name);
|
||||
}
|
||||
//opts.put("name", name + "_idx");
|
||||
opts.put("dropDups", dropDups);
|
||||
opts.put("sparse", sparse);
|
||||
opts.put("unique", unique);
|
||||
|
||||
@@ -24,14 +24,11 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.document.mongodb.MongoTemplate;
|
||||
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
|
||||
import org.springframework.data.document.mongodb.query.Criteria;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -49,26 +46,7 @@ public class MappingTests {
|
||||
@Autowired
|
||||
MongoTemplate template;
|
||||
@Autowired
|
||||
MappingMongoConverter mappingConverter;
|
||||
|
||||
@Test
|
||||
public void setUp() {
|
||||
template.dropCollection("person");
|
||||
template.dropCollection("account");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertSimpleProperty() {
|
||||
PersonPojo p = new PersonPojo(1234, "Person", "Pojo");
|
||||
DBObject dbo = new BasicDBObject();
|
||||
mappingConverter.write(p, dbo);
|
||||
|
||||
assertEquals(dbo.get("ssn"), 1234);
|
||||
|
||||
PersonPojo p2 = mappingConverter.read(PersonPojo.class, dbo);
|
||||
|
||||
assertEquals(p.getFirstName(), p2.getFirstName());
|
||||
}
|
||||
MongoMappingContext mappingContext;
|
||||
|
||||
@Test
|
||||
public void testPersonPojo() {
|
||||
@@ -76,7 +54,7 @@ public class MappingTests {
|
||||
template.insert(p);
|
||||
assertNotNull(p.getId());
|
||||
|
||||
List<PersonPojo> result = template.find(new Query(Criteria.where("_id").is(p.getId())), PersonPojo.class);
|
||||
List<PersonPojo> result = template.find(new Query(Criteria.where("ssn").is(12345)), PersonPojo.class);
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(result.get(0).getSsn(), is(12345));
|
||||
}
|
||||
@@ -141,14 +119,29 @@ public class MappingTests {
|
||||
template.save("person", p);
|
||||
|
||||
assertNotNull(p.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadEntity() {
|
||||
List<Person> result = template.find(new Query(Criteria.where("ssn").is(123456789)), Person.class);
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(result.get(0).getAddress().getCountry(), is("USA"));
|
||||
assertThat(result.get(0).getAccounts(), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUniqueIndex() {
|
||||
Address addr = new Address();
|
||||
addr.setLines(new String[]{"1234 W. 1st Street", "Apt. 12"});
|
||||
addr.setCity("Anytown");
|
||||
addr.setPostalCode(12345);
|
||||
addr.setCountry("USA");
|
||||
|
||||
Person p1 = new Person(1234567890, "John", "Doe", 37, addr);
|
||||
Person p2 = new Person(1234567890, "John", "Doe", 37, addr);
|
||||
|
||||
template.insert("person", p1);
|
||||
template.insert("person", p2);
|
||||
|
||||
List<Person> result = template.find(new Query(Criteria.where("ssn").is(1234567890)), Person.class);
|
||||
assertThat(result.size(), is(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user