org.springframework.beans.NotWritablePropertyException Java Examples

The following examples show how to use org.springframework.beans.NotWritablePropertyException. 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 File: DataBinderTests.java    From spring-analysis-note with 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 File: DataBinderTests.java    From spring-analysis-note with 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 File: DataBinderFieldAccessTests.java    From spring-analysis-note with 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
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 File: DataBinderTests.java    From java-technology-stack with 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 File: DataBinderTests.java    From java-technology-stack with 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 File: DataBinderFieldAccessTests.java    From java-technology-stack with 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
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 File: DataBinderTests.java    From spring4-understanding with 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 File: DataBinderTests.java    From spring4-understanding with 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
Source File: DataBinderFieldAccessTests.java    From spring4-understanding with Apache License 2.0 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 #12
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 File: BeanPropertyRowMapper.java    From effectivejava with 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;
}