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    *      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 junit.framework.TestCase;
21  
22  import org.apache.commons.beanutils.ConversionException;
23  
24  /**
25   * @version $Id$
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              // Exception is successful test
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              // Exception is successful test
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              // Exception is successful test
95          }
96      }
97  
98      /**
99       * Tests a conversion to another target type. This should not be possible.
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             // Expected result
108         }
109     }
110 
111     /**
112      * Tests whether a conversion to a primitive boolean is possible.
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 }