KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > coerce > TestBooleanConverters


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.coerce;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.hivemind.test.HiveMindTestCase;
23
24 /**
25  * Tests several implementations of {@link org.apache.tapestry.coerce.TypeConverter}that return
26  * Boolean values.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 4.0
30  */

31 public class TestBooleanConverters extends HiveMindTestCase
32 {
33     public void testStringToBoolean()
34     {
35         StringToBooleanConverter c = new StringToBooleanConverter();
36
37         assertSame(Boolean.TRUE, c.convertValue("fred"));
38
39         // The special case
40
assertSame(Boolean.FALSE, c.convertValue("false"));
41
42         assertSame(Boolean.FALSE, c.convertValue(" "));
43         assertSame(Boolean.FALSE, c.convertValue(""));
44
45         // Actually, null will never be passed in, but ..
46

47         assertSame(Boolean.FALSE, c.convertValue(null));
48     }
49
50     public void testMapToBoolean()
51     {
52         Map JavaDoc m = new HashMap JavaDoc();
53
54         TypeConverter c = new MapToBooleanConverter();
55
56         assertSame(Boolean.FALSE, c.convertValue(m));
57
58         m.put("foo", "bar");
59
60         assertSame(Boolean.TRUE, c.convertValue(m));
61     }
62
63     public void testNullToBoolean()
64     {
65         assertSame(Boolean.FALSE, new NullToBooleanConverter().convertValue("doesn't matter"));
66     }
67
68     public void testCollectionToBoolean()
69     {
70         List JavaDoc l = new ArrayList JavaDoc();
71         TypeConverter c = new CollectionToBooleanConverter();
72
73         assertSame(Boolean.FALSE, c.convertValue(l));
74
75         l.add("foo");
76
77         assertSame(Boolean.TRUE, c.convertValue(l));
78     }
79
80     public void testNumberToBoolean()
81     {
82         TypeConverter c = new NumberToBooleanConverter();
83
84         assertSame(Boolean.FALSE, c.convertValue(new Integer JavaDoc(0)));
85         assertSame(Boolean.TRUE, c.convertValue(new Integer JavaDoc(7)));
86     }
87
88     public void testObjectToBoolean()
89     {
90         TypeConverter c = new ObjectToBooleanConverter();
91
92         assertSame(Boolean.TRUE, c.convertValue("foo"));
93     }
94
95 }
Popular Tags