Java Code Examples for org.springframework.beans.NotWritablePropertyException

The following examples show how to use org.springframework.beans.NotWritablePropertyException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: DataBinderTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testBindingNoErrorsNotIgnoreUnknown() {
	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
	}
}
 
Example 2
Source Project: spring-analysis-note   Source File: DataBinderTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testBindingWithSystemFieldError() {
	TestBean rod = new TestBean();
	DataBinder binder = new DataBinder(rod, "person");
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("class.classLoader.URLs[0]", "https://myserver");
	binder.setIgnoreUnknownFields(false);

	try {
		binder.bind(pvs);
		fail("Should have thrown NotWritablePropertyException");
	}
	catch (NotWritablePropertyException ex) {
		assertTrue(ex.getMessage().contains("classLoader"));
	}
}
 
Example 3
Source Project: spring-analysis-note   Source File: DataBinderFieldAccessTests.java    License: MIT License 6 votes vote down vote up
@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
	}
}
 
Example 4
@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]);
	}
}
 
Example 5
Source Project: java-technology-stack   Source File: DataBinderTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testBindingNoErrorsNotIgnoreUnknown() {
	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
	}
}
 
Example 6
Source Project: java-technology-stack   Source File: DataBinderTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testBindingWithSystemFieldError() {
	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"));
	}
}
 
Example 7
Source Project: java-technology-stack   Source File: DataBinderFieldAccessTests.java    License: MIT License 6 votes vote down vote up
@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
	}
}
 
Example 8
@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]);
	}
}
 
Example 9
Source Project: spring4-understanding   Source File: DataBinderTests.java    License: Apache License 2.0 6 votes vote down vote up
@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
	}
}
 
Example 10
Source Project: spring4-understanding   Source File: DataBinderTests.java    License: Apache License 2.0 6 votes vote down vote up
@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"));
	}
}
 
Example 11
@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
	}
}
 
Example 12
@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]);
	}
}
 
Example 13
Source Project: effectivejava   Source File: BeanPropertyRowMapper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}