KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typeconverter > test > TypeConverterTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typeconverter.test;
8
9
10 import junit.framework.TestCase;
11
12 import com.inversoft.util.typeconverter.BaseTypeConverter;
13 import com.inversoft.util.typeconverter.TypeConversionException;
14 import com.inversoft.util.typeconverter.TypeConverter;
15 import com.inversoft.util.typeconverter.TypeConverterRegistry;
16 import com.inversoft.util.typeconverter.converters.BooleanTypeConverter;
17 import com.inversoft.util.typeconverter.converters.CharacterTypeConverter;
18 import com.inversoft.util.typeconverter.converters.NumberTypeConverter;
19
20
21 /**
22  * This class handles all of the test cases for the Type
23  * conversion support in the JBeans package.
24  *
25  * @author Brian Pontarelli
26  */

27 public class TypeConverterTest extends TestCase {
28
29     /** Construct and new test instance */
30     public TypeConverterTest(String JavaDoc name) {
31         super(name);
32     }
33
34     /**
35      * Test the lookup of converters
36      */

37     public void testLookups() {
38         TypeConverter tc = TypeConverterRegistry.lookup(Character JavaDoc.class);
39         assertEquals(CharacterTypeConverter.class, tc.getClass());
40         tc = TypeConverterRegistry.lookup(Character.TYPE);
41         assertEquals(CharacterTypeConverter.class, tc.getClass());
42         tc = TypeConverterRegistry.lookup(Character JavaDoc[].class);
43         assertEquals(CharacterTypeConverter.class, tc.getClass());
44         tc = TypeConverterRegistry.lookup(char[].class);
45         assertEquals(CharacterTypeConverter.class, tc.getClass());
46
47         tc = TypeConverterRegistry.lookup(Byte JavaDoc.class);
48         assertEquals(NumberTypeConverter.class, tc.getClass());
49         tc = TypeConverterRegistry.lookup(Byte.TYPE);
50         assertEquals(NumberTypeConverter.class, tc.getClass());
51         tc = TypeConverterRegistry.lookup(Byte JavaDoc[].class);
52         assertEquals(NumberTypeConverter.class, tc.getClass());
53         tc = TypeConverterRegistry.lookup(byte[].class);
54         assertEquals(NumberTypeConverter.class, tc.getClass());
55
56         tc = TypeConverterRegistry.lookup(Short JavaDoc.class);
57         assertEquals(NumberTypeConverter.class, tc.getClass());
58         tc = TypeConverterRegistry.lookup(Short.TYPE);
59         assertEquals(NumberTypeConverter.class, tc.getClass());
60         tc = TypeConverterRegistry.lookup(Short JavaDoc[].class);
61         assertEquals(NumberTypeConverter.class, tc.getClass());
62         tc = TypeConverterRegistry.lookup(short[].class);
63         assertEquals(NumberTypeConverter.class, tc.getClass());
64
65         tc = TypeConverterRegistry.lookup(Integer JavaDoc.class);
66         assertEquals(NumberTypeConverter.class, tc.getClass());
67         tc = TypeConverterRegistry.lookup(Integer.TYPE);
68         assertEquals(NumberTypeConverter.class, tc.getClass());
69         tc = TypeConverterRegistry.lookup(Integer JavaDoc[].class);
70         assertEquals(NumberTypeConverter.class, tc.getClass());
71         tc = TypeConverterRegistry.lookup(int[].class);
72         assertEquals(NumberTypeConverter.class, tc.getClass());
73
74         tc = TypeConverterRegistry.lookup(Long JavaDoc.class);
75         assertEquals(NumberTypeConverter.class, tc.getClass());
76         tc = TypeConverterRegistry.lookup(Long.TYPE);
77         assertEquals(NumberTypeConverter.class, tc.getClass());
78         tc = TypeConverterRegistry.lookup(Long JavaDoc[].class);
79         assertEquals(NumberTypeConverter.class, tc.getClass());
80         tc = TypeConverterRegistry.lookup(long[].class);
81         assertEquals(NumberTypeConverter.class, tc.getClass());
82
83         tc = TypeConverterRegistry.lookup(Float JavaDoc.class);
84         assertEquals(NumberTypeConverter.class, tc.getClass());
85         tc = TypeConverterRegistry.lookup(Float.TYPE);
86         assertEquals(NumberTypeConverter.class, tc.getClass());
87         tc = TypeConverterRegistry.lookup(Float JavaDoc[].class);
88         assertEquals(NumberTypeConverter.class, tc.getClass());
89         tc = TypeConverterRegistry.lookup(float[].class);
90         assertEquals(NumberTypeConverter.class, tc.getClass());
91
92         tc = TypeConverterRegistry.lookup(Double JavaDoc.class);
93         assertEquals(NumberTypeConverter.class, tc.getClass());
94         tc = TypeConverterRegistry.lookup(Double.TYPE);
95         assertEquals(NumberTypeConverter.class, tc.getClass());
96         tc = TypeConverterRegistry.lookup(Double JavaDoc[].class);
97         assertEquals(NumberTypeConverter.class, tc.getClass());
98         tc = TypeConverterRegistry.lookup(double[].class);
99         assertEquals(NumberTypeConverter.class, tc.getClass());
100
101         tc = TypeConverterRegistry.lookup(Boolean JavaDoc.class);
102         assertEquals(BooleanTypeConverter.class, tc.getClass());
103         tc = TypeConverterRegistry.lookup(Boolean.TYPE);
104         assertEquals(BooleanTypeConverter.class, tc.getClass());
105         tc = TypeConverterRegistry.lookup(Boolean JavaDoc[].class);
106         assertEquals(BooleanTypeConverter.class, tc.getClass());
107         tc = TypeConverterRegistry.lookup(boolean[].class);
108         assertEquals(BooleanTypeConverter.class, tc.getClass());
109
110         tc = TypeConverterRegistry.lookup(String JavaDoc.class);
111         assertEquals(BaseTypeConverter.class, tc.getClass());
112         tc = TypeConverterRegistry.lookup(String JavaDoc[].class);
113         assertEquals(BaseTypeConverter.class, tc.getClass());
114     }
115
116     /**
117      * Tests lookup failures
118      */

119     public void testLookupFailures() {
120         TypeConverter tc = TypeConverterRegistry.lookup(this.getClass());
121         assertNull(tc);
122     }
123
124     /**
125      * Tests registration and unregistration
126      */

127     public void testRegistration() {
128         TypeConverter tc = new NumberTypeConverter();
129         TypeConverterRegistry.register(this.getClass(), tc);
130         assertSame(tc, TypeConverterRegistry.lookup(this.getClass()));
131
132         TypeConverterRegistry.unregister(this.getClass());
133         assertNull(TypeConverterRegistry.lookup(this.getClass()));
134     }
135
136     /**
137      * Tests the convert method correctly delegates out to stringToArray
138      */

139     public void testConvertCallsStringToArray() {
140
141         try {
142             // Test convert calls convertStringToArray
143
BaseTypeConverter c = new BaseTypeConverter();
144             Object JavaDoc obj = c.convert("foo,bar,john", String JavaDoc[].class);
145
146             if (obj instanceof String JavaDoc[]) {
147                 String JavaDoc [] array = (String JavaDoc []) obj;
148                 assertTrue("Length should be three", array.length == 3);
149                 assertTrue("First indices should be foo", array[0].equals("foo"));
150                 assertTrue("Second indices should be bar", array[1].equals("bar"));
151                 assertTrue("Third indices should be john", array[2].equals("john"));
152             } else {
153                 fail("Type is not correct: " + obj.getClass().getName());
154             }
155
156             c = new BaseTypeConverter();
157             obj = c.convert("", String JavaDoc[].class);
158             assertNotNull(obj);
159             assertEquals(0, ((Object JavaDoc[]) obj).length);
160         } catch (TypeConversionException tce) {
161             fail(tce.toString());
162         }
163     }
164
165     /**
166      * Tests the convert method correctly delegates out to stringToArray
167      */

168     public void testConvertCallsString() {
169
170         try {
171             // Test convert calls convertStringToArray
172
BaseTypeConverter c = new BaseTypeConverter();
173             Object JavaDoc obj = c.convert("foo,bar,john", String JavaDoc.class);
174
175             if (obj instanceof String JavaDoc) {
176                 assertEquals(obj, "foo,bar,john");
177             } else {
178                 fail("Type is not correct: " + obj.getClass().getName());
179             }
180
181             c = new BaseTypeConverter();
182             obj = c.convert("", String JavaDoc.class);
183             assertEquals("", obj);
184         } catch (TypeConversionException tce) {
185             fail(tce.toString());
186         }
187     }
188
189     /**
190      * Tests the convertToArray method handles null
191      */

192     public void testConvertToArray() {
193
194         try {
195             // Test convert calls convertStringToArray
196
BaseTypeConverter c = new BaseTypeConverter();
197             Object JavaDoc obj = c.convertToArray(null, String JavaDoc[].class);
198             assertNull(obj);
199         } catch (TypeConversionException tce) {
200             fail(tce.toString());
201         }
202     }
203
204
205     /**
206      * Tests the convertString method calls the convertStringToArray
207      */

208     public void testConvertStringCallsStringToArray() {
209
210         try {
211             // Test convert calls convertStringToArray
212
BaseTypeConverter c = new BaseTypeConverter();
213             Object JavaDoc[] obj = (Object JavaDoc[]) c.convertString("john,foo,bar", String JavaDoc[].class);
214             assertNotNull(obj);
215             assertEquals(3, obj.length);
216             assertEquals("john", obj[0]);
217             assertEquals("foo", obj[1]);
218             assertEquals("bar", obj[2]);
219         } catch (TypeConversionException tce) {
220             fail(tce.toString());
221         }
222     }
223 }
Popular Tags