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 ClassConverter class.
27   *
28   * @version $Id$
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(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(ClassConverterTestCase.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          final Converter converter = new ClassConverter();
68  
69          assertEquals("Class Test", "java.lang.Integer", converter.convert(String.class, Integer.class));
70          assertEquals("Value Test", "foo", converter.convert(String.class, "foo"));
71          assertEquals("Value Test", "bar", converter.convert(String.class, new StringBuilder("bar")));
72          assertEquals("Null Test",   null, converter.convert(String.class, null));
73      }
74  
75      /**
76       * Test Conversion to Class
77       */
78      public void testConvertToClass() {
79          final Converter converter = new ClassConverter();
80  
81          assertEquals("Class Test",        Integer.class, converter.convert(Class.class, Integer.class));
82          assertEquals("String Test",       Integer.class, converter.convert(Class.class, "java.lang.Integer"));
83          assertEquals("StringBuilder Test", Integer.class, converter.convert(Class.class, new StringBuilder("java.lang.Integer")));
84  
85          // Invalid Test
86          try {
87              converter.convert(Class.class, new Integer(6));
88              fail("Expected invalid value to fail");
89          } catch (final ConversionException e) {
90              // expected result
91          }
92  
93          // Test Null
94          try {
95              converter.convert(Class.class, null);
96              fail("Expected null value to fail");
97          } catch (final ConversionException e) {
98              // expected result
99          }
100     }
101 
102     /**
103      * Test Invalid Conversion with default
104      */
105     public void testConvertToClassDefault() {
106 
107         final Converter converter = new ClassConverter(Object.class);
108 
109         assertEquals("Invalid Test", Object.class, converter.convert(Class.class, new Integer(6)));
110         assertEquals("Null Test",    Object.class, converter.convert(Class.class, null));
111     }
112 
113     /**
114      * Test Invalid Conversion with default "null"
115      */
116     public void testConvertToClassDefaultNull() {
117 
118         final Converter converter = new ClassConverter(null);
119 
120         assertEquals("Invalid Test", null, converter.convert(Class.class, new Integer(6)));
121         assertEquals("Null Test",    null, converter.convert(Class.class, null));
122     }
123 
124     /**
125      * Test Array Conversion
126      */
127     public void testArray() {
128         final Converter converter = new ClassConverter();
129 
130         // Test Array Class to String
131         assertEquals("Array to String", "[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class));
132 
133         // *** N.B. for some reason the following works on m1, but not m2
134         // Test String to Array Class
135         // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
136     }
137 
138     /**
139      * Test Invalid
140      */
141     public void testInvalid() {
142         final Converter converter = new ClassConverter();
143 
144         // Test invalid class name
145         try {
146             converter.convert(Class.class, "foo.bar");
147             fail("Invalid class name, expected ConversionException");
148         } catch (final ConversionException e) {
149             // expected result
150         }
151     }
152 
153     /**
154      * Tries a conversion to an unsupported target type.
155      */
156     public void testUnsupportedTargetType() {
157         final Converter converter = new ClassConverter();
158         try {
159             converter.convert(Integer.class, getClass().getName());
160             fail("Invalid target class not detected!");
161         } catch (final ConversionException cex) {
162             // expected result
163         }
164     }
165 }