Search in sources :

Example 6 with StringTrimmerEditor

use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueConvertedToNull.

// SPR-10578
@Test
public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
    WebDataBinder binder = new WebRequestDataBinder(null);
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
    this.request.addParameter("stringNotAnnot", "");
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).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) RequestParam(org.springframework.web.bind.annotation.RequestParam) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) MethodParameter(org.springframework.core.MethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 7 with StringTrimmerEditor

use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setPrimitiveArrayPropertyLargeMatching.

@Test
public void setPrimitiveArrayPropertyLargeMatching() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(LogFactory.getLog(AbstractPropertyAccessorTests.class));
    PrimitiveArrayBean target = new PrimitiveArrayBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    int[] input = new int[1024];
    StopWatch sw = new StopWatch();
    sw.start("array1");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertEquals(1024, target.getArray().length);
    assertEquals(0, target.getArray()[0]);
    long time1 = sw.getLastTaskTimeMillis();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);
    accessor.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    sw.start("array2");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125);
    accessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);
    accessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);
    accessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
    sw.start("array4");
    for (int i = 0; i < 100; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertEquals(1024, target.getArray().length);
    assertEquals(0, target.getArray()[0]);
    assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1);
}
Also used : BigInteger(java.math.BigInteger) CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 8 with StringTrimmerEditor

use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.

the class BeanWrapperGenericsTests method testGenericListOfArraysWithElementConversion.

@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
    GenericBean<String> gb = new GenericBean<>();
    ArrayList<String[]> list = new ArrayList<>();
    list.add(new String[] { "str1", "str2" });
    gb.setListOfArrays(list);
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
    assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]"));
    assertEquals("str3", gb.getListOfArrays().get(0)[1]);
}
Also used : StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) ArrayList(java.util.ArrayList) GenericBean(org.springframework.tests.sample.beans.GenericBean) Test(org.junit.Test)

Example 9 with StringTrimmerEditor

use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.

the class DataBinderTests method testSetAutoGrowCollectionLimitAfterInitialization.

// SPR-14888
@Test
public void testSetAutoGrowCollectionLimitAfterInitialization() {
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");
    DataBinder binder = new DataBinder(new BeanWithIntegerList());
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    binder.setAutoGrowCollectionLimit(257);
}
Also used : StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) Test(org.junit.Test)

Example 10 with StringTrimmerEditor

use of org.springframework.beans.propertyeditors.StringTrimmerEditor in project spring-framework by spring-projects.

the class DataBinderTests method testConversionWithInappropriateStringEditor.

@Test
public void testConversionWithInappropriateStringEditor() {
    DataBinder dataBinder = new DataBinder(null);
    DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
    dataBinder.setConversionService(conversionService);
    dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    NameBean bean = new NameBean("Fred");
    assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class));
    conversionService.addConverter(new NameBeanConverter());
    assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class));
}
Also used : StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) 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