@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 } }
/** * 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")); }
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; } }
@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; } }
@Override public Object[] extract(BaseData item) { Object[] values = new Object[names.length]; Term extensionTerm = termFactory.findTerm(extension); Map<Term,String> propertyMap = DarwinCorePropertyMap.getPropertyMap(extensionTerm); BeanWrapper beanWrapper = new BeanWrapperImpl(item); for(int i = 0; i < names.length; i++) { String property = names[i]; Term propertyTerm = termFactory.findTerm(property); String propertyName = propertyMap.get(propertyTerm); try { String value = conversionService.convert(beanWrapper.getPropertyValue(propertyName), String.class); if(quoteCharacter == null) { values[i] = value; } else if(value != null) { values[i] = new StringBuilder().append(quoteCharacter).append(value).append(quoteCharacter).toString(); } else { values[i] = new StringBuilder().append(quoteCharacter).append(quoteCharacter).toString(); } } catch(PropertyAccessException pae) { if(quoteCharacter != null) { values[i] = new StringBuilder().append(quoteCharacter).append(quoteCharacter).toString(); } } catch(NullValueInNestedPathException nvinpe) { if(quoteCharacter != null) { values[i] = new StringBuilder().append(quoteCharacter).append(quoteCharacter).toString(); } } } return values; }
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 void testChildPropertyAccessStrategy() { final TestBean nestedProperty = new TestBean(); testBean.setNestedProperty(nestedProperty); MutablePropertyAccessStrategy cpas = pas.getPropertyAccessStrategyForPath("nestedProperty"); assertEquals("Child domainObjectHolder should equal equivalent parent ValueModel", pas.getPropertyValueModel("nestedProperty"), cpas.getDomainObjectHolder()); vm = cpas.getPropertyValueModel("simpleProperty"); assertEquals("Child should return the same ValueModel as parent", pas.getPropertyValueModel("nestedProperty.simpleProperty"), vm); Block setValueDirectly = new Block() { public void handle(Object newValue) { nestedProperty.setSimpleProperty((String)newValue); } }; Closure getValueDirectly = new Closure() { public Object call(Object ignore) { return nestedProperty.getSimpleProperty(); } }; Object[] valuesToTest = new Object[] {"1", "2", null, "3"}; testSettingAndGetting(valuesToTest, getValueDirectly, setValueDirectly); try { pas.getPropertyValueModel("nestedProperty").setValue(null); if (isStrictNullHandlingEnabled()) fail("Should have thrown a NullValueInNestedPathException"); } catch (NullValueInNestedPathException e) { if (!isStrictNullHandlingEnabled()) fail("Should not have thrown a NullValueInNestedPathException"); } }
public void testMapProperty() { final Map map = new HashMap(); testBean.setMapProperty(map); vm = pas.getPropertyValueModel("mapProperty[.key]"); Block setValueDirectly = new Block() { public void handle(Object newValue) { map.put(".key", newValue); } }; Closure getValueDirectly = new Closure() { public Object call(Object ignore) { return map.get(".key"); } }; Object[] valuesToTest = new Object[] {"1", "2", null, "3"}; testSettingAndGetting(valuesToTest, getValueDirectly, setValueDirectly); try { pas.getPropertyValueModel("mapProperty").setValue(null); if (isStrictNullHandlingEnabled()) fail("Should have thrown a NullValueInNestedPathException"); } catch (NullValueInNestedPathException e) { if (!isStrictNullHandlingEnabled()) fail("Should not have thrown a NullValueInNestedPathException"); } }
public Object getIndexedPropertyValue(String propertyName) throws BeansException { if (getPropertyType(propertyName) == null) { throw new NotReadablePropertyException(getTargetClass(), propertyName, "property type could not be determined"); } String rootPropertyName = getRootPropertyName(propertyName); Member readAccessor = getReadPropertyAccessor(rootPropertyName); if (readAccessor == null) { throw new NotReadablePropertyException(getTargetClass(), propertyName, "Neither non-static field nor get-method exists for indexed property"); } Object rootProperty = getPropertyValue(rootPropertyName); if (rootProperty == null) { if (isStrictNullHandlingEnabled()) { throw new NullValueInNestedPathException(getTargetClass(), propertyName); } else if (isWritableProperty(rootPropertyName)) { return null; } else { throw new NotReadablePropertyException(getTargetClass(), propertyName); } } Object[] indices; try { indices = getIndices(propertyName); } catch (Exception e) { // could not convert indices throw createNotReadablePropertyException(propertyName, e); } return getPropertyValue(rootProperty, indices); }
private Object getPropertyValue(Object assemblage, Object[] indices, int parameterIndex) { if (assemblage == null) { if (isStrictNullHandlingEnabled()) { throw new NullValueInNestedPathException(getTargetClass(), ""); } else { return null; } } Object value = null; if (assemblage.getClass().isArray()) { value = getArrayValue(assemblage, (Integer) indices[parameterIndex]); } else if (assemblage instanceof List) { value = getListValue((List) assemblage, (Integer) indices[parameterIndex]); } else if (assemblage instanceof Map) { value = getMapValue((Map) assemblage, indices[parameterIndex]); } else if (assemblage instanceof Collection) { value = getCollectionValue((Collection) assemblage, (Integer) indices[parameterIndex]); } else { throw new IllegalStateException( "getPropertyValue(Object, Object[], int) called with neither array nor collection nor map"); } if (parameterIndex == indices.length - 1) { return value; } if (value == null) { if (isStrictNullHandlingEnabled()) { throw new InvalidPropertyException(getTargetClass(), "", ""); } else { return null; } } return getPropertyValue(value, indices, parameterIndex + 1); }
public Object getPropertyValue(String propertyPath) { if (PropertyAccessorUtils.isNestedProperty(propertyPath)) { String baseProperty = getBasePropertyName(propertyPath); String childPropertyPath = getChildPropertyPath(propertyPath); return ((PropertyAccessor) childPropertyAccessors.get(baseProperty)).getPropertyValue(childPropertyPath); } else if (isStrictNullHandlingEnabled() && getTarget() == null) { throw new NullValueInNestedPathException(getTargetClass(), propertyPath); } else { return super.getPropertyValue(propertyPath); } }
public void setPropertyValue(String propertyPath, Object value) { if (PropertyAccessorUtils.isNestedProperty(propertyPath)) { String baseProperty = getBasePropertyName(propertyPath); String childPropertyPath = getChildPropertyPath(propertyPath); ((PropertyAccessor) childPropertyAccessors.get(baseProperty)).setPropertyValue(childPropertyPath, value); } else if (isStrictNullHandlingEnabled() && getTarget() == null) { throw new NullValueInNestedPathException(getTargetClass(), propertyPath); } else { super.setPropertyValue(propertyPath, value); } }
@Override public Object getPropertyValue(String propertyName) throws BeansException { registerEditorFromView(propertyName); Object value = null; try { value = super.getPropertyValue(propertyName); } catch (NullValueInNestedPathException e) { // swallow null values in path and return null as the value } return value; }