DATACMNS-186 - Improved the way id properties are detected.

The id properties are now discovered by the BasicPersistentEntity during the call to addPersistentProperty(…). Beyond that the method rejects the id property if one was already set.
This commit is contained in:
Oliver Gierke
2012-06-22 14:17:38 +02:00
parent f540306774
commit ebf8fdcbc4
6 changed files with 86 additions and 31 deletions

View File

@@ -9,7 +9,10 @@ import java.util.Iterator;
import java.util.SortedSet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentEntitySpec;
@@ -23,8 +26,12 @@ import org.springframework.test.util.ReflectionTestUtils;
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
@Mock
T property;
@Test
public void assertInvariants() {
PersistentEntitySpec.assertInvariants(createEntity(null));
@@ -90,6 +97,40 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
assertThat(iterator.next(), is(entity.getPersistentProperty("ssn")));
}
/**
* @see DATACMNS-186
*/
@Test
public void addingAndIdPropertySetsIdPropertyInternally() {
MutablePersistentEntity<Person, T> entity = createEntity(null);
assertThat(entity.getIdProperty(), is(nullValue()));
when(property.isIdProperty()).thenReturn(true);
entity.addPersistentProperty(property);
assertThat(entity.getIdProperty(), is(property));
}
/**
* @see DATACMNS-186
*/
@Test
public void rejectsIdPropertyIfAlreadySet() {
MutablePersistentEntity<Person, T> entity = createEntity(null);
when(property.isIdProperty()).thenReturn(true);
entity.addPersistentProperty(property);
try {
entity.addPersistentProperty(property);
fail("Expected MappingException!");
} catch (MappingException e) {
// expected
}
}
private BasicPersistentEntity<Person, T> createEntity(Comparator<T> comparator) {
return new BasicPersistentEntity<Person, T>(ClassTypeInformation.from(Person.class), comparator);
}