use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setCollectionPropertyWithStringValueAndCustomEditor.
@Test
public void setCollectionPropertyWithStringValueAndCustomEditor() {
IndexedTestBean target = new IndexedTestBean();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));
accessor.setPropertyValue("set", "set1 ");
accessor.setPropertyValue("sortedSet", "sortedSet1");
accessor.setPropertyValue("list", "list1 ");
assertEquals(1, target.getSet().size());
assertTrue(target.getSet().contains("set1"));
assertEquals(1, target.getSortedSet().size());
assertTrue(target.getSortedSet().contains("sortedSet1"));
assertEquals(1, target.getList().size());
assertTrue(target.getList().contains("list1"));
accessor.setPropertyValue("list", Collections.singletonList("list1 "));
assertTrue(target.getList().contains("list1"));
}
use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method propertyTypeIndexedProperty.
@Test
public void propertyTypeIndexedProperty() {
IndexedTestBean target = new IndexedTestBean();
AbstractPropertyAccessor accessor = createAccessor(target);
assertEquals(null, accessor.getPropertyType("map[key0]"));
accessor = createAccessor(target);
accessor.setPropertyValue("map[key0]", "my String");
assertEquals(String.class, accessor.getPropertyType("map[key0]"));
accessor = createAccessor(target);
accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
assertEquals(String.class, accessor.getPropertyType("map[key0]"));
}
use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.
the class MethodInvokingFactoryBeanTests method testGetObjectType.
@Test
public void testGetObjectType() throws Exception {
TestClass1 tc1 = new TestClass1();
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetObject(tc1);
mcfb.setTargetMethod("method1");
mcfb.afterPropertiesSet();
assertTrue(int.class.equals(mcfb.getObjectType()));
mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("voidRetvalMethod");
mcfb.afterPropertiesSet();
Class<?> objType = mcfb.getObjectType();
assertTrue(objType.equals(void.class));
// verify that we can call a method with args that are subtypes of the
// target method arg types
TestClass1._staticField1 = 0;
mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes");
mcfb.setArguments(new Object[] { new ArrayList<>(), new ArrayList<Object>(), "hello" });
mcfb.afterPropertiesSet();
mcfb.getObjectType();
// fail on improper argument types at afterPropertiesSet
mcfb = new MethodInvokingFactoryBean();
mcfb.registerCustomEditor(String.class, new StringTrimmerEditor(false));
mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes");
mcfb.setArguments(new Object[] { "1", new Object() });
try {
mcfb.afterPropertiesSet();
fail("Should have thrown NoSuchMethodException");
} catch (NoSuchMethodException ex) {
// expected
}
}
use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueNotRequired.
@Test
public void missingRequestParamEmptyValueNotRequired() throws Exception {
WebDataBinder binder = new WebRequestDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);
this.request.addParameter("name", "");
MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
assertNull(arg);
}
use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.
the class CheckboxTagTests method withSingleValueAndEditor.
@Test
public void withSingleValueAndEditor() throws Exception {
this.bean.setName("Rob Harrop");
this.tag.setPath("name");
this.tag.setValue(" Rob Harrop");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, new StringTrimmerEditor(false));
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
int result = this.tag.doStartTag();
assertEquals(Tag.SKIP_BODY, result);
String output = getOutput();
// wrap the output so it is valid XML
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element checkboxElement = (Element) document.getRootElement().elements().get(0);
assertEquals("input", checkboxElement.getName());
assertEquals("checkbox", checkboxElement.attribute("type").getValue());
assertEquals("name", checkboxElement.attribute("name").getValue());
assertEquals("checked", checkboxElement.attribute("checked").getValue());
assertEquals(" Rob Harrop", checkboxElement.attribute("value").getValue());
}
Aggregations