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 ClassConverter class.
30   */
31  public class ClassConverterTest {
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       * Test Array Conversion
45       */
46      @Test
47      public void testArray() {
48          final Converter<Class<?>> converter = new ClassConverter();
49          // Test Array Class to String
50          assertEquals("[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class), "Array to String");
51          // *** For some reason the following works on m1, but not m2
52          // Test String to Array Class
53          // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
54      }
55  
56      /**
57       * Test Conversion to Class
58       */
59      @Test
60      public void testConvertToClass() {
61          final Converter<Class<?>> converter = new ClassConverter();
62          assertEquals(Integer.class, converter.convert(Class.class, Integer.class), "Class Test");
63          assertEquals(Integer.class, converter.convert(Class.class, "java.lang.Integer"), "String Test");
64          assertEquals(Integer.class, converter.convert(Class.class, new StringBuilder("java.lang.Integer")), "StringBuilder Test");
65          // Invalid Test
66          assertThrows(ConversionException.class, () -> converter.convert(Class.class, Integer.valueOf(6)));
67          // Test Null
68          assertThrows(ConversionException.class, () -> converter.convert(Class.class, null));
69      }
70  
71      /**
72       * Test Invalid Conversion with default
73       */
74      @Test
75      public void testConvertToClassDefault() {
76          final Converter<Class<?>> converter = new ClassConverter(Object.class);
77          assertEquals(Object.class, converter.convert(Class.class, Integer.valueOf(6)), "Invalid Test");
78          assertEquals(Object.class, converter.convert(Class.class, null), "Null Test");
79      }
80  
81      /**
82       * Test Invalid Conversion with default "null"
83       */
84      @Test
85      public void testConvertToClassDefaultNull() {
86          final Converter<Class<?>> converter = new ClassConverter(null);
87          assertEquals(null, converter.convert(Class.class, Integer.valueOf(6)), "Invalid Test");
88          assertEquals(null, converter.convert(Class.class, null), "Null Test");
89      }
90  
91      /**
92       * Test Conversion to String
93       */
94      @Test
95      public void testConvertToString() {
96          final Converter<Class<?>> converter = new ClassConverter();
97          assertEquals("java.lang.Integer", converter.convert(String.class, Integer.class), "Class Test");
98          assertEquals("foo", converter.convert(String.class, "foo"), "Value Test");
99          assertEquals("bar", converter.convert(String.class, new StringBuilder("bar")), "Value Test");
100         assertEquals(null, converter.convert(String.class, null), "Null Test");
101     }
102 
103     /**
104      * Test Invalid
105      */
106     @Test
107     public void testInvalid() {
108         final Converter<Class<?>> converter = new ClassConverter();
109         // Test invalid class name
110         assertThrows(ConversionException.class, () -> converter.convert(Class.class, "foo.bar"));
111     }
112 
113     /**
114      * Tries a conversion to an unsupported target type.
115      */
116     @Test
117     public void testUnsupportedTargetType() {
118         final Converter<Class<?>> converter = new ClassConverter();
119         assertThrows(ConversionException.class, () -> converter.convert(Integer.class, getClass().getName()));
120     }
121 }