Java 类org.springframework.beans.NotWritablePropertyException 实例源码

项目:spring4-understanding    文件:DataBinderTests.java   
@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
    }
}
项目:spring4-understanding    文件:DataBinderTests.java   
@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"));
    }
}
项目:spring4-understanding    文件:DataBinderFieldAccessTests.java   
@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
    }
}
项目:spring4-understanding    文件:DefaultListableBeanFactoryTests.java   
@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]);
    }
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:RelaxedDataBinder.java   
@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);
    }
}
项目:spring-boot-concourse    文件:RelaxedDataBinder.java   
@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);
    }
}
项目:contestparser    文件:RelaxedDataBinder.java   
@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);
    }
}
项目:class-guard    文件:DataBinderTests.java   
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
    }
}
项目:class-guard    文件:DataBinderFieldAccessTests.java   
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
    }
}
项目:class-guard    文件:DefaultListableBeanFactoryTests.java   
@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]);
    }
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:RelaxedBindingNotWritablePropertyException.java   
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() + "'";
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:RelaxedDataBinderTests.java   
@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]");
}
项目:spring-boot-concourse    文件:RelaxedBindingNotWritablePropertyException.java   
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() + "'";
}
项目:spring-boot-concourse    文件:RelaxedDataBinderTests.java   
@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]");
}
项目:contestparser    文件:RelaxedBindingNotWritablePropertyException.java   
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() + "'";
}
项目:contestparser    文件:RelaxedDataBinderTests.java   
@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());
}
项目:class-guard    文件:DataBinderTests.java   
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"));
    }
}
项目:spring-rich-client    文件:AbstractPropertyAccessStrategyTests.java   
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
    }
}
项目:spring-richclient    文件:AbstractPropertyAccessStrategyTests.java   
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
    }
}
项目:lams    文件:BeanPropertyRowMapper.java   
/**
 * 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;
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:PropertiesConfigurationFactoryTests.java   
@Test(expected = NotWritablePropertyException.class)
public void testUnknownPropertyCausesLoadFailure() throws Exception {
    this.ignoreUnknownFields = false;
    createFoo("hi: hello\nname: foo\nbar: blah");
}
项目:nbone    文件:EntityPropertyRowMapper.java   
@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;
}
项目:spring-boot-concourse    文件:PropertiesConfigurationFactoryTests.java   
@Test(expected = NotWritablePropertyException.class)
public void testUnknownPropertyCausesLoadFailure() throws Exception {
    this.ignoreUnknownFields = false;
    createFoo("hi: hello\nname: foo\nbar: blah");
}
项目:camunda-bpm-spring-boot-starter    文件:GenericPropertiesConfigurationTest.java   
@Test(expected = NotWritablePropertyException.class)
public void genericBindingTestWithNotExistingProperty() {
  final int dontExistValue = Integer.MAX_VALUE;
  camundaBpmProperties.getGenericProperties().getProperties().put("dont-exist", dontExistValue);
  genericPropertiesConfiguration.preInit(processEngineConfiguration);
}
项目:contestparser    文件:PropertiesConfigurationFactoryTests.java   
@Test(expected = NotWritablePropertyException.class)
public void testUnknownPropertyCausesLoadFailure() throws Exception {
    this.ignoreUnknownFields = false;
    createFoo("hi: hello\nname: foo\nbar: blah");
}
项目:class-guard    文件:BeanPropertyRowMapper.java   
/**
 * 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;
}
项目:mtools    文件:BeanPropRowMap.java   
/**
 * 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;
}