View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * Tests a conversion to another target type. This should not be possible.
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       * Tests whether a conversion to a primitive boolean is possible.
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 }