DATADOC-134 - Added test case to show indexes with unique flag work.

This commit is contained in:
Oliver Gierke
2011-07-08 10:23:45 +02:00
parent 2d5f41f65c
commit a9670de959
4 changed files with 47 additions and 14 deletions

View File

@@ -12,6 +12,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.document.mongodb.geo.Box;
import org.springframework.data.document.mongodb.geo.Circle;
import org.springframework.data.document.mongodb.geo.Point;
@@ -267,4 +268,15 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
public void existsWorksCorrectly() {
assertThat(repository.exists(dave.getId()), is(true));
}
@Test(expected = DuplicateKeyException.class)
public void rejectsDuplicateEmailAddressOnSave() {
assertThat(dave.getEmail(), is("dave@dmband.com"));
Person daveSyer = new Person("Dave", "Syer");
assertThat(daveSyer.getEmail(), is("dave@dmband.com"));
repository.save(daveSyer);
}
}

View File

@@ -19,6 +19,7 @@ import java.util.Set;
import org.springframework.data.document.mongodb.geo.Point;
import org.springframework.data.document.mongodb.index.GeoSpatialIndexed;
import org.springframework.data.document.mongodb.index.Indexed;
import org.springframework.data.document.mongodb.mapping.Document;
/**
@@ -35,6 +36,8 @@ public class Person extends Contact {
private String firstname;
private String lastname;
@Indexed(unique = true, dropDups = true)
private String email;
private Integer age;
private Sex sex;
@@ -66,6 +69,7 @@ public class Person extends Contact {
this.lastname = lastname;
this.age = age;
this.sex = sex;
this.email = (firstname == null ? "noone" : firstname.toLowerCase()) + "@dmband.com";
}
/**
@@ -102,6 +106,20 @@ public class Person extends Contact {
this.lastname = lastname;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the age
*/

View File

@@ -7,12 +7,13 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<mongo:db-factory dbname="repositories"/>
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.repository"/>
<mongo:db-factory dbname="repositories" />
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg ref="mongoDbFactory"/>
<constructor-arg ref="mappingConverter"/>
<constructor-arg ref="mongoDbFactory" />
<property name="writeConcern">
<util:constant static-field="com.mongodb.WriteConcern.SAFE" />
</property>
</bean>
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">

View File

@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.0.xsd">
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<mongo:db-factory dbname="repositories"/>
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter">
<mongo:mapping-converter/>
</constructor-arg>
<property name="writeConcern">
<util:constant static-field="com.mongodb.WriteConcern.SAFE" />
</property>
</bean>
<mongo:repositories base-package="org.springframework.data.**.repository">