001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.coerce; 016 017 import java.util.ArrayList; 018 import java.util.HashMap; 019 import java.util.Iterator; 020 import java.util.List; 021 import java.util.Map; 022 023 import org.apache.tapestry.BaseComponentTestCase; 024 import org.testng.annotations.Test; 025 026 /** 027 * Tests several implementations of {@link org.apache.tapestry.coerce.TypeConverter}that return 028 * Boolean values. 029 * 030 * @author Howard M. Lewis Ship 031 * @since 4.0 032 */ 033 @Test 034 public class TestBooleanConverters extends BaseComponentTestCase 035 { 036 public void testStringToBoolean() 037 { 038 StringToBooleanConverter c = new StringToBooleanConverter(); 039 040 assertSame(Boolean.TRUE, c.convertValue("fred")); 041 042 // The special case 043 assertSame(Boolean.FALSE, c.convertValue("false")); 044 045 assertSame(Boolean.FALSE, c.convertValue(" ")); 046 assertSame(Boolean.FALSE, c.convertValue("")); 047 048 // Actually, null will never be passed in, but .. 049 050 assertSame(Boolean.FALSE, c.convertValue(null)); 051 } 052 053 public void testMapToBoolean() 054 { 055 Map m = new HashMap(); 056 057 TypeConverter c = new MapToBooleanConverter(); 058 059 assertSame(Boolean.FALSE, c.convertValue(m)); 060 061 m.put("foo", "bar"); 062 063 assertSame(Boolean.TRUE, c.convertValue(m)); 064 } 065 066 public void testNullToBoolean() 067 { 068 assertSame(Boolean.FALSE, new NullToBooleanConverter().convertValue("doesn't matter")); 069 } 070 071 public void testCollectionToBoolean() 072 { 073 List l = new ArrayList(); 074 TypeConverter c = new CollectionToBooleanConverter(); 075 076 assertSame(Boolean.FALSE, c.convertValue(l)); 077 078 l.add("foo"); 079 080 assertSame(Boolean.TRUE, c.convertValue(l)); 081 } 082 083 public void testNumberToBoolean() 084 { 085 TypeConverter c = new NumberToBooleanConverter(); 086 087 assertSame(Boolean.FALSE, c.convertValue(new Integer(0))); 088 assertSame(Boolean.TRUE, c.convertValue(new Integer(7))); 089 } 090 091 public void testObjectToBoolean() 092 { 093 TypeConverter c = new ObjectToBooleanConverter(); 094 095 assertSame(Boolean.TRUE, c.convertValue("foo")); 096 } 097 098 public void testBooleanArrayToIterator() 099 { 100 TypeConverter c = new BooleanArrayToIteratorConverter(); 101 102 boolean[] input = 103 { false, true, true, false }; 104 105 Iterator i = (Iterator) c.convertValue(input); 106 107 for (int j = 0; j < input.length; j++) 108 { 109 Boolean expected = input[j] ? Boolean.TRUE : Boolean.FALSE; 110 111 assertSame(expected, i.next()); 112 } 113 114 assertEquals(false, i.hasNext()); 115 116 } 117 118 }