1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils.converters;
19
20 import junit.framework.TestCase;
21
22 import org.apache.commons.beanutils.ConversionException;
23
24
25
26
27 public class BooleanConverterTestCase extends TestCase {
28
29 public static final String[] STANDARD_TRUES = new String[] {
30 "yes", "y", "true", "on", "1"
31 };
32
33 public static final String[] STANDARD_FALSES = new String[] {
34 "no", "n", "false", "off", "0"
35 };
36
37
38 public BooleanConverterTestCase(final String name) {
39 super(name);
40 }
41
42 public void testStandardValues() {
43 final BooleanConverter converter = new BooleanConverter();
44 testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
45 }
46
47 public void testCaseInsensitivity() {
48 final BooleanConverter converter = new BooleanConverter();
49 testConversionValues(
50 converter,
51 new String[] {"Yes", "TRUE"},
52 new String[] {"NO", "fAlSe"});
53 }
54
55
56 public void testInvalidString() {
57 final BooleanConverter converter = new BooleanConverter();
58 try {
59 converter.convert(Boolean.class, "bogus");
60 fail("Converting invalid string should have generated an exception");
61 } catch (final ConversionException expected) {
62
63 }
64 }
65
66 public void testDefaultValue() {
67 final Object defaultValue = Boolean.TRUE;
68 final BooleanConverter converter = new BooleanConverter(defaultValue);
69
70 assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
71 testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
72 }
73
74 public void testAdditionalStrings() {
75 final String[] trueStrings = {"sure"};
76 final String[] falseStrings = {"nope"};
77 final BooleanConverter converter = new BooleanConverter(
78 trueStrings, falseStrings, BooleanConverter.NO_DEFAULT);
79 testConversionValues(
80 converter,
81 new String[] {"sure", "Sure"},
82 new String[] {"nope", "nOpE"});
83
84 try {
85 converter.convert(Boolean.class, "true");
86 fail("Converting obsolete true value should have generated an exception");
87 } catch (final ConversionException expected) {
88
89 }
90 try {
91 converter.convert(Boolean.class, "bogus");
92 fail("Converting invalid string should have generated an exception");
93 } catch (final ConversionException expected) {
94
95 }
96 }
97
98
99
100
101 public void testConversionToOtherType() {
102 final BooleanConverter converter = new BooleanConverter();
103 try {
104 converter.convert(Integer.class, STANDARD_TRUES[0]);
105 fail("Could convert to unsupported type!");
106 } catch (final ConversionException cex) {
107
108 }
109 }
110
111
112
113
114 public void testPrimitiveTargetClass() {
115 final BooleanConverter converter = new BooleanConverter();
116 assertTrue("Wrong result", converter.convert(Boolean.TYPE, STANDARD_TRUES[0]));
117 }
118
119 protected void testConversionValues(final BooleanConverter converter,
120 final String[] trueValues, final String[] falseValues) {
121
122 for (String trueValue : trueValues) {
123 assertEquals(Boolean.TRUE, converter.convert(Boolean.class, trueValue));
124 }
125 for (String falseValue : falseValues) {
126 assertEquals(Boolean.FALSE, converter.convert(Boolean.class, falseValue));
127 }
128 }
129
130 }