Tweaks to tests, BasePerson class, common inheritance to test classes, tweaks to complex/simple type detection.

This commit is contained in:
Jon Brisbin
2011-03-14 10:58:36 -05:00
committed by J. Brisbin
parent 255fbb3379
commit 21048f211d
8 changed files with 26 additions and 91 deletions

View File

@@ -58,7 +58,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
protected MappingContext mappingContext;
protected ApplicationContext applicationContext;
protected boolean autowirePersistentBeans = false;
protected boolean useFieldAccessOnly = false;
protected boolean useFieldAccessOnly = true;
public MappingMongoConverter() {
initializeConverters();
@@ -367,13 +367,12 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
}
protected void writeMapInternal(Map<Object, Object> obj, DBObject dbo) {
Set<String> simpleTypes = MappingBeanHelper.getSimpleTypes();
for (Map.Entry<Object, Object> entry : obj.entrySet()) {
Object key = entry.getKey();
Object val = entry.getValue();
if (simpleTypes.contains(key.getClass().getName())) {
if (MappingBeanHelper.isSimpleType(key.getClass())) {
String simpleKey = conversionService.convert(key, String.class);
if (simpleTypes.contains(val.getClass().toString())) {
if (MappingBeanHelper.isSimpleType(val.getClass())) {
dbo.put(simpleKey, val);
} else {
DBObject newDbo = new BasicDBObject();

View File

@@ -36,7 +36,6 @@ import org.springframework.data.mapping.model.*;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -53,15 +52,15 @@ public class MongoMappingConfigurationBuilder extends BasicMappingConfigurationB
public MongoMappingConfigurationBuilder(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
// Augment simpleTypes with MongoDB-specific classes
Set<String> simpleTypes = MappingBeanHelper.getSimpleTypes();
simpleTypes.add(DBRef.class.getName());
simpleTypes.add(ObjectId.class.getName());
simpleTypes.add(CodeWScope.class.getName());
Set<Class<?>> simpleTypes = MappingBeanHelper.getSimpleTypes();
simpleTypes.add(com.mongodb.DBRef.class);
simpleTypes.add(ObjectId.class);
simpleTypes.add(CodeWScope.class);
}
@Override
public PersistentProperty<?> createPersistentProperty(Field field, PropertyDescriptor descriptor) throws MappingConfigurationException {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
PersistentProperty<?> property = new MongoPersistentProperty(field.getName(), field.getType(), field, descriptor);
if (field.isAnnotationPresent(Indexed.class)) {
Indexed index = field.getAnnotation(Indexed.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 by the original author(s).
* 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.

View File

@@ -19,11 +19,14 @@ package org.springframework.data.document.mongodb.mapping;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public class BasePerson {
public abstract class BasePerson {
private Integer ssn;
private String firstName;
private String lastName;
protected Integer ssn;
protected String firstName;
protected String lastName;
public BasePerson() {
}
public BasePerson(Integer ssn, String firstName, String lastName) {
this.ssn = ssn;

View File

@@ -16,9 +16,7 @@
package org.springframework.data.document.mongodb.mapping;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
@@ -50,9 +48,6 @@ public class MappingTests {
@Autowired
BasicMappingContext mappingContext;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void setUp() {
template.dropCollection("person");
@@ -100,14 +95,12 @@ public class MappingTests {
@Test
@SuppressWarnings({"unchecked"})
public void testWriteEntity() {
Person p = new Person(123456789, "John", "Doe", 37);
Address addr = new Address();
addr.setLines(new String[]{"1234 W. 1st Street", "Apt. 12"});
addr.setCity("Anytown");
addr.setPostalCode(12345);
addr.setCountry("USA");
p.setAddress(addr);
Account acct = new Account();
acct.setBalance(1000.00f);
@@ -115,8 +108,9 @@ public class MappingTests {
List<Account> accounts = new ArrayList<Account>();
accounts.add(acct);
p.setAccounts(accounts);
Person p = new Person(123456789, "John", "Doe", 37, addr);
p.setAccounts(accounts);
template.insert("person", p);
assertNotNull(p.getId());

View File

@@ -52,13 +52,6 @@ public class Person<T extends Address> {
@Autowired
private MongoTemplate mongoTemplate;
public Person(Integer ssn, String firstName, String lastName, Integer age) {
this.ssn = ssn;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@PersistenceConstructor
public Person(Integer ssn, String firstName, String lastName, Integer age, T address) {
this.ssn = ssn;
@@ -80,6 +73,10 @@ public class Person<T extends Address> {
return ssn;
}
public void setSsn(Integer ssn) {
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}

View File

@@ -22,18 +22,13 @@ import org.springframework.data.annotation.Id;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public class PersonCustomIdName {
public class PersonCustomIdName extends BasePerson {
@Id
private ObjectId customId;
private Integer ssn;
private String firstName;
private String lastName;
public PersonCustomIdName(Integer ssn, String firstName, String lastName) {
this.ssn = ssn;
this.firstName = firstName;
this.lastName = lastName;
super(ssn, firstName, lastName);
}
public ObjectId getCustomId() {
@@ -44,27 +39,4 @@ public class PersonCustomIdName {
this.customId = customId;
}
public Integer getSsn() {
return ssn;
}
public void setSsn(Integer ssn) {
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@@ -21,17 +21,12 @@ import org.bson.types.ObjectId;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public class PersonPojo {
public class PersonPojo extends BasePerson {
private ObjectId id;
private Integer ssn;
private String firstName;
private String lastName;
public PersonPojo(Integer ssn, String firstName, String lastName) {
this.ssn = ssn;
this.firstName = firstName;
this.lastName = lastName;
super(ssn, firstName, lastName);
}
public ObjectId getId() {
@@ -41,28 +36,4 @@ public class PersonPojo {
public void setId(ObjectId id) {
this.id = id;
}
public Integer getSsn() {
return ssn;
}
public void setSsn(Integer ssn) {
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}