KickJava   Java API By Example, From Geeks To Geeks.

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


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.CharacterTypeConverter;
14
15
16 /**
17  * This class handles all of the test cases for the character
18  * type conversion support in the JBeans package.
19  *
20  * @author Brian Pontarelli
21  */

22 public class CharacterTypeConverterTest extends TestCase {
23
24     public static final char DEFAULT = 0;
25
26
27     /**
28      * Tests the convertString method works for primitive types and wrapper
29      */

30     public void testString() {
31         try {
32             CharacterTypeConverter ctc = new CharacterTypeConverter();
33
34             // Test primitive
35
Character JavaDoc c = (Character JavaDoc) ctc.convertString("a", Character.TYPE);
36             assertEquals(new Character JavaDoc('a'), c);
37
38             // Test wrapper
39
c = (Character JavaDoc) ctc.convertString("b", Character JavaDoc.class);
40             assertEquals(new Character JavaDoc('b'), c);
41
42             // Test null primitive
43
c = (Character JavaDoc) ctc.convertString(null, Character.TYPE);
44             assertEquals(new Character JavaDoc(DEFAULT), c);
45
46             // Test null wrapper
47
c = (Character JavaDoc) ctc.convertString(null, Character JavaDoc.class);
48             assertEquals(null, c);
49
50             // Test spaces primitive
51
c = (Character JavaDoc) ctc.convertString(" ", Character.TYPE);
52             assertEquals(new Character JavaDoc(DEFAULT), c);
53
54             // Test null wrapper
55
c = (Character JavaDoc) ctc.convertString(" ", Character JavaDoc.class);
56             assertEquals(null, c);
57         } catch (TypeConversionException tce) {
58             fail(tce.toString());
59         }
60
61         // Test primitive
62
try {
63             CharacterTypeConverter ctc = new CharacterTypeConverter();
64             ctc.convertString("ab", Character.TYPE);
65             fail("Should have failed because string is two characters");
66         } catch (TypeConversionException e) {
67             // Expected
68
System.out.println(e.toString());
69         }
70
71         // Test wrapper
72
try {
73             CharacterTypeConverter ctc = new CharacterTypeConverter();
74             ctc.convertString("ab", Character JavaDoc.class);
75             fail("Should have failed because string is two characters");
76         } catch (TypeConversionException e) {
77             // Expected
78
System.out.println(e.toString());
79         }
80     }
81
82     /**
83      * Tests the convertStringToArray method works for primitive types and wrapper
84      */

85     public void testStringToArray() {
86         try {
87             CharacterTypeConverter ctc = new CharacterTypeConverter();
88
89             // Test primitive
90
Character JavaDoc[] c = (Character JavaDoc[]) ctc.convertStringToArray("a,b", ",",
91                 char[].class);
92             assertEquals(new Character JavaDoc('a'), c[0]);
93             assertEquals(new Character JavaDoc('b'), c[1]);
94
95             // Test wrapper
96
c = (Character JavaDoc[]) ctc.convertStringToArray("b,c", ",",
97                 Character JavaDoc[].class);
98             assertEquals(new Character JavaDoc('b'), c[0]);
99             assertEquals(new Character JavaDoc('c'), c[1]);
100
101             // Test null primitive
102
c = (Character JavaDoc[]) ctc.convertStringToArray(null, ",", char[].class);
103             assertEquals(null, c);
104
105             // Test null wrapper
106
c = (Character JavaDoc[]) ctc.convertStringToArray(null, ",", Character JavaDoc[].class);
107             assertEquals(null, c);
108
109             // Test space primitive
110
c = (Character JavaDoc[]) ctc.convertStringToArray(" , ", ",", char[].class);
111             assertEquals(new Character JavaDoc(DEFAULT), c[0]);
112             assertEquals(new Character JavaDoc(DEFAULT), c[1]);
113
114             // Test spaces wrapper
115
c = (Character JavaDoc[]) ctc.convertStringToArray(" , ", ",", Character JavaDoc[].class);
116             assertEquals(null, c[0]);
117             assertEquals(null, c[1]);
118
119             // Test delegates primitive
120
c = (Character JavaDoc[]) ctc.convertString("a,b", char[].class);
121             assertEquals(new Character JavaDoc('a'), c[0]);
122             assertEquals(new Character JavaDoc('b'), c[1]);
123
124             // Test spaces wrapper
125
c = (Character JavaDoc[]) ctc.convertString("a,b", Character JavaDoc[].class);
126             assertEquals(new Character JavaDoc('a'), c[0]);
127             assertEquals(new Character JavaDoc('b'), c[1]);
128         } catch (TypeConversionException tce) {
129             fail(tce.toString());
130         }
131
132         // Test primitive
133
try {
134             CharacterTypeConverter ctc = new CharacterTypeConverter();
135             ctc.convertStringToArray("ab,cd", ",", char[].class);
136             fail("Should have failed because string is two characters");
137         } catch (TypeConversionException e) {
138             // Expected
139
System.out.println(e.toString());
140         }
141
142         // Test wrapper
143
try {
144             CharacterTypeConverter ctc = new CharacterTypeConverter();
145             ctc.convertStringToArray("ab, cd", ",", Character JavaDoc[].class);
146             fail("Should have failed because string is two characters");
147         } catch (TypeConversionException e) {
148             // Expected
149
System.out.println(e.toString());
150         }
151     }
152
153     /**
154      * Tests the convertArray method works for primitive types and wrapper
155      */

156     public void testArray() {
157         try {
158             CharacterTypeConverter ctc = new CharacterTypeConverter();
159
160             // Test primitive
161
Character JavaDoc c = (Character JavaDoc) ctc.convertArray(new String JavaDoc[]{"a"},
162                 Character.TYPE);
163             assertEquals(new Character JavaDoc('a'), c);
164
165             // Test wrapper
166
c = (Character JavaDoc) ctc.convertArray(new String JavaDoc[]{"b"}, Character JavaDoc.class);
167             assertEquals(new Character JavaDoc('b'), c);
168
169             // Test null primitive
170
c = (Character JavaDoc) ctc.convertArray(null, Character.TYPE);
171             assertEquals(new Character JavaDoc(DEFAULT), c);
172
173             // Test null wrapper
174
c = (Character JavaDoc) ctc.convertArray(null, Character JavaDoc.class);
175             assertEquals(null, c);
176
177             // Test spaces primitive
178
c = (Character JavaDoc) ctc.convertArray(new String JavaDoc[]{" "}, Character.TYPE);
179             assertEquals(new Character JavaDoc(DEFAULT), c);
180
181             // Test null wrapper
182
c = (Character JavaDoc) ctc.convertArray(new String JavaDoc[]{" "}, Character JavaDoc.class);
183             assertEquals(null, c);
184
185             // Test 0 length primitive
186
c = (Character JavaDoc) ctc.convertArray(new String JavaDoc[0], Character.TYPE);
187             assertEquals(new Character JavaDoc(DEFAULT), c);
188
189             // Test 0 length wrapper
190
c = (Character JavaDoc) ctc.convertArray(new String JavaDoc[0], Character JavaDoc.class);
191             assertEquals(null, c);
192         } catch (TypeConversionException tce) {
193             fail(tce.toString());
194         }
195
196         // Test primitive
197
try {
198             CharacterTypeConverter ctc = new CharacterTypeConverter();
199             ctc.convertArray(new String JavaDoc[]{"ab"}, Character.TYPE);
200             fail("Should have failed because string is two characters");
201         } catch (TypeConversionException e) {
202             // Expected
203
System.out.println(e.toString());
204         }
205
206         // Test wrapper
207
try {
208             CharacterTypeConverter ctc = new CharacterTypeConverter();
209             ctc.convertArray(new String JavaDoc[]{"ab"}, Character JavaDoc.class);
210             fail("Should have failed because string is two characters");
211         } catch (TypeConversionException e) {
212             // Expected
213
System.out.println(e.toString());
214         }
215     }
216
217     /**
218      * Tests the convertArrayToArray method works for primitive types and wrapper
219      */

220     public void testArrayToArray() {
221         try {
222             CharacterTypeConverter ctc = new CharacterTypeConverter();
223
224             // Test primitive
225
Character JavaDoc[] c = (Character JavaDoc[]) ctc.convertArrayToArray(new String JavaDoc[]{"a","b"},
226                 Character.TYPE);
227             assertEquals(new Character JavaDoc('a'), c[0]);
228             assertEquals(new Character JavaDoc('b'), c[1]);
229
230             // Test wrapper
231
c = (Character JavaDoc[]) ctc.convertArrayToArray(new String JavaDoc[]{"b", "c"},
232                 Character JavaDoc.class);
233             assertEquals(new Character JavaDoc('b'), c[0]);
234             assertEquals(new Character JavaDoc('c'), c[1]);
235
236             // Test null primitive
237
c = (Character JavaDoc[]) ctc.convertArrayToArray(null, Character.TYPE);
238             assertEquals(null, c);
239
240             // Test null wrapper
241
c = (Character JavaDoc[]) ctc.convertArrayToArray(null, Character JavaDoc.class);
242             assertEquals(null, c);
243
244             // Test null primitive
245
c = (Character JavaDoc[]) ctc.convertArrayToArray(new Character JavaDoc[2],
246                 Character.TYPE);
247             assertEquals(new Character JavaDoc(DEFAULT), c[0]);
248             assertEquals(new Character JavaDoc(DEFAULT), c[1]);
249
250             // Test null wrapper
251
c = (Character JavaDoc[]) ctc.convertArrayToArray(new Character JavaDoc[2],
252                 Character JavaDoc.class);
253             assertEquals(null, c[0]);
254             assertEquals(null, c[1]);
255
256             // Test spaces primitive
257
c = (Character JavaDoc[]) ctc.convertArrayToArray(new String JavaDoc[]{" ", " "},
258                 Character.TYPE);
259             assertEquals(new Character JavaDoc(DEFAULT), c[0]);
260             assertEquals(new Character JavaDoc(DEFAULT), c[1]);
261
262             // Test spaces wrapper
263
c = (Character JavaDoc[]) ctc.convertArrayToArray(new String JavaDoc[]{" ", " "},
264                 Character JavaDoc.class);
265             assertEquals(null, c[0]);
266             assertEquals(null, c[1]);
267
268             // Test delegates primitive
269
c = (Character JavaDoc[]) ctc.convertArray(new String JavaDoc[]{"a", "b"},
270                 char[].class);
271             assertEquals(new Character JavaDoc('a'), c[0]);
272             assertEquals(new Character JavaDoc('b'), c[1]);
273
274             // Test spaces wrapper
275
c = (Character JavaDoc[]) ctc.convertArrayToArray(new String JavaDoc[]{"a", "b"},
276                 Character JavaDoc[].class);
277             assertEquals(new Character JavaDoc('a'), c[0]);
278             assertEquals(new Character JavaDoc('b'), c[1]);
279         } catch (TypeConversionException tce) {
280             fail(tce.toString());
281         }
282
283         // Test primitive
284
try {
285             CharacterTypeConverter ctc = new CharacterTypeConverter();
286             ctc.convertArrayToArray(new String JavaDoc[]{"ab", "cd"}, Character.TYPE);
287             fail("Should have failed because string is two characters");
288         } catch (TypeConversionException e) {
289             // Expected
290
System.out.println(e.toString());
291         }
292
293         // Test wrapper
294
try {
295             CharacterTypeConverter ctc = new CharacterTypeConverter();
296             ctc.convertArrayToArray(new String JavaDoc[]{"ab", "cd"}, Character JavaDoc.class);
297             fail("Should have failed because string is two characters");
298         } catch (TypeConversionException e) {
299             // Expected
300
System.out.println(e.toString());
301         }
302     }
303 }
Popular Tags