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    *      https://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.beanutils2.converters;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.apache.commons.beanutils2.ConversionException;
23  import org.apache.commons.beanutils2.Converter;
24  import org.junit.jupiter.api.AfterEach;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Test Case for the CharacterConverter class.
30   */
31  public class CharacterConverterTest {
32  
33      /** Sets Up */
34      @BeforeEach
35      public void setUp() throws Exception {
36      }
37  
38      /** Tear Down */
39      @AfterEach
40      public void tearDown() throws Exception {
41      }
42  
43      /**
44       * Tests whether the primitive char class can be passed as target type.
45       */
46      @Test
47      public void testConvertToChar() {
48          final Converter<Character> converter = new CharacterConverter();
49          assertEquals(Character.valueOf('F'), converter.convert(Character.TYPE, "FOO"), "Wrong result");
50      }
51  
52      /**
53       * Test Conversion to Character
54       */
55      @Test
56      public void testConvertToCharacter() {
57          final Converter<Character> converter = new CharacterConverter();
58          assertEquals(Character.valueOf('N'), converter.convert(Character.class, Character.valueOf('N')), "Character Test");
59          assertEquals(Character.valueOf('F'), converter.convert(Character.class, "FOO"), "String Test");
60          assertEquals(Character.valueOf('3'), converter.convert(Character.class, Integer.valueOf(321)), "Integer Test");
61      }
62  
63      /**
64       * Tests a conversion to character for null input if no default value is provided.
65       */
66      @Test
67      public void testConvertToCharacterNullNoDefault() {
68          final Converter<Character> converter = new CharacterConverter();
69          assertThrows(ConversionException.class, () -> converter.convert(Character.class, null));
70      }
71  
72      /**
73       * Test Conversion to String
74       */
75      @Test
76      @SuppressWarnings("unchecked") // testing raw conversion
77      public void testConvertToString() {
78  
79          final Converter<Character> converter = new CharacterConverter();
80          @SuppressWarnings("rawtypes")
81          final Converter raw = converter;
82  
83          assertEquals("N", raw.convert(String.class, Character.valueOf('N')), "Character Test");
84          assertEquals("F", raw.convert(String.class, "FOO"), "String Test");
85          assertEquals("3", raw.convert(String.class, Integer.valueOf(321)), "Integer Test");
86          assertEquals(null, raw.convert(String.class, null), "Null Test");
87      }
88  
89      /**
90       * Tries a conversion to an unsupported type.
91       */
92      @Test
93      @SuppressWarnings("unchecked") // tests failure so allow mismatch
94      public void testConvertToUnsupportedType() {
95          @SuppressWarnings("rawtypes") // tests failure so allow mismatch
96          final Converter converter = new CharacterConverter();
97          assertThrows(ConversionException.class, () -> converter.convert(Integer.class, "Test"));
98      }
99  
100     /**
101      * Test Conversion to Character (with default)
102      */
103     @Test
104     public void testDefault() {
105         final CharacterConverter converter = new CharacterConverter('C');
106         assertEquals(Character.valueOf('C'), converter.convert(Character.class, null), "Default Test");
107     }
108 }