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 16
16
17
17
package org .springframework .core .convert .support ;
18
18
19
+ import java .util .HashSet ;
20
+ import java .util .Set ;
21
+
19
22
import org .springframework .core .convert .converter .Converter ;
23
+ import org .springframework .util .StringUtils ;
20
24
21
25
/**
22
26
* Converts String to a Boolean.
23
27
*
24
28
* @author Keith Donald
29
+ * @author Juergen Hoeller
25
30
* @since 3.0
26
31
*/
27
32
final class StringToBooleanConverter implements Converter <String , Boolean > {
28
33
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
+
29
53
public Boolean convert (String source ) {
30
- if (source .equals ("" )) {
54
+ String value = (source != null ? source .trim () : null );
55
+ if (!StringUtils .hasLength (value )) {
31
56
return null ;
32
- } else if (source .equals ("true" )) {
57
+ }
58
+ else if (trueValues .contains (value )) {
33
59
return Boolean .TRUE ;
34
60
}
35
- else if (source . equals ( "false" )) {
61
+ else if (falseValues . contains ( value )) {
36
62
return Boolean .FALSE ;
37
63
}
38
64
else {
39
- throw new IllegalArgumentException ("Invalid boolean string '" + source + "'; expected \" \" , 'true', or 'false '" );
65
+ throw new IllegalArgumentException ("Invalid boolean value '" + source + "'" );
40
66
}
41
67
}
42
68
Original file line number Diff line number Diff line change 16
16
17
17
package org .springframework .core .convert .support ;
18
18
19
- import static org .junit .Assert .assertEquals ;
20
- import static org .junit .Assert .fail ;
21
-
22
19
import java .math .BigDecimal ;
23
20
import java .math .BigInteger ;
24
21
import java .util .Locale ;
25
22
23
+ import static org .junit .Assert .*;
26
24
import org .junit .Test ;
25
+
27
26
import org .springframework .core .convert .converter .Converter ;
28
27
29
28
/**
@@ -56,12 +55,18 @@ public void testStringToCharacterInvalidString() {
56
55
public void testStringToBooleanTrue () {
57
56
StringToBooleanConverter c = new StringToBooleanConverter ();
58
57
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" ));
59
61
}
60
62
61
63
@ Test
62
64
public void testStringToBooleanFalse () {
63
65
StringToBooleanConverter c = new StringToBooleanConverter ();
64
66
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" ));
65
70
}
66
71
67
72
@ Test
You can’t perform that action at this time.
0 commit comments