1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils2.converters;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import org.apache.commons.beanutils2.ConversionException;
26 import org.junit.jupiter.api.Test;
27
28
29
30 public class BooleanConverterTest {
31
32 public static final String[] STANDARD_TRUES = { "yes", "y", "true", "on", "1" };
33
34 public static final String[] STANDARD_FALSES = { "no", "n", "false", "off", "0" };
35
36 @Test
37 public void testAdditionalStrings() {
38 final String[] trueStrings = { "sure" };
39 final String[] falseStrings = { "nope" };
40 final AbstractConverter<Boolean> converter = new BooleanConverter(trueStrings, falseStrings);
41 testConversionValues(converter, new String[] { "sure", "Sure" }, new String[] { "nope", "nOpE" });
42
43 assertThrows(ConversionException.class, () -> converter.convert(Boolean.class, "true"));
44 assertThrows(ConversionException.class, () -> converter.convert(Boolean.class, "bogus"));
45 }
46
47 @Test
48 public void testCaseInsensitivity() {
49 final AbstractConverter<Boolean> converter = new BooleanConverter();
50 testConversionValues(converter, new String[] { "Yes", "TRUE" }, new String[] { "NO", "fAlSe" });
51 }
52
53
54
55
56 @Test
57 public void testConversionToOtherType() {
58 final AbstractConverter<Boolean> converter = new BooleanConverter();
59 assertThrows(ConversionException.class, () -> converter.convert(Integer.class, STANDARD_TRUES[0]));
60 }
61
62 protected void testConversionValues(final AbstractConverter<Boolean> converter, final String[] trueValues, final String[] falseValues) {
63
64 for (final String trueValue : trueValues) {
65 assertEquals(Boolean.TRUE, converter.convert(Boolean.class, trueValue));
66 }
67 for (final String falseValue : falseValues) {
68 assertEquals(Boolean.FALSE, converter.convert(Boolean.class, falseValue));
69 }
70 }
71
72 @Test
73 public void testDefaultValue() {
74 final Boolean defaultValue = Boolean.TRUE;
75 final AbstractConverter<Boolean> converter = new BooleanConverter(defaultValue);
76
77 assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
78 testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
79 }
80
81 @Test
82 public void testInvalidString() {
83 final AbstractConverter<Boolean> converter = new BooleanConverter();
84 assertThrows(ConversionException.class, () -> converter.convert(Boolean.class, "bogus"));
85 }
86
87
88
89
90 @Test
91 public void testPrimitiveTargetClass() {
92 final AbstractConverter<Boolean> converter = new BooleanConverter();
93 assertTrue(converter.convert(Boolean.TYPE, STANDARD_TRUES[0]), "Wrong result");
94 }
95
96 @Test
97 public void testStandardValues() {
98 final AbstractConverter<Boolean> converter = new BooleanConverter();
99 testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
100 }
101
102 }