File tree Expand file tree Collapse file tree 2 files changed +38
-7
lines changed
org.springframework.core/src
main/java/org/springframework/core/convert/support
test/java/org/springframework/core/convert/support Expand file tree Collapse file tree 2 files changed +38
-7
lines changed Original file line number Diff line number Diff line change 1616
1717package org .springframework .core .convert .support ;
1818
19+ import java .util .HashSet ;
20+ import java .util .Set ;
21+
1922import org .springframework .core .convert .converter .Converter ;
23+ import org .springframework .util .StringUtils ;
2024
2125/**
2226 * Converts String to a Boolean.
2327 *
2428 * @author Keith Donald
29+ * @author Juergen Hoeller
2530 * @since 3.0
2631 */
2732final class StringToBooleanConverter implements Converter <String , Boolean > {
2833
34+ private static final Set <String > trueValues = new HashSet <String >(4 );
35+
36+ private static final Set <String > falseValues = new HashSet <String >(4 );
37+
38+ static {
39+ trueValues .add ("true" );
40+ falseValues .add ("false" );
41+
42+ trueValues .add ("on" );
43+ falseValues .add ("off" );
44+
45+ trueValues .add ("yes" );
46+ falseValues .add ("no" );
47+
48+ trueValues .add ("1" );
49+ falseValues .add ("0" );
50+ }
51+
52+
2953 public Boolean convert (String source ) {
30- if (source .equals ("" )) {
54+ String value = (source != null ? source .trim () : null );
55+ if (!StringUtils .hasLength (value )) {
3156 return null ;
32- } else if (source .equals ("true" )) {
57+ }
58+ else if (trueValues .contains (value )) {
3359 return Boolean .TRUE ;
3460 }
35- else if (source . equals ( "false" )) {
61+ else if (falseValues . contains ( value )) {
3662 return Boolean .FALSE ;
3763 }
3864 else {
39- throw new IllegalArgumentException ("Invalid boolean string '" + source + "'; expected \" \" , 'true', or 'false '" );
65+ throw new IllegalArgumentException ("Invalid boolean value '" + source + "'" );
4066 }
4167 }
4268
Original file line number Diff line number Diff line change 1616
1717package org .springframework .core .convert .support ;
1818
19- import static org .junit .Assert .assertEquals ;
20- import static org .junit .Assert .fail ;
21-
2219import java .math .BigDecimal ;
2320import java .math .BigInteger ;
2421import java .util .Locale ;
2522
23+ import static org .junit .Assert .*;
2624import org .junit .Test ;
25+
2726import org .springframework .core .convert .converter .Converter ;
2827
2928/**
@@ -56,12 +55,18 @@ public void testStringToCharacterInvalidString() {
5655 public void testStringToBooleanTrue () {
5756 StringToBooleanConverter c = new StringToBooleanConverter ();
5857 assertEquals (Boolean .valueOf (true ), c .convert ("true" ));
58+ assertEquals (Boolean .valueOf (true ), c .convert ("on" ));
59+ assertEquals (Boolean .valueOf (true ), c .convert ("yes" ));
60+ assertEquals (Boolean .valueOf (true ), c .convert ("1" ));
5961 }
6062
6163 @ Test
6264 public void testStringToBooleanFalse () {
6365 StringToBooleanConverter c = new StringToBooleanConverter ();
6466 assertEquals (Boolean .valueOf (false ), c .convert ("false" ));
67+ assertEquals (Boolean .valueOf (false ), c .convert ("off" ));
68+ assertEquals (Boolean .valueOf (false ), c .convert ("no" ));
69+ assertEquals (Boolean .valueOf (false ), c .convert ("0" ));
6570 }
6671
6772 @ Test
You can’t perform that action at this time.
0 commit comments