Browse Source

DATACMNS-703 - PropertyReferenceException now exposes potential matches.

We now use Spring's PropertyMatches to build up the exception message and include a list of potentially meant properties.

Removed unused config file for tests along the way.

Original pull request: #125.
pull/131/head
Oliver Gierke 11 years ago
parent
commit
d859540f92
  1. 41
      src/main/java/org/springframework/data/mapping/PropertyReferenceException.java
  2. 22
      src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java
  3. 18
      src/test/resources/org/springframework/data/repository/config/populators.xml

41
src/main/java/org/springframework/data/mapping/PropertyReferenceException.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,10 +15,14 @@ @@ -15,10 +15,14 @@
*/
package org.springframework.data.mapping;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.PropertyMatches;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Exception being thrown when creating {@link PropertyPath} instances.
@ -29,10 +33,12 @@ public class PropertyReferenceException extends RuntimeException { @@ -29,10 +33,12 @@ public class PropertyReferenceException extends RuntimeException {
private static final long serialVersionUID = -5254424051438976570L;
private static final String ERROR_TEMPLATE = "No property %s found for type %s!";
private static final String HINTS_TEMPLATE = " Did you mean %s?";
private final String propertyName;
private final TypeInformation<?> type;
private final List<PropertyPath> alreadyResolvedPath;
private final List<String> propertyMatches;
/**
* Creates a new {@link PropertyReferenceException}.
@ -41,7 +47,8 @@ public class PropertyReferenceException extends RuntimeException { @@ -41,7 +47,8 @@ public class PropertyReferenceException extends RuntimeException {
* @param type the type the property could not be found on.
* @param alreadyResolvedPah the previously calculated {@link PropertyPath}s.
*/
public PropertyReferenceException(String propertyName, TypeInformation<?> type, List<PropertyPath> alreadyResolvedPah) {
public PropertyReferenceException(String propertyName, TypeInformation<?> type,
List<PropertyPath> alreadyResolvedPah) {
Assert.hasText(propertyName);
Assert.notNull(type);
@ -49,6 +56,7 @@ public class PropertyReferenceException extends RuntimeException { @@ -49,6 +56,7 @@ public class PropertyReferenceException extends RuntimeException {
this.propertyName = propertyName;
this.type = type;
this.alreadyResolvedPath = alreadyResolvedPah;
this.propertyMatches = detectPotentialMatches(propertyName, type.getType());
}
/**
@ -76,11 +84,18 @@ public class PropertyReferenceException extends RuntimeException { @@ -76,11 +84,18 @@ public class PropertyReferenceException extends RuntimeException {
@Override
public String getMessage() {
StringBuilder builder = new StringBuilder(String.format(ERROR_TEMPLATE, propertyName, type.getType()
.getSimpleName()));
StringBuilder builder = new StringBuilder(
String.format(ERROR_TEMPLATE, propertyName, type.getType().getSimpleName()));
if (!propertyMatches.isEmpty()) {
String matches = StringUtils.collectionToDelimitedString(propertyMatches, ",", "'", "'");
builder.append(String.format(HINTS_TEMPLATE, matches));
}
if (!alreadyResolvedPath.isEmpty()) {
builder.append(" Traversed path: ").append(alreadyResolvedPath.get(0).toString()).append(".");
builder.append(" Traversed path: ");
builder.append(alreadyResolvedPath.get(0).toString());
builder.append(".");
}
return builder.toString();
@ -105,4 +120,20 @@ public class PropertyReferenceException extends RuntimeException { @@ -105,4 +120,20 @@ public class PropertyReferenceException extends RuntimeException {
public boolean hasDeeperResolutionDepthThan(PropertyReferenceException exception) {
return this.alreadyResolvedPath.size() > exception.alreadyResolvedPath.size();
}
/**
* Detects all potential matches for the given property name and type.
*
* @param propertyName must not be {@literal null} or empty.
* @param type must not be {@literal null}.
* @return
*/
private static List<String> detectPotentialMatches(String propertyName, Class<?> type) {
List<String> result = new ArrayList<String>();
result.addAll(Arrays.asList(PropertyMatches.forField(propertyName, type).getPossibleMatches()));
result.addAll(Arrays.asList(PropertyMatches.forProperty(propertyName, type).getPossibleMatches()));
return result;
}
}

22
src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -353,6 +353,9 @@ public class PropertyPathUnitTests { @@ -353,6 +353,9 @@ public class PropertyPathUnitTests {
from("foo", (TypeInformation<?>) null);
}
/**
* @see DATACMNS-546
*/
@Test
public void returnsCompletePathIfResolutionFailedCompletely() {
@ -362,8 +365,11 @@ public class PropertyPathUnitTests { @@ -362,8 +365,11 @@ public class PropertyPathUnitTests {
from("somethingDifferent", Foo.class);
}
/**
* @see DATACMNS-546
*/
@Test
public void foobar() {
public void includesResolvedPathInExceptionMessage() {
exception.expect(PropertyReferenceException.class);
exception.expectMessage("fooName");
@ -373,6 +379,18 @@ public class PropertyPathUnitTests { @@ -373,6 +379,18 @@ public class PropertyPathUnitTests {
from("userFooName", Bar.class);
}
/**
* @see DATACMNS-703
*/
@Test
public void includesPropertyHintsOnTypos() {
exception.expect(PropertyReferenceException.class);
exception.expectMessage("userName");
from("userAme", Foo.class);
}
private class Foo {
String userName;

18
src/test/resources/org/springframework/data/repository/config/populators.xml

@ -1,18 +0,0 @@ @@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<repository:jackson2-populator id="jackson2-populator"
locations="classpath:org/springframework/data/repository/init/data.json" />
<repository:unmarshaller-populator
id="xml-populator" locations="classpath:org/springframework/data/repository/init/data.xml"
unmarshaller-ref="unmarshaller" />
<oxm:jaxb2-marshaller id="unmarshaller" contextPath="org.springframework.data.repository.config" />
</beans>
Loading…
Cancel
Save