@Test public void testBindingNoErrorsNotIgnoreUnknown() throws Exception { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("age", 32); pvs.add("nonExisting", "someValue"); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
@Test public void testBindingWithSystemFieldError() throws Exception { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("class.classLoader.URLs[0]", "http://myserver"); binder.setIgnoreUnknownFields(false); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { assertTrue(ex.getMessage().contains("classLoader")); } }
@Test public void bindingNoErrorsNotIgnoreUnknown() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.initDirectFieldAccess(); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); pvs.addPropertyValue(new PropertyValue("age", new Integer(32))); pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue")); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
@Test public void testPossibleMatches() { try { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("ag", "foobar"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("tb", bd); lbf.getBean("tb"); fail("Should throw exception on invalid property"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof NotWritablePropertyException); NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); // expected assertEquals(1, cause.getPossibleMatches().length); assertEquals("age", cause.getPossibleMatches()[0]); } }
@Override public void setPropertyValue(PropertyValue pv) throws BeansException { try { super.setPropertyValue(pv); } catch (NotWritablePropertyException ex) { PropertyOrigin origin = OriginCapablePropertyValue.getOrigin(pv); if (isBenign(origin)) { logger.debug("Ignoring benign property binding failure", ex); return; } if (origin == null) { throw ex; } throw new RelaxedBindingNotWritablePropertyException(ex, origin); } }
public void testBindingNoErrorsNotIgnoreUnknown() throws Exception { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("age", 32); pvs.add("nonExisting", "someValue"); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
public void testBindingNoErrorsNotIgnoreUnknown() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.initDirectFieldAccess(); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); pvs.addPropertyValue(new PropertyValue("age", new Integer(32))); pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue")); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
RelaxedBindingNotWritablePropertyException(NotWritablePropertyException ex, PropertyOrigin propertyOrigin) { super(ex.getBeanClass(), ex.getPropertyName()); this.propertyOrigin = propertyOrigin; this.message = "Failed to bind '" + propertyOrigin.getName() + "' from '" + propertyOrigin.getSource().getName() + "' to '" + ex.getPropertyName() + "' property on '" + ex.getBeanClass().getName() + "'"; }
@Test(expected = NotWritablePropertyException.class) public void testBindNestedReadOnlyListCommaSeparated() throws Exception { TargetWithReadOnlyNestedList target = new TargetWithReadOnlyNestedList(); this.conversionService = new DefaultConversionService(); bind(target, "nested: bar,foo"); assertThat(target.getNested().toString()).isEqualTo("[bar, foo]"); }
@Test(expected = NotWritablePropertyException.class) public void testBindNestedReadOnlyListCommaSeparated() throws Exception { TargetWithReadOnlyNestedList target = new TargetWithReadOnlyNestedList(); this.conversionService = new DefaultConversionService(); bind(target, "nested: bar,foo"); assertEquals("[bar, foo]", target.getNested().toString()); }
public void testBindingWithSystemFieldError() throws Exception { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("class.classLoader.URLs[0]", "http://myserver"); binder.setIgnoreUnknownFields(false); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { assertTrue(ex.getMessage().contains("classLoader")); } }
public void testReadOnlyProperty() { vm = pas.getPropertyValueModel("readOnly"); testBean.readOnly = "1"; assertEquals(testBean.readOnly, vm.getValue()); try { vm.setValue("2"); fail("should have thrown NotWritablePropertyException"); } catch(NotWritablePropertyException e) { // expected } }
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData */ @Override public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase()); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (logger.isDebugEnabled() && rowNumber == 0) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type " + pd.getPropertyType()); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException e) { if (value == null && primitivesDefaultedForNullValue) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + pd.getName(), ex); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; }
@Test(expected = NotWritablePropertyException.class) public void testUnknownPropertyCausesLoadFailure() throws Exception { this.ignoreUnknownFields = false; createFoo("hi: hello\nname: foo\nbar: blah"); }
@Override public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); String dbFieldName = column.replaceAll(" ", ""); FieldMapper fieldMapper = this.tableMapper.getFieldMapper(dbFieldName); PropertyDescriptor pd; if (fieldMapper != null && (pd=fieldMapper.getPropertyDescriptor()) != null ) { try { Object value = getColumnValue(rs, index, pd); if (logger.isDebugEnabled() && rowNumber == 0) { logger.debug("Mapping column '" + column + "' to property '" +pd.getName() + "' of type " + pd.getPropertyType()); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException e) { if (value == null) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException("Unable to map column " + column + " to property " + pd.getName(), ex); } } } return mappedObject; }
@Test(expected = NotWritablePropertyException.class) public void genericBindingTestWithNotExistingProperty() { final int dontExistValue = Integer.MAX_VALUE; camundaBpmProperties.getGenericProperties().getProperties().put("dont-exist", dontExistValue); genericPropertiesConfiguration.preInit(processEngineConfiguration); }
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData */ public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase()); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (logger.isDebugEnabled() && rowNumber == 0) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type " + pd.getPropertyType()); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException e) { if (value == null && primitivesDefaultedForNullValue) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + pd.getName(), ex); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; }
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData */ @SuppressWarnings("rawtypes") public Object mapRow2SubObj(ResultSet rs, int rowNumber,Class subClz) throws SQLException { Assert.state(subClz != null, "Mapped class was not specified"); Object mappedObject = BeanUtils.instantiate(subClz); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); PropertyDescriptor pd = this.subMappedFields.get(column.replaceAll(" ", "").toLowerCase()); Class clz = this.subMappedFieldsSuperClz.get(column.replaceAll(" ", "").toLowerCase()); if (pd != null&&clz==subClz) { try { Object value = getColumnValue(rs, index, pd); if (logger.isDebugEnabled() && rowNumber == 0) { //logger.debug("Mapping column '" + column + "' to property '" + // pd.getName() + "' of type " + pd.getPropertyType()); } try { bw.setPropertyValue(pd.getName(), value); }catch (TypeMismatchException e) { if (value == null && primitivesDefaultedForNullValue) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + pd.getName(), ex); } } } if (populatedProperties != null && !populatedProperties.equals(this.subMappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + subClz+ "]: " + this.subMappedProperties); } return mappedObject; }