Java Code Examples for org.springframework.tests.sample.beans.TestBean#setSpouse()

The following examples show how to use org.springframework.tests.sample.beans.TestBean#setSpouse() . 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 testBindingWithRequiredMapFields() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());

	DataBinder binder = new DataBinder(tb, "person");
	binder.setRequiredFields("someMap[key1]", "someMap[key2]", "someMap['key3']", "someMap[key4]");

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("someMap[key1]", "value1");
	pvs.add("someMap['key2']", "value2");
	pvs.add("someMap[key3]", "value3");

	binder.bind(pvs);

	BindingResult br = binder.getBindingResult();
	assertEquals("Wrong number of errors", 1, br.getErrorCount());
	assertEquals("required", br.getFieldError("someMap[key4]").getCode());
}
 
Example 2
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForSingleNestedProperty() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, "spouse.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("spouse.name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
	assertEquals("prefixvalue", tb.getSpouse().getName());
	assertEquals("value", bw.getPropertyValue("touchy"));
	assertEquals("value", tb.getTouchy());
}
 
Example 3
Source File: WebRequestDataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testFieldDefaultWithNestedProperty() throws Exception {
	TestBean target = new TestBean();
	target.setSpouse(new TestBean());
	WebRequestDataBinder binder = new WebRequestDataBinder(target);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addParameter("!spouse.postProcessed", "on");
	request.addParameter("_spouse.postProcessed", "visible");
	request.addParameter("spouse.postProcessed", "on");
	binder.bind(new ServletWebRequest(request));
	assertTrue(((TestBean) target.getSpouse()).isPostProcessed());

	request.removeParameter("spouse.postProcessed");
	binder.bind(new ServletWebRequest(request));
	assertTrue(((TestBean) target.getSpouse()).isPostProcessed());

	request.removeParameter("!spouse.postProcessed");
	binder.bind(new ServletWebRequest(request));
	assertFalse(((TestBean) target.getSpouse()).isPostProcessed());
}
 
Example 4
Source File: OptionTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	TestBean bean = new TestBean();
	bean.setName("foo");
	bean.setFavouriteColour(Colour.GREEN);
	bean.setStringArray(ARRAY);
	bean.setSpouse(new TestBean("Sally"));
	bean.setSomeNumber(new Float("12.34"));

	List friends = new ArrayList();
	friends.add(new TestBean("bar"));
	friends.add(new TestBean("penc"));
	bean.setFriends(friends);

	request.setAttribute("testBean", bean);
}
 
Example 5
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindingWithRequiredMapFields() throws Exception {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());

	DataBinder binder = new DataBinder(tb, "person");
	binder.setRequiredFields("someMap[key1]", "someMap[key2]", "someMap['key3']", "someMap[key4]");

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("someMap[key1]", "value1");
	pvs.add("someMap['key2']", "value2");
	pvs.add("someMap[key3]", "value3");

	binder.bind(pvs);

	BindingResult br = binder.getBindingResult();
	assertEquals("Wrong number of errors", 1, br.getErrorCount());
	assertEquals("required", br.getFieldError("someMap[key4]").getCode());
}
 
Example 6
Source File: OptionTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	TestBean bean = new TestBean();
	bean.setName("foo");
	bean.setFavouriteColour(Colour.GREEN);
	bean.setStringArray(ARRAY);
	bean.setSpouse(new TestBean("Sally"));
	bean.setSomeNumber(new Float("12.34"));

	List friends = new ArrayList();
	friends.add(new TestBean("bar"));
	friends.add(new TestBean("penc"));
	bean.setFriends(friends);

	request.setAttribute("testBean", bean);
}
 
Example 7
Source File: CustomEditorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForAllNestedStringProperties() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("spouse.name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
	assertEquals("prefixvalue", tb.getSpouse().getName());
	assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
	assertEquals("prefixvalue", tb.getTouchy());
}
 
Example 8
Source File: BindTagTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void bindTagWithNestedFieldErrors() throws JspException {
	PageContext pc = createPageContext();
	TestBean tb = new TestBean();
	tb.setName("name1");
	TestBean spouse = new TestBean();
	spouse.setName("name2");
	tb.setSpouse(spouse);
	Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
	errors.rejectValue("spouse.name", "code1", "message1");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.spouse.name");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "spouse.name".equals(status.getExpression()));
	assertTrue("Correct value", "name2".equals(status.getValue()));
	assertTrue("Correct displayValue", "name2".equals(status.getDisplayValue()));
	assertTrue("Correct isError", status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
	assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(" - ")));
}
 
