KickJava   Java API By Example, From Geeks To Geeks.

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


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.TypeConverter;
14 import com.inversoft.util.typeconverter.converters.BooleanTypeConverter;
15
16
17 /**
18  * This class handles all of the test cases for the Type
19  * conversion support in the JBeans package.
20  *
21  * @author Brian Pontarelli
22  */

23 public class BooleanTypeConverterTest extends TestCase {
24
25     /** Construct and new test instance */
26     public BooleanTypeConverterTest(String JavaDoc name) {
27         super(name);
28     }
29
30     /**
31      * Test the conversion of null values
32      */

33     public void testNullConversions() {
34         try {
35             String JavaDoc stringValue = null;
36             TypeConverter converter = new BooleanTypeConverter();
37             Object JavaDoc obj = converter.convert(stringValue, Boolean JavaDoc.class);
38
39             assertTrue("obj should be null", obj == null);
40         } catch (TypeConversionException tce) {
41             fail(tce.toString());
42         }
43     }
44
45     /**
46      * Test the conversion of empty (whitespace) values
47      */

48     public void testEmptyConversions() {
49         try {
50             String JavaDoc stringValue = " ";
51             TypeConverter converter = new BooleanTypeConverter();
52             Object JavaDoc obj = converter.convert(stringValue, Boolean JavaDoc.class);
53
54             assertTrue("obj should be null", obj == null);
55         } catch (TypeConversionException tce) {
56             fail(tce.toString());
57         }
58     }
59
60     /**
61      * Tests the conversion from a string to a boolean
62      */

63     public void testBooleanConversion() {
64         try {
65             String JavaDoc value = "true";
66             TypeConverter converter = new BooleanTypeConverter();
67             Object JavaDoc booleanObject = converter.convertString(value, Boolean JavaDoc.class);
68
69             if (booleanObject instanceof Boolean JavaDoc) {
70                 assertTrue("Should be true", ((Boolean JavaDoc) booleanObject).booleanValue());
71             } else {
72                 fail("Type is not correct: " + booleanObject.getClass().getName());
73             }
74         } catch (TypeConversionException tce) {
75             fail(tce.toString());
76         }
77
78         try {
79             String JavaDoc value = "fals3";
80             TypeConverter converter = new BooleanTypeConverter();
81             converter.convertString(value, Boolean JavaDoc.class);
82             fail("Should have thrown an exception");
83         } catch (TypeConversionException tce) {
84             //System.err.println(tce.toString());
85
assertTrue("Should have no root cause or target", tce.getCause() == null);
86         }
87     }
88
89     /**
90      * Tests that sending in empty primitives returns a Boolean.FALSE value.
91      */

92     public void testPrimitiveFalse() {
93         try {
94             String JavaDoc value = " ";
95             TypeConverter converter = new BooleanTypeConverter();
96             Object JavaDoc booleanObject = converter.convertString(value, Boolean.TYPE);
97
98             if (booleanObject instanceof Boolean JavaDoc) {
99                 assertFalse("Should be false", ((Boolean JavaDoc) booleanObject).booleanValue());
100             } else {
101                 fail("Type is not correct: " + booleanObject.getClass().getName());
102             }
103         } catch (TypeConversionException tce) {
104             fail(tce.toString());
105         }
106     }
107
108     /**
109      * Tests that the convertString method delegates to other methods if type is
110      * an array
111      */

112     public void testStringDelegates() {
113         try {
114             String JavaDoc value = " ,true, false";
115             BooleanTypeConverter converter = new BooleanTypeConverter();
116             Object JavaDoc booleanObject = converter.convertString(value, new boolean[0].getClass());
117
118             if (booleanObject instanceof Boolean JavaDoc[]) {
119                 assertEquals(3, ((Boolean JavaDoc[]) booleanObject).length);
120                 assertEquals(false, ((Boolean JavaDoc[]) booleanObject)[0].booleanValue());
121                 assertTrue("Should be false", ((Boolean JavaDoc[]) booleanObject)[1].booleanValue());
122                 assertFalse("Should be false", ((Boolean JavaDoc[]) booleanObject)[2].booleanValue());
123             } else {
124                 fail("Type is not correct: " + booleanObject.getClass().getName());
125             }
126         } catch (TypeConversionException tce) {
127             fail(tce.toString());
128         }
129     }
130
131     /**
132      * Tests the conversion from an array of booleans or strings to a single Boolean
133      * using the convertArray method
134      */

135     public void testConvertArray() {
136         try {
137             Boolean JavaDoc [] bool = new Boolean JavaDoc[1];
138             bool[0] = new Boolean JavaDoc(true);
139             TypeConverter converter = new BooleanTypeConverter();
140             Object JavaDoc booleanObject = converter.convertArray(bool, Boolean JavaDoc.class);
141
142             if (booleanObject instanceof Boolean JavaDoc) {
143                 assertTrue("Should be true", ((Boolean JavaDoc) booleanObject).booleanValue());
144             } else {
145                 fail("Type is not correct: " + booleanObject.getClass().getName());
146             }
147         } catch (TypeConversionException tce) {
148             fail(tce.toString());
149         }
150
151         try {
152             Boolean JavaDoc [] bool = new Boolean JavaDoc[2];
153             TypeConverter converter = new BooleanTypeConverter();
154             converter.convertArray(bool, Boolean JavaDoc.class);
155             fail("Should have thrown an exception because array is length 2");
156         } catch (TypeConversionException tce) {
157             //System.err.println(tce.toString());
158
assertTrue("Should have no root cause or target", tce.getCause() == null);
159         }
160
161         try {
162             String JavaDoc [] bool = new String JavaDoc[1];
163             bool[0] = new String JavaDoc("true");
164             TypeConverter converter = new BooleanTypeConverter();
165             Object JavaDoc booleanObject = converter.convertArray(bool, Boolean JavaDoc.class);
166
167             if (booleanObject instanceof Boolean JavaDoc) {
168                 assertTrue("Should be true", ((Boolean JavaDoc) booleanObject).booleanValue());
169             } else {
170                 fail("Type is not correct: " + booleanObject.getClass().getName());
171             }
172         } catch (TypeConversionException tce) {
173             fail(tce.toString());
174         }
175
176         try {
177             TypeConverter converter = new BooleanTypeConverter();
178             Object JavaDoc booleanObject = converter.convertArray(null, Boolean.TYPE);
179
180             if (booleanObject instanceof Boolean JavaDoc) {
181                 assertFalse(((Boolean JavaDoc) booleanObject).booleanValue());
182             } else {
183                 fail("Type is not correct: " + booleanObject.getClass().getName());
184             }
185         } catch (TypeConversionException tce) {
186             fail(tce.toString());
187         }
188
189         try {
190             TypeConverter converter = new BooleanTypeConverter();
191             Object JavaDoc booleanObject = converter.convertArray(new String JavaDoc[0], Boolean.TYPE);
192
193             if (booleanObject instanceof Boolean JavaDoc) {
194                 assertFalse(((Boolean JavaDoc) booleanObject).booleanValue());
195             } else {
196                 fail("Type is not correct: " + booleanObject.getClass().getName());
197             }
198         } catch (TypeConversionException tce) {
199             fail(tce.toString());
200         }
201     }
202
203     /**
204      * Tests the conversion from an array of booleans or strings to and array of booleans
205      * using the convertArrayToArray method
206      */

207     public void testConvertArrayToArray() {
208         try {
209             Boolean JavaDoc [] bool = new Boolean JavaDoc[2];
210             bool[0] = new Boolean JavaDoc(true);
211             bool[1] = new Boolean JavaDoc(false);
212             TypeConverter converter = new BooleanTypeConverter();
213             Object JavaDoc booleanObject = converter.convertArrayToArray(bool, Boolean JavaDoc.class);
214
215             if (booleanObject instanceof Boolean JavaDoc []) {
216                 assertTrue("Test 1 should be true", ((Boolean JavaDoc []) booleanObject)[0].booleanValue());
217                 assertTrue("Test 1 should be false", !((Boolean JavaDoc []) booleanObject)[1].booleanValue());
218             } else {
219                 fail("Type is not correct: " + booleanObject.getClass().getName());
220             }
221         } catch (TypeConversionException tce) {
222             fail(tce.toString());
223         }
224
225         try {
226             String JavaDoc [] bool = new String JavaDoc[2];
227             bool[0] = "true";
228             bool[1] = "false";
229             TypeConverter converter = new BooleanTypeConverter();
230             Object JavaDoc booleanObject = converter.convertArrayToArray(bool, Boolean JavaDoc.class);
231
232             if (booleanObject instanceof Boolean JavaDoc []) {
233                 assertTrue("Test 2 should be true", ((Boolean JavaDoc []) booleanObject)[0].booleanValue());
234                 assertTrue("Test 2 should be false", !((Boolean JavaDoc []) booleanObject)[1].booleanValue());
235             } else {
236                 fail("Type is not correct: " + booleanObject.getClass().getName());
237             }
238         } catch (TypeConversionException tce) {
239             fail(tce.toString());
240         }
241
242         try {
243             String JavaDoc [] bool = new String JavaDoc[2];
244             bool[0] = "true";
245             bool[1] = "fals3";
246             TypeConverter converter = new BooleanTypeConverter();
247             converter.convertArrayToArray(bool, Boolean JavaDoc.class);
248             fail("Should have failed because second array is fals3");
249         } catch (TypeConversionException tce) {
250             //System.err.println(tce.toString());
251
assertTrue("Should have no root cause or target", tce.getCause() == null);
252         }
253     }
254
255     /**
256      * Tests the conversion from an array of strings or a comma separated list of
257      * booleans to an array of Booleans using the convertToArray method
258      */

259     public void testConvertToArray() {
260         try {
261             String JavaDoc [] bool = new String JavaDoc[2];
262             bool[0] = "true";
263             bool[1] = "false";
264             TypeConverter converter = new BooleanTypeConverter();
265             Object JavaDoc booleanObject = converter.convertToArray(bool, Boolean JavaDoc.class);
266
267             if (booleanObject instanceof Boolean JavaDoc []) {
268                 assertTrue("Should be true", ((Boolean JavaDoc []) booleanObject)[0].booleanValue());
269                 assertTrue("Should be false", !((Boolean JavaDoc []) booleanObject)[1].booleanValue());
270             } else {
271                 fail("Type is not correct: " + booleanObject.getClass().getName());
272             }
273         } catch (TypeConversionException tce) {
274             fail(tce.toString());
275         }
276
277         try {
278             String JavaDoc bool = "true,false";
279             TypeConverter converter = new BooleanTypeConverter();
280             Object JavaDoc booleanObject = converter.convertToArray(bool, Boolean JavaDoc.class);
281
282             if (booleanObject instanceof Boolean JavaDoc []) {
283                 assertTrue("Should be true", ((Boolean JavaDoc []) booleanObject)[0].booleanValue());
284                 assertTrue("Should be false", !((Boolean JavaDoc []) booleanObject)[1].booleanValue());
285             } else {
286                 fail("Type is not correct: " + booleanObject.getClass().getName());
287             }
288         } catch (TypeConversionException tce) {
289             fail(tce.toString());
290         }
291
292         try {
293             String JavaDoc bool = "true,fals3";
294             TypeConverter converter = new BooleanTypeConverter();
295             converter.convertToArray(bool, Boolean JavaDoc.class);
296             fail("Should have failed because second array is fals3");
297         } catch (TypeConversionException tce) {
298             //System.err.println(tce.toString());
299
assertTrue("Should have no root cause or target", tce.getCause() == null);
300         }
301     }
302
303     /**
304      * Tests the multiplexing of the convert method.
305      */

306     public void testConvertMethod() {
307
308         // Test that the convert method correctly calls convertArray
309
try {
310             Boolean JavaDoc [] bool = new Boolean JavaDoc[1];
311             bool[0] = new Boolean JavaDoc(true);
312             TypeConverter converter = new BooleanTypeConverter();
313             Object JavaDoc booleanObject = converter.convert(bool, Boolean JavaDoc.class);
314
315             if (booleanObject instanceof Boolean JavaDoc) {
316                 assertTrue("Should be true", ((Boolean JavaDoc) booleanObject).booleanValue());
317             } else {
318                 fail("Type is not correct: " + booleanObject.getClass().getName());
319             }
320         } catch (TypeConversionException tce) {
321             fail("Convert test 1 failed: " + tce.toString());
322         }
323
324         // Test that the convert method correctly calls convertToArray
325
try {
326             String JavaDoc bool = "true,false";
327             TypeConverter converter = new BooleanTypeConverter();
328             Object JavaDoc booleanObject = converter.convert(bool, Boolean JavaDoc[].class);
329
330             if (booleanObject instanceof Boolean JavaDoc []) {
331                 assertTrue("Should be true", ((Boolean JavaDoc []) booleanObject)[0].booleanValue());
332                 assertTrue("Should be false", !((Boolean JavaDoc []) booleanObject)[1].booleanValue());
333             } else {
334                 fail("Type is not correct: " + booleanObject.getClass().getName());
335             }
336         } catch (TypeConversionException tce) {
337             fail("Convert test 2 failed: " + tce.toString());
338         }
339
340         // Test the the convert method correctly calls convertArrayToArray
341
try {
342             String JavaDoc [] bool = new String JavaDoc[2];
343             bool[0] = "true";
344             bool[1] = "false";
345             TypeConverter converter = new BooleanTypeConverter();
346             Object JavaDoc booleanObject = converter.convert(bool, Boolean JavaDoc[].class);
347
348             if (booleanObject instanceof Boolean JavaDoc []) {
349                 assertTrue("Test 1 should be true", ((Boolean JavaDoc []) booleanObject)[0].booleanValue());
350                 assertTrue("Test 1 should be false", !((Boolean JavaDoc []) booleanObject)[1].booleanValue());
351             } else {
352                 fail("Type is not correct: " + booleanObject.getClass().getName());
353             }
354         } catch (TypeConversionException tce) {
355             fail("Convert test 3 failed: " + tce.toString());
356         }
357     }
358 }
Popular Tags