use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testDirectBindingToIndexedField.
@Test
void testDirectBindingToIndexedField() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0]", "a");
binder.bind(pvs);
Errors errors = binder.getBindingResult();
errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?");
errors.rejectValue("map[key1]", "NOT_ROD", "are you sure you're not Rod?");
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
assertThat(errors.getFieldErrorCount("array[0]")).isEqualTo(1);
assertThat(errors.getFieldError("array[0]").getCode()).isEqualTo("NOT_ROD");
assertThat(errors.getFieldError("array[0]").getCodes()[0]).isEqualTo("NOT_ROD.tb.array[0]");
assertThat(errors.getFieldError("array[0]").getCodes()[1]).isEqualTo("NOT_ROD.tb.array");
assertThat(errors.getFieldError("array[0]").getCodes()[2]).isEqualTo("NOT_ROD.array[0]");
assertThat(errors.getFieldError("array[0]").getCodes()[3]).isEqualTo("NOT_ROD.array");
assertThat(errors.getFieldError("array[0]").getCodes()[4]).isEqualTo("NOT_ROD.org.springframework.beans.testfixture.beans.DerivedTestBean");
assertThat(errors.getFieldError("array[0]").getCodes()[5]).isEqualTo("NOT_ROD");
assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
assertThat(errors.getFieldErrorCount("map[key1]")).isEqualTo(1);
assertThat(errors.getFieldError("map[key1]").getCode()).isEqualTo("NOT_ROD");
assertThat(errors.getFieldError("map[key1]").getCodes()[0]).isEqualTo("NOT_ROD.tb.map[key1]");
assertThat(errors.getFieldError("map[key1]").getCodes()[1]).isEqualTo("NOT_ROD.tb.map");
assertThat(errors.getFieldError("map[key1]").getCodes()[2]).isEqualTo("NOT_ROD.map[key1]");
assertThat(errors.getFieldError("map[key1]").getCodes()[3]).isEqualTo("NOT_ROD.map");
assertThat(errors.getFieldError("map[key1]").getCodes()[4]).isEqualTo("NOT_ROD.org.springframework.beans.testfixture.beans.TestBean");
assertThat(errors.getFieldError("map[key1]").getCodes()[5]).isEqualTo("NOT_ROD");
assertThat(errors.getFieldErrorCount("map[key0]")).isEqualTo(1);
assertThat(errors.getFieldError("map[key0]").getCode()).isEqualTo("NOT_NULL");
assertThat(errors.getFieldError("map[key0]").getCodes()[0]).isEqualTo("NOT_NULL.tb.map[key0]");
assertThat(errors.getFieldError("map[key0]").getCodes()[1]).isEqualTo("NOT_NULL.tb.map");
assertThat(errors.getFieldError("map[key0]").getCodes()[2]).isEqualTo("NOT_NULL.map[key0]");
assertThat(errors.getFieldError("map[key0]").getCodes()[3]).isEqualTo("NOT_NULL.map");
assertThat(errors.getFieldError("map[key0]").getCodes()[4]).isEqualTo("NOT_NULL");
}
use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testCustomEditorWithSubclass.
@Test
void testCustomEditorWithSubclass() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0]", "a");
binder.bind(pvs);
Errors errors = binder.getBindingResult();
errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?");
assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
assertThat(errors.getFieldErrorCount("array[0]")).isEqualTo(1);
assertThat(errors.getFieldError("array[0]").getCode()).isEqualTo("NOT_ROD");
assertThat(errors.getFieldError("array[0]").getCodes()[0]).isEqualTo("NOT_ROD.tb.array[0]");
assertThat(errors.getFieldError("array[0]").getCodes()[1]).isEqualTo("NOT_ROD.tb.array");
assertThat(errors.getFieldError("array[0]").getCodes()[2]).isEqualTo("NOT_ROD.array[0]");
assertThat(errors.getFieldError("array[0]").getCodes()[3]).isEqualTo("NOT_ROD.array");
assertThat(errors.getFieldError("array[0]").getCodes()[4]).isEqualTo("NOT_ROD.org.springframework.beans.testfixture.beans.DerivedTestBean");
assertThat(errors.getFieldError("array[0]").getCodes()[5]).isEqualTo("NOT_ROD");
assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
}
use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class WebApplicationContextScopeTests method testApplicationScope.
@Test
public void testApplicationScope() {
WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
assertThat(ac.getServletContext().getAttribute(NAME)).isNull();
DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
assertThat(ac.getServletContext().getAttribute(NAME)).isSameAs(bean);
assertThat(ac.getBean(NAME)).isSameAs(bean);
new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
assertThat(bean.wasDestroyed()).isTrue();
}
use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class WebApplicationContextScopeTests method testSessionScope.
@Test
public void testSessionScope() {
WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION);
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
assertThat(request.getSession().getAttribute(NAME)).isNull();
DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
assertThat(request.getSession().getAttribute(NAME)).isSameAs(bean);
assertThat(ac.getBean(NAME)).isSameAs(bean);
request.getSession().invalidate();
assertThat(bean.wasDestroyed()).isTrue();
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class RequestScopedProxyTests method testDestructionAtRequestCompletion.
@Test
public void testDestructionAtRequestCompletion() {
String name = "requestScopedDisposableObject";
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertThat(AopUtils.isCglibProxy(bean)).isTrue();
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes 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();
assertThat(request.getAttribute("scopedTarget." + name).getClass()).isEqualTo(DerivedTestBean.class);
assertThat(((TestBean) request.getAttribute("scopedTarget." + name)).getName()).isEqualTo("scoped");
assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
requestAttributes.requestCompleted();
assertThat(((TestBean) request.getAttribute("scopedTarget." + name)).wasDestroyed()).isTrue();
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
Aggregations