View Javadoc
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 CharacterConverter class.
27   *
28   * @version $Id$
29   */
30  public class CharacterConverterTestCase extends TestCase {
31  
32      /**
33       * Construct a new Character Converter test case.
34       * @param name Test Name
35       */
36      public CharacterConverterTestCase(final 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(CharacterConverterTestCase.class);
48      }
49  
50      /** Set Up */
51      @Override
52      public void setUp() throws Exception {
53      }
54  
55      /** Tear Down */
56      @Override
57      public void tearDown() throws Exception {
58      }
59  
60  
61      // ------------------------------------------------------------------------
62  
63      /**
64       * Test Conversion to String
65       */
66      public void testConvertToString() {
67  
68          final Converter converter = new CharacterConverter();
69  
70          assertEquals("Character Test", "N", converter.convert(String.class, new Character('N')));
71          assertEquals("String Test",    "F", converter.convert(String.class, "FOO"));
72          assertEquals("Integer Test",   "3", converter.convert(String.class, new Integer(321)));
73          assertEquals("Null Test",     null, converter.convert(String.class, null));
74      }
75  
76      /**
77       * Test Conversion to Character
78       */
79      public void testConvertToCharacter() {
80          final Converter converter = new CharacterConverter();
81          assertEquals("Character Test", new Character('N'), converter.convert(Character.class, new Character('N')));
82          assertEquals("String Test",    new Character('F'), converter.convert(Character.class, "FOO"));
83          assertEquals("Integer Test",   new Character('3'), converter.convert(Character.class, new Integer(321)));
84      }
85  
86      /**
87       * Tests whether the primitive char class can be passed as target type.
88       */
89      public void testConvertToChar() {
90          final Converter converter = new CharacterConverter();
91          assertEquals("Wrong result", new Character('F'), converter.convert(Character.TYPE, "FOO"));
92      }
93  
94      /**
95       * Tests a conversion to character for null input if no default value is
96       * provided.
97       */
98      public void testConvertToCharacterNullNoDefault() {
99          final Converter converter = new CharacterConverter();
100         try {
101             converter.convert(Character.class, null);
102             fail("Expected a ConversionException for null value");
103         } catch (final Exception e) {
104             // expected result
105         }
106     }
107 
108     /**
109      * Test Conversion to Character (with default)
110      */
111     public void testDefault() {
112         final Converter converter = new CharacterConverter("C");
113         assertEquals("Default Test",   new Character('C'), converter.convert(Character.class, null));
114     }
115 
116     /**
117      * Tries a conversion to an unsupported type.
118      */
119     public void testConvertToUnsupportedType() {
120         final Converter converter = new CharacterConverter();
121         try {
122             converter.convert(Integer.class, "Test");
123             fail("Could convert to unsupported type!");
124         } catch (final ConversionException cex) {
125             // expected result
126         }
127     }
128 }