/** * Create PropertyMatches for the given field property. * @param propertyName the name of the field to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forField(String propertyName, Class<?> beanClass) { return forField(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
/** * Create PropertyMatches for the given field property. * @param propertyName the name of the field to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forField(String propertyName, Class<?> beanClass) { return forField(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
@Test public void simpleFieldPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forField("naem", SampleFieldProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("naem")); assertThat(msg, containsString("name")); assertThat(msg, containsString("field")); assertThat(msg, not(containsString("setter"))); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forField(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
@Test public void complexFieldPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forField("counter", SampleFieldProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("counter")); assertThat(msg, containsString("counter1")); assertThat(msg, containsString("counter2")); assertThat(msg, containsString("counter3")); }
@Test public void complexFieldPropertyTypo() { PropertyMatches matches = PropertyMatches.forField("desriptn", SampleFieldProperties.class); assertThat(matches.getPossibleMatches(), emptyArray()); }
@Test public void simpleFieldPropertyTypo() { PropertyMatches matches = PropertyMatches.forField("naem", SampleFieldProperties.class); assertThat(matches.getPossibleMatches(), hasItemInArray("name")); }
@Test public void unknownFieldProperty() { PropertyMatches matches = PropertyMatches.forField("unknown", SampleFieldProperties.class); assertThat(matches.getPossibleMatches(), emptyArray()); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forField(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
@Test public void severalMatchesFieldProperty() { PropertyMatches matches = PropertyMatches.forField("counter", SampleFieldProperties.class); assertThat(matches.getPossibleMatches(), hasItemInArray("counter1")); assertThat(matches.getPossibleMatches(), hasItemInArray("counter2")); assertThat(matches.getPossibleMatches(), hasItemInArray("counter3")); }
/** * Create PropertyMatches for the given field property. * @param propertyName the name of the field to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forField(String propertyName, Class<?> beanClass) { return forField(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
private static Set<String> detectPotentialMatches(String propertyName, Class<?> type) { Set<String> result = new HashSet<>(); result.addAll(Arrays.asList(PropertyMatches.forField(propertyName, type).getPossibleMatches())); result.addAll(Arrays.asList(PropertyMatches.forProperty(propertyName, type).getPossibleMatches())); return result; } }
/** * 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 Set<String> detectPotentialMatches(String propertyName, Class<?> type) { Set<String> result = new HashSet<>(); result.addAll(Arrays.asList(PropertyMatches.forField(propertyName, type).getPossibleMatches())); result.addAll(Arrays.asList(PropertyMatches.forProperty(propertyName, type).getPossibleMatches())); return result; } }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forField(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }