java类org.springframework.validation.DataBinder的实例源码

MoneyFormattingTests.java 文件源码 项目:spring4-understanding 阅读 37 收藏 0 点赞 0 评论 0
@Test
public void testAmountAndUnit() {
    MoneyHolder bean = new MoneyHolder();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "USD 10.50");
    propertyValues.add("unit", "USD");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
    assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());

    LocaleContextHolder.setLocale(Locale.CANADA);
    binder.bind(propertyValues);
    LocaleContextHolder.setLocale(Locale.US);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
    assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
MoneyFormattingTests.java 文件源码 项目:spring4-understanding 阅读 38 收藏 0 点赞 0 评论 0
@Test
public void testAmountWithNumberFormat1() {
    FormattedMoneyHolder1 bean = new FormattedMoneyHolder1();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "$10.50");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());

    LocaleContextHolder.setLocale(Locale.CANADA);
    binder.bind(propertyValues);
    LocaleContextHolder.setLocale(Locale.US);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("CAD", bean.getAmount().getCurrency().getCurrencyCode());
}
MoneyFormattingTests.java 文件源码 项目:spring4-understanding 阅读 34 收藏 0 点赞 0 评论 0
@Test
public void testAmountWithNumberFormat3() {
    FormattedMoneyHolder3 bean = new FormattedMoneyHolder3();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "10%");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("10%", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
MoneyFormattingTests.java 文件源码 项目:spring4-understanding 阅读 38 收藏 0 点赞 0 评论 0
@Test
public void testAmountWithNumberFormat5() {
    FormattedMoneyHolder5 bean = new FormattedMoneyHolder5();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "USD 10.50");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());

    LocaleContextHolder.setLocale(Locale.CANADA);
    binder.bind(propertyValues);
    LocaleContextHolder.setLocale(Locale.US);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
NumberFormattingTests.java 文件源码 项目:spring4-understanding 阅读 36 收藏 0 点赞 0 评论 0
@Before
public void setUp() {
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.setEmbeddedValueResolver(new StringValueResolver() {
        @Override
        public String resolveStringValue(String strVal) {
            if ("${pattern}".equals(strVal)) {
                return "#,##.00";
            }
            else {
                return strVal;
            }
        }
    });
    conversionService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
    LocaleContextHolder.setLocale(Locale.US);
    binder = new DataBinder(new TestBean());
    binder.setConversionService(conversionService);
}
ServletModelAttributeMethodProcessor.java 文件源码 项目:spring4-understanding 阅读 32 收藏 0 点赞 0 评论 0
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param methodParam the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
        MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request)
        throws Exception {

    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
        TypeDescriptor source = TypeDescriptor.valueOf(String.class);
        TypeDescriptor target = new TypeDescriptor(methodParam);
        if (conversionService.canConvert(source, target)) {
            return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
        }
    }
    return null;
}
PropertySourcesPropertyValuesTests.java 文件源码 项目:https-github.com-g0t4-jenkins2-course-spring-boot 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void testPlaceholdersErrorInNonEnumerable() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    this.propertySources.addFirst(new PropertySource<Object>("application", "STUFF") {

        @Override
        public Object getProperty(String name) {
            return new Object();
        }

    });
    binder.bind(new PropertySourcesPropertyValues(this.propertySources,
            (Collection<String>) null, Collections.singleton("name")));
    assertThat(target.getName()).isNull();
}
PropertySourcesPropertyValuesTests.java 文件源码 项目:https-github.com-g0t4-jenkins2-course-spring-boot 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void testFirstCollectionPropertyWinsNestedAttributes() throws Exception {
    ListTestBean target = new ListTestBean();
    DataBinder binder = new DataBinder(target);
    Map<String, Object> first = new LinkedHashMap<String, Object>();
    first.put("list[0].description", "another description");
    Map<String, Object> second = new LinkedHashMap<String, Object>();
    second.put("list[0].name", "first name");
    second.put("list[0].description", "first description");
    second.put("list[1].name", "second name");
    second.put("list[1].description", "second description");
    this.propertySources.addFirst(new MapPropertySource("s", second));
    this.propertySources.addFirst(new MapPropertySource("f", first));
    binder.bind(new PropertySourcesPropertyValues(this.propertySources));
    target.getList();
    assertThat(target.getList()).hasSize(1);
    assertThat(target.getList().get(0).getDescription())
            .isEqualTo("another description");
    assertThat(target.getList().get(0).getName()).isNull();
}
NamespaceModelAttributeMethodProcessor.java 文件源码 项目:nbone 阅读 53 收藏 0 点赞 0 评论 0
protected Object createAttributeFromRequestValue(String sourceValue,
                                         String attributeName,
                                         MethodParameter parameter,
                                         WebDataBinderFactory binderFactory,
                                         NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
    TypeDescriptor source = TypeDescriptor.valueOf(String.class);
    TypeDescriptor target = new TypeDescriptor(parameter);
    if (conversionService.canConvert(source, target)) {
        return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
    }
}
    return null;
}
FormModelMethodArgumentResolver.java 文件源码 项目:nbone 阅读 37 收藏 0 点赞 0 评论 0
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link org.springframework.core.convert.converter.Converter} that can perform the conversion.
 *
 * @param sourceValue   the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param parameter     the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request       the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue,
                                                 String attributeName,
                                                 MethodParameter parameter,
                                                 WebDataBinderFactory binderFactory,
                                                 NativeWebRequest request) throws Exception {
    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
        TypeDescriptor source = TypeDescriptor.valueOf(String.class);
        TypeDescriptor target = new TypeDescriptor(parameter);
        if (conversionService.canConvert(source, target)) {
            return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
        }
    }
    return null;
}
FormModelMethodArgumentResolver.java 文件源码 项目:nbone 阅读 30 收藏 0 点赞 0 评论 0
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered 
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param parameter the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue,
                                             String attributeName, 
                                             MethodParameter parameter, 
                                             WebDataBinderFactory binderFactory, 
                                             NativeWebRequest request) throws Exception {
    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
        TypeDescriptor source = TypeDescriptor.valueOf(String.class);
        TypeDescriptor target = new TypeDescriptor(parameter);
        if (conversionService.canConvert(source, target)) {
            return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
        }
    }
    return null;
}
PropertySourcesPropertyValuesTests.java 文件源码 项目:spring-boot-concourse 阅读 26 收藏 0 点赞 0 评论 0
@Test
public void testPlaceholdersErrorInNonEnumerable() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    this.propertySources.addFirst(new PropertySource<Object>("application", "STUFF") {

        @Override
        public Object getProperty(String name) {
            return new Object();
        }

    });
    binder.bind(new PropertySourcesPropertyValues(this.propertySources,
            (Collection<String>) null, Collections.singleton("name")));
    assertThat(target.getName()).isNull();
}
PropertySourcesPropertyValuesTests.java 文件源码 项目:contestparser 阅读 34 收藏 0 点赞 0 评论 0
@Test
public void testPlaceholdersErrorInNonEnumerable() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    this.propertySources.addFirst(new PropertySource<Object>("application", "STUFF") {

        @Override
        public Object getProperty(String name) {
            return new Object();
        }

    });
    binder.bind(new PropertySourcesPropertyValues(this.propertySources,
            (Collection<String>) null, Collections.singleton("name")));
    assertEquals(null, target.getName());
}
UserFormControllerTest.java 文件源码 项目:ldadmin 阅读 37 收藏 0 点赞 0 评论 0
@Test
public void testSave() throws Exception {
    request = newPost("/userform.html");
    // set updated properties first since adding them later will
    // result in multiple parameters with the same name getting sent
    User user = ((UserManager) applicationContext.getBean("userManager")).getUser("-1");
    user.setConfirmPassword(user.getPassword());
    user.setLastName("Updated Last Name");

    request.setRemoteUser(user.getUsername());

    BindingResult errors = new DataBinder(user).getBindingResult();
    c.onSubmit(user, errors, request, new MockHttpServletResponse());

    assertFalse(errors.hasErrors());
    assertNotNull(request.getSession().getAttribute("successMessages"));
}
NumberFormattingTests.java 文件源码 项目:class-guard 阅读 29 收藏 0 点赞 0 评论 0
@Before
public void setUp() {
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.setEmbeddedValueResolver(new StringValueResolver() {
        @Override
        public String resolveStringValue(String strVal) {
            if ("${pattern}".equals(strVal)) {
                return "#,##.00";
            } else {
                return strVal;
            }
        }
    });
    conversionService.addFormatterForFieldType(Number.class, new NumberFormatter());
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
    LocaleContextHolder.setLocale(Locale.US);
    binder = new DataBinder(new TestBean());
    binder.setConversionService(conversionService);
}
JodaTimeFormattingTests.java 文件源码 项目:class-guard 阅读 27 收藏 0 点赞 0 评论 0
private void setUp(JodaTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);

    registrar.registerFormatters(conversionService);

    JodaTimeBean bean = new JodaTimeBean();
    bean.getChildren().add(new JodaTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
    JodaTimeContext context = new JodaTimeContext();
    context.setTimeZone(DateTimeZone.forID("-05:00"));
    JodaTimeContextHolder.setJodaTimeContext(context);
}
ServletModelAttributeMethodProcessor.java 文件源码 项目:class-guard 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param parameter the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue,
                                             String attributeName,
                                             MethodParameter parameter,
                                             WebDataBinderFactory binderFactory,
                                             NativeWebRequest request) throws Exception {
    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
        TypeDescriptor source = TypeDescriptor.valueOf(String.class);
        TypeDescriptor target = new TypeDescriptor(parameter);
        if (conversionService.canConvert(source, target)) {
            return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
        }
    }
    return null;
}
BindTagTests.java 文件源码 项目:class-guard 阅读 47 收藏 0 点赞 0 评论 0
public void testBindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new ServletRequestDataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            return "something";
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
    // because of the custom editor getValue() should return a String
    assertTrue("Value is TestBean", status.getValue() instanceof String);
    assertTrue("Correct value", "something".equals(status.getValue()));
}
FormValidationTest.java 文件源码 项目:egovframework.rte.root 阅读 57 收藏 0 点赞 0 评论 0
@Test
public void testValidation() throws BindException{

    Employee employee = new Employee();
    DataBinder binder = new DataBinder(employee);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("empId", "AA1000");
    pvs.addPropertyValue("empName", "Nobody");
    pvs.addPropertyValue("empAge", 10);
    binder.bind(pvs);

    Validator validator = new EmployeeValidator();
    Errors errors = binder.getBindingResult();
    validator.validate(employee, errors);

    assertFalse(errors.hasFieldErrors("empId"));
    assertFalse(errors.hasFieldErrors("empName"));
    assertTrue(errors.hasFieldErrors("empAge"));
}
ValidationApiResource.java 文件源码 项目:oma-riista-web 阅读 38 收藏 0 点赞 0 评论 0
private ResponseEntity<String> validate(Object dto) {
    final DataBinder binder = new DataBinder(dto);
    binder.setValidator(mvcValidator);
    binder.validate();

    return ResponseEntity.ok(binder.getBindingResult().hasErrors()
            ? BODY_INVALID_VALUE
            : BODY_VALID_VALUE);
}
MoneyFormattingTests.java 文件源码 项目:spring4-understanding 阅读 38 收藏 0 点赞 0 评论 0
@Test
public void testAmountWithNumberFormat2() {
    FormattedMoneyHolder2 bean = new FormattedMoneyHolder2();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "10.50");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("10.5", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
MoneyFormattingTests.java 文件源码 项目:spring4-understanding 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void testAmountWithNumberFormat4() {
    FormattedMoneyHolder4 bean = new FormattedMoneyHolder4();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "010.500");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("010.500", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
JodaTimeFormattingTests.java 文件源码 项目:spring4-understanding 阅读 34 收藏 0 点赞 0 评论 0
private void setUp(JodaTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);

    JodaTimeBean bean = new JodaTimeBean();
    bean.getChildren().add(new JodaTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
    JodaTimeContext context = new JodaTimeContext();
    context.setTimeZone(DateTimeZone.forID("-05:00"));
    JodaTimeContextHolder.setJodaTimeContext(context);
}
JodaTimeFormattingTests.java 文件源码 项目:spring4-understanding 阅读 33 收藏 0 点赞 0 评论 0
@Test
public void testBindDateWithoutErrorFallingBackToDateConstructor() {
    DataBinder binder = new DataBinder(new JodaTimeBean());
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
}
DateTimeFormattingTests.java 文件源码 项目:spring4-understanding 阅读 34 收藏 0 点赞 0 评论 0
private void setUp(DateTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);

    DateTimeBean bean = new DateTimeBean();
    bean.getChildren().add(new DateTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
    DateTimeContext context = new DateTimeContext();
    context.setTimeZone(ZoneId.of("-05:00"));
    DateTimeContextHolder.setDateTimeContext(context);
}
DateFormattingTests.java 文件源码 项目:spring4-understanding 阅读 33 收藏 0 点赞 0 评论 0
private void setUp(DateFormatterRegistrar registrar) {
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);

    SimpleDateBean bean = new SimpleDateBean();
    bean.getChildren().add(new SimpleDateBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);

    LocaleContextHolder.setLocale(Locale.US);
}
RedirectAttributesMethodArgumentResolver.java 文件源码 项目:spring4-understanding 阅读 34 收藏 0 点赞 0 评论 0
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

    DataBinder dataBinder = binderFactory.createBinder(webRequest, null, null);
    ModelMap redirectAttributes  = new RedirectAttributesModelMap(dataBinder);
    mavContainer.setRedirectModel(redirectAttributes);
    return redirectAttributes;
}
RedirectAttributesModelMapTests.java 文件源码 项目:spring4-understanding 阅读 32 收藏 0 点赞 0 评论 0
@Before
public void setup() {
    this.conversionService = new DefaultFormattingConversionService();
    DataBinder dataBinder = new DataBinder(null);
    dataBinder.setConversionService(conversionService);

    this.redirectAttributes = new RedirectAttributesModelMap(dataBinder);
}
BindTagTests.java 文件源码 项目:spring4-understanding 阅读 38 收藏 0 点赞 0 评论 0
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new ServletRequestDataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            return "something";
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
    // because of the custom editor getValue() should return a String
    assertTrue("Value is TestBean", status.getValue() instanceof String);
    assertTrue("Correct value", "something".equals(status.getValue()));
}
PropertySourcesPropertyValuesTests.java 文件源码 项目:https-github.com-g0t4-jenkins2-course-spring-boot 阅读 34 收藏 0 点赞 0 评论 0
@Test
public void testPlaceholdersBinding() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    binder.bind(new PropertySourcesPropertyValues(this.propertySources));
    assertThat(target.getName()).isEqualTo("bar");
}


问题


面经


文章

微信
公众号

扫码关注公众号