001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    
018    package org.apache.commons.beanutils.converters;
019    
020    import org.apache.commons.beanutils.ConversionException;
021    
022    import junit.framework.TestCase;
023    
024    
025    public class BooleanConverterTestCase extends TestCase {
026    
027        public static final String[] STANDARD_TRUES = new String[] {
028                "yes", "y", "true", "on", "1"
029            };
030    
031        public static final String[] STANDARD_FALSES = new String[] {
032                "no", "n", "false", "off", "0"
033            };
034    
035    
036        public BooleanConverterTestCase(String name) {
037            super(name);
038        }
039    
040        public void testStandardValues() {
041            BooleanConverter converter = new BooleanConverter();
042            testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
043        }
044    
045        public void testCaseInsensitivity() {
046            BooleanConverter converter = new BooleanConverter();
047            testConversionValues(
048                converter,
049                new String[] {"Yes", "TRUE"},
050                new String[] {"NO", "fAlSe"});
051        }
052    
053    
054        public void testInvalidString() {
055            BooleanConverter converter = new BooleanConverter();
056            try {
057                converter.convert(Boolean.class, "bogus");
058                fail("Converting invalid string should have generated an exception");
059            } catch (ConversionException expected) {
060                // Exception is successful test
061            }
062        }
063    
064        public void testDefaultValue() {
065            Object defaultValue = Boolean.TRUE;
066            BooleanConverter converter = new BooleanConverter(defaultValue);
067    
068            assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
069            testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
070        }
071    
072        public void testAdditionalStrings() {
073            String[] trueStrings = {"sure"};
074            String[] falseStrings = {"nope"};
075            BooleanConverter converter = new BooleanConverter(
076                trueStrings, falseStrings, BooleanConverter.NO_DEFAULT);
077            testConversionValues(
078                converter,
079                new String[] {"sure", "Sure"},
080                new String[] {"nope", "nOpE"});
081    
082            try {
083                converter.convert(Boolean.class, "true");
084                fail("Converting obsolete true value should have generated an exception");
085            } catch (ConversionException expected) {
086                // Exception is successful test
087            }
088            try {
089                converter.convert(Boolean.class, "bogus");
090                fail("Converting invalid string should have generated an exception");
091            } catch (ConversionException expected) {
092                // Exception is successful test
093            }
094        }
095    
096    
097        protected void testConversionValues(BooleanConverter converter, 
098                String[] trueValues, String[] falseValues) {
099    
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    }