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.Collections; 018 import java.util.Iterator; 019 import java.util.List; 020 021 import org.apache.tapestry.BaseComponentTestCase; 022 import org.testng.annotations.Test; 023 024 /** 025 * @author Howard M. Lewis Ship 026 * @since 4.0 027 */ 028 @Test 029 public class TestIteratorConverters extends BaseComponentTestCase 030 { 031 public void testObjectToIterator() 032 { 033 Object o = new Object(); 034 TypeConverter c = new ObjectToIteratorConverter(); 035 036 Iterator i = (Iterator) c.convertValue(o); 037 038 assertTrue(i.hasNext()); 039 assertSame(o, i.next()); 040 assertFalse(i.hasNext()); 041 } 042 043 public void testNullToIterator() 044 { 045 TypeConverter c = new NullToIteratorConverter(); 046 047 Iterator i = (Iterator) c.convertValue("will be null"); 048 049 assertFalse(i.hasNext()); 050 } 051 052 public void testCollectionToIterator() 053 { 054 Object o = new Object(); 055 List l = Collections.singletonList(o); 056 TypeConverter c = new CollectionToIteratorConverter(); 057 058 Iterator i = (Iterator) c.convertValue(l); 059 060 assertTrue(i.hasNext()); 061 assertSame(o, i.next()); 062 assertFalse(i.hasNext()); 063 } 064 065 public void testObjectArrayToIterator() 066 { 067 Object o = new Object(); 068 Object[] a = new Object[] 069 { o }; 070 071 TypeConverter c = new ObjectArrayToIteratorConverter(); 072 073 Iterator i = (Iterator) c.convertValue(a); 074 075 assertTrue(i.hasNext()); 076 assertSame(o, i.next()); 077 assertFalse(i.hasNext()); 078 } 079 }