DATACMNS-866 - Improved error message in invalid invocations of BasicPersistentEntity.getPropertyAccessor(…).

We now explicitly report the given object's type and the one expected in case of a mismatch when BasicPersistentEntity.getPropertyAccessor(…) is invoked.
This commit is contained in:
Oliver Gierke
2016-06-08 16:04:06 +02:00
parent c15c2f9f83
commit 2a59e22699
2 changed files with 19 additions and 1 deletions

View File

@@ -54,6 +54,8 @@ import org.springframework.util.StringUtils;
*/
public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implements MutablePersistentEntity<T, P> {
private static final String TYPE_MISMATCH = "Target bean of type %s is not of type of the persistent entity (%s)!";
private final PreferredConstructor<T, P> constructor;
private final TypeInformation<T> information;
private final List<P> properties;
@@ -391,7 +393,9 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
public PersistentPropertyAccessor getPropertyAccessor(Object bean) {
Assert.notNull(bean, "Target bean must not be null!");
Assert.isTrue(getType().isInstance(bean), "Target bean is not of type of the persistent entity!");
Assert.isTrue(getType().isInstance(bean),
String.format(TYPE_MISMATCH, bean.getClass().getName(), getType().getName()));
return new BeanWrapper<Object>(bean);
}

View File

@@ -238,6 +238,20 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
assertThat(entity.getTypeAlias(), is((Object) "bar"));
}
/**
* @see DATACMNS-866
*/
@Test
public void invalidBeanAccessCreatesDescriptiveErrorMessage() {
PersistentEntity<Entity, T> entity = createEntity(Entity.class);
exception.expectMessage(Entity.class.getName());
exception.expectMessage(Object.class.getName());
entity.getPropertyAccessor(new Object());
}
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
return createEntity(type, null);
}