    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    *      http://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.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              // Exception is successful test
    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              // Exception is successful test
    87          }
    88          try {
    89              converter.convert(Boolean.class, "bogus");
    90              fail("Converting invalid string should have generated an exception");
    91          } catch (ConversionException expected) {
    92              // Exception is successful test
    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 }