alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Beanutils example source code file (BooleanConverterTestCase.java)

This example Commons Beanutils source code file (BooleanConverterTestCase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Beanutils tags/keywords

booleanconverter, booleanconverter, booleanconvertertestcase, conversionexception, conversionexception, converting, no, standard_falses, standard_falses, standard_trues, string, string, testcase, true

The Commons Beanutils BooleanConverterTestCase.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package org.apache.commons.beanutils.converters;

import org.apache.commons.beanutils.ConversionException;

import junit.framework.TestCase;


public class BooleanConverterTestCase extends TestCase {

    public static final String[] STANDARD_TRUES = new String[] {
            "yes", "y", "true", "on", "1"
        };

    public static final String[] STANDARD_FALSES = new String[] {
            "no", "n", "false", "off", "0"
        };


    public BooleanConverterTestCase(String name) {
        super(name);
    }

    public void testStandardValues() {
        BooleanConverter converter = new BooleanConverter();
        testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
    }

    public void testCaseInsensitivity() {
        BooleanConverter converter = new BooleanConverter();
        testConversionValues(
            converter,
            new String[] {"Yes", "TRUE"},
            new String[] {"NO", "fAlSe"});
    }


    public void testInvalidString() {
        BooleanConverter converter = new BooleanConverter();
        try {
            converter.convert(Boolean.class, "bogus");
            fail("Converting invalid string should have generated an exception");
        } catch (ConversionException expected) {
            // Exception is successful test
        }
    }

    public void testDefaultValue() {
        Object defaultValue = Boolean.TRUE;
        BooleanConverter converter = new BooleanConverter(defaultValue);

        assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
        testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
    }

    public void testAdditionalStrings() {
        String[] trueStrings = {"sure"};
        String[] falseStrings = {"nope"};
        BooleanConverter converter = new BooleanConverter(
            trueStrings, falseStrings, BooleanConverter.NO_DEFAULT);
        testConversionValues(
            converter,
            new String[] {"sure", "Sure"},
            new String[] {"nope", "nOpE"});

        try {
            converter.convert(Boolean.class, "true");
            fail("Converting obsolete true value should have generated an exception");
        } catch (ConversionException expected) {
            // Exception is successful test
        }
        try {
            converter.convert(Boolean.class, "bogus");
            fail("Converting invalid string should have generated an exception");
        } catch (ConversionException expected) {
            // Exception is successful test
        }
    }


    protected void testConversionValues(BooleanConverter converter, 
            String[] trueValues, String[] falseValues) {

        for (int i = 0; i < trueValues.length; i++) {
            assertEquals(Boolean.TRUE, converter.convert(Boolean.class, trueValues[i]));
        }
        for (int i = 0; i < falseValues.length; i++) {
            assertEquals(Boolean.FALSE, converter.convert(Boolean.class, falseValues[i]));
        }
    }

}

Other Commons Beanutils examples (source code examples)

Here is a short list of links related to this Commons Beanutils BooleanConverterTestCase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.