Author: skitching
Date: Mon Mar  7 01:12:37 2005
New Revision: 156411

URL: http://svn.apache.org/viewcvs?view=rev&rev=156411
Log:
Test cases. Much of this code (and the recent changes to make BooleanConverter 
configurable)
were provided by Eric Rizzo.

Added:
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanArrayConverterTestCase.java
   (with props)
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
   (with props)

Added: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanArrayConverterTestCase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanArrayConverterTestCase.java?view=auto&rev=156411
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanArrayConverterTestCase.java
 (added)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanArrayConverterTestCase.java
 Mon Mar  7 01:12:37 2005
@@ -0,0 +1,271 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.ConvertUtils;
+import org.apache.commons.beanutils.ConversionException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test conversions of String[]->boolean[] and String->boolean[].
+ *
+ * <p>Note that the tests here don't rigorously test conversions of individual
+ * strings to booleans, as the BooleanArrayConverter class uses a
+ * BooleanConverter instance to do those conversions, and the BooleanConverter
+ * class has its own unit tests. Here, the tests focus on the array-related
+ * behaviour.</p>
+ */
+public class BooleanArrayConverterTestCase 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 BooleanArrayConverterTestCase(String name) {
+        super(name);
+    }
+
+    /**
+     * Check that an object of type String[] with valid boolean string
+     * values gets converted nicely.
+     */
+    public void testStandardStringArrayConversion() {
+        String[] values = {
+            "true", "false", 
+            "yes", "no",
+            "y", "n",
+            "1", "0",
+        };
+        
+        BooleanArrayConverter converter = new BooleanArrayConverter();
+        boolean[] results = (boolean[]) converter.convert(null, values);
+
+        assertNotNull(results);
+        assertEquals(8, results.length);
+        assertTrue(results[0]);
+        assertFalse(results[1]);
+        assertTrue(results[2]);
+        assertFalse(results[3]);
+        assertTrue(results[4]);
+        assertFalse(results[5]);
+        assertTrue(results[6]);
+        assertFalse(results[7]);
+    }
+
+    /**
+     * Check that an object whose toString method returns a list of boolean
+     * values gets converted nicely.
+     */
+    public void testStandardStringConversion() {
+        BooleanArrayConverter converter = new BooleanArrayConverter();
+
+        StringBuffer input = new StringBuffer();
+        boolean[] results;
+
+        // string has {}
+        input.setLength(0);
+        input.append("{true, 'yes', Y, 1, 'FALSE', \"no\", 'n', 0}");
+        results = (boolean[]) converter.convert(null, input);
+
+        assertNotNull(results);
+        assertEquals(8, results.length);
+        assertTrue(results[0]);
+        assertTrue(results[1]);
+        assertTrue(results[2]);
+        assertTrue(results[3]);
+        assertFalse(results[4]);
+        assertFalse(results[5]);
+        assertFalse(results[6]);
+        assertFalse(results[7]);
+
+        // string does not have {}
+        input.setLength(0);
+        input.append("'falsE', 'no', 'N', 0, \"truE\", yeS, 'y', '1'");
+        results = (boolean[]) converter.convert(null, input);
+
+        assertNotNull(results);
+        assertEquals(8, results.length);
+        assertFalse(results[0]);
+        assertFalse(results[1]);
+        assertFalse(results[2]);
+        assertFalse(results[3]);
+        assertTrue(results[4]);
+        assertTrue(results[5]);
+        assertTrue(results[6]);
+        assertTrue(results[7]);
+
+        // string has only one element, non-quoted
+        input.setLength(0);
+        input.append("y");
+        results = (boolean[]) converter.convert(null, input);
+
+        assertNotNull(results);
+        assertEquals(1, results.length);
+        assertTrue(results[0]);
+
+        // string has only one element, quoted with ".
+        input.setLength(0);
+        input.append("\"1\"");
+        results = (boolean[]) converter.convert(null, input);
+
+        assertNotNull(results);
+        assertEquals(1, results.length);
+        assertTrue(results[0]);
+
+        // string has only one element, quoted with '
+        // Here we also pass an object of type String rather than the 
+        // StringBuffer
+        results = (boolean[]) converter.convert(null, "'yes'");
+
+        assertNotNull(results);
+        assertEquals(1, results.length);
+        assertTrue(results[0]);
+
+    }
+
+    /**
+     * Check that the user can specify non-standard true/false values by
+     * providing a customised BooleanConverter.
+     */
+    public void testAdditionalStrings() {
+        String[] trueStrings = {"sure"};
+        String[] falseStrings = {"nope"};
+        BooleanConverter bc = new BooleanConverter(
+            trueStrings, falseStrings, BooleanConverter.NO_DEFAULT);
+        BooleanArrayConverter converter = new BooleanArrayConverter(
+            bc, BooleanArrayConverter.NO_DEFAULT);
+        
+        boolean[] results = (boolean[]) converter.convert(null, "NOPE, sure, 
sure");
+        assertNotNull(results);
+        assertEquals(3, results.length);
+        assertFalse(results[0]);
+        assertTrue(results[1]);
+        assertTrue(results[2]);
+        
+        try {
+            // the literal string 'true' should no longer be recognised as
+            // a true value..
+            converter.convert(null, "true");
+            fail("Converting invalid string should have generated an 
exception");
+        } catch(Exception ex) {
+            // ok, expected
+        }
+    }
+
+    /**
+     * Check that when the input string cannot be split into a String[], and
+     * there is no default value then an exception is thrown.
+     */
+    public void testInvalidStringWithoutDefault() {
+        BooleanArrayConverter converter = new BooleanArrayConverter();
+        try {
+            converter.convert(null, "true!");
+            fail("Converting invalid string should have generated an 
exception");
+        } catch (ConversionException expected) {
+            // Exception is successful test
+        }
+    }
+
+    /**
+     * Check that when the input string cannot be split into a String[], and
+     * there is a default value then that default is returned.
+     */
+    public void testInvalidStringWithDefault() {
+        boolean[] defaults = new boolean[1];
+        BooleanArrayConverter converter = new BooleanArrayConverter(defaults);
+        Object o = converter.convert(null, "true!");
+        assertSame("Unexpected object returned for failed conversion", o, 
defaults);
+    }
+
+    /**
+     * Check that when one of the elements in a comma-separated string is not
+     * a valid boolean, and there is no default value then an exception is 
thrown.
+     */
+    public void testInvalidElementWithoutDefault() {
+        BooleanArrayConverter converter = new BooleanArrayConverter();
+        try {
+            converter.convert(null, "true,bogus");
+            fail("Converting invalid string should have generated an 
exception");
+        } catch (ConversionException expected) {
+            // Exception is successful test
+        }
+    }
+
+    /**
+     * Check that when one of the elements in a comma-separated string is not
+     * a valid boolean, and there is a default value then the default value
+     * is returned.
+     * <p>
+     * Note that the default value is for the complete array object returned,
+     * not for the failed element.
+     */
+    public void testInvalidElementWithDefault() {
+        boolean[] defaults = new boolean[1];
+        BooleanArrayConverter converter = new BooleanArrayConverter(defaults);
+        Object o = converter.convert(null, "true,bogus");
+        assertSame("Unexpected object returned for failed conversion", o, 
defaults);
+    }
+    
+    /**
+     * Check that when a custom BooleanConverter is used, and that converter
+     * has a (per-element) default, then that element (and just that element)
+     * is assigned the default value.
+     * <p>
+     * With the standard BooleanArrayConverter, if <i>any</i> of the elements
+     * in the array are bad, then the array-wide default value is returned.
+     * However by specifying a custom BooleanConverter which has a per-element
+     * default, the unrecognised elements get that per-element default but the
+     * others are converted as expected.
+     */
+    public void testElementDefault() {
+        boolean[] defaults = new boolean[1];
+        BooleanConverter bc = new BooleanConverter(Boolean.TRUE);
+        BooleanArrayConverter converter = new BooleanArrayConverter(bc, 
defaults);
+        boolean[] results = (boolean[]) converter.convert(null, "true,bogus");
+        
+        assertEquals(2, results.length);
+        assertTrue(results[0]);
+        assertTrue(results[1]);
+    }
+    
+    /**
+     * Check that registration of a custom converter works.
+     */
+    public void testRegistration() {
+        String[] trueStrings = {"sure"};
+        String[] falseStrings = {"nope"};
+        BooleanConverter bc = new BooleanConverter(
+            trueStrings, falseStrings, BooleanConverter.NO_DEFAULT);
+
+        BooleanArrayConverter converter = new BooleanArrayConverter(
+            bc, BooleanArrayConverter.NO_DEFAULT);
+            
+        ConvertUtils.register(converter, BooleanArrayConverter.MODEL);
+        boolean[] sample = new boolean[0];
+        boolean[] results = (boolean[]) ConvertUtils.convert("sure,nope", 
sample.getClass());
+        
+        assertEquals(2, results.length);
+        assertTrue(results[0]);
+        assertFalse(results[1]);
+    }
+}

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanArrayConverterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java?view=auto&rev=156411
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
 (added)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
 Mon Mar  7 01:12:37 2005
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed 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 = new Object();
+        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]));
+        }
+    }
+
+}

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/BooleanConverterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to