/** * Create PropertyMatches for the given bean property. * @param propertyName the name of the property to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forProperty(String propertyName, Class<?> beanClass) { return forProperty(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
/** * Create PropertyMatches for the given bean property. * @param propertyName the name of the property to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forProperty(String propertyName, Class<?> beanClass) { return forProperty(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
@Test public void simpleBeanPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forProperty("naem", SampleBeanProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("naem")); assertThat(msg, containsString("name")); assertThat(msg, containsString("setter")); assertThat(msg, not(containsString("field"))); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException(getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
@Test public void complexBeanPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forProperty("counter", SampleBeanProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("counter")); assertThat(msg, containsString("counter1")); assertThat(msg, containsString("counter2")); assertThat(msg, containsString("counter3")); }
@Test public void simpleBeanPropertyTypo() { PropertyMatches matches = PropertyMatches.forProperty("naem", SampleBeanProperties.class); assertThat(matches.getPossibleMatches(), hasItemInArray("name")); }
@Test public void unknownBeanProperty() { PropertyMatches matches = PropertyMatches.forProperty("unknown", SampleBeanProperties.class); assertThat(matches.getPossibleMatches(), emptyArray()); }
@Test public void complexBeanPropertyTypo() { PropertyMatches matches = PropertyMatches.forProperty("desriptn", SampleBeanProperties.class); assertThat(matches.getPossibleMatches(), emptyArray()); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException(getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
/** * Create PropertyMatches for the given bean property. * @param propertyName the name of the property to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forProperty(String propertyName, Class beanClass) { return forProperty(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
@Test public void severalMatchesBeanProperty() { PropertyMatches matches = PropertyMatches.forProperty("counter", SampleBeanProperties.class); assertThat(matches.getPossibleMatches(), hasItemInArray("counter1")); assertThat(matches.getPossibleMatches(), hasItemInArray("counter2")); assertThat(matches.getPossibleMatches(), hasItemInArray("counter3")); }
PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), this.nestedPath + propertyName,
/** * Create PropertyMatches for the given bean property. * @param propertyName the name of the property to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forProperty(String propertyName, Class<?> beanClass) { return forProperty(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.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException(getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }