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.Converter;
23  
24  /***
25   * Test Case for the CharacterConverter class.
26   *
27   * @version $Revision: 471350 $ $Date: 2006-11-05 03:10:58 +0000 (Sun, 05 Nov 2006) $
28   */
29  public class CharacterConverterTestCase extends TestCase {
30  
31      /***
32       * Construct a new Character Converter test case.
33       * @param name Test Name
34       */
35      public CharacterConverterTestCase(String name) {
36          super(name);
37      }
38      
39      // ------------------------------------------------------------------------
40  
41      /***
42       * Create Test Suite
43       * @return test suite
44       */
45      public static TestSuite suite() {
46          return new TestSuite(CharacterConverterTestCase.class);        
47      }
48  
49      /*** Set Up */
50      public void setUp() throws Exception {
51      }
52  
53      /*** Tear Down */
54      public void tearDown() throws Exception {
55      }
56  
57  
58      // ------------------------------------------------------------------------
59  
60      /***
61       * Test Conversion to String
62       */
63      public void testConvertToString() {
64  
65          Converter converter = new CharacterConverter();
66  
67          assertEquals("Character Test", "N", converter.convert(String.class, new Character('N')));
68          assertEquals("String Test",    "F", converter.convert(String.class, "FOO"));
69          assertEquals("Integer Test",   "3", converter.convert(String.class, new Integer(321)));
70          assertEquals("Null Test",     null, converter.convert(String.class, null));
71      }
72  
73      /***
74       * Test Conversion to Character
75       */
76      public void testConvertToCharacter() {
77          Converter converter = new CharacterConverter();
78          assertEquals("Character Test", new Character('N'), converter.convert(Character.class, new Character('N')));
79          assertEquals("String Test",    new Character('F'), converter.convert(Character.class, "FOO"));
80          assertEquals("Integer Test",   new Character('3'), converter.convert(Character.class, new Integer(321)));
81          try {
82              converter.convert(Character.class, null);
83              fail("Expected a ConversionException for null value");
84          } catch (Exception e) {
85              // expected result
86          }
87      }
88  
89      /***
90       * Test Conversion to Character (with default)
91       */
92      public void testDefault() {
93          Converter converter = new CharacterConverter("C");
94          assertEquals("Default Test",   new Character('C'), converter.convert(Character.class, null));
95      }
96  }