Example 9
Source File: BeanWrapperTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyDescriptors() {
	TestBean target = new TestBean();
	target.setSpouse(new TestBean());
	BeanWrapper accessor = createAccessor(target);
	accessor.setPropertyValue("name", "a");
	accessor.setPropertyValue("spouse.name", "b");
	assertEquals("a", target.getName());
	assertEquals("b", target.getSpouse().getName());
	assertEquals("a", accessor.getPropertyValue("name"));
	assertEquals("b", accessor.getPropertyValue("spouse.name"));
	assertEquals(String.class, accessor.getPropertyDescriptor("name").getPropertyType());
	assertEquals(String.class, accessor.getPropertyDescriptor("spouse.name").getPropertyType());
}
 
Example 10
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Tests for required field, both null, non-existing and empty strings.
 */
@Test
public void testBindingWithRequiredFields() throws Exception {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());

	DataBinder binder = new DataBinder(tb, "person");
	binder.setRequiredFields("touchy", "name", "age", "date", "spouse.name");

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("touchy", "");
	pvs.add("name", null);
	pvs.add("age", null);
	pvs.add("spouse.name", "     ");

	binder.bind(pvs);

	BindingResult br = binder.getBindingResult();
	assertEquals("Wrong number of errors", 5, br.getErrorCount());

	assertEquals("required", br.getFieldError("touchy").getCode());
	assertEquals("", br.getFieldValue("touchy"));
	assertEquals("required", br.getFieldError("name").getCode());
	assertEquals("", br.getFieldValue("name"));
	assertEquals("required", br.getFieldError("age").getCode());
	assertEquals("", br.getFieldValue("age"));
	assertEquals("required", br.getFieldError("date").getCode());
	assertEquals("", br.getFieldValue("date"));
	assertEquals("required", br.getFieldError("spouse.name").getCode());
	assertEquals("", br.getFieldValue("spouse.name"));
}
 
Example 11
Source File: BeanWrapperTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void propertyDescriptors() {
	TestBean target = new TestBean();
	target.setSpouse(new TestBean());
	BeanWrapper accessor = createAccessor(target);
	accessor.setPropertyValue("name", "a");
	accessor.setPropertyValue("spouse.name", "b");
	assertEquals("a", target.getName());
	assertEquals("b", target.getSpouse().getName());
	assertEquals("a", accessor.getPropertyValue("name"));
	assertEquals("b", accessor.getPropertyValue("spouse.name"));
	assertEquals(String.class, accessor.getPropertyDescriptor("name").getPropertyType());
	assertEquals(String.class, accessor.getPropertyDescriptor("spouse.name").getPropertyType());
}
 
Example 12
Source File: BindTagTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void bindTagWithNestedFieldErrors() throws JspException {
	PageContext pc = createPageContext();
	TestBean tb = new TestBean();
	tb.setName("name1");
	TestBean spouse = new TestBean();
	spouse.setName("name2");
	tb.setSpouse(spouse);
	Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
	errors.rejectValue("spouse.name", "code1", "message1");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.spouse.name");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "spouse.name".equals(status.getExpression()));
	assertTrue("Correct value", "name2".equals(status.getValue()));
	assertTrue("Correct displayValue", "name2".equals(status.getDisplayValue()));
	assertTrue("Correct isError", status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
	assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(" - ")));
}
 
Example 13
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Tests for required field, both null, non-existing and empty strings.
 */
@Test
public void testBindingWithRequiredFields() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());

	DataBinder binder = new DataBinder(tb, "person");
	binder.setRequiredFields("touchy", "name", "age", "date", "spouse.name");

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("touchy", "");
	pvs.add("name", null);
	pvs.add("age", null);
	pvs.add("spouse.name", "     ");

	binder.bind(pvs);

	BindingResult br = binder.getBindingResult();
	assertEquals("Wrong number of errors", 5, br.getErrorCount());

	assertEquals("required", br.getFieldError("touchy").getCode());
	assertEquals("", br.getFieldValue("touchy"));
	assertEquals("required", br.getFieldError("name").getCode());
	assertEquals("", br.getFieldValue("name"));
	assertEquals("required", br.getFieldError("age").getCode());
	assertEquals("", br.getFieldValue("age"));
	assertEquals("required", br.getFieldError("date").getCode());
	assertEquals("", br.getFieldValue("date"));
	assertEquals("required", br.getFieldError("spouse.name").getCode());
	assertEquals("", br.getFieldValue("spouse.name"));
}
 
