Here are the examples of the java api org.springframework.validation.DataBinder taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
67 Examples
19
View Source File : MyBeanWrapperFieldSetMapper.java
License : MIT License
Project Creator : yidao620c
License : MIT License
Project Creator : yidao620c
@Override
protected void initBinder(DataBinder binder) {
binder.registerCustomEditor(Timestamp.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.isNotEmpty(text)) {
setValue(DateUtil.parseTimestamp(text));
} else {
setValue(null);
}
}
@Override
public String getAsText() throws IllegalArgumentException {
Object date = getValue();
if (date != null) {
return DateUtil.formatTimestamp((Timestamp) date);
} else {
return "";
}
}
});
}
19
View Source File : RedirectAttributesModelMapTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Before
public void setup() {
this.conversionService = new DefaultFormattingConversionService();
DataBinder dataBinder = new DataBinder(null);
dataBinder.setConversionService(conversionService);
this.redirectAttributes = new RedirectAttributesModelMap(dataBinder);
}
19
View Source File : RedirectAttributesModelMap.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* A {@link ModelMap} implementation of {@link RedirectAttributes} that formats
* values as Strings using a {@link DataBinder}. Also provides a place to store
* flash attributes so they can survive a redirect without the need to be
* embedded in the redirect URL.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
@SuppressWarnings("serial")
public clreplaced RedirectAttributesModelMap extends ModelMap implements RedirectAttributes {
@Nullable
private final DataBinder dataBinder;
private final ModelMap flashAttributes = new ModelMap();
/**
* Default constructor without a DataBinder.
* Attribute values are converted to String via {@link #toString()}.
*/
public RedirectAttributesModelMap() {
this(null);
}
/**
* Constructor with a DataBinder.
* @param dataBinder used to format attribute values as Strings
*/
public RedirectAttributesModelMap(@Nullable DataBinder dataBinder) {
this.dataBinder = dataBinder;
}
/**
* Return the attributes candidate for flash storage or an empty Map.
*/
@Override
public Map<String, ?> getFlashAttributes() {
return this.flashAttributes;
}
/**
* {@inheritDoc}
* <p>Formats the attribute value as a String before adding it.
*/
@Override
public RedirectAttributesModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {
super.addAttribute(attributeName, formatValue(attributeValue));
return this;
}
@Nullable
private String formatValue(@Nullable Object value) {
if (value == null) {
return null;
}
return (this.dataBinder != null ? this.dataBinder.convertIfNecessary(value, String.clreplaced) : value.toString());
}
/**
* {@inheritDoc}
* <p>Formats the attribute value as a String before adding it.
*/
@Override
public RedirectAttributesModelMap addAttribute(Object attributeValue) {
super.addAttribute(attributeValue);
return this;
}
/**
* {@inheritDoc}
* <p>Each attribute value is formatted as a String before being added.
*/
@Override
public RedirectAttributesModelMap addAllAttributes(@Nullable Collection<?> attributeValues) {
super.addAllAttributes(attributeValues);
return this;
}
/**
* {@inheritDoc}
* <p>Each attribute value is formatted as a String before being added.
*/
@Override
public RedirectAttributesModelMap addAllAttributes(@Nullable Map<String, ?> attributes) {
if (attributes != null) {
attributes.forEach(this::addAttribute);
}
return this;
}
/**
* {@inheritDoc}
* <p>Each attribute value is formatted as a String before being merged.
*/
@Override
public RedirectAttributesModelMap mergeAttributes(@Nullable Map<String, ?> attributes) {
if (attributes != null) {
attributes.forEach((key, attribute) -> {
if (!containsKey(key)) {
addAttribute(key, attribute);
}
});
}
return this;
}
@Override
public Map<String, Object> asMap() {
return this;
}
/**
* {@inheritDoc}
* <p>The value is formatted as a String before being added.
*/
@Override
public Object put(String key, @Nullable Object value) {
return super.put(key, formatValue(value));
}
/**
* {@inheritDoc}
* <p>Each value is formatted as a String before being added.
*/
@Override
public void putAll(@Nullable Map<? extends String, ? extends Object> map) {
if (map != null) {
map.forEach((key, value) -> put(key, formatValue(value)));
}
}
@Override
public RedirectAttributes addFlashAttribute(String attributeName, @Nullable Object attributeValue) {
this.flashAttributes.addAttribute(attributeName, attributeValue);
return this;
}
@Override
public RedirectAttributes addFlashAttribute(Object attributeValue) {
this.flashAttributes.addAttribute(attributeValue);
return this;
}
}
19
View Source File : ErrorsMethodArgumentResolverTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
private BindingResult createBindingResult(Foo target, String name) {
DataBinder binder = this.bindingContext.createDataBinder(this.exchange, target, name);
return binder.getBindingResult();
}
19
View Source File : NumberFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* @author Keith Donald
* @author Juergen Hoeller
*/
public clreplaced NumberFormattingTests {
private final FormattingConversionService conversionService = new FormattingConversionService();
private DataBinder binder;
@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.clreplaced, new NumberStyleFormatter());
conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
LocaleContextHolder.setLocale(Locale.US);
binder = new DataBinder(new TestBean());
binder.setConversionService(conversionService);
}
@After
public void tearDown() {
LocaleContextHolder.setLocale(null);
}
@Test
public void testDefaultNumberFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("numberDefault", "3,339.12");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("3,339", binder.getBindingResult().getFieldValue("numberDefault"));
}
@Test
public void testDefaultNumberFormattingAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("numberDefaultAnnotated", "3,339.12");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("3,339.12", binder.getBindingResult().getFieldValue("numberDefaultAnnotated"));
}
@Test
public void testCurrencyFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("currency", "$3,339.12");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("$3,339.12", binder.getBindingResult().getFieldValue("currency"));
}
@Test
public void testPercentFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("percent", "53%");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("53%", binder.getBindingResult().getFieldValue("percent"));
}
@Test
public void testPatternFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("pattern", "1,25.00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1,25.00", binder.getBindingResult().getFieldValue("pattern"));
}
@Test
public void testPatternArrayFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternArray", new String[] { "1,25.00", "2,35.00" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1,25.00", binder.getBindingResult().getFieldValue("patternArray[0]"));
replacedertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternArray[1]"));
propertyValues = new MutablePropertyValues();
propertyValues.add("patternArray[0]", "1,25.00");
propertyValues.add("patternArray[1]", "2,35.00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1,25.00", binder.getBindingResult().getFieldValue("patternArray[0]"));
replacedertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternArray[1]"));
}
@Test
public void testPatternListFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternList", new String[] { "1,25.00", "2,35.00" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1,25.00", binder.getBindingResult().getFieldValue("patternList[0]"));
replacedertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternList[1]"));
propertyValues = new MutablePropertyValues();
propertyValues.add("patternList[0]", "1,25.00");
propertyValues.add("patternList[1]", "2,35.00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1,25.00", binder.getBindingResult().getFieldValue("patternList[0]"));
replacedertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternList[1]"));
}
@Test
public void testPatternList2FormattingListElement() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternList2[0]", "1,25.00");
propertyValues.add("patternList2[1]", "2,35.00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1,25.00", binder.getBindingResult().getFieldValue("patternList2[0]"));
replacedertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternList2[1]"));
}
@Test
public void testPatternList2FormattingList() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternList2[0]", "1,25.00");
propertyValues.add("patternList2[1]", "2,35.00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1,25.00,2,35.00", binder.getBindingResult().getFieldValue("patternList2"));
}
@SuppressWarnings("unused")
private static clreplaced TestBean {
private Integer numberDefault;
@NumberFormat
private Double numberDefaultAnnotated;
@NumberFormat(style = Style.CURRENCY)
private BigDecimal currency;
@NumberFormat(style = Style.PERCENT)
private BigDecimal percent;
@NumberFormat(pattern = "${pattern}")
private BigDecimal pattern;
@NumberFormat(pattern = "#,##.00")
private BigDecimal[] patternArray;
@NumberFormat(pattern = "#,##.00")
private List<BigDecimal> patternList;
@NumberFormat(pattern = "#,##.00")
private List<BigDecimal> patternList2;
public Integer getNumberDefault() {
return numberDefault;
}
public void setNumberDefault(Integer numberDefault) {
this.numberDefault = numberDefault;
}
public Double getNumberDefaultAnnotated() {
return numberDefaultAnnotated;
}
public void setNumberDefaultAnnotated(Double numberDefaultAnnotated) {
this.numberDefaultAnnotated = numberDefaultAnnotated;
}
public BigDecimal getCurrency() {
return currency;
}
public void setCurrency(BigDecimal currency) {
this.currency = currency;
}
public BigDecimal getPercent() {
return percent;
}
public void setPercent(BigDecimal percent) {
this.percent = percent;
}
public BigDecimal getPattern() {
return pattern;
}
public void setPattern(BigDecimal pattern) {
this.pattern = pattern;
}
public BigDecimal[] getPatternArray() {
return patternArray;
}
public void setPatternArray(BigDecimal[] patternArray) {
this.patternArray = patternArray;
}
public List<BigDecimal> getPatternList() {
return patternList;
}
public void setPatternList(List<BigDecimal> patternList) {
this.patternList = patternList;
}
public List<BigDecimal> getPatternList2() {
return patternList2;
}
public void setPatternList2(List<BigDecimal> patternList2) {
this.patternList2 = patternList2;
}
}
}
19
View Source File : DateTimeFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
*/
public clreplaced DateTimeFormattingTests {
private FormattingConversionService conversionService;
private DataBinder binder;
@Before
public void setup() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
setup(registrar);
}
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);
}
@After
public void cleanup() {
LocaleContextHolder.setLocale(null);
DateTimeContextHolder.setDateTimeContext(null);
}
@Test
public void testBindLocalDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateStyle(FormatStyle.LONG);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "October 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("October 31, 2009", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificFormatter() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "20091031");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("20091031", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new String[] { "10/31/09" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct -31, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct -31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindNestedLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("children[0].localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccess() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct -31, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct -31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new GregorianCalendar(2009, 9, 31, 0, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeStyle(FormatStyle.MEDIUM);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificFormatter() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HHmmss"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "130000");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("130000", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTimeAnnotated", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTimeAnnotated"));
}
@Test
public void testBindLocalTimeFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00:00 PM"));
}
@Test
public void testBindLocalDateTimeFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindDateTimeWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateTimeStyle(FormatStyle.MEDIUM);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00:00 PM"));
}
@Test
public void testBindDateTimeAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "10/31/09 12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedPattern"));
}
@Test
public void testBindDateTimeOverflow() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "02/29/09 12:00 PM");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISOTimeWithZone() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T12:00:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindISODateTimeWithZone() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T12:00:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindInstant() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31T12:00"));
}
@Test
@SuppressWarnings("deprecation")
public void testBindInstantFromJavaUtilDate() {
TimeZone defaultZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
try {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", new Date(109, 9, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31"));
} finally {
TimeZone.setDefault(defaultZone);
}
}
@Test
public void testBindPeriod() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("period", "P6Y3M1D");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D"));
}
@Test
public void testBindDuration() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("duration", "PT8H6M12.345S");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("duration").toString().equals("PT8H6M12.345S"));
}
@Test
public void testBindYear() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("year", "2007");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("year").toString().equals("2007"));
}
@Test
public void testBindMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("month", "JULY");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("month").toString().equals("JULY"));
}
@Test
public void testBindMonthInAnyCase() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("month", "July");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("month").toString().equals("JULY"));
}
@Test
public void testBindYearMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonth", "2007-12");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12"));
}
@Test
public void testBindMonthDay() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDay", "--12-03");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03"));
}
public static clreplaced DateTimeBean {
private LocalDate localDate;
@DateTimeFormat(style = "M-")
private LocalDate localDateAnnotated;
private LocalTime localTime;
@DateTimeFormat(style = "-M")
private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@DateTimeFormat(style = "MM")
private LocalDateTime localDateTimeAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm a")
private LocalDateTime dateTimeAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private LocalDate isoDate;
@DateTimeFormat(iso = ISO.TIME)
private LocalTime isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private LocalDateTime isoDateTime;
private Instant instant;
private Period period;
private Duration duration;
private Year year;
private Month month;
private YearMonth yearMonth;
private MonthDay monthDay;
private final List<DateTimeBean> children = new ArrayList<>();
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDate getLocalDateAnnotated() {
return localDateAnnotated;
}
public void setLocalDateAnnotated(LocalDate localDateAnnotated) {
this.localDateAnnotated = localDateAnnotated;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated;
}
public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public LocalDateTime getLocalDateTimeAnnotated() {
return localDateTimeAnnotated;
}
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
this.localDateTimeAnnotated = localDateTimeAnnotated;
}
public LocalDateTime getDateTimeAnnotatedPattern() {
return dateTimeAnnotatedPattern;
}
public void setDateTimeAnnotatedPattern(LocalDateTime dateTimeAnnotatedPattern) {
this.dateTimeAnnotatedPattern = dateTimeAnnotatedPattern;
}
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public LocalDateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(LocalDateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
public Instant getInstant() {
return instant;
}
public void setInstant(Instant instant) {
this.instant = instant;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public YearMonth getYearMonth() {
return yearMonth;
}
public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
public List<DateTimeBean> getChildren() {
return children;
}
}
}
19
View Source File : JodaTimeFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
*/
public clreplaced JodaTimeFormattingTests {
private FormattingConversionService conversionService;
private DataBinder binder;
@Before
public void setup() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
setup(registrar);
}
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);
}
@After
public void cleanup() {
LocaleContextHolder.setLocale(null);
JodaTimeContextHolder.setJodaTimeContext(null);
}
@Test
public void testJodaTimePatternsForStyle() {
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("SS", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("MM", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("LL", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("FF", LocaleContextHolder.getLocale()));
}
@Test
public void testBindLocalDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificStyle() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateStyle("L");
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "October 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("October 31, 2009", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificFormatter() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "20091031");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("20091031", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new String[] { "10/31/09" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindNestedLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("children[0].localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccess() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificStyle() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setTimeStyle("M");
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificFormatter() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("HHmmss"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "130000");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("130000", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTimeAnnotated", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTimeAnnotated"));
}
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("dateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
}
@Test
public void testBindDateTimeWithSpecificStyle() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeStyle("MM");
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00:00 PM"));
}
@Test
public void testBindDateTimeISO() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setUseIsoFormat(true);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("dateTime"));
}
@Test
public void testBindDateTimeWithSpecificFormatter() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMddHHmmss"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", "20091031130000");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("20091031130000", binder.getBindingResult().getFieldValue("dateTime"));
}
@Test
public void testBindDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotated", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotated").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
}
@Test
public void testBindDateTimeAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "10/31/09 12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedPattern"));
}
@Test
public void testBindDateTimeOverflow() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "02/29/09 12:00 PM");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindDateTimeAnnotatedDefault() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedDefault", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault").toString();
replacedertTrue(value.startsWith("10/31/09"));
}
@Test
public void testBindDateWithErrorAvoidingDateConstructor() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getErrorCount());
replacedertEquals("Sat, 12 Aug 1995 13:30:00 GMT", binder.getBindingResult().getFieldValue("date"));
}
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
}
@Test
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
}
@Test
public void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00.000", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindInstantAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instantAnnotated", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("instantAnnotated"));
}
@Test
public void testBindMutableDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("mutableDateTimeAnnotated", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("mutableDateTimeAnnotated"));
}
@Test
public void dateToStringWithFormat() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.shortDateTime());
setup(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = JodaTimeContextHolder.getFormatter(org.joda.time.format.DateTimeFormat.shortDateTime(), Locale.US).print(new DateTime(date));
replacedertEquals(expected, actual);
}
// SPR-10105
@Test
@SuppressWarnings("deprecation")
public void stringToDateWithoutGlobalFormat() {
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date, equalTo(new Date(string)));
}
// SPR-10105
@Test
public void stringToDateWithGlobalFormat() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
DateTimeFormatterFactory factory = new DateTimeFormatterFactory();
factory.setIso(ISO.DATE_TIME);
registrar.setDateTimeFormatter(factory.createDateTimeFormatter());
setup(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-10-31T07:00:00.000-05:00";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertNotNull(date);
}
@Test
public void testBindPeriod() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("period", "P6Y3M1D");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D"));
}
@Test
public void testBindDuration() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("duration", "PT72.345S");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("duration").toString().equals("PT72.345S"));
}
@Test
public void testBindYearMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonth", "2007-12");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12"));
}
@Test
public void testBindMonthDay() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDay", "--12-03");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03"));
}
@SuppressWarnings("unused")
private static clreplaced JodaTimeBean {
private LocalDate localDate;
@DateTimeFormat(style = "M-")
private LocalDate localDateAnnotated;
private LocalTime localTime;
@DateTimeFormat(style = "-M")
private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@DateTimeFormat(style = "MS")
private LocalDateTime localDateTimeAnnotated;
private DateTime dateTime;
@DateTimeFormat(style = "MS")
private DateTime dateTimeAnnotated;
@DateTimeFormat
private Date date;
@DateTimeFormat(style = "S-")
private Date dateAnnotated;
@DateTimeFormat(style = "S-")
private Calendar calendarAnnotated;
private Long millis;
@DateTimeFormat
private DateTime dateTimeAnnotatedDefault;
private Long millisAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm a")
private DateTime dateTimeAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private LocalDate isoDate;
@DateTimeFormat(iso = ISO.TIME)
private LocalTime isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private DateTime isoDateTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Instant instantAnnotated;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Instant mutableDateTimeAnnotated;
private Period period;
private Duration duration;
private YearMonth yearMonth;
private MonthDay monthDay;
private final List<JodaTimeBean> children = new ArrayList<>();
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDate getLocalDateAnnotated() {
return localDateAnnotated;
}
public void setLocalDateAnnotated(LocalDate localDateAnnotated) {
this.localDateAnnotated = localDateAnnotated;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated;
}
public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public LocalDateTime getLocalDateTimeAnnotated() {
return localDateTimeAnnotated;
}
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
this.localDateTimeAnnotated = localDateTimeAnnotated;
}
public DateTime getDateTime() {
return dateTime;
}
public void setDateTime(DateTime dateTime) {
this.dateTime = dateTime;
}
public DateTime getDateTimeAnnotated() {
return dateTimeAnnotated;
}
public void setDateTimeAnnotated(DateTime dateTimeAnnotated) {
this.dateTimeAnnotated = dateTimeAnnotated;
}
public DateTime getDateTimeAnnotatedPattern() {
return dateTimeAnnotatedPattern;
}
public void setDateTimeAnnotatedPattern(DateTime dateTimeAnnotatedPattern) {
this.dateTimeAnnotatedPattern = dateTimeAnnotatedPattern;
}
public DateTime getDateTimeAnnotatedDefault() {
return dateTimeAnnotatedDefault;
}
public void setDateTimeAnnotatedDefault(DateTime dateTimeAnnotatedDefault) {
this.dateTimeAnnotatedDefault = dateTimeAnnotatedDefault;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDateAnnotated() {
return dateAnnotated;
}
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
}
public Calendar getCalendarAnnotated() {
return calendarAnnotated;
}
public void setCalendarAnnotated(Calendar calendarAnnotated) {
this.calendarAnnotated = calendarAnnotated;
}
public Long getMillis() {
return millis;
}
public void setMillis(Long millis) {
this.millis = millis;
}
@DateTimeFormat(style = "S-")
public Long getMillisAnnotated() {
return millisAnnotated;
}
public void setMillisAnnotated(@DateTimeFormat(style = "S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
}
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public DateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(DateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
public Instant getInstantAnnotated() {
return instantAnnotated;
}
public void setInstantAnnotated(Instant instantAnnotated) {
this.instantAnnotated = instantAnnotated;
}
public Instant getMutableDateTimeAnnotated() {
return mutableDateTimeAnnotated;
}
public void setMutableDateTimeAnnotated(Instant mutableDateTimeAnnotated) {
this.mutableDateTimeAnnotated = mutableDateTimeAnnotated;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public YearMonth getYearMonth() {
return yearMonth;
}
public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
public List<JodaTimeBean> getChildren() {
return children;
}
}
}
19
View Source File : DateFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* @author Phillip Webb
* @author Keith Donald
* @author Juergen Hoeller
*/
public clreplaced DateFormattingTests {
private final FormattingConversionService conversionService = new FormattingConversionService();
private DataBinder binder;
@Before
public void setup() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
setup(registrar);
}
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);
}
@After
public void tearDown() {
LocaleContextHolder.setLocale(null);
}
@Test
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
}
@Test
public void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
}
@Test
public void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", new String[] { "10/31/09 12:00 PM" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "Oct X31, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("dateAnnotated"));
replacedertEquals("Oct X31, 2009", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
@Ignore
public void testBindDateAnnotatedWithFallbackError() {
// TODO This currently preplacedes because of the Date(String) constructor fallback is used
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("dateAnnotated"));
replacedertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindDateAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09 1:05", binder.getBindingResult().getFieldValue("dateAnnotatedPattern"));
}
@Test
public void testBindDateTimeOverflow() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotatedPattern", "02/29/09 12:00 PM");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("17:00:00.000Z", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000-08:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T20:00:00.000Z", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindNestedDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("children[0].dateAnnotated"));
}
@Test
public void dateToStringWithoutGlobalFormat() {
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = date.toString();
replacedertEquals(expected, actual);
}
@Test
public void dateToStringWithGlobalFormat() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
registrar.setFormatter(new DateFormatter());
setup(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = new DateFormatter().print(date, Locale.US);
replacedertEquals(expected, actual);
}
// SPR-10105
@Test
@SuppressWarnings("deprecation")
public void stringToDateWithoutGlobalFormat() {
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date, equalTo(new Date(string)));
}
// SPR-10105
@Test
public void stringToDateWithGlobalFormat() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
DateFormatter dateFormatter = new DateFormatter();
dateFormatter.setIso(ISO.DATE_TIME);
registrar.setFormatter(dateFormatter);
setup(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-06-01T14:23:05.003+00:00";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertNotNull(date);
}
@SuppressWarnings("unused")
private static clreplaced SimpleDateBean {
private Long millis;
private Long millisAnnotated;
@DateTimeFormat(style = "S-")
private Calendar calendarAnnotated;
@DateTimeFormat(style = "S-")
private Date dateAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm")
private Date dateAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private Date isoDate;
@DateTimeFormat(iso = ISO.TIME)
private Date isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Date isoDateTime;
private final List<SimpleDateBean> children = new ArrayList<>();
public Long getMillis() {
return millis;
}
public void setMillis(Long millis) {
this.millis = millis;
}
@DateTimeFormat(style = "S-")
public Long getMillisAnnotated() {
return millisAnnotated;
}
public void setMillisAnnotated(@DateTimeFormat(style = "S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
}
public Calendar getCalendarAnnotated() {
return calendarAnnotated;
}
public void setCalendarAnnotated(Calendar calendarAnnotated) {
this.calendarAnnotated = calendarAnnotated;
}
public Date getDateAnnotated() {
return dateAnnotated;
}
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
}
public Date getDateAnnotatedPattern() {
return dateAnnotatedPattern;
}
public void setDateAnnotatedPattern(Date dateAnnotatedPattern) {
this.dateAnnotatedPattern = dateAnnotatedPattern;
}
public Date getIsoDate() {
return isoDate;
}
public void setIsoDate(Date isoDate) {
this.isoDate = isoDate;
}
public Date getIsoTime() {
return isoTime;
}
public void setIsoTime(Date isoTime) {
this.isoTime = isoTime;
}
public Date getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(Date isoDateTime) {
this.isoDateTime = isoDateTime;
}
public List<SimpleDateBean> getChildren() {
return children;
}
}
}
19
View Source File : RedirectAttributesModelMapTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@BeforeEach
public void setup() {
this.conversionService = new DefaultFormattingConversionService();
DataBinder dataBinder = new DataBinder(null);
dataBinder.setConversionService(conversionService);
this.redirectAttributes = new RedirectAttributesModelMap(dataBinder);
}
19
View Source File : NumberFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* @author Keith Donald
* @author Juergen Hoeller
*/
public clreplaced NumberFormattingTests {
private final FormattingConversionService conversionService = new FormattingConversionService();
private DataBinder binder;
@BeforeEach
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.clreplaced, new NumberStyleFormatter());
conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
LocaleContextHolder.setLocale(Locale.US);
binder = new DataBinder(new TestBean());
binder.setConversionService(conversionService);
}
@AfterEach
public void tearDown() {
LocaleContextHolder.setLocale(null);
}
@Test
public void testDefaultNumberFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("numberDefault", "3,339.12");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("numberDefault")).isEqualTo("3,339");
}
@Test
public void testDefaultNumberFormattingAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("numberDefaultAnnotated", "3,339.12");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("numberDefaultAnnotated")).isEqualTo("3,339.12");
}
@Test
public void testCurrencyFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("currency", "$3,339.12");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("currency")).isEqualTo("$3,339.12");
}
@Test
public void testPercentFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("percent", "53%");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("percent")).isEqualTo("53%");
}
@Test
public void testPatternFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("pattern", "1,25.00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("pattern")).isEqualTo("1,25.00");
}
@Test
public void testPatternArrayFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternArray", new String[] { "1,25.00", "2,35.00" });
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("patternArray[0]")).isEqualTo("1,25.00");
replacedertThat(binder.getBindingResult().getFieldValue("patternArray[1]")).isEqualTo("2,35.00");
propertyValues = new MutablePropertyValues();
propertyValues.add("patternArray[0]", "1,25.00");
propertyValues.add("patternArray[1]", "2,35.00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("patternArray[0]")).isEqualTo("1,25.00");
replacedertThat(binder.getBindingResult().getFieldValue("patternArray[1]")).isEqualTo("2,35.00");
}
@Test
public void testPatternListFormatting() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternList", new String[] { "1,25.00", "2,35.00" });
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("patternList[0]")).isEqualTo("1,25.00");
replacedertThat(binder.getBindingResult().getFieldValue("patternList[1]")).isEqualTo("2,35.00");
propertyValues = new MutablePropertyValues();
propertyValues.add("patternList[0]", "1,25.00");
propertyValues.add("patternList[1]", "2,35.00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("patternList[0]")).isEqualTo("1,25.00");
replacedertThat(binder.getBindingResult().getFieldValue("patternList[1]")).isEqualTo("2,35.00");
}
@Test
public void testPatternList2FormattingListElement() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternList2[0]", "1,25.00");
propertyValues.add("patternList2[1]", "2,35.00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("patternList2[0]")).isEqualTo("1,25.00");
replacedertThat(binder.getBindingResult().getFieldValue("patternList2[1]")).isEqualTo("2,35.00");
}
@Test
public void testPatternList2FormattingList() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("patternList2[0]", "1,25.00");
propertyValues.add("patternList2[1]", "2,35.00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("patternList2")).isEqualTo("1,25.00,2,35.00");
}
@SuppressWarnings("unused")
private static clreplaced TestBean {
private Integer numberDefault;
@NumberFormat
private Double numberDefaultAnnotated;
@NumberFormat(style = Style.CURRENCY)
private BigDecimal currency;
@NumberFormat(style = Style.PERCENT)
private BigDecimal percent;
@NumberFormat(pattern = "${pattern}")
private BigDecimal pattern;
@NumberFormat(pattern = "#,##.00")
private BigDecimal[] patternArray;
@NumberFormat(pattern = "#,##.00")
private List<BigDecimal> patternList;
@NumberFormat(pattern = "#,##.00")
private List<BigDecimal> patternList2;
public Integer getNumberDefault() {
return numberDefault;
}
public void setNumberDefault(Integer numberDefault) {
this.numberDefault = numberDefault;
}
public Double getNumberDefaultAnnotated() {
return numberDefaultAnnotated;
}
public void setNumberDefaultAnnotated(Double numberDefaultAnnotated) {
this.numberDefaultAnnotated = numberDefaultAnnotated;
}
public BigDecimal getCurrency() {
return currency;
}
public void setCurrency(BigDecimal currency) {
this.currency = currency;
}
public BigDecimal getPercent() {
return percent;
}
public void setPercent(BigDecimal percent) {
this.percent = percent;
}
public BigDecimal getPattern() {
return pattern;
}
public void setPattern(BigDecimal pattern) {
this.pattern = pattern;
}
public BigDecimal[] getPatternArray() {
return patternArray;
}
public void setPatternArray(BigDecimal[] patternArray) {
this.patternArray = patternArray;
}
public List<BigDecimal> getPatternList() {
return patternList;
}
public void setPatternList(List<BigDecimal> patternList) {
this.patternList = patternList;
}
public List<BigDecimal> getPatternList2() {
return patternList2;
}
public void setPatternList2(List<BigDecimal> patternList2) {
this.patternList2 = patternList2;
}
}
}
19
View Source File : DateTimeFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
*/
public clreplaced DateTimeFormattingTests {
private FormattingConversionService conversionService;
private DataBinder binder;
@BeforeEach
public void setup() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
setup(registrar);
}
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);
}
@AfterEach
public void cleanup() {
LocaleContextHolder.setLocale(null);
DateTimeContextHolder.setDateTimeContext(null);
}
@Test
public void testBindLocalDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09");
}
@Test
public void testBindLocalDateWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateStyle(FormatStyle.LONG);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "October 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("October 31, 2009");
}
@Test
public void testBindLocalDateWithSpecificFormatter() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "20091031");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("20091031");
}
@Test
public void testBindLocalDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new String[] { "10/31/09" });
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
}
@Test
public void testBindLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct 31, 2009");
}
@Test
public void testBindLocalDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct -31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getFieldErrorCount("localDateAnnotated")).isEqualTo(1);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct -31, 2009");
}
@Test
public void testBindNestedLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("children[0].localDateAnnotated")).isEqualTo("Oct 31, 2009");
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccess() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct 31, 2009");
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct -31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getFieldErrorCount("localDateAnnotated")).isEqualTo(1);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct -31, 2009");
}
@Test
public void testBindLocalDateFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new GregorianCalendar(2009, 9, 31, 0, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09");
}
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
}
@Test
public void testBindLocalTimeWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeStyle(FormatStyle.MEDIUM);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00:00 PM");
}
@Test
public void testBindLocalTimeWithSpecificFormatter() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HHmmss"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "130000");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("130000");
}
@Test
public void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTimeAnnotated", "12:00:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTimeAnnotated")).isEqualTo("12:00:00 PM");
}
@Test
public void testBindLocalTimeFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
}
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertThat(value.startsWith("10/31/09")).isTrue();
replacedertThat(value.endsWith("12:00 PM")).isTrue();
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
replacedertThat(value.startsWith("Oct 31, 2009")).isTrue();
replacedertThat(value.endsWith("12:00:00 PM")).isTrue();
}
@Test
public void testBindLocalDateTimeFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertThat(value.startsWith("10/31/09")).isTrue();
replacedertThat(value.endsWith("12:00 PM")).isTrue();
}
@Test
public void testBindDateTimeWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateTimeStyle(FormatStyle.MEDIUM);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertThat(value.startsWith("Oct 31, 2009")).isTrue();
replacedertThat(value.endsWith("12:00:00 PM")).isTrue();
}
@Test
public void testBindDateTimeAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "10/31/09 12:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateTimeAnnotatedPattern")).isEqualTo("10/31/09 12:00 PM");
}
@Test
public void testBindDateTimeOverflow() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "02/29/09 12:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(1);
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoDate")).isEqualTo("2009-10-31");
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoTime")).isEqualTo("12:00:00");
}
@Test
public void testBindISOTimeWithZone() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoTime")).isEqualTo("12:00:00");
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoDateTime")).isEqualTo("2009-10-31T12:00:00");
}
@Test
public void testBindISODateTimeWithZone() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoDateTime")).isEqualTo("2009-10-31T12:00:00");
}
@Test
public void testBindInstant() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31T12:00")).isTrue();
}
@Test
@SuppressWarnings("deprecation")
public void testBindInstantFromJavaUtilDate() {
TimeZone defaultZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
try {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", new Date(109, 9, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31")).isTrue();
} finally {
TimeZone.setDefault(defaultZone);
}
}
@Test
public void testBindPeriod() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("period", "P6Y3M1D");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D")).isTrue();
}
@Test
public void testBindDuration() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("duration", "PT8H6M12.345S");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("duration").toString().equals("PT8H6M12.345S")).isTrue();
}
@Test
public void testBindYear() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("year", "2007");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("year").toString().equals("2007")).isTrue();
}
@Test
public void testBindMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("month", "JULY");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("month").toString().equals("JULY")).isTrue();
}
@Test
public void testBindMonthInAnyCase() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("month", "July");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("month").toString().equals("JULY")).isTrue();
}
@Test
public void testBindYearMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonth", "2007-12");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12")).isTrue();
}
@Test
public void testBindMonthDay() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDay", "--12-03");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03")).isTrue();
}
public static clreplaced DateTimeBean {
private LocalDate localDate;
@DateTimeFormat(style = "M-")
private LocalDate localDateAnnotated;
private LocalTime localTime;
@DateTimeFormat(style = "-M")
private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@DateTimeFormat(style = "MM")
private LocalDateTime localDateTimeAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm a")
private LocalDateTime dateTimeAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private LocalDate isoDate;
@DateTimeFormat(iso = ISO.TIME)
private LocalTime isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private LocalDateTime isoDateTime;
private Instant instant;
private Period period;
private Duration duration;
private Year year;
private Month month;
private YearMonth yearMonth;
private MonthDay monthDay;
private final List<DateTimeBean> children = new ArrayList<>();
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDate getLocalDateAnnotated() {
return localDateAnnotated;
}
public void setLocalDateAnnotated(LocalDate localDateAnnotated) {
this.localDateAnnotated = localDateAnnotated;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated;
}
public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public LocalDateTime getLocalDateTimeAnnotated() {
return localDateTimeAnnotated;
}
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
this.localDateTimeAnnotated = localDateTimeAnnotated;
}
public LocalDateTime getDateTimeAnnotatedPattern() {
return dateTimeAnnotatedPattern;
}
public void setDateTimeAnnotatedPattern(LocalDateTime dateTimeAnnotatedPattern) {
this.dateTimeAnnotatedPattern = dateTimeAnnotatedPattern;
}
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public LocalDateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(LocalDateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
public Instant getInstant() {
return instant;
}
public void setInstant(Instant instant) {
this.instant = instant;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public YearMonth getYearMonth() {
return yearMonth;
}
public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
public List<DateTimeBean> getChildren() {
return children;
}
}
}
19
View Source File : JodaTimeFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
*/
public clreplaced JodaTimeFormattingTests {
private FormattingConversionService conversionService;
private DataBinder binder;
@BeforeEach
public void setup() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
setup(registrar);
}
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);
}
@AfterEach
public void cleanup() {
LocaleContextHolder.setLocale(null);
JodaTimeContextHolder.setJodaTimeContext(null);
}
@Test
public void testJodaTimePatternsForStyle() {
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("SS", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("MM", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("LL", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("FF", LocaleContextHolder.getLocale()));
}
@Test
public void testBindLocalDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09");
}
@Test
public void testBindLocalDateWithSpecificStyle() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateStyle("L");
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "October 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("October 31, 2009");
}
@Test
public void testBindLocalDateWithSpecificFormatter() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "20091031");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("20091031");
}
@Test
public void testBindLocalDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new String[] { "10/31/09" });
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
}
@Test
public void testBindLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct 31, 2009");
}
@Test
public void testBindLocalDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getFieldErrorCount("localDateAnnotated")).isEqualTo(1);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct 031, 2009");
}
@Test
public void testBindNestedLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("children[0].localDateAnnotated")).isEqualTo("Oct 31, 2009");
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccess() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct 31, 2009");
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getFieldErrorCount("localDateAnnotated")).isEqualTo(1);
replacedertThat(binder.getBindingResult().getFieldValue("localDateAnnotated")).isEqualTo("Oct 031, 2009");
}
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
}
@Test
public void testBindLocalTimeWithSpecificStyle() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setTimeStyle("M");
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00:00 PM");
}
@Test
public void testBindLocalTimeWithSpecificFormatter() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("HHmmss"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "130000");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("130000");
}
@Test
public void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTimeAnnotated", "12:00:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("localTimeAnnotated")).isEqualTo("12:00:00 PM");
}
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertThat(value.startsWith("10/31/09")).isTrue();
replacedertThat(value.endsWith("12:00 PM")).isTrue();
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
replacedertThat(value.startsWith("Oct 31, 2009")).isTrue();
replacedertThat(value.endsWith("12:00 PM")).isTrue();
}
@Test
public void testBindDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("dateTime").toString();
replacedertThat(value.startsWith("10/31/09")).isTrue();
}
@Test
public void testBindDateTimeWithSpecificStyle() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeStyle("MM");
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertThat(value.startsWith("Oct 31, 2009")).isTrue();
replacedertThat(value.endsWith("12:00:00 PM")).isTrue();
}
@Test
public void testBindDateTimeISO() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setUseIsoFormat(true);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateTime")).isEqualTo("2009-10-31T07:00:00.000-05:00");
}
@Test
public void testBindDateTimeWithSpecificFormatter() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMddHHmmss"));
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", "20091031130000");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateTime")).isEqualTo("20091031130000");
}
@Test
public void testBindDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotated", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotated").toString();
replacedertThat(value.startsWith("Oct 31, 2009")).isTrue();
}
@Test
public void testBindDateTimeAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "10/31/09 12:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateTimeAnnotatedPattern")).isEqualTo("10/31/09 12:00 PM");
}
@Test
public void testBindDateTimeOverflow() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "02/29/09 12:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(1);
}
@Test
public void testBindDateTimeAnnotatedDefault() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedDefault", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault").toString();
replacedertThat(value.startsWith("10/31/09")).isTrue();
}
@Test
public void testBindDateWithErrorAvoidingDateConstructor() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(1);
replacedertThat(binder.getBindingResult().getFieldValue("date")).isEqualTo("Sat, 12 Aug 1995 13:30:00 GMT");
}
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
}
@Test
public void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateAnnotated")).isEqualTo("10/31/09");
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("calendarAnnotated")).isEqualTo("10/31/09");
}
@Test
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("millis")).isEqualTo("1256961600");
}
@Test
public void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("millisAnnotated")).isEqualTo("10/31/09");
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoDate")).isEqualTo("2009-10-31");
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoTime")).isEqualTo("12:00:00.000");
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoDateTime")).isEqualTo("2009-10-31T07:00:00.000-05:00");
}
@Test
public void testBindInstantAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instantAnnotated", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("instantAnnotated")).isEqualTo("2009-10-31T07:00:00.000-05:00");
}
@Test
public void testBindMutableDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("mutableDateTimeAnnotated", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("mutableDateTimeAnnotated")).isEqualTo("2009-10-31T07:00:00.000-05:00");
}
@Test
public void dateToStringWithFormat() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.shortDateTime());
setup(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = JodaTimeContextHolder.getFormatter(org.joda.time.format.DateTimeFormat.shortDateTime(), Locale.US).print(new DateTime(date));
replacedertThat(actual).isEqualTo(expected);
}
// SPR-10105
@Test
@SuppressWarnings("deprecation")
public void stringToDateWithoutGlobalFormat() {
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date).isEqualTo(new Date(string));
}
// SPR-10105
@Test
public void stringToDateWithGlobalFormat() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
DateTimeFormatterFactory factory = new DateTimeFormatterFactory();
factory.setIso(ISO.DATE_TIME);
registrar.setDateTimeFormatter(factory.createDateTimeFormatter());
setup(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-10-31T07:00:00.000-05:00";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date).isNotNull();
}
@Test
public void testBindPeriod() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("period", "P6Y3M1D");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D")).isTrue();
}
@Test
public void testBindDuration() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("duration", "PT72.345S");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("duration").toString().equals("PT72.345S")).isTrue();
}
@Test
public void testBindYearMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonth", "2007-12");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12")).isTrue();
}
@Test
public void testBindMonthDay() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDay", "--12-03");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03")).isTrue();
}
@SuppressWarnings("unused")
private static clreplaced JodaTimeBean {
private LocalDate localDate;
@DateTimeFormat(style = "M-")
private LocalDate localDateAnnotated;
private LocalTime localTime;
@DateTimeFormat(style = "-M")
private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@DateTimeFormat(style = "MS")
private LocalDateTime localDateTimeAnnotated;
private DateTime dateTime;
@DateTimeFormat(style = "MS")
private DateTime dateTimeAnnotated;
@DateTimeFormat
private Date date;
@DateTimeFormat(style = "S-")
private Date dateAnnotated;
@DateTimeFormat(style = "S-")
private Calendar calendarAnnotated;
private Long millis;
@DateTimeFormat
private DateTime dateTimeAnnotatedDefault;
private Long millisAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm a")
private DateTime dateTimeAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private LocalDate isoDate;
@DateTimeFormat(iso = ISO.TIME)
private LocalTime isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private DateTime isoDateTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Instant instantAnnotated;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Instant mutableDateTimeAnnotated;
private Period period;
private Duration duration;
private YearMonth yearMonth;
private MonthDay monthDay;
private final List<JodaTimeBean> children = new ArrayList<>();
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDate getLocalDateAnnotated() {
return localDateAnnotated;
}
public void setLocalDateAnnotated(LocalDate localDateAnnotated) {
this.localDateAnnotated = localDateAnnotated;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated;
}
public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public LocalDateTime getLocalDateTimeAnnotated() {
return localDateTimeAnnotated;
}
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
this.localDateTimeAnnotated = localDateTimeAnnotated;
}
public DateTime getDateTime() {
return dateTime;
}
public void setDateTime(DateTime dateTime) {
this.dateTime = dateTime;
}
public DateTime getDateTimeAnnotated() {
return dateTimeAnnotated;
}
public void setDateTimeAnnotated(DateTime dateTimeAnnotated) {
this.dateTimeAnnotated = dateTimeAnnotated;
}
public DateTime getDateTimeAnnotatedPattern() {
return dateTimeAnnotatedPattern;
}
public void setDateTimeAnnotatedPattern(DateTime dateTimeAnnotatedPattern) {
this.dateTimeAnnotatedPattern = dateTimeAnnotatedPattern;
}
public DateTime getDateTimeAnnotatedDefault() {
return dateTimeAnnotatedDefault;
}
public void setDateTimeAnnotatedDefault(DateTime dateTimeAnnotatedDefault) {
this.dateTimeAnnotatedDefault = dateTimeAnnotatedDefault;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDateAnnotated() {
return dateAnnotated;
}
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
}
public Calendar getCalendarAnnotated() {
return calendarAnnotated;
}
public void setCalendarAnnotated(Calendar calendarAnnotated) {
this.calendarAnnotated = calendarAnnotated;
}
public Long getMillis() {
return millis;
}
public void setMillis(Long millis) {
this.millis = millis;
}
@DateTimeFormat(style = "S-")
public Long getMillisAnnotated() {
return millisAnnotated;
}
public void setMillisAnnotated(@DateTimeFormat(style = "S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
}
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public DateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(DateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
public Instant getInstantAnnotated() {
return instantAnnotated;
}
public void setInstantAnnotated(Instant instantAnnotated) {
this.instantAnnotated = instantAnnotated;
}
public Instant getMutableDateTimeAnnotated() {
return mutableDateTimeAnnotated;
}
public void setMutableDateTimeAnnotated(Instant mutableDateTimeAnnotated) {
this.mutableDateTimeAnnotated = mutableDateTimeAnnotated;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public YearMonth getYearMonth() {
return yearMonth;
}
public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
public List<JodaTimeBean> getChildren() {
return children;
}
}
}
19
View Source File : JodaTimeFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
}
19
View Source File : DateFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
/**
* @author Phillip Webb
* @author Keith Donald
* @author Juergen Hoeller
*/
public clreplaced DateFormattingTests {
private FormattingConversionService conversionService;
private DataBinder binder;
@BeforeEach
void setup() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
setup(registrar);
}
private void setup(DateFormatterRegistrar registrar) {
conversionService = new FormattingConversionService();
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);
}
@AfterEach
void tearDown() {
LocaleContextHolder.setLocale(null);
}
@Test
void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("millis")).isEqualTo("1256961600");
}
@Test
void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("millisAnnotated")).isEqualTo("10/31/09");
}
@Test
void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("calendarAnnotated")).isEqualTo("10/31/09");
}
@Test
void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateAnnotated")).isEqualTo("10/31/09");
}
@Test
void testBindDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", new String[] { "10/31/09 12:00 PM" });
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
}
@Test
void testBindDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "Oct X31, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getFieldErrorCount("dateAnnotated")).isEqualTo(1);
replacedertThat(binder.getBindingResult().getFieldValue("dateAnnotated")).isEqualTo("Oct X31, 2009");
}
@Test
@Disabled
void testBindDateAnnotatedWithFallbackError() {
// TODO This currently preplacedes because of the Date(String) constructor fallback is used
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getFieldErrorCount("dateAnnotated")).isEqualTo(1);
replacedertThat(binder.getBindingResult().getFieldValue("dateAnnotated")).isEqualTo("Oct 031, 2009");
}
@Test
void testBindDateAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateAnnotatedPattern")).isEqualTo("10/31/09 1:05");
}
@Test
void testBindDateAnnotatedPatternWithGlobalFormat() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
DateFormatter dateFormatter = new DateFormatter();
dateFormatter.setIso(ISO.DATE_TIME);
registrar.setFormatter(dateFormatter);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("dateAnnotatedPattern")).isEqualTo("10/31/09 1:05");
}
@Test
void testBindDateTimeOverflow() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotatedPattern", "02/29/09 12:00 PM");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(1);
}
@Test
void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoDate")).isEqualTo("2009-10-31");
}
@Test
void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoTime")).isEqualTo("17:00:00.000Z");
}
@Test
void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000-08:00");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("isoDateTime")).isEqualTo("2009-10-31T20:00:00.000Z");
}
@Test
void testBindNestedDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("children[0].dateAnnotated")).isEqualTo("10/31/09");
}
@Test
void dateToStringWithoutGlobalFormat() {
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = date.toString();
replacedertThat(actual).isEqualTo(expected);
}
@Test
void dateToStringWithGlobalFormat() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
registrar.setFormatter(new DateFormatter());
setup(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = new DateFormatter().print(date, Locale.US);
replacedertThat(actual).isEqualTo(expected);
}
// SPR-10105
@Test
@SuppressWarnings("deprecation")
void stringToDateWithoutGlobalFormat() {
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date).isEqualTo(new Date(string));
}
// SPR-10105
@Test
void stringToDateWithGlobalFormat() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
DateFormatter dateFormatter = new DateFormatter();
dateFormatter.setIso(ISO.DATE_TIME);
registrar.setFormatter(dateFormatter);
setup(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-06-01T14:23:05.003+00:00";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date).isNotNull();
}
@SuppressWarnings("unused")
private static clreplaced SimpleDateBean {
private Long millis;
private Long millisAnnotated;
@DateTimeFormat(style = "S-")
private Calendar calendarAnnotated;
@DateTimeFormat(style = "S-")
private Date dateAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm")
private Date dateAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private Date isoDate;
@DateTimeFormat(iso = ISO.TIME)
private Date isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Date isoDateTime;
private final List<SimpleDateBean> children = new ArrayList<>();
public Long getMillis() {
return millis;
}
public void setMillis(Long millis) {
this.millis = millis;
}
@DateTimeFormat(style = "S-")
public Long getMillisAnnotated() {
return millisAnnotated;
}
public void setMillisAnnotated(@DateTimeFormat(style = "S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
}
public Calendar getCalendarAnnotated() {
return calendarAnnotated;
}
public void setCalendarAnnotated(Calendar calendarAnnotated) {
this.calendarAnnotated = calendarAnnotated;
}
public Date getDateAnnotated() {
return dateAnnotated;
}
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
}
public Date getDateAnnotatedPattern() {
return dateAnnotatedPattern;
}
public void setDateAnnotatedPattern(Date dateAnnotatedPattern) {
this.dateAnnotatedPattern = dateAnnotatedPattern;
}
public Date getIsoDate() {
return isoDate;
}
public void setIsoDate(Date isoDate) {
this.isoDate = isoDate;
}
public Date getIsoTime() {
return isoTime;
}
public void setIsoTime(Date isoTime) {
this.isoTime = isoTime;
}
public Date getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(Date isoDateTime) {
this.isoDateTime = isoDateTime;
}
public List<SimpleDateBean> getChildren() {
return children;
}
}
}
19
View Source File : RedirectAttributesModelMap.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* A {@link ModelMap} implementation of {@link RedirectAttributes} that formats
* values as Strings using a {@link DataBinder}. Also provides a place to store
* flash attributes so they can survive a redirect without the need to be
* embedded in the redirect URL.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
@SuppressWarnings("serial")
public clreplaced RedirectAttributesModelMap extends ModelMap implements RedirectAttributes {
private final DataBinder dataBinder;
private final ModelMap flashAttributes = new ModelMap();
/**
* Clreplaced constructor.
* @param dataBinder used to format attribute values as Strings.
*/
public RedirectAttributesModelMap(DataBinder dataBinder) {
this.dataBinder = dataBinder;
}
/**
* Default constructor without a DataBinder.
* Attribute values are converted to String via {@link #toString()}.
*/
public RedirectAttributesModelMap() {
this(null);
}
/**
* Return the attributes candidate for flash storage or an empty Map.
*/
@Override
public Map<String, ?> getFlashAttributes() {
return this.flashAttributes;
}
/**
* {@inheritDoc}
* <p>Formats the attribute value as a String before adding it.
*/
@Override
public RedirectAttributesModelMap addAttribute(String attributeName, Object attributeValue) {
super.addAttribute(attributeName, formatValue(attributeValue));
return this;
}
private String formatValue(Object value) {
if (value == null) {
return null;
}
return (dataBinder != null) ? dataBinder.convertIfNecessary(value, String.clreplaced) : value.toString();
}
/**
* {@inheritDoc}
* <p>Formats the attribute value as a String before adding it.
*/
@Override
public RedirectAttributesModelMap addAttribute(Object attributeValue) {
super.addAttribute(attributeValue);
return this;
}
/**
* {@inheritDoc}
* <p>Each attribute value is formatted as a String before being added.
*/
@Override
public RedirectAttributesModelMap addAllAttributes(Collection<?> attributeValues) {
super.addAllAttributes(attributeValues);
return this;
}
/**
* {@inheritDoc}
* <p>Each attribute value is formatted as a String before being added.
*/
@Override
public RedirectAttributesModelMap addAllAttributes(Map<String, ?> attributes) {
if (attributes != null) {
for (String key : attributes.keySet()) {
addAttribute(key, attributes.get(key));
}
}
return this;
}
/**
* {@inheritDoc}
* <p>Each attribute value is formatted as a String before being merged.
*/
@Override
public RedirectAttributesModelMap mergeAttributes(Map<String, ?> attributes) {
if (attributes != null) {
for (String key : attributes.keySet()) {
if (!containsKey(key)) {
addAttribute(key, attributes.get(key));
}
}
}
return this;
}
@Override
public Map<String, Object> asMap() {
return this;
}
/**
* {@inheritDoc}
* <p>The value is formatted as a String before being added.
*/
@Override
public Object put(String key, Object value) {
return super.put(key, formatValue(value));
}
/**
* {@inheritDoc}
* <p>Each value is formatted as a String before being added.
*/
@Override
public void putAll(Map<? extends String, ? extends Object> map) {
if (map != null) {
for (String key : map.keySet()) {
put(key, formatValue(map.get(key)));
}
}
}
@Override
public RedirectAttributes addFlashAttribute(String attributeName, Object attributeValue) {
this.flashAttributes.addAttribute(attributeName, attributeValue);
return this;
}
@Override
public RedirectAttributes addFlashAttribute(Object attributeValue) {
this.flashAttributes.addAttribute(attributeValue);
return this;
}
}
19
View Source File : DateTimeFormattingTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
*/
public clreplaced DateTimeFormattingTests {
private FormattingConversionService conversionService;
private DataBinder binder;
@Before
public void setUp() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
setUp(registrar);
}
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);
}
@After
public void tearDown() {
LocaleContextHolder.setLocale(null);
DateTimeContextHolder.setDateTimeContext(null);
}
@Test
public void testBindLocalDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificStyle() throws Exception {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateStyle(FormatStyle.LONG);
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "October 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("October 31, 2009", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificFormatter() throws Exception {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "20091031");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("20091031", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new String[] { "10/31/09" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct -31, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct -31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindNestedLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("children[0].localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccess() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct -31, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct -31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateFromJavaUtilCalendar() throws Exception {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new GregorianCalendar(2009, 9, 31, 0, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificStyle() throws Exception {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeStyle(FormatStyle.MEDIUM);
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificFormatter() throws Exception {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HHmmss"));
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "130000");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("130000", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTimeAnnotated", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTimeAnnotated"));
}
@Test
public void testBindLocalTimeFromJavaUtilCalendar() throws Exception {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00:00 PM"));
}
@Test
public void testBindLocalDateTimeFromJavaUtilCalendar() throws Exception {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindDateTimeWithSpecificStyle() throws Exception {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateTimeStyle(FormatStyle.MEDIUM);
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00:00 PM"));
}
@Test
public void testBindDateTimeAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "10/31/09 12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedPattern"));
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T12:00:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindInstant() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31T12:00"));
}
@Test
public void testBindInstantFromJavaUtilDate() throws Exception {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", new Date(109, 9, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31"));
}
@Test
public void testBindPeriod() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("period", "P6Y3M1D");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D"));
}
@Test
public void testBindDuration() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("duration", "PT8H6M12.345S");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("duration").toString().equals("PT8H6M12.345S"));
}
@Test
public void testBindYearMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonth", "2007-12");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12"));
}
@Test
public void testBindMonthDay() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDay", "--12-03");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03"));
}
public static clreplaced DateTimeBean {
private LocalDate localDate;
@DateTimeFormat(style = "M-")
private LocalDate localDateAnnotated;
private LocalTime localTime;
@DateTimeFormat(style = "-M")
private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@DateTimeFormat(style = "MM")
private LocalDateTime localDateTimeAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm a")
private LocalDateTime dateTimeAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private LocalDate isoDate;
@DateTimeFormat(iso = ISO.TIME)
private LocalTime isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private LocalDateTime isoDateTime;
private Instant instant;
private Period period;
private Duration duration;
private YearMonth yearMonth;
private MonthDay monthDay;
private final List<DateTimeBean> children = new ArrayList<DateTimeBean>();
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDate getLocalDateAnnotated() {
return localDateAnnotated;
}
public void setLocalDateAnnotated(LocalDate localDateAnnotated) {
this.localDateAnnotated = localDateAnnotated;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated;
}
public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public LocalDateTime getLocalDateTimeAnnotated() {
return localDateTimeAnnotated;
}
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
this.localDateTimeAnnotated = localDateTimeAnnotated;
}
public LocalDateTime getDateTimeAnnotatedPattern() {
return dateTimeAnnotatedPattern;
}
public void setDateTimeAnnotatedPattern(LocalDateTime dateTimeAnnotatedPattern) {
this.dateTimeAnnotatedPattern = dateTimeAnnotatedPattern;
}
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public LocalDateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(LocalDateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
public Instant getInstant() {
return instant;
}
public void setInstant(Instant instant) {
this.instant = instant;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public YearMonth getYearMonth() {
return yearMonth;
}
public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
public List<DateTimeBean> getChildren() {
return children;
}
}
}
19
View Source File : JodaTimeFormattingTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
*/
public clreplaced JodaTimeFormattingTests {
private FormattingConversionService conversionService;
private DataBinder binder;
@Before
public void setUp() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
setUp(registrar);
}
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);
}
@After
public void tearDown() {
LocaleContextHolder.setLocale(null);
JodaTimeContextHolder.setJodaTimeContext(null);
}
@Test
public void testJodaTimePatternsForStyle() {
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("SS", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("MM", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("LL", LocaleContextHolder.getLocale()));
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("FF", LocaleContextHolder.getLocale()));
}
@Test
public void testBindLocalDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificStyle() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateStyle("L");
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "October 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("October 31, 2009", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateWithSpecificFormatter() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd"));
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "20091031");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("20091031", binder.getBindingResult().getFieldValue("localDate"));
}
@Test
public void testBindLocalDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new String[] { "10/31/09" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindNestedLocalDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("children[0].localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccess() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 31, 2009");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() {
binder.initDirectFieldAccess();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
replacedertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificStyle() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setTimeStyle("M");
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeWithSpecificFormatter() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("HHmmss"));
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "130000");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("130000", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTimeAnnotated", "12:00:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTimeAnnotated"));
}
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("dateTime").toString();
replacedertTrue(value.startsWith("10/31/09"));
}
@Test
public void testBindDateTimeWithSpecificStyle() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeStyle("MM");
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
replacedertTrue(value.endsWith("12:00:00 PM"));
}
@Test
public void testBindDateTimeISO() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setUseIsoFormat(true);
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("dateTime"));
}
@Test
public void testBindDateTimeWithSpecificFormatter() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMddHHmmss"));
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", "20091031130000");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("20091031130000", binder.getBindingResult().getFieldValue("dateTime"));
}
@Test
public void testBindDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotated", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotated").toString();
replacedertTrue(value.startsWith("Oct 31, 2009"));
}
@Test
public void testBindDateTimeAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedPattern", "10/31/09 12:00 PM");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedPattern"));
}
@Test
public void testBindDateTimeAnnotatedDefault() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedDefault", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC()));
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault").toString();
replacedertTrue(value.startsWith("10/31/09"));
}
@Test
public void testBindDateWithErrorAvoidingDateConstructor() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getErrorCount());
replacedertEquals("Sat, 12 Aug 1995 13:30:00 GMT", binder.getBindingResult().getFieldValue("date"));
}
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
}
@Test
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
}
@Test
public void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("12:00:00.000", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindInstantAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instantAnnotated", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("instantAnnotated"));
}
@Test
public void testBindMutableDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("mutableDateTimeAnnotated", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("mutableDateTimeAnnotated"));
}
@Test
public void dateToStringWithFormat() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.shortDateTime());
setUp(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = JodaTimeContextHolder.getFormatter(org.joda.time.format.DateTimeFormat.shortDateTime(), Locale.US).print(new DateTime(date));
replacedertEquals(expected, actual);
}
@Test
@SuppressWarnings("deprecation")
public void stringToDateWithoutGlobalFormat() throws Exception {
// SPR-10105
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date, equalTo(new Date(string)));
}
@Test
public void stringToDateWithGlobalFormat() throws Exception {
// SPR-10105
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
DateTimeFormatterFactory factory = new DateTimeFormatterFactory();
factory.setIso(ISO.DATE_TIME);
registrar.setDateTimeFormatter(factory.createDateTimeFormatter());
setUp(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-10-31T07:00:00.000-05:00";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertNotNull(date);
}
@Test
public void testBindPeriod() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("period", "P6Y3M1D");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D"));
}
@Test
public void testBindDuration() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("duration", "PT72.345S");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("duration").toString().equals("PT72.345S"));
}
@Test
public void testBindYearMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonth", "2007-12");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12"));
}
@Test
public void testBindMonthDay() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDay", "--12-03");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertTrue(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03"));
}
@SuppressWarnings("unused")
private static clreplaced JodaTimeBean {
private LocalDate localDate;
@DateTimeFormat(style = "M-")
private LocalDate localDateAnnotated;
private LocalTime localTime;
@DateTimeFormat(style = "-M")
private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@DateTimeFormat(style = "MS")
private LocalDateTime localDateTimeAnnotated;
private DateTime dateTime;
@DateTimeFormat(style = "MS")
private DateTime dateTimeAnnotated;
@DateTimeFormat
private Date date;
@DateTimeFormat(style = "S-")
private Date dateAnnotated;
@DateTimeFormat(style = "S-")
private Calendar calendarAnnotated;
private Long millis;
@DateTimeFormat
private DateTime dateTimeAnnotatedDefault;
private Long millisAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm a")
private DateTime dateTimeAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private LocalDate isoDate;
@DateTimeFormat(iso = ISO.TIME)
private LocalTime isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private DateTime isoDateTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Instant instantAnnotated;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Instant mutableDateTimeAnnotated;
private Period period;
private Duration duration;
private YearMonth yearMonth;
private MonthDay monthDay;
private final List<JodaTimeBean> children = new ArrayList<JodaTimeBean>();
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDate getLocalDateAnnotated() {
return localDateAnnotated;
}
public void setLocalDateAnnotated(LocalDate localDateAnnotated) {
this.localDateAnnotated = localDateAnnotated;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated;
}
public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public LocalDateTime getLocalDateTimeAnnotated() {
return localDateTimeAnnotated;
}
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
this.localDateTimeAnnotated = localDateTimeAnnotated;
}
public DateTime getDateTime() {
return dateTime;
}
public void setDateTime(DateTime dateTime) {
this.dateTime = dateTime;
}
public DateTime getDateTimeAnnotated() {
return dateTimeAnnotated;
}
public void setDateTimeAnnotated(DateTime dateTimeAnnotated) {
this.dateTimeAnnotated = dateTimeAnnotated;
}
public DateTime getDateTimeAnnotatedPattern() {
return dateTimeAnnotatedPattern;
}
public void setDateTimeAnnotatedPattern(DateTime dateTimeAnnotatedPattern) {
this.dateTimeAnnotatedPattern = dateTimeAnnotatedPattern;
}
public DateTime getDateTimeAnnotatedDefault() {
return dateTimeAnnotatedDefault;
}
public void setDateTimeAnnotatedDefault(DateTime dateTimeAnnotatedDefault) {
this.dateTimeAnnotatedDefault = dateTimeAnnotatedDefault;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDateAnnotated() {
return dateAnnotated;
}
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
}
public Calendar getCalendarAnnotated() {
return calendarAnnotated;
}
public void setCalendarAnnotated(Calendar calendarAnnotated) {
this.calendarAnnotated = calendarAnnotated;
}
public Long getMillis() {
return millis;
}
public void setMillis(Long millis) {
this.millis = millis;
}
@DateTimeFormat(style = "S-")
public Long getMillisAnnotated() {
return millisAnnotated;
}
public void setMillisAnnotated(@DateTimeFormat(style = "S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
}
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public DateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(DateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
public Instant getInstantAnnotated() {
return instantAnnotated;
}
public void setInstantAnnotated(Instant instantAnnotated) {
this.instantAnnotated = instantAnnotated;
}
public Instant getMutableDateTimeAnnotated() {
return mutableDateTimeAnnotated;
}
public void setMutableDateTimeAnnotated(Instant mutableDateTimeAnnotated) {
this.mutableDateTimeAnnotated = mutableDateTimeAnnotated;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public YearMonth getYearMonth() {
return yearMonth;
}
public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
public MonthDay getMonthDay() {
return monthDay;
}
public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}
public List<JodaTimeBean> getChildren() {
return children;
}
}
}
19
View Source File : DateFormattingTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* @author Phillip Webb
* @author Keith Donald
* @author Juergen Hoeller
*/
public clreplaced DateFormattingTests {
private final FormattingConversionService conversionService = new FormattingConversionService();
private DataBinder binder;
@Before
public void setUp() {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
setUp(registrar);
}
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);
}
@After
public void tearDown() {
LocaleContextHolder.setLocale(null);
}
@Test
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
}
@Test
public void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
}
@Test
public void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", new String[] { "10/31/09 12:00 PM" });
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "Oct X31, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("dateAnnotated"));
replacedertEquals("Oct X31, 2009", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
@Ignore
public void testBindDateAnnotatedWithFallbackError() {
// TODO This currently preplacedes because of the Date(String) constructor fallback is used
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", "Oct 031, 2009");
binder.bind(propertyValues);
replacedertEquals(1, binder.getBindingResult().getFieldErrorCount("dateAnnotated"));
replacedertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindDateAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09 1:05", binder.getBindingResult().getFieldValue("dateAnnotatedPattern"));
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDate", "2009-10-31");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoTime", "12:00:00.000-0500");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("17:00:00.000+0000", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000-0800");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("2009-10-31T20:00:00.000+0000", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindNestedDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("children[0].dateAnnotated", "10/31/09");
binder.bind(propertyValues);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10/31/09", binder.getBindingResult().getFieldValue("children[0].dateAnnotated"));
}
@Test
public void dateToStringWithoutGlobalFormat() throws Exception {
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = date.toString();
replacedertEquals(expected, actual);
}
@Test
public void dateToStringWithGlobalFormat() throws Exception {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
registrar.setFormatter(new DateFormatter());
setUp(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.clreplaced), TypeDescriptor.valueOf(String.clreplaced));
String expected = new DateFormatter().print(date, Locale.US);
replacedertEquals(expected, actual);
}
@Test
@SuppressWarnings("deprecation")
public void stringToDateWithoutGlobalFormat() throws Exception {
// SPR-10105
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertThat(date, equalTo(new Date(string)));
}
@Test
public void stringToDateWithGlobalFormat() throws Exception {
// SPR-10105
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
DateFormatter dateFormatter = new DateFormatter();
dateFormatter.setIso(ISO.DATE_TIME);
registrar.setFormatter(dateFormatter);
setUp(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-06-01T14:23:05.003+0000";
Date date = this.conversionService.convert(string, Date.clreplaced);
replacedertNotNull(date);
}
@SuppressWarnings("unused")
private static clreplaced SimpleDateBean {
private Long millis;
private Long millisAnnotated;
@DateTimeFormat(style = "S-")
private Calendar calendarAnnotated;
@DateTimeFormat(style = "S-")
private Date dateAnnotated;
@DateTimeFormat(pattern = "M/d/yy h:mm")
private Date dateAnnotatedPattern;
@DateTimeFormat(iso = ISO.DATE)
private Date isoDate;
@DateTimeFormat(iso = ISO.TIME)
private Date isoTime;
@DateTimeFormat(iso = ISO.DATE_TIME)
private Date isoDateTime;
private final List<SimpleDateBean> children = new ArrayList<SimpleDateBean>();
public Long getMillis() {
return millis;
}
public void setMillis(Long millis) {
this.millis = millis;
}
@DateTimeFormat(style = "S-")
public Long getMillisAnnotated() {
return millisAnnotated;
}
public void setMillisAnnotated(@DateTimeFormat(style = "S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
}
public Calendar getCalendarAnnotated() {
return calendarAnnotated;
}
public void setCalendarAnnotated(Calendar calendarAnnotated) {
this.calendarAnnotated = calendarAnnotated;
}
public Date getDateAnnotated() {
return dateAnnotated;
}
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
}
public Date getDateAnnotatedPattern() {
return dateAnnotatedPattern;
}
public void setDateAnnotatedPattern(Date dateAnnotatedPattern) {
this.dateAnnotatedPattern = dateAnnotatedPattern;
}
public Date getIsoDate() {
return isoDate;
}
public void setIsoDate(Date isoDate) {
this.isoDate = isoDate;
}
public Date getIsoTime() {
return isoTime;
}
public void setIsoTime(Date isoTime) {
this.isoTime = isoTime;
}
public Date getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(Date isoDateTime) {
this.isoDateTime = isoDateTime;
}
public List<SimpleDateBean> getChildren() {
return children;
}
}
}
19
View Source File : ReferenceBeanBuilder.java
License : Apache License 2.0
Project Creator : boomblog
License : Apache License 2.0
Project Creator : boomblog
@Override
protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) {
replacedert.notNull(interfaceClreplaced, "The interface clreplaced must set first!");
DataBinder dataBinder = new DataBinder(referenceBean);
// Set ConversionService
dataBinder.setConversionService(getConversionService());
// Ignore those fields
String[] ignoreAttributeNames = of("application", "module", "consumer", "monitor", "registry");
// dataBinder.setDisallowedFields(ignoreAttributeNames);
// Bind annotation attributes
dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), ignoreAttributeNames));
}
19
View Source File : ConfigurableContentNegotiationManagerWebMvcConfigurer.java
License : GNU General Public License v3.0
Project Creator : alibaba
License : GNU General Public License v3.0
Project Creator : alibaba
protected void configureContentNegotiationManagerFactoryBean(ContentNegotiationManagerFactoryBean factoryBean) {
DataBinder dataBinder = new DataBinder(factoryBean);
dataBinder.setDisallowedFields("contentNegotiationManager", "servletContext");
dataBinder.setAutoGrowNestedPaths(true);
dataBinder.registerCustomEditor(MediaType.clreplaced, "defaultContentType", new MediaTypePropertyEditor());
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValues(this.propertyValues);
dataBinder.bind(propertyValues);
}
18
View Source File : JodaTimeFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
}
18
View Source File : BindTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
PageContext pc = createPageContext();
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new ServletRequestDataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.clreplaced, 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]");
replacedertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
replacedertThat(status != null).as("Has status variable").isTrue();
replacedertThat("array[0]".equals(status.getExpression())).as("Correct expression").isTrue();
// because of the custom editor getValue() should return a String
boolean condition = status.getValue() instanceof String;
replacedertThat(condition).as("Value is TestBean").isTrue();
replacedertThat("something".equals(status.getValue())).as("Correct value").isTrue();
}
18
View Source File : ServletModelAttributeMethodProcessor.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* 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.clreplaced);
TypeDescriptor target = new TypeDescriptor(methodParam);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
}
}
return null;
}
18
View Source File : ApplicationDetailsMarkAsCompleteValidatorTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void validApplication() {
DataBinder dataBinder = new DataBinder(validApplication);
bindingResult = dataBinder.getBindingResult();
validator.validate(validApplication, bindingResult);
replacedertFalse(bindingResult.hasErrors());
}
18
View Source File : ApplicationDetailsMarkAsCompleteValidatorTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void valid_applicationDurationBeneathMinDurationShouldResultInError() {
validApplication.setDurationInMonths(9L);
DataBinder dataBinder = new DataBinder(validApplication);
bindingResult = dataBinder.getBindingResult();
validator.validate(validApplication, bindingResult);
replacedertFalse(simpleFilter(bindingResult.getFieldErrors(), error -> error.getField().equals("durationInMonths") && error.getDefaultMessage().equals("validation.project.duration.input.invalid") && (Integer) error.getArguments()[0] == 10 && (Integer) error.getArguments()[1] == 20).isEmpty());
}
18
View Source File : ApplicationDetailsMarkAsCompleteValidatorTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void valid_applicationDurationExceedsMaxDurationShouldResultInError() {
validApplication.setDurationInMonths(21L);
DataBinder dataBinder = new DataBinder(validApplication);
bindingResult = dataBinder.getBindingResult();
validator.validate(validApplication, bindingResult);
replacedertFalse(simpleFilter(bindingResult.getFieldErrors(), error -> error.getField().equals("durationInMonths") && error.getDefaultMessage().equals("validation.project.duration.input.invalid") && (Integer) error.getArguments()[0] == 10 && (Integer) error.getArguments()[1] == 20).isEmpty());
}
18
View Source File : ApplicationDetailsMarkAsCompleteValidatorTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void valid_applicationDurationIsEqualToMaxAndMinDurationShouldNotResultInError() {
validApplication.setDurationInMonths(10L);
validApplication.setCompereplacedion(newCompereplacedion().withMinProjectDuration(10).withResubmission(false).withMaxProjectDuration(10).build());
DataBinder dataBinder = new DataBinder(validApplication);
bindingResult = dataBinder.getBindingResult();
validator.validate(validApplication, bindingResult);
replacedertTrue(simpleFilter(bindingResult.getFieldErrors(), error -> error.getField().equals("durationInMonths")).isEmpty());
}
18
View Source File : MarsConfigurationPropertiesBinder.java
License : Apache License 2.0
Project Creator : fashionbrot
License : Apache License 2.0
Project Creator : fashionbrot
private void bindBean(Object bean, MarsConfigurationProperties properties, PropertyValues propertyValues) {
ObjectUtils.cleanMapOrCollectionField(bean);
DataBinder dataBinder = new DataBinder(bean);
dataBinder.setIgnoreInvalidFields(properties.ignoreInvalidFields());
dataBinder.setIgnoreUnknownFields(properties.ignoreUnknownFields());
dataBinder.setAutoGrowNestedPaths(properties.autoGrowNestedPaths());
dataBinder.bind(propertyValues);
}
17
View Source File : BindTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
PageContext pc = createPageContext();
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new ServletRequestDataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.clreplaced, 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]");
replacedertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
replacedertTrue("Has status variable", status != null);
replacedertTrue("Correct expression", "array[0]".equals(status.getExpression()));
// because of the custom editor getValue() should return a String
replacedertTrue("Value is TestBean", status.getValue() instanceof String);
replacedertTrue("Correct value", "something".equals(status.getValue()));
}
17
View Source File : MoneyFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
replacedertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
replacedertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
17
View Source File : MoneyFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
17
View Source File : MoneyFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10.5", binder.getBindingResult().getFieldValue("amount"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
17
View Source File : MoneyFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("$10.50", binder.getBindingResult().getFieldValue("amount"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("$10.50", binder.getBindingResult().getFieldValue("amount"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("CAD", bean.getAmount().getCurrency().getCurrencyCode());
}
17
View Source File : MoneyFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("10%", binder.getBindingResult().getFieldValue("amount"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
17
View Source File : MoneyFormattingTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(0, binder.getBindingResult().getErrorCount());
replacedertEquals("010.500", binder.getBindingResult().getFieldValue("amount"));
replacedertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
replacedertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
17
View Source File : MoneyFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("010.500");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
}
17
View Source File : MoneyFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("$10.50");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("$10.50");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("CAD");
}
17
View Source File : MoneyFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("USD10.50");
replacedertThat(binder.getBindingResult().getFieldValue("unit")).isEqualTo("USD");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("USD10.50");
replacedertThat(binder.getBindingResult().getFieldValue("unit")).isEqualTo("USD");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
}
17
View Source File : MoneyFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("10.5");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
}
17
View Source File : MoneyFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("USD 010.500");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("USD 010.500");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 10.5d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
}
17
View Source File : MoneyFormattingTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
replacedertThat(binder.getBindingResult().getFieldValue("amount")).isEqualTo("10%");
replacedertThat(bean.getAmount().getNumber().doubleValue() == 0.1d).isTrue();
replacedertThat(bean.getAmount().getCurrency().getCurrencyCode()).isEqualTo("USD");
}
17
View Source File : DefaultDubboConfigBinder.java
License : Apache License 2.0
Project Creator : smallFive55
License : Apache License 2.0
Project Creator : smallFive55
@Override
public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) {
DataBinder dataBinder = new DataBinder(dubboConfig);
// Set ignored*
dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields());
dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields());
// Get properties under specified prefix from PropertySources
Map<String, Object> properties = getSubProperties(getPropertySources(), prefix);
// Convert Map to MutablePropertyValues
MutablePropertyValues propertyValues = new MutablePropertyValues(properties);
// Bind
dataBinder.bind(propertyValues);
}
17
View Source File : DataBinderBootstrap.java
License : Apache License 2.0
Project Creator : mercyblitz
License : Apache License 2.0
Project Creator : mercyblitz
public static void main(String[] args) {
Integer integer = new Integer(0);
DataBinder dataBinder = new DataBinder(integer);
dataBinder.setIgnoreUnknownFields(false);
dataBinder.setIgnoreInvalidFields(false);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("value", 2);
dataBinder.bind(propertyValues);
System.out.println(integer);
}
17
View Source File : TaskController.java
License : GNU General Public License v3.0
Project Creator : marcinadd
License : GNU General Public License v3.0
Project Creator : marcinadd
@PatchMapping("/{taskId}")
public Task editTaskDetailsPatch(@PathVariable Long taskId, @RequestBody Task task) throws BindException {
task.setId(taskId);
Task newTaskCandidate = taskService.findTaskInRepositoryAndUpdateFields(task);
if (newTaskCandidate != null && projectService.hasCurrentUserPermissionToEdit(newTaskCandidate.getProject())) {
DataBinder dataBinder = new DataBinder(newTaskCandidate);
dataBinder.setValidator(taskValidator);
dataBinder.validate();
BindingResult result = dataBinder.getBindingResult();
if (result.hasErrors()) {
throw new BindException(result);
}
return taskRepository.save(newTaskCandidate);
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
17
View Source File : ApplicationResearchMarkAsCompleteValidatorTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void validate() {
Application application = ApplicationBuilder.newApplication().withResearchCategory(newResearchCategory().build()).build();
DataBinder binder = new DataBinder(application);
BindingResult bindingResult = binder.getBindingResult();
validator.validate(application, bindingResult);
replacedertFalse(bindingResult.hasErrors());
}
17
View Source File : ApplicationDetailsMarkAsCompleteValidatorTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void valid_applicationInnovationAreaIsApplicableAndSet() {
Application validApplicationInnovationAreaApplicableAndSet = newApplication().withName("Valid Application").withStartDate(currentDate.plusDays(1)).withDurationInMonths(18L).withNoInnovationAreaApplicable(false).withInnovationArea(newInnovationArea().build()).withResubmission(true).withCompereplacedion(compereplacedion).withPreviousApplicationNumber("Previous Application Number").withPreviousApplicationreplacedle("Failed Application").withResearchCategory(newResearchCategory().build()).build();
DataBinder dataBinder = new DataBinder(validApplicationInnovationAreaApplicableAndSet);
bindingResult = dataBinder.getBindingResult();
validator.validate(validApplicationInnovationAreaApplicableAndSet, bindingResult);
replacedertFalse(bindingResult.hasErrors());
}
17
View Source File : ApplicationDetailsMarkAsCompleteValidatorTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void validProcurementApplication() {
Application validProcurementApplication = newApplication().withName("Valid Procurement Application").withStartDate(currentDate.plusDays(1)).withDurationInMonths(18L).withNoInnovationAreaApplicable(true).withResubmission(true).withCompereplacedion(procurementCompereplacedion).withPreviousApplicationNumber("Previous Application Number").withPreviousApplicationreplacedle("Failed Application").withCompereplacedionReferralSource(BUSINESS_CONTACT).withCompereplacedionPrimaryFocus(CHEMICALS).withCompanyAge(PRE_START_UP).withResearchCategory(newResearchCategory().build()).build();
DataBinder dataBinder = new DataBinder(validProcurementApplication);
bindingResult = dataBinder.getBindingResult();
validator.validate(validProcurementApplication, bindingResult);
replacedertFalse(bindingResult.hasErrors());
}
17
View Source File : DefaultDubboConfigBinder.java
License : Apache License 2.0
Project Creator : boomblog
License : Apache License 2.0
Project Creator : boomblog
@Override
public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) {
DataBinder dataBinder = new DataBinder(dubboConfig);
// Set ignored*
dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields());
dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields());
// Get properties under specified prefix from PropertySources
Map<String, String> properties = getSubProperties(getPropertySources(), prefix);
// Convert Map to MutablePropertyValues
MutablePropertyValues propertyValues = new MutablePropertyValues(properties);
// Bind
dataBinder.bind(propertyValues);
}
17
View Source File : DefaultConfigurationBeanBinder.java
License : Apache License 2.0
Project Creator : alibaba
License : Apache License 2.0
Project Creator : alibaba
@Override
public void bind(Map<String, Object> configurationProperties, boolean ignoreUnknownFields, boolean ignoreInvalidFields, Object configurationBean) {
DataBinder dataBinder = new DataBinder(configurationBean);
// Set ignored*
dataBinder.setIgnoreInvalidFields(ignoreUnknownFields);
dataBinder.setIgnoreUnknownFields(ignoreInvalidFields);
// Get properties under specified prefix from PropertySources
// Convert Map to MutablePropertyValues
MutablePropertyValues propertyValues = new MutablePropertyValues(configurationProperties);
// Bind
dataBinder.bind(propertyValues);
}
17
View Source File : DubboGenericServiceFactory.java
License : Apache License 2.0
Project Creator : alibaba
License : Apache License 2.0
Project Creator : alibaba
private void bindReferenceBean(ReferenceBean<GenericService> referenceBean, Map<String, Object> dubboTranslatedAttributes) {
DataBinder dataBinder = new DataBinder(referenceBean);
// Register CustomEditors for special fields
dataBinder.registerCustomEditor(String.clreplaced, "filter", new StringTrimmerEditor(true));
dataBinder.registerCustomEditor(String.clreplaced, "listener", new StringTrimmerEditor(true));
dataBinder.registerCustomEditor(Map.clreplaced, "parameters", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws java.lang.IllegalArgumentException {
// Trim all whitespace
String content = StringUtils.trimAllWhitespace(text);
if (!StringUtils.hasText(content)) {
// No content , ignore
// directly
return;
}
// replace "=" to ","
content = StringUtils.replace(content, "=", ",");
// replace ":" to ","
content = StringUtils.replace(content, ":", ",");
// String[] to Map
Map<String, String> parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content));
setValue(parameters);
}
});
// ignore "registries" field and then use RegistryConfig beans
dataBinder.setDisallowedFields("registries");
dataBinder.bind(new MutablePropertyValues(dubboTranslatedAttributes));
registryConfigs.ifAvailable(referenceBean::setRegistries);
}
See More Examples