1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29
30 public class ClassConverterTestCase extends TestCase {
31
32
33
34
35
36 public ClassConverterTestCase(String name) {
37 super(name);
38 }
39
40
41
42
43
44
45
46 public static TestSuite suite() {
47 return new TestSuite(ClassConverterTestCase.class);
48 }
49
50
51 public void setUp() throws Exception {
52 }
53
54
55 public void tearDown() throws Exception {
56 }
57
58
59
60
61
62
63
64 public void testConvertToString() {
65 Converter converter = new ClassConverter();
66
67 assertEquals("Class Test", "java.lang.Integer", converter.convert(String.class, Integer.class));
68 assertEquals("Value Test", "foo", converter.convert(String.class, "foo"));
69 assertEquals("Value Test", "bar", converter.convert(String.class, new StringBuffer("bar")));
70 assertEquals("Null Test", null, converter.convert(String.class, null));
71 }
72
73
74
75
76 public void testConvertToClass() {
77 Converter converter = new ClassConverter();
78
79 assertEquals("Class Test", Integer.class, converter.convert(Class.class, Integer.class));
80 assertEquals("String Test", Integer.class, converter.convert(Class.class, "java.lang.Integer"));
81 assertEquals("StringBuffer Test", Integer.class, converter.convert(Class.class, new StringBuffer("java.lang.Integer")));
82
83
84 try {
85 converter.convert(Class.class, new Integer(6));
86 fail("Expected invalid value to fail");
87 } catch (ConversionException e) {
88
89 }
90
91
92 try {
93 converter.convert(Class.class, null);
94 fail("Expected null value to fail");
95 } catch (ConversionException e) {
96
97 }
98 }
99
100
101
102
103 public void testConvertToClassDefault() {
104
105 Converter converter = new ClassConverter(Object.class);
106
107 assertEquals("Invalid Test", Object.class, converter.convert(Class.class, new Integer(6)));
108 assertEquals("Null Test", Object.class, converter.convert(Class.class, null));
109 }
110
111
112
113
114 public void testConvertToClassDefaultNull() {
115
116 Converter converter = new ClassConverter(null);
117
118 assertEquals("Invalid Test", null, converter.convert(Class.class, new Integer(6)));
119 assertEquals("Null Test", null, converter.convert(Class.class, null));
120 }
121
122
123
124
125 public void testArray() {
126 Converter converter = new ClassConverter();
127
128
129 assertEquals("Array to String", "[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class));
130
131
132
133
134 }
135
136
137
138
139 public void testInvalid() {
140 Converter converter = new ClassConverter();
141
142
143 try {
144 converter.convert(Class.class, "foo.bar");
145 fail("Invalid class name, expected ConversionException");
146 } catch (ConversionException e) {
147
148 }
149 }
150
151 }