use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class ServletRequestDataBinderTests method testFieldDefaultNonBoolean.
@Test
public void testFieldDefaultNonBoolean() throws Exception {
TestBean target = new TestBean();
ServletRequestDataBinder binder = new ServletRequestDataBinder(target);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("!name", "anonymous");
request.addParameter("name", "Scott");
binder.bind(request);
assertThat(target.getName()).isEqualTo("Scott");
request.removeParameter("name");
binder.bind(request);
assertThat(target.getName()).isEqualTo("anonymous");
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class EscapedErrorsTests method testEscapedErrors.
@Test
public void testEscapedErrors() {
TestBean tb = new TestBean();
tb.setName("empty &");
Errors errors = new EscapedErrors(new BindException(tb, "tb"));
errors.rejectValue("name", "NAME_EMPTY &", null, "message: &");
errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>");
errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>");
errors.reject("GENERAL_ERROR \" '", null, "message: \" '");
assertThat(errors.hasErrors()).as("Correct errors flag").isTrue();
assertThat(errors.getErrorCount() == 4).as("Correct number of errors").isTrue();
assertThat("tb".equals(errors.getObjectName())).as("Correct object name").isTrue();
assertThat(errors.hasGlobalErrors()).as("Correct global errors flag").isTrue();
assertThat(errors.getGlobalErrorCount() == 1).as("Correct number of global errors").isTrue();
ObjectError globalError = errors.getGlobalError();
String defaultMessage = globalError.getDefaultMessage();
assertThat("message: " '".equals(defaultMessage)).as("Global error message escaped").isTrue();
assertThat("GENERAL_ERROR \" '".equals(globalError.getCode())).as("Global error code not escaped").isTrue();
ObjectError globalErrorInList = errors.getGlobalErrors().get(0);
assertThat(defaultMessage.equals(globalErrorInList.getDefaultMessage())).as("Same global error in list").isTrue();
ObjectError globalErrorInAllList = errors.getAllErrors().get(3);
assertThat(defaultMessage.equals(globalErrorInAllList.getDefaultMessage())).as("Same global error in list").isTrue();
assertThat(errors.hasFieldErrors()).as("Correct field errors flag").isTrue();
assertThat(errors.getFieldErrorCount() == 3).as("Correct number of field errors").isTrue();
assertThat(errors.getFieldErrors().size() == 3).as("Correct number of field errors in list").isTrue();
FieldError fieldError = errors.getFieldError();
assertThat("NAME_EMPTY &".equals(fieldError.getCode())).as("Field error code not escaped").isTrue();
assertThat("empty &".equals(errors.getFieldValue("name"))).as("Field value escaped").isTrue();
FieldError fieldErrorInList = errors.getFieldErrors().get(0);
assertThat(fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())).as("Same field error in list").isTrue();
assertThat(errors.hasFieldErrors("name")).as("Correct name errors flag").isTrue();
assertThat(errors.getFieldErrorCount("name") == 1).as("Correct number of name errors").isTrue();
assertThat(errors.getFieldErrors("name").size() == 1).as("Correct number of name errors in list").isTrue();
FieldError nameError = errors.getFieldError("name");
assertThat("message: &".equals(nameError.getDefaultMessage())).as("Name error message escaped").isTrue();
assertThat("NAME_EMPTY &".equals(nameError.getCode())).as("Name error code not escaped").isTrue();
assertThat("empty &".equals(errors.getFieldValue("name"))).as("Name value escaped").isTrue();
FieldError nameErrorInList = errors.getFieldErrors("name").get(0);
assertThat(nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage())).as("Same name error in list").isTrue();
assertThat(errors.hasFieldErrors("age")).as("Correct age errors flag").isTrue();
assertThat(errors.getFieldErrorCount("age") == 2).as("Correct number of age errors").isTrue();
assertThat(errors.getFieldErrors("age").size() == 2).as("Correct number of age errors in list").isTrue();
FieldError ageError = errors.getFieldError("age");
assertThat("message: <tag>".equals(ageError.getDefaultMessage())).as("Age error message escaped").isTrue();
assertThat("AGE_NOT_SET <tag>".equals(ageError.getCode())).as("Age error code not escaped").isTrue();
assertThat((Integer.valueOf(0)).equals(errors.getFieldValue("age"))).as("Age value not escaped").isTrue();
FieldError ageErrorInList = errors.getFieldErrors("age").get(0);
assertThat(ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage())).as("Same name error in list").isTrue();
FieldError ageError2 = errors.getFieldErrors("age").get(1);
assertThat("message: <tag>".equals(ageError2.getDefaultMessage())).as("Age error 2 message escaped").isTrue();
assertThat("AGE_NOT_32 <tag>".equals(ageError2.getCode())).as("Age error 2 code not escaped").isTrue();
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class RequestScopedProxyTests method testGetFromScopeThroughDynamicProxy.
@Test
public void testGetFromScopeThroughDynamicProxy() {
String name = "requestScopedProxy";
ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
// assertTrue(AopUtils.isJdkDynamicProxy(bean));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
assertThat(request.getAttribute("scopedTarget." + name)).isNull();
assertThat(bean.getName()).isEqualTo("scoped");
assertThat(request.getAttribute("scopedTarget." + name)).isNotNull();
TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
assertThat(target.getClass()).isEqualTo(TestBean.class);
assertThat(target.getName()).isEqualTo("scoped");
assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
assertThat(target.toString()).isEqualTo(bean.toString());
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class RequestScopedProxyTests method testGetInnerBeanFromScope.
@Test
public void testGetInnerBeanFromScope() {
TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
assertThat(AopUtils.isAopProxy(bean)).isFalse();
assertThat(AopUtils.isCglibProxy(bean.getSpouse())).isTrue();
String name = "scopedInnerBean";
MockHttpServletRequest request = new MockHttpServletRequest();
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
assertThat(request.getAttribute("scopedTarget." + name)).isNull();
assertThat(bean.getSpouse().getName()).isEqualTo("scoped");
assertThat(request.getAttribute("scopedTarget." + name)).isNotNull();
assertThat(request.getAttribute("scopedTarget." + name).getClass()).isEqualTo(TestBean.class);
assertThat(((TestBean) request.getAttribute("scopedTarget." + name)).getName()).isEqualTo("scoped");
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class RequestAndSessionScopedBeanTests method testPutBeanInSession.
@Test
@SuppressWarnings("resource")
public void testPutBeanInSession() {
String targetBeanName = "target";
HttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
StaticWebApplicationContext wac = new StaticWebApplicationContext();
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setScope(WebApplicationContext.SCOPE_SESSION);
bd.getPropertyValues().add("name", "abc");
wac.registerBeanDefinition(targetBeanName, bd);
wac.refresh();
TestBean target = (TestBean) wac.getBean(targetBeanName);
assertThat(target.getName()).isEqualTo("abc");
assertThat(request.getSession().getAttribute(targetBeanName)).isSameAs(target);
RequestContextHolder.setRequestAttributes(null);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> wac.getBean(targetBeanName));
}
Aggregations