Example 14
Source File: ScopingTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Bean
public TestBean singletonWithScopedInterfaceDep() {
	TestBean singleton = new TestBean();
	singleton.setSpouse(scopedProxyInterface());
	return singleton;
}
 
Example 15
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testValidatorWithErrorsAndCodesPrefix() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(tb, "tb");
	DefaultMessageCodesResolver codesResolver = new DefaultMessageCodesResolver();
	codesResolver.setPrefix("validation.");
	errors.setMessageCodesResolver(codesResolver);

	Validator testValidator = new TestBeanValidator();
	testValidator.validate(tb, errors);

	errors.setNestedPath("spouse.");
	assertEquals("spouse.", errors.getNestedPath());
	Validator spouseValidator = new SpouseValidator();
	spouseValidator.validate(tb.getSpouse(), errors);

	errors.setNestedPath("");
	assertTrue(errors.hasErrors());
	assertEquals(6, errors.getErrorCount());

	assertEquals(2, errors.getGlobalErrorCount());
	assertEquals("validation.NAME_TOUCHY_MISMATCH", errors.getGlobalError().getCode());
	assertEquals("validation.NAME_TOUCHY_MISMATCH", (errors.getGlobalErrors().get(0)).getCode());
	assertEquals("validation.NAME_TOUCHY_MISMATCH.tb", (errors.getGlobalErrors().get(0)).getCodes()[0]);
	assertEquals("validation.NAME_TOUCHY_MISMATCH", (errors.getGlobalErrors().get(0)).getCodes()[1]);
	assertEquals("tb", (errors.getGlobalErrors().get(0)).getObjectName());
	assertEquals("validation.GENERAL_ERROR", (errors.getGlobalErrors().get(1)).getCode());
	assertEquals("validation.GENERAL_ERROR.tb", (errors.getGlobalErrors().get(1)).getCodes()[0]);
	assertEquals("validation.GENERAL_ERROR", (errors.getGlobalErrors().get(1)).getCodes()[1]);
	assertEquals("msg", (errors.getGlobalErrors().get(1)).getDefaultMessage());
	assertEquals("arg", (errors.getGlobalErrors().get(1)).getArguments()[0]);

	assertTrue(errors.hasFieldErrors());
	assertEquals(4, errors.getFieldErrorCount());
	assertEquals("validation.TOO_YOUNG", errors.getFieldError().getCode());
	assertEquals("validation.TOO_YOUNG", (errors.getFieldErrors().get(0)).getCode());
	assertEquals("age", (errors.getFieldErrors().get(0)).getField());
	assertEquals("validation.AGE_NOT_ODD", (errors.getFieldErrors().get(1)).getCode());
	assertEquals("age", (errors.getFieldErrors().get(1)).getField());
	assertEquals("validation.NOT_ROD", (errors.getFieldErrors().get(2)).getCode());
	assertEquals("name", (errors.getFieldErrors().get(2)).getField());
	assertEquals("validation.TOO_YOUNG", (errors.getFieldErrors().get(3)).getCode());
	assertEquals("spouse.age", (errors.getFieldErrors().get(3)).getField());

	assertTrue(errors.hasFieldErrors("age"));
	assertEquals(2, errors.getFieldErrorCount("age"));
	assertEquals("validation.TOO_YOUNG", errors.getFieldError("age").getCode());
	assertEquals("validation.TOO_YOUNG", (errors.getFieldErrors("age").get(0)).getCode());
	assertEquals("tb", (errors.getFieldErrors("age").get(0)).getObjectName());
	assertEquals("age", (errors.getFieldErrors("age").get(0)).getField());
	assertEquals(new Integer(0), (errors.getFieldErrors("age").get(0)).getRejectedValue());
	assertEquals("validation.AGE_NOT_ODD", (errors.getFieldErrors("age").get(1)).getCode());

	assertTrue(errors.hasFieldErrors("name"));
	assertEquals(1, errors.getFieldErrorCount("name"));
	assertEquals("validation.NOT_ROD", errors.getFieldError("name").getCode());
	assertEquals("validation.NOT_ROD.tb.name", errors.getFieldError("name").getCodes()[0]);
	assertEquals("validation.NOT_ROD.name", errors.getFieldError("name").getCodes()[1]);
	assertEquals("validation.NOT_ROD.java.lang.String", errors.getFieldError("name").getCodes()[2]);
	assertEquals("validation.NOT_ROD", errors.getFieldError("name").getCodes()[3]);
	assertEquals("name", (errors.getFieldErrors("name").get(0)).getField());
	assertEquals(null, (errors.getFieldErrors("name").get(0)).getRejectedValue());

	assertTrue(errors.hasFieldErrors("spouse.age"));
	assertEquals(1, errors.getFieldErrorCount("spouse.age"));
	assertEquals("validation.TOO_YOUNG", errors.getFieldError("spouse.age").getCode());
	assertEquals("tb", (errors.getFieldErrors("spouse.age").get(0)).getObjectName());
	assertEquals(new Integer(0), (errors.getFieldErrors("spouse.age").get(0)).getRejectedValue());
}
 
