Search in sources :

Example 1 with StringTrimmerEditor

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"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) Test(org.junit.Test)

Example 2 with StringTrimmerEditor

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]"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) Test(org.junit.Test)

Example 3 with StringTrimmerEditor

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
    }
}
Also used : StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) Test(org.junit.Test)

Example 4 with StringTrimmerEditor

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);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) MethodParameter(org.springframework.core.MethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 5 with StringTrimmerEditor

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());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)10 MethodParameter (org.springframework.core.MethodParameter)2 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)2 WebDataBinder (org.springframework.web.bind.WebDataBinder)2 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)2 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)2 StringReader (java.io.StringReader)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 SAXReader (org.dom4j.io.SAXReader)1 CustomNumberEditor (org.springframework.beans.propertyeditors.CustomNumberEditor)1 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)1 GenericBean (org.springframework.tests.sample.beans.GenericBean)1 StopWatch (org.springframework.util.StopWatch)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1