Skip to content

Commit cca44f4

Browse files
committed
added test for correct handling of ParseException from Formatter for String->String case (SPR-8944)
1 parent 9b0412f commit cca44f4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

org.springframework.context/src/test/java/org/springframework/validation/DataBinderTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.ObjectInputStream;
2424
import java.io.ObjectOutputStream;
25+
import java.text.ParseException;
2526
import java.util.AbstractList;
2627
import java.util.ArrayList;
2728
import java.util.Collection;
@@ -52,6 +53,7 @@
5253
import org.springframework.context.support.ResourceBundleMessageSource;
5354
import org.springframework.context.support.StaticMessageSource;
5455
import org.springframework.core.convert.support.ConversionServiceFactory;
56+
import org.springframework.format.Formatter;
5557
import org.springframework.format.number.NumberFormatter;
5658
import org.springframework.format.support.FormattingConversionService;
5759
import org.springframework.util.StringUtils;
@@ -373,6 +375,28 @@ public void testBindingErrorWithFormatter() {
373375
}
374376
}
375377

378+
public void testBindingErrorWithStringFormatter() {
379+
TestBean tb = new TestBean();
380+
DataBinder binder = new DataBinder(tb);
381+
FormattingConversionService conversionService = new FormattingConversionService();
382+
ConversionServiceFactory.addDefaultConverters(conversionService);
383+
conversionService.addFormatterForFieldType(String.class, new Formatter<String>() {
384+
public String parse(String text, Locale locale) throws ParseException {
385+
throw new ParseException(text, 0);
386+
}
387+
public String print(String object, Locale locale) {
388+
return object;
389+
}
390+
});
391+
binder.setConversionService(conversionService);
392+
MutablePropertyValues pvs = new MutablePropertyValues();
393+
pvs.add("name", "test");
394+
395+
binder.bind(pvs);
396+
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
397+
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
398+
}
399+
376400
public void testBindingWithFormatterAgainstList() {
377401
BeanWithIntegerList tb = new BeanWithIntegerList();
378402
DataBinder binder = new DataBinder(tb);

0 commit comments

Comments
 (0)