Example 16
Source File: BeanMethodQualificationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean @Qualifier("boring") @Lazy
public TestBean testBean2(@Lazy TestBean testBean1) {
	TestBean tb = new TestBean("boring");
	tb.setSpouse(testBean1);
	return tb;
}
 
Example 17
Source File: FreeMarkerMacroTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private String getMacroOutput(String name) throws Exception {
	String macro = fetchMacro(name);
	assertNotNull(macro);

	FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl");
	FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath()));

	DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
	Map<String, String> msgMap = new HashMap<>();
	msgMap.put("hello", "Howdy");
	msgMap.put("world", "Mundo");
	rc.setMessageMap(msgMap);
	Map<String, String> themeMsgMap = new HashMap<>();
	themeMsgMap.put("hello", "Howdy!");
	themeMsgMap.put("world", "Mundo!");
	rc.setThemeMessageMap(themeMsgMap);
	rc.setContextPath("/springtest");

	TestBean darren = new TestBean("Darren", 99);
	TestBean fred = new TestBean("Fred");
	fred.setJedi(true);
	darren.setSpouse(fred);
	darren.setJedi(true);
	darren.setStringArray(new String[] {"John", "Fred"});
	request.setAttribute("command", darren);

	Map<String, String> names = new HashMap<>();
	names.put("Darren", "Darren Davison");
	names.put("John", "John Doe");
	names.put("Fred", "Fred Bloggs");
	names.put("Rob&Harrop", "Rob Harrop");

	Configuration config = fc.getConfiguration();
	Map<String, Object> model = new HashMap<>();
	model.put("command", darren);
	model.put("springMacroRequestContext", rc);
	model.put("msgArgs", new Object[] { "World" });
	model.put("nameOptionMap", names);
	model.put("options", names.values());

	FreeMarkerView view = new FreeMarkerView();
	view.setBeanName("myView");
	view.setUrl("tmp.ftl");
	view.setExposeSpringMacroHelpers(false);
	view.setConfiguration(config);
	view.setServletContext(new MockServletContext());

	view.render(model, request, response);

	// tokenize output and ignore whitespace
	String output = response.getContentAsString();
	output = output.replace("\r\n", "\n");
	return output.trim();
}
 
