下面列出了怎么用org.springframework.beans.NullValueInNestedPathException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testBindingNoErrorsWithInvalidField() {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "Rod");
pvs.add("spouse.age", 32);
try {
binder.bind(pvs);
fail("Should have thrown NullValueInNestedPathException");
}
catch (NullValueInNestedPathException ex) {
// expected
}
}
@Test
public void testBindingNoErrorsWithInvalidField() {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "Rod");
pvs.add("spouse.age", 32);
try {
binder.bind(pvs);
fail("Should have thrown NullValueInNestedPathException");
}
catch (NullValueInNestedPathException ex) {
// expected
}
}
@Test
public void testBindingNoErrorsWithInvalidField() throws Exception {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "Rod");
pvs.add("spouse.age", 32);
try {
binder.bind(pvs);
fail("Should have thrown NullValueInNestedPathException");
}
catch (NullValueInNestedPathException ex) {
// expected
}
}
public static <T> T copyFieldValues(final List<String> fieldsToCopy, final T source, final Class<T> typeOfT) {
try {
final BeanWrapper src = new BeanWrapperImpl(source);
final BeanWrapper target = new BeanWrapperImpl(typeOfT.newInstance());
fieldsToCopy.forEach(t -> {
try {
Object propertyValue = src.getPropertyValue(t);
if (propertyValue instanceof String) {
propertyValue = ((String) propertyValue).trim();
}
target.setPropertyValue(t, propertyValue);
} catch (final NullValueInNestedPathException ignore) {
}
});
return (T) target.getWrappedInstance();
} catch (InstantiationException | IllegalAccessException e) {
return null;
}
}
/**
* Returns the value for the given property growing nested paths depending on the parameter.
*
* @param propertyName name of the property to get value for
* @param autoGrowNestedPaths whether nested paths should be grown (initialized if null)
* @return value for property
*/
protected Object getPropertyValue(String propertyName, boolean autoGrowNestedPaths) {
setAutoGrowNestedPaths(autoGrowNestedPaths);
Object value = null;
try {
value = super.getPropertyValue(propertyName);
} catch (NullValueInNestedPathException e) {
// swallow null values in path and return null as the value
} catch (InvalidPropertyException e1) {
if (!(e1.getRootCause() instanceof NullValueInNestedPathException)) {
throw e1;
}
}
return value;
}
@Test
public void testGetPropertyValueNullSafe() {
DataObject dataObject = new DataObject("a", "b", 3, "one");
DataObjectWrapper<DataObject> wrap = new DataObjectWrapperImpl<DataObject>(dataObject, dataObjectMetadata,
dataObjectService,
referenceLinker);
assertNull(wrap.getPropertyValue("dataObject2"));
//wrap.setPropertyValue("dataObject2.dataObject3", new DataObject3());
// assert that a NullValueInNestedPathException is thrown
try {
wrap.getPropertyValue("dataObject2.dataObject3");
fail("NullValueInNestedPathException should have been thrown");
} catch (NullValueInNestedPathException e) {
// this should be thrown!
}
// now do a null-safe check
assertNull(wrap.getPropertyValueNullSafe("dataObject2.dataObject3"));
}
@Test
public void nestedBindingWithDisabledAutoGrow() throws Exception {
FieldAccessBean rod = new FieldAccessBean();
DataBinder binder = new DataBinder(rod, "person");
binder.setAutoGrowNestedPaths(false);
binder.initDirectFieldAccess();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
assertThatExceptionOfType(NullValueInNestedPathException.class).isThrownBy(() ->
binder.bind(pvs));
}
@Test
public void nestedBindingWithDisabledAutoGrow() throws Exception {
FieldAccessBean rod = new FieldAccessBean();
DataBinder binder = new DataBinder(rod, "person");
binder.setAutoGrowNestedPaths(false);
binder.initDirectFieldAccess();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
thrown.expect(NullValueInNestedPathException.class);
binder.bind(pvs);
}
@Test
public void nestedBindingWithDisabledAutoGrow() throws Exception {
FieldAccessBean rod = new FieldAccessBean();
DataBinder binder = new DataBinder(rod, "person");
binder.setAutoGrowNestedPaths(false);
binder.initDirectFieldAccess();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
thrown.expect(NullValueInNestedPathException.class);
binder.bind(pvs);
}
/**
* Checks whether the given property is secure.
*
* @param wrappedClass class the property is associated with
* @param propertyPath path to the property
* @return boolean true if the property is secure, false if not
*/
protected boolean isSecure(Class<?> wrappedClass, String propertyPath) {
if (KRADUtils.isSecure(propertyPath, wrappedClass)) {
return true;
}
// since this is part of a set, we want to make sure nested paths grow
setAutoGrowNestedPaths(true);
BeanWrapperImpl beanWrapper;
try {
beanWrapper = getBeanWrapperForPropertyPath(propertyPath);
} catch (NotReadablePropertyException | NullValueInNestedPathException e) {
LOG.debug("Bean wrapper was not found for " + propertyPath
+ ", but since it cannot be accessed it will not be set as secure.", e);
return false;
}
if (org.apache.commons.lang.StringUtils.isNotBlank(beanWrapper.getNestedPath())) {
PropertyTokenHolder tokens = getPropertyNameTokens(propertyPath);
String nestedPropertyPath = org.apache.commons.lang.StringUtils.removeStart(tokens.canonicalName,
beanWrapper.getNestedPath());
return isSecure(beanWrapper.getWrappedClass(), nestedPropertyPath);
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public Object getPropertyValueNullSafe(String propertyName) throws BeansException {
try {
return getPropertyValue(propertyName);
} catch (NullValueInNestedPathException e) {
return null;
}
}