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: public class BooleanConverterTestCase extends TestCase {
025:
026: public static final String[] STANDARD_TRUES = new String[] { "yes",
027: "y", "true", "on", "1" };
028:
029: public static final String[] STANDARD_FALSES = new String[] { "no",
030: "n", "false", "off", "0" };
031:
032: public BooleanConverterTestCase(String name) {
033: super (name);
034: }
035:
036: public void testStandardValues() {
037: BooleanConverter converter = new BooleanConverter();
038: testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
039: }
040:
041: public void testCaseInsensitivity() {
042: BooleanConverter converter = new BooleanConverter();
043: testConversionValues(converter, new String[] { "Yes", "TRUE" },
044: new String[] { "NO", "fAlSe" });
045: }
046:
047: public void testInvalidString() {
048: BooleanConverter converter = new BooleanConverter();
049: try {
050: converter.convert(Boolean.class, "bogus");
051: fail("Converting invalid string should have generated an exception");
052: } catch (ConversionException expected) {
053: // Exception is successful test
054: }
055: }
056:
057: public void testDefaultValue() {
058: Object defaultValue = Boolean.TRUE;
059: BooleanConverter converter = new BooleanConverter(defaultValue);
060:
061: assertSame(defaultValue, converter.convert(Boolean.class,
062: "bogus"));
063: testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
064: }
065:
066: public void testAdditionalStrings() {
067: String[] trueStrings = { "sure" };
068: String[] falseStrings = { "nope" };
069: BooleanConverter converter = new BooleanConverter(trueStrings,
070: falseStrings, BooleanConverter.NO_DEFAULT);
071: testConversionValues(converter,
072: new String[] { "sure", "Sure" }, new String[] { "nope",
073: "nOpE" });
074:
075: try {
076: converter.convert(Boolean.class, "true");
077: fail("Converting obsolete true value should have generated an exception");
078: } catch (ConversionException expected) {
079: // Exception is successful test
080: }
081: try {
082: converter.convert(Boolean.class, "bogus");
083: fail("Converting invalid string should have generated an exception");
084: } catch (ConversionException expected) {
085: // Exception is successful test
086: }
087: }
088:
089: protected void testConversionValues(BooleanConverter converter,
090: String[] trueValues, String[] falseValues) {
091:
092: for (int i = 0; i < trueValues.length; i++) {
093: assertEquals(Boolean.TRUE, converter.convert(Boolean.class,
094: trueValues[i]));
095: }
096: for (int i = 0; i < falseValues.length; i++) {
097: assertEquals(Boolean.FALSE, converter.convert(
098: Boolean.class, falseValues[i]));
099: }
100: }
101:
102: }
|