Example 18
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testValidatorWithErrors() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());

	Errors errors = new BeanPropertyBindingResult(tb, "tb");

	Validator testValidator = new TestBeanValidator();
	testValidator.validate(tb, errors);

	errors.setNestedPath("spouse.");
	assertEquals("spouse.", errors.getNestedPath());
	Validator spouseValidator = new SpouseValidator();
	spouseValidator.validate(tb.getSpouse(), errors);

	errors.setNestedPath("");
	assertTrue(errors.hasErrors());
	assertEquals(6, errors.getErrorCount());

	assertEquals(2, errors.getGlobalErrorCount());
	assertEquals("NAME_TOUCHY_MISMATCH", errors.getGlobalError().getCode());
	assertEquals("NAME_TOUCHY_MISMATCH", (errors.getGlobalErrors().get(0)).getCode());
	assertEquals("NAME_TOUCHY_MISMATCH.tb", (errors.getGlobalErrors().get(0)).getCodes()[0]);
	assertEquals("NAME_TOUCHY_MISMATCH", (errors.getGlobalErrors().get(0)).getCodes()[1]);
	assertEquals("tb", (errors.getGlobalErrors().get(0)).getObjectName());
	assertEquals("GENERAL_ERROR", (errors.getGlobalErrors().get(1)).getCode());
	assertEquals("GENERAL_ERROR.tb", (errors.getGlobalErrors().get(1)).getCodes()[0]);
	assertEquals("GENERAL_ERROR", (errors.getGlobalErrors().get(1)).getCodes()[1]);
	assertEquals("msg", (errors.getGlobalErrors().get(1)).getDefaultMessage());
	assertEquals("arg", (errors.getGlobalErrors().get(1)).getArguments()[0]);

	assertTrue(errors.hasFieldErrors());
	assertEquals(4, errors.getFieldErrorCount());
	assertEquals("TOO_YOUNG", errors.getFieldError().getCode());
	assertEquals("TOO_YOUNG", (errors.getFieldErrors().get(0)).getCode());
	assertEquals("age", (errors.getFieldErrors().get(0)).getField());
	assertEquals("AGE_NOT_ODD", (errors.getFieldErrors().get(1)).getCode());
	assertEquals("age", (errors.getFieldErrors().get(1)).getField());
	assertEquals("NOT_ROD", (errors.getFieldErrors().get(2)).getCode());
	assertEquals("name", (errors.getFieldErrors().get(2)).getField());
	assertEquals("TOO_YOUNG", (errors.getFieldErrors().get(3)).getCode());
	assertEquals("spouse.age", (errors.getFieldErrors().get(3)).getField());

	assertTrue(errors.hasFieldErrors("age"));
	assertEquals(2, errors.getFieldErrorCount("age"));
	assertEquals("TOO_YOUNG", errors.getFieldError("age").getCode());
	assertEquals("TOO_YOUNG", (errors.getFieldErrors("age").get(0)).getCode());
	assertEquals("tb", (errors.getFieldErrors("age").get(0)).getObjectName());
	assertEquals("age", (errors.getFieldErrors("age").get(0)).getField());
	assertEquals(new Integer(0), (errors.getFieldErrors("age").get(0)).getRejectedValue());
	assertEquals("AGE_NOT_ODD", (errors.getFieldErrors("age").get(1)).getCode());

	assertTrue(errors.hasFieldErrors("name"));
	assertEquals(1, errors.getFieldErrorCount("name"));
	assertEquals("NOT_ROD", errors.getFieldError("name").getCode());
	assertEquals("NOT_ROD.tb.name", errors.getFieldError("name").getCodes()[0]);
	assertEquals("NOT_ROD.name", errors.getFieldError("name").getCodes()[1]);
	assertEquals("NOT_ROD.java.lang.String", errors.getFieldError("name").getCodes()[2]);
	assertEquals("NOT_ROD", errors.getFieldError("name").getCodes()[3]);
	assertEquals("name", (errors.getFieldErrors("name").get(0)).getField());
	assertEquals(null, (errors.getFieldErrors("name").get(0)).getRejectedValue());

	assertTrue(errors.hasFieldErrors("spouse.age"));
	assertEquals(1, errors.getFieldErrorCount("spouse.age"));
	assertEquals("TOO_YOUNG", errors.getFieldError("spouse.age").getCode());
	assertEquals("tb", (errors.getFieldErrors("spouse.age").get(0)).getObjectName());
	assertEquals(new Integer(0), (errors.getFieldErrors("spouse.age").get(0)).getRejectedValue());
}
 
Example 19
Source File: ScopingTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
public TestBean singletonWithScopedInterfaceDep() {
	TestBean singleton = new TestBean();
	singleton.setSpouse(scopedProxyInterface());
	return singleton;
}
 
Example 20
Source File: DataBinderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testValidatorWithErrorsAndCodesPrefix() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(tb, "tb");
	DefaultMessageCodesResolver codesResolver = new DefaultMessageCodesResolver();
	codesResolver.setPrefix("validation.");
	errors.setMessageCodesResolver(codesResolver);

	Validator testValidator = new TestBeanValidator();
	testValidator.validate(tb, errors);

	errors.setNestedPath("spouse.");
	assertEquals("spouse.", errors.getNestedPath());
	Validator spouseValidator = new SpouseValidator();
	spouseValidator.validate(tb.getSpouse(), errors);

	errors.setNestedPath("");
	assertTrue(errors.hasErrors());
	assertEquals(6, errors.getErrorCount());

	assertEquals(2, errors.getGlobalErrorCount());
	assertEquals("validation.NAME_TOUCHY_MISMATCH", errors.getGlobalError().getCode());
	assertEquals("validation.NAME_TOUCHY_MISMATCH", (errors.getGlobalErrors().get(0)).getCode());
	assertEquals("validation.NAME_TOUCHY_MISMATCH.tb", (errors.getGlobalErrors().get(0)).getCodes()[0]);
	assertEquals("validation.NAME_TOUCHY_MISMATCH", (errors.getGlobalErrors().get(0)).getCodes()[1]);
	assertEquals("tb", (errors.getGlobalErrors().get(0)).getObjectName());
	assertEquals("validation.GENERAL_ERROR", (errors.getGlobalErrors().get(1)).getCode());
	assertEquals("validation.GENERAL_ERROR.tb", (errors.getGlobalErrors().get(1)).getCodes()[0]);
	assertEquals("validation.GENERAL_ERROR", (errors.getGlobalErrors().get(1)).getCodes()[1]);
	assertEquals("msg", (errors.getGlobalErrors().get(1)).getDefaultMessage());
	assertEquals("arg", (errors.getGlobalErrors().get(1)).getArguments()[0]);

	assertTrue(errors.hasFieldErrors());
	assertEquals(4, errors.getFieldErrorCount());
	assertEquals("validation.TOO_YOUNG", errors.getFieldError().getCode());
	assertEquals("validation.TOO_YOUNG", (errors.getFieldErrors().get(0)).getCode());
	assertEquals("age", (errors.getFieldErrors().get(0)).getField());
	assertEquals("validation.AGE_NOT_ODD", (errors.getFieldErrors().get(1)).getCode());
	assertEquals("age", (errors.getFieldErrors().get(1)).getField());
	assertEquals("validation.NOT_ROD", (errors.getFieldErrors().get(2)).getCode());
	assertEquals("name", (errors.getFieldErrors().get(2)).getField());
	assertEquals("validation.TOO_YOUNG", (errors.getFieldErrors().get(3)).getCode());
	assertEquals("spouse.age", (errors.getFieldErrors().get(3)).getField());

	assertTrue(errors.hasFieldErrors("age"));
	assertEquals(2, errors.getFieldErrorCount("age"));
	assertEquals("validation.TOO_YOUNG", errors.getFieldError("age").getCode());
	assertEquals("validation.TOO_YOUNG", (errors.getFieldErrors("age").get(0)).getCode());
	assertEquals("tb", (errors.getFieldErrors("age").get(0)).getObjectName());
	assertEquals("age", (errors.getFieldErrors("age").get(0)).getField());
	assertEquals(new Integer(0), (errors.getFieldErrors("age").get(0)).getRejectedValue());
	assertEquals("validation.AGE_NOT_ODD", (errors.getFieldErrors("age").get(1)).getCode());

	assertTrue(errors.hasFieldErrors("name"));
	assertEquals(1, errors.getFieldErrorCount("name"));
	assertEquals("validation.NOT_ROD", errors.getFieldError("name").getCode());
	assertEquals("validation.NOT_ROD.tb.name", errors.getFieldError("name").getCodes()[0]);
	assertEquals("validation.NOT_ROD.name", errors.getFieldError("name").getCodes()[1]);
	assertEquals("validation.NOT_ROD.java.lang.String", errors.getFieldError("name").getCodes()[2]);
	assertEquals("validation.NOT_ROD", errors.getFieldError("name").getCodes()[3]);
	assertEquals("name", (errors.getFieldErrors("name").get(0)).getField());
	assertEquals(null, (errors.getFieldErrors("name").get(0)).getRejectedValue());

	assertTrue(errors.hasFieldErrors("spouse.age"));
	assertEquals(1, errors.getFieldErrorCount("spouse.age"));
	assertEquals("validation.TOO_YOUNG", errors.getFieldError("spouse.age").getCode());
	assertEquals("tb", (errors.getFieldErrors("spouse.age").get(0)).getObjectName());
	assertEquals(new Integer(0), (errors.getFieldErrors("spouse.age").get(0)).getRejectedValue());
}