KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typeconverter > converters > test > NumberTypeConverterTest


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.converters.test;
8
9
10 import junit.framework.TestCase;
11
12 import com.inversoft.util.typeconverter.TypeConversionException;
13 import com.inversoft.util.typeconverter.converters.NumberTypeConverter;
14
15
16 /**
17  * This class handles all of the test cases for the Type
18  * conversion support in the JBeans package.
19  *
20  * @author Brian Pontarelli
21  */

22 public class NumberTypeConverterTest extends TestCase {
23
24     /** Construct and new test instance */
25     public NumberTypeConverterTest(String JavaDoc name) {
26         super(name);
27     }
28
29     /**
30      * Tests the convertString method works with null values
31      */

32     public void testStringNull() {
33
34         try {
35             NumberTypeConverter c = new NumberTypeConverter();
36             Object JavaDoc obj;
37
38             obj = c.convertString(null, Byte JavaDoc.class);
39             assertNull(obj);
40             obj = c.convertString(null, Byte.TYPE);
41             assertEquals(new Byte JavaDoc((byte) 0), obj);
42
43             obj = c.convertString(null, Short JavaDoc.class);
44             assertNull(obj);
45             obj = c.convertString(null, Short.TYPE);
46             assertEquals(new Short JavaDoc((short)0), obj);
47
48             obj = c.convertString(null, Integer JavaDoc.class);
49             assertNull(obj);
50             obj = c.convertString(null, Integer.TYPE);
51             assertEquals(new Integer JavaDoc(0), obj);
52
53             obj = c.convertString(null, Long JavaDoc.class);
54             assertNull(obj);
55             obj = c.convertString(null, Long.TYPE);
56             assertEquals(new Long JavaDoc(0), obj);
57
58             obj = c.convertString(null, Float JavaDoc.class);
59             assertNull(obj);
60             obj = c.convertString(null, Float.TYPE);
61             assertEquals(new Float JavaDoc(0), obj);
62
63             obj = c.convertString(null, Double JavaDoc.class);
64             assertNull(obj);
65             obj = c.convertString(null, Double.TYPE);
66             assertEquals(new Double JavaDoc(0), obj);
67         } catch (TypeConversionException tce) {
68             fail(tce.toString());
69         }
70     }
71
72     /**
73      * Tests the convertString method works with String values
74      */

75     public void testString() {
76
77         try {
78             NumberTypeConverter c = new NumberTypeConverter();
79             Object JavaDoc obj;
80
81             obj = c.convertString("42", Byte JavaDoc.class);
82             assertEquals(new Byte JavaDoc((byte) 42), obj);
83             assertTrue(obj instanceof Byte JavaDoc);
84             obj = c.convertString("42", Byte.TYPE);
85             assertEquals(new Byte JavaDoc((byte) 42), obj);
86             assertTrue(obj instanceof Byte JavaDoc);
87
88             obj = c.convertString("1", Short JavaDoc.class);
89             assertEquals(new Short JavaDoc((short) 1), obj);
90             assertTrue(obj instanceof Short JavaDoc);
91             obj = c.convertString("1", Short.TYPE);
92             assertEquals(new Short JavaDoc((short) 1), obj);
93             assertTrue(obj instanceof Short JavaDoc);
94
95             obj = c.convertString("2", Integer JavaDoc.class);
96             assertEquals(new Integer JavaDoc(2), obj);
97             assertTrue(obj instanceof Integer JavaDoc);
98             obj = c.convertString("2", Integer.TYPE);
99             assertEquals(new Integer JavaDoc(2), obj);
100             assertTrue(obj instanceof Integer JavaDoc);
101
102             obj = c.convertString("3", Long JavaDoc.class);
103             assertEquals(new Long JavaDoc(3), obj);
104             assertTrue(obj instanceof Long JavaDoc);
105             obj = c.convertString("3", Long.TYPE);
106             assertEquals(new Long JavaDoc(3), obj);
107             assertTrue(obj instanceof Long JavaDoc);
108
109             obj = c.convertString("2.5", Float JavaDoc.class);
110             assertEquals(new Float JavaDoc(2.5f), obj);
111             assertTrue(obj instanceof Float JavaDoc);
112             obj = c.convertString("2.5", Float.TYPE);
113             assertEquals(new Float JavaDoc(2.5f), obj);
114             assertTrue(obj instanceof Float JavaDoc);
115
116             obj = c.convertString("3.14", Double JavaDoc.class);
117             assertEquals(new Double JavaDoc(3.14), obj);
118             assertTrue(obj instanceof Double JavaDoc);
119             obj = c.convertString("3.14", Double.TYPE);
120             assertEquals(new Double JavaDoc(3.14), obj);
121             assertTrue(obj instanceof Double JavaDoc);
122         } catch (TypeConversionException tce) {
123             fail(tce.toString());
124         }
125     }
126
127     /**
128      * Tests that the convertString method fails when the type is bogus
129      */

130     public void testStringFail() {
131         try {
132             NumberTypeConverter c = new NumberTypeConverter();
133             c.convertString("3.14", Object JavaDoc.class);
134             fail("Should have failed");
135         } catch (TypeConversionException tce) {
136             // Expected
137
}
138     }
139
140     /**
141      * Tests the convertString method delegates properly
142      */

143     public void testStringDelegates() {
144
145         try {
146             NumberTypeConverter c = new NumberTypeConverter();
147             Object JavaDoc[] obj;
148
149             obj = (Object JavaDoc[]) c.convertString("42,41", Byte JavaDoc[].class);
150             assertEquals(new Byte JavaDoc((byte) 42), obj[0]);
151             assertEquals(new Byte JavaDoc((byte) 41), obj[1]);
152             assertTrue(obj instanceof Byte JavaDoc[]);
153         } catch (TypeConversionException tce) {
154             fail(tce.toString());
155         }
156     }
157
158     /**
159      * Tests the convertString method works with empty values
160      */

161     public void testStringEmpty() {
162
163         try {
164             NumberTypeConverter c = new NumberTypeConverter();
165             Object JavaDoc obj;
166
167             obj = c.convertString(" ", Byte JavaDoc.class);
168             assertNull(obj);
169             obj = c.convertString(" ", Byte.TYPE);
170             assertEquals(new Byte JavaDoc((byte) 0), obj);
171
172             obj = c.convertString(" ", Short JavaDoc.class);
173             assertNull(obj);
174             obj = c.convertString(" ", Short.TYPE);
175             assertEquals(new Short JavaDoc((short)0), obj);
176
177             obj = c.convertString(" ", Integer JavaDoc.class);
178             assertNull(obj);
179             obj = c.convertString(" ", Integer.TYPE);
180             assertEquals(new Integer JavaDoc(0), obj);
181
182             obj = c.convertString(" ", Long JavaDoc.class);
183             assertNull(obj);
184             obj = c.convertString(" ", Long.TYPE);
185             assertEquals(new Long JavaDoc(0), obj);
186
187             obj = c.convertString("", Float JavaDoc.class);
188             assertNull(obj);
189             obj = c.convertString("", Float.TYPE);
190             assertEquals(new Float JavaDoc(0), obj);
191
192             obj = c.convertString("", Double JavaDoc.class);
193             assertNull(obj);
194             obj = c.convertString("", Double.TYPE);
195             assertEquals(new Double JavaDoc(0), obj);
196         } catch (TypeConversionException tce) {
197             fail(tce.toString());
198         }
199     }
200
201     /**
202      * Tests the convertArray method works with null values
203      */

204     public void testArrayNull() {
205
206         try {
207             NumberTypeConverter c = new NumberTypeConverter();
208             Object JavaDoc obj;
209
210             obj = c.convertArray(null, Byte JavaDoc.class);
211             assertEquals(null, obj);
212             obj = c.convertArray(null, Byte.TYPE);
213             assertEquals(new Byte JavaDoc((byte) 0), obj);
214             assertTrue(obj instanceof Byte JavaDoc);
215
216             obj = c.convertArray(null, Short JavaDoc.class);
217             assertEquals(null, obj);
218             obj = c.convertArray(null, Short.TYPE);
219             assertEquals(new Short JavaDoc((short) 0), obj);
220             assertTrue(obj instanceof Short JavaDoc);
221
222             obj = c.convertArray(null, Integer JavaDoc.class);
223             assertEquals(null, obj);
224             obj = c.convertArray(null, Integer.TYPE);
225             assertEquals(new Integer JavaDoc(0), obj);
226             assertTrue(obj instanceof Integer JavaDoc);
227
228             obj = c.convertArray(null, Long JavaDoc.class);
229             assertEquals(null, obj);
230             obj = c.convertArray(null, Long.TYPE);
231             assertEquals(new Long JavaDoc(0), obj);
232             assertTrue(obj instanceof Long JavaDoc);
233
234             obj = c.convertArray(null, Float JavaDoc.class);
235             assertEquals(null, obj);
236             obj = c.convertArray(null, Float.TYPE);
237             assertEquals(new Float JavaDoc(0), obj);
238             assertTrue(obj instanceof Float JavaDoc);
239
240             obj = c.convertString(null, Double JavaDoc.class);
241             assertEquals(null, obj);
242             obj = c.convertString(null, Double.TYPE);
243             assertEquals(new Double JavaDoc(0), obj);
244             assertTrue(obj instanceof Double JavaDoc);
245         } catch (TypeConversionException tce) {
246             fail(tce.toString());
247         }
248     }
249
250     /**
251      * Tests the convertArray method works with String values
252      */

253     public void testArray() {
254
255         try {
256             NumberTypeConverter c = new NumberTypeConverter();
257             Object JavaDoc obj;
258
259             obj = c.convertArray(new String JavaDoc[]{"42"}, Byte JavaDoc.class);
260             assertEquals(new Byte JavaDoc((byte) 42), obj);
261             assertTrue(obj instanceof Byte JavaDoc);
262             obj = c.convertString("42", Byte.TYPE);
263             assertEquals(new Byte JavaDoc((byte) 42), obj);
264             assertTrue(obj instanceof Byte JavaDoc);
265
266             obj = c.convertString("1", Short JavaDoc.class);
267             assertEquals(new Short JavaDoc((short) 1), obj);
268             assertTrue(obj instanceof Short JavaDoc);
269             obj = c.convertString("1", Short.TYPE);
270             assertEquals(new Short JavaDoc((short) 1), obj);
271             assertTrue(obj instanceof Short JavaDoc);
272
273             obj = c.convertString("2", Integer JavaDoc.class);
274             assertEquals(new Integer JavaDoc(2), obj);
275             assertTrue(obj instanceof Integer JavaDoc);
276             obj = c.convertString("2", Integer.TYPE);
277             assertEquals(new Integer JavaDoc(2), obj);
278             assertTrue(obj instanceof Integer JavaDoc);
279
280             obj = c.convertString("3", Long JavaDoc.class);
281             assertEquals(new Long JavaDoc(3), obj);
282             assertTrue(obj instanceof Long JavaDoc);
283             obj = c.convertString("3", Long.TYPE);
284             assertEquals(new Long JavaDoc(3), obj);
285             assertTrue(obj instanceof Long JavaDoc);
286
287             obj = c.convertString("2.5", Float JavaDoc.class);
288             assertEquals(new Float JavaDoc(2.5f), obj);
289             assertTrue(obj instanceof Float JavaDoc);
290             obj = c.convertString("2.5", Float.TYPE);
291             assertEquals(new Float JavaDoc(2.5f), obj);
292             assertTrue(obj instanceof Float JavaDoc);
293
294             obj = c.convertString("3.14", Double JavaDoc.class);
295             assertEquals(new Double JavaDoc(3.14), obj);
296             assertTrue(obj instanceof Double JavaDoc);
297             obj = c.convertString("3.14", Double.TYPE);
298             assertEquals(new Double JavaDoc(3.14), obj);
299             assertTrue(obj instanceof Double JavaDoc);
300         } catch (TypeConversionException tce) {
301             fail(tce.toString());
302         }
303     }
304 }
Popular Tags