    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  package org.apache.commons.beanutils.converters;
    18
    19  import junit.framework.TestCase;
    20  import junit.framework.TestSuite;
    21
    22  import org.apache.commons.beanutils.ConversionException;
    23  import org.apache.commons.beanutils.Converter;
    24
    25  /***
    26   * Test Case for the ClassConverter class.
    27   *
    28   * @version $Revision: 539508 $ $Date: 2007-05-18 16:57:59 +0100 (Fri, 18 May 2007) $
    29   */
    30  public class ClassConverterTestCase extends TestCase {
    31
    32      /***
    33       * Construct a new Class Converter test case.
    34       * @param name Test Name
    35       */
    36      public ClassConverterTestCase(String name) {
    37          super(name);
    38      }
    39
    40      // ------------------------------------------------------------------------
    41
    42      /***
    43       * Create Test Suite
    44       * @return test suite
    45       */
    46      public static TestSuite suite() {
    47          return new TestSuite(ClassConverterTestCase.class);
    48      }
    49
    50      /*** Set Up */
    51      public void setUp() throws Exception {
    52      }
    53
    54      /*** Tear Down */
    55      public void tearDown() throws Exception {
    56      }
    57
    58
    59      // ------------------------------------------------------------------------
    60
    61      /***
    62       * Test Conversion to String
    63       */
    64      public void testConvertToString() {
    65          Converter converter = new ClassConverter();
    66
    67          assertEquals("Class Test", "java.lang.Integer", converter.convert(String.class, Integer.class));
    68          assertEquals("Value Test", "foo", converter.convert(String.class, "foo"));
    69          assertEquals("Value Test", "bar", converter.convert(String.class, new StringBuffer("bar")));
    70          assertEquals("Null Test",   null, converter.convert(String.class, null));
    71      }
    72
    73      /***
    74       * Test Conversion to Class
    75       */
    76      public void testConvertToClass() {
    77          Converter converter = new ClassConverter();
    78
    79          assertEquals("Class Test",        Integer.class, converter.convert(Class.class, Integer.class));
    80          assertEquals("String Test",       Integer.class, converter.convert(Class.class, "java.lang.Integer"));
    81          assertEquals("StringBuffer Test", Integer.class, converter.convert(Class.class, new StringBuffer("java.lang.Integer")));
    82
    83          // Invalid Test
    84          try {
    85              converter.convert(Class.class, new Integer(6));
    86              fail("Expected invalid value to fail");
    87          } catch (ConversionException e) {
    88              // expected result
    89          }
    90
    91          // Test Null
    92          try {
    93              converter.convert(Class.class, null);
    94              fail("Expected null value to fail");
    95          } catch (ConversionException e) {
    96              // expected result
    97          }
    98      }
    99
    100     /***
    101      * Test Invalid Conversion with default
    102      */
    103     public void testConvertToClassDefault() {
    104
    105         Converter converter = new ClassConverter(Object.class);
    106
    107         assertEquals("Invalid Test", Object.class, converter.convert(Class.class, new Integer(6)));
    108         assertEquals("Null Test",    Object.class, converter.convert(Class.class, null));
    109     }
    110
    111     /***
    112      * Test Invalid Conversion with default "null"
    113      */
    114     public void testConvertToClassDefaultNull() {
    115
    116         Converter converter = new ClassConverter(null);
    117
    118         assertEquals("Invalid Test", null, converter.convert(Class.class, new Integer(6)));
    119         assertEquals("Null Test",    null, converter.convert(Class.class, null));
    120     }
    121
    122     /***
    123      * Test Array Conversion
    124      */
    125     public void testArray() {
    126         Converter converter = new ClassConverter();
    127
    128         // Test Array Class to String
    129         assertEquals("Array to String", "[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class));
    130
    131         // *** N.B. for some reason the following works on m1, but not m2
    132         // Test String to Array Class
    133         // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
    134     }
    135
    136     /***
    137      * Test Invalid
    138      */
    139     public void testInvalid() {
    140         Converter converter = new ClassConverter();
    141
    142         // Test invalid class name
    143         try {
    144             converter.convert(Class.class, "foo.bar");
    145             fail("Invalid class name, expected ConversionException");
    146         } catch (ConversionException e) {
    147             // expected result
    148         }
    149     }
    150
    151 }