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 org.apache.commons.beanutils.ConversionException;
21
22 import junit.framework.TestCase;
23
24
25 public class BooleanConverterTestCase extends TestCase {
26
27 public static final String[] STANDARD_TRUES = new String[] {
28 "yes", "y", "true", "on", "1"
29 };
30
31 public static final String[] STANDARD_FALSES = new String[] {
32 "no", "n", "false", "off", "0"
33 };
34
35
36 public BooleanConverterTestCase(String name) {
37 super(name);
38 }
39
40 public void testStandardValues() {
41 BooleanConverter converter = new BooleanConverter();
42 testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
43 }
44
45 public void testCaseInsensitivity() {
46 BooleanConverter converter = new BooleanConverter();
47 testConversionValues(
48 converter,
49 new String[] {"Yes", "TRUE"},
50 new String[] {"NO", "fAlSe"});
51 }
52
53
54 public void testInvalidString() {
55 BooleanConverter converter = new BooleanConverter();
56 try {
57 converter.convert(Boolean.class, "bogus");
58 fail("Converting invalid string should have generated an exception");
59 } catch (ConversionException expected) {
60
61 }
62 }
63
64 public void testDefaultValue() {
65 Object defaultValue = Boolean.TRUE;
66 BooleanConverter converter = new BooleanConverter(defaultValue);
67
68 assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
69 testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
70 }
71
72 public void testAdditionalStrings() {
73 String[] trueStrings = {"sure"};
74 String[] falseStrings = {"nope"};
75 BooleanConverter converter = new BooleanConverter(
76 trueStrings, falseStrings, BooleanConverter.NO_DEFAULT);
77 testConversionValues(
78 converter,
79 new String[] {"sure", "Sure"},
80 new String[] {"nope", "nOpE"});
81
82 try {
83 converter.convert(Boolean.class, "true");
84 fail("Converting obsolete true value should have generated an exception");
85 } catch (ConversionException expected) {
86
87 }
88 try {
89 converter.convert(Boolean.class, "bogus");
90 fail("Converting invalid string should have generated an exception");
91 } catch (ConversionException expected) {
92
93 }
94 }
95
96
97 protected void testConversionValues(BooleanConverter converter,
98 String[] trueValues, String[] falseValues) {
99
100 for (int i = 0; i < trueValues.length; i++) {
101 assertEquals(Boolean.TRUE, converter.convert(Boolean.class, trueValues[i]));
102 }
103 for (int i = 0; i < falseValues.length; i++) {
104 assertEquals(Boolean.FALSE, converter.convert(Boolean.class, falseValues[i]));
105 }
106 }
107
108 }