|
|
|
@ -403,11 +403,12 @@ public class DataBinderTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBindingErrorWithStringFormatter() { |
|
|
|
public void testBindingErrorWithParseExceptionFromFormatter() { |
|
|
|
TestBean tb = new TestBean(); |
|
|
|
TestBean tb = new TestBean(); |
|
|
|
DataBinder binder = new DataBinder(tb); |
|
|
|
DataBinder binder = new DataBinder(tb); |
|
|
|
FormattingConversionService conversionService = new FormattingConversionService(); |
|
|
|
FormattingConversionService conversionService = new FormattingConversionService(); |
|
|
|
DefaultConversionService.addDefaultConverters(conversionService); |
|
|
|
DefaultConversionService.addDefaultConverters(conversionService); |
|
|
|
|
|
|
|
|
|
|
|
conversionService.addFormatter(new Formatter<String>() { |
|
|
|
conversionService.addFormatter(new Formatter<String>() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String parse(String text, Locale locale) throws ParseException { |
|
|
|
public String parse(String text, Locale locale) throws ParseException { |
|
|
|
@ -418,6 +419,35 @@ public class DataBinderTests { |
|
|
|
return object; |
|
|
|
return object; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
binder.setConversionService(conversionService); |
|
|
|
|
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
|
|
|
|
pvs.add("name", "test"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
binder.bind(pvs); |
|
|
|
|
|
|
|
assertTrue(binder.getBindingResult().hasFieldErrors("name")); |
|
|
|
|
|
|
|
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); |
|
|
|
|
|
|
|
assertEquals("test", binder.getBindingResult().getFieldValue("name")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testBindingErrorWithRuntimeExceptionFromFormatter() { |
|
|
|
|
|
|
|
TestBean tb = new TestBean(); |
|
|
|
|
|
|
|
DataBinder binder = new DataBinder(tb); |
|
|
|
|
|
|
|
FormattingConversionService conversionService = new FormattingConversionService(); |
|
|
|
|
|
|
|
DefaultConversionService.addDefaultConverters(conversionService); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conversionService.addFormatter(new Formatter<String>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String parse(String text, Locale locale) throws ParseException { |
|
|
|
|
|
|
|
throw new RuntimeException(text); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String print(String object, Locale locale) { |
|
|
|
|
|
|
|
return object; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
binder.setConversionService(conversionService); |
|
|
|
binder.setConversionService(conversionService); |
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
pvs.add("name", "test"); |
|
|
|
pvs.add("name", "test"); |
|
|
|
@ -581,9 +611,10 @@ public class DataBinderTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBindingErrorWithCustomStringFormatter() { |
|
|
|
public void testBindingErrorWithParseExceptionFromCustomFormatter() { |
|
|
|
TestBean tb = new TestBean(); |
|
|
|
TestBean tb = new TestBean(); |
|
|
|
DataBinder binder = new DataBinder(tb); |
|
|
|
DataBinder binder = new DataBinder(tb); |
|
|
|
|
|
|
|
|
|
|
|
binder.addCustomFormatter(new Formatter<String>() { |
|
|
|
binder.addCustomFormatter(new Formatter<String>() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String parse(String text, Locale locale) throws ParseException { |
|
|
|
public String parse(String text, Locale locale) throws ParseException { |
|
|
|
@ -594,12 +625,39 @@ public class DataBinderTests { |
|
|
|
return object; |
|
|
|
return object; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
pvs.add("name", "test"); |
|
|
|
pvs.add("name", "test"); |
|
|
|
|
|
|
|
|
|
|
|
binder.bind(pvs); |
|
|
|
binder.bind(pvs); |
|
|
|
assertTrue(binder.getBindingResult().hasFieldErrors("name")); |
|
|
|
assertTrue(binder.getBindingResult().hasFieldErrors("name")); |
|
|
|
assertEquals("test", binder.getBindingResult().getFieldValue("name")); |
|
|
|
assertEquals("test", binder.getBindingResult().getFieldValue("name")); |
|
|
|
|
|
|
|
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testBindingErrorWithRuntimeExceptionFromCustomFormatter() { |
|
|
|
|
|
|
|
TestBean tb = new TestBean(); |
|
|
|
|
|
|
|
DataBinder binder = new DataBinder(tb); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
binder.addCustomFormatter(new Formatter<String>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String parse(String text, Locale locale) throws ParseException { |
|
|
|
|
|
|
|
throw new RuntimeException(text); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String print(String object, Locale locale) { |
|
|
|
|
|
|
|
return object; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
|
|
|
|
pvs.add("name", "test"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
binder.bind(pvs); |
|
|
|
|
|
|
|
assertTrue(binder.getBindingResult().hasFieldErrors("name")); |
|
|
|
|
|
|
|
assertEquals("test", binder.getBindingResult().getFieldValue("name")); |
|
|
|
|
|
|
|
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -991,7 +1049,7 @@ public class DataBinderTests { |
|
|
|
}, "age"); |
|
|
|
}, "age"); |
|
|
|
|
|
|
|
|
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
MutablePropertyValues pvs = new MutablePropertyValues(); |
|
|
|
pvs.add("age", ""); |
|
|
|
pvs.add("age", "x"); |
|
|
|
binder.bind(pvs); |
|
|
|
binder.bind(pvs); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals("argh", binder.getBindingResult().getFieldValue("age")); |
|
|
|
assertEquals("argh", binder.getBindingResult().getFieldValue("age")); |
|
|
|
|