DATACMNS-727 - Improved error handling in MappingContext.getPersistentPropertyPath(…).

A failure to resolve a property path into a PersistentPropertyPath now causes an InvalidPropertyPath exception being thrown. It captures the context of the failure like the resolved path, the offending segment. Added getPersistentPropertyPath(InvalidPropertyPath) that resolves the resolvable part for further use downstream.

Added null-checking assertions to AbstractMappingContext.getPersistentPropertyPath(…) variants. Improved DefaultPersistentPropertyPath to be able to append properties while building and to allow representing an empty path.
This commit is contained in:
Oliver Gierke
2015-07-09 13:23:43 +02:00
parent d859540f92
commit 709bb5c4b1
6 changed files with 215 additions and 16 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.mapping.context;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import groovy.lang.MetaClass;
import java.util.Collections;
@@ -59,8 +60,8 @@ public class AbstractMappingContextUnitTests {
@Test
public void doesNotTryToLookupPersistentEntityForLeafProperty() {
PersistentPropertyPath<SamplePersistentProperty> path = context.getPersistentPropertyPath(PropertyPath.from("name",
Person.class));
PersistentPropertyPath<SamplePersistentProperty> path = context
.getPersistentPropertyPath(PropertyPath.from("name", Person.class));
assertThat(path, is(notNullValue()));
}
@@ -255,6 +256,28 @@ public class AbstractMappingContextUnitTests {
is(Matchers.<SamplePersistentProperty> iterableWithSize(3)));
}
/**
* @see DATACMNS-727
*/
@Test
public void exposesContextForFailingPropertyPathLookup() {
try {
context.getPersistentPropertyPath("persons.firstname", Sample.class);
fail("Expected InvalidPersistentPropertyPath!");
} catch (InvalidPersistentPropertyPath o_O) {
assertThat(o_O.getMessage(), not(isEmptyOrNullString()));
assertThat(o_O.getResolvedPath(), is("persons"));
assertThat(o_O.getUnresolvableSegment(), is("firstname"));
// Make sure, the resolvable part can be obtained
assertThat(context.getPersistentPropertyPath(o_O), is(notNullValue()));
}
}
private static void assertHasEntityFor(Class<?> type, SampleMappingContext context, boolean expected) {
boolean found = false;