org.springframework.tests.sample.beans.FieldAccessBean Java Examples

The following examples show how to use org.springframework.tests.sample.beans.FieldAccessBean. 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: 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 #2
Source File: DataBinderFieldAccessTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void nestedBindingWithDefaultConversionNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
	pvs.addPropertyValue(new PropertyValue("spouse.jedi", "on"));

	binder.bind(pvs);
	binder.close();

	assertEquals("Kerry", rod.getSpouse().getName());
	assertTrue((rod.getSpouse()).isJedi());
}
 
Example #3
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 #4
Source File: DataBinderFieldAccessTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void bindingNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
	pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue"));

	binder.bind(pvs);
	binder.close();

	assertTrue("changed name correctly", rod.getName().equals("Rod"));
	assertTrue("changed age correctly", rod.getAge() == 32);

	Map<?, ?> m = binder.getBindingResult().getModel();
	assertTrue("There is one element in map", m.size() == 2);
	FieldAccessBean tb = (FieldAccessBean) m.get("person");
	assertTrue("Same object", tb.equals(rod));
}
 
Example #5
Source File: DataBinderFieldAccessTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void nestedBindingWithDefaultConversionNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
	pvs.addPropertyValue(new PropertyValue("spouse.jedi", "on"));

	binder.bind(pvs);
	binder.close();

	assertEquals("Kerry", rod.getSpouse().getName());
	assertTrue((rod.getSpouse()).isJedi());
}
 
Example #6
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 #7
Source File: DataBinderFieldAccessTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bindingNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
	pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue"));

	binder.bind(pvs);
	binder.close();

	assertTrue("changed name correctly", rod.getName().equals("Rod"));
	assertTrue("changed age correctly", rod.getAge() == 32);

	Map<?, ?> m = binder.getBindingResult().getModel();
	assertTrue("There is one element in map", m.size() == 2);
	FieldAccessBean tb = (FieldAccessBean) m.get("person");
	assertTrue("Same object", tb.equals(rod));
}
 
Example #8
Source File: DataBinderFieldAccessTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void nestedBindingWithDefaultConversionNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
	pvs.addPropertyValue(new PropertyValue("spouse.jedi", "on"));

	binder.bind(pvs);
	binder.close();

	assertEquals("Kerry", rod.getSpouse().getName());
	assertTrue((rod.getSpouse()).isJedi());
}
 
Example #9
Source File: DataBinderFieldAccessTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindingNoErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
	pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue"));

	binder.bind(pvs);
	binder.close();

	assertTrue("changed name correctly", rod.getName().equals("Rod"));
	assertTrue("changed age correctly", rod.getAge() == 32);

	Map<?, ?> m = binder.getBindingResult().getModel();
	assertTrue("There is one element in map", m.size() == 2);
	FieldAccessBean tb = (FieldAccessBean) m.get("person");
	assertTrue("Same object", tb.equals(rod));
}
 
Example #10
Source File: DataBinderFieldAccessTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bindingWithErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", "32x"));
	binder.bind(pvs);

	try {
		binder.close();
		fail("Should have thrown BindException");
	}
	catch (BindException ex) {
		assertTrue("changed name correctly", rod.getName().equals("Rod"));
		//assertTrue("changed age correctly", rod.getAge() == 32);

		Map<?, ?> map = binder.getBindingResult().getModel();
		//assertTrue("There are 3 element in map", m.size() == 1);
		FieldAccessBean tb = (FieldAccessBean) map.get("person");
		assertTrue("Same object", tb.equals(rod));

		BindingResult br = (BindingResult) map.get(BindingResult.MODEL_KEY_PREFIX + "person");
		assertTrue("Added itself to map", br == binder.getBindingResult());
		assertTrue(br.hasErrors());
		assertTrue("Correct number of errors", br.getErrorCount() == 1);

		assertTrue("Has age errors", br.hasFieldErrors("age"));
		assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
		assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
		assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
		assertEquals(0, tb.getAge());
	}
}
 
Example #11
Source File: DataBinderFieldAccessTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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"));

	assertThatExceptionOfType(NullValueInNestedPathException.class).isThrownBy(() ->
			binder.bind(pvs));
}
 
Example #12
Source File: DataBinderFieldAccessTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example #13
Source File: DataBinderFieldAccessTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void bindingWithErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", "32x"));
	binder.bind(pvs);

	try {
		binder.close();
		fail("Should have thrown BindException");
	}
	catch (BindException ex) {
		assertTrue("changed name correctly", rod.getName().equals("Rod"));
		//assertTrue("changed age correctly", rod.getAge() == 32);

		Map<?, ?> map = binder.getBindingResult().getModel();
		//assertTrue("There are 3 element in map", m.size() == 1);
		FieldAccessBean tb = (FieldAccessBean) map.get("person");
		assertTrue("Same object", tb.equals(rod));

		BindingResult br = (BindingResult) map.get(BindingResult.MODEL_KEY_PREFIX + "person");
		assertTrue("Added itself to map", br == binder.getBindingResult());
		assertTrue(br.hasErrors());
		assertTrue("Correct number of errors", br.getErrorCount() == 1);

		assertTrue("Has age errors", br.hasFieldErrors("age"));
		assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
		assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
		assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
		assertEquals(0, tb.getAge());
	}
}
 
Example #14
Source File: DataBinderFieldAccessTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void bindingWithErrors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", "32x"));
	binder.bind(pvs);

	try {
		binder.close();
		fail("Should have thrown BindException");
	}
	catch (BindException ex) {
		assertTrue("changed name correctly", rod.getName().equals("Rod"));
		//assertTrue("changed age correctly", rod.getAge() == 32);

		Map<?, ?> map = binder.getBindingResult().getModel();
		//assertTrue("There are 3 element in map", m.size() == 1);
		FieldAccessBean tb = (FieldAccessBean) map.get("person");
		assertTrue("Same object", tb.equals(rod));

		BindingResult br = (BindingResult) map.get(BindingResult.MODEL_KEY_PREFIX + "person");
		assertTrue("Added itself to map", br == binder.getBindingResult());
		assertTrue(br.hasErrors());
		assertTrue("Correct number of errors", br.getErrorCount() == 1);

		assertTrue("Has age errors", br.hasFieldErrors("age"));
		assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
		assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
		assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
		assertEquals(0, tb.getAge());
	}
}
 
Example #15
Source File: DataBinderFieldAccessTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #16
Source File: DataBinderFieldAccessTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void bindingWithErrorsAndCustomEditors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	binder.initDirectFieldAccess();
	binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean(text, 0));
		}
		@Override
		public String getAsText() {
			return ((TestBean) getValue()).getName();
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", "32x"));
	pvs.addPropertyValue(new PropertyValue("spouse", "Kerry"));
	binder.bind(pvs);

	try {
		binder.close();
		fail("Should have thrown BindException");
	}
	catch (BindException ex) {
		assertTrue("changed name correctly", rod.getName().equals("Rod"));
		//assertTrue("changed age correctly", rod.getAge() == 32);

		Map<?, ?> model = binder.getBindingResult().getModel();
		//assertTrue("There are 3 element in map", m.size() == 1);
		FieldAccessBean tb = (FieldAccessBean) model.get("person");
		assertTrue("Same object", tb.equals(rod));

		BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
		assertTrue("Added itself to map", br == binder.getBindingResult());
		assertTrue(br.hasErrors());
		assertTrue("Correct number of errors", br.getErrorCount() == 1);

		assertTrue("Has age errors", br.hasFieldErrors("age"));
		assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
		assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
		assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
		assertEquals(0, tb.getAge());

		assertTrue("Does not have spouse errors", !br.hasFieldErrors("spouse"));
		assertEquals("Kerry", binder.getBindingResult().getFieldValue("spouse"));
		assertNotNull(tb.getSpouse());
	}
}
 
Example #17
Source File: DataBinderFieldAccessTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void bindingWithErrorsAndCustomEditors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	binder.initDirectFieldAccess();
	binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean(text, 0));
		}
		@Override
		public String getAsText() {
			return ((TestBean) getValue()).getName();
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", "32x"));
	pvs.addPropertyValue(new PropertyValue("spouse", "Kerry"));
	binder.bind(pvs);

	try {
		binder.close();
		fail("Should have thrown BindException");
	}
	catch (BindException ex) {
		assertTrue("changed name correctly", rod.getName().equals("Rod"));
		//assertTrue("changed age correctly", rod.getAge() == 32);

		Map<?, ?> model = binder.getBindingResult().getModel();
		//assertTrue("There are 3 element in map", m.size() == 1);
		FieldAccessBean tb = (FieldAccessBean) model.get("person");
		assertTrue("Same object", tb.equals(rod));

		BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
		assertTrue("Added itself to map", br == binder.getBindingResult());
		assertTrue(br.hasErrors());
		assertTrue("Correct number of errors", br.getErrorCount() == 1);

		assertTrue("Has age errors", br.hasFieldErrors("age"));
		assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
		assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
		assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
		assertEquals(0, tb.getAge());

		assertTrue("Does not have spouse errors", !br.hasFieldErrors("spouse"));
		assertEquals("Kerry", binder.getBindingResult().getFieldValue("spouse"));
		assertNotNull(tb.getSpouse());
	}
}
 
Example #18
Source File: DataBinderFieldAccessTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void bindingWithErrorsAndCustomEditors() throws Exception {
	FieldAccessBean rod = new FieldAccessBean();
	DataBinder binder = new DataBinder(rod, "person");
	binder.initDirectFieldAccess();
	binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean(text, 0));
		}
		@Override
		public String getAsText() {
			return ((TestBean) getValue()).getName();
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "Rod"));
	pvs.addPropertyValue(new PropertyValue("age", "32x"));
	pvs.addPropertyValue(new PropertyValue("spouse", "Kerry"));
	binder.bind(pvs);

	try {
		binder.close();
		fail("Should have thrown BindException");
	}
	catch (BindException ex) {
		assertTrue("changed name correctly", rod.getName().equals("Rod"));
		//assertTrue("changed age correctly", rod.getAge() == 32);

		Map<?, ?> model = binder.getBindingResult().getModel();
		//assertTrue("There are 3 element in map", m.size() == 1);
		FieldAccessBean tb = (FieldAccessBean) model.get("person");
		assertTrue("Same object", tb.equals(rod));

		BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
		assertTrue("Added itself to map", br == binder.getBindingResult());
		assertTrue(br.hasErrors());
		assertTrue("Correct number of errors", br.getErrorCount() == 1);

		assertTrue("Has age errors", br.hasFieldErrors("age"));
		assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
		assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
		assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
		assertEquals(0, tb.getAge());

		assertTrue("Does not have spouse errors", !br.hasFieldErrors("spouse"));
		assertEquals("Kerry", binder.getBindingResult().getFieldValue("spouse"));
		assertNotNull(tb.getSpouse());
	}
}