1 6 7 package com.hp.hpl.jena.util.test; 8 9 import java.util.*; 10 11 import junit.framework.TestSuite; 12 13 import com.hp.hpl.jena.graph.test.GraphTestBase; 14 import com.hp.hpl.jena.util.*; 15 import com.hp.hpl.jena.util.iterator.*; 16 17 20 public class TestIteratorCollection extends GraphTestBase 21 { 22 public TestIteratorCollection( String name ) 23 { super( name ); } 24 25 public static TestSuite suite() 26 { return new TestSuite( TestIteratorCollection.class ); } 27 28 public void testEmptyToEmptySet() 29 { 30 assertEquals( CollectionFactory.createHashedSet(), IteratorCollection.iteratorToSet( NullIterator.instance ) ); 31 } 32 33 public void testSingletonToSingleSet() 34 { 35 assertEquals( oneSet( "single" ), iteratorToSet( new SingletonIterator( "single" ) ) ); 36 } 37 38 public void testLotsToSet() 39 { 40 Object [] elements = new Object [] {"now", "is", "the", "time"}; 41 Iterator it = Arrays.asList( elements ).iterator(); 42 assertEquals( setLots( elements ), IteratorCollection.iteratorToSet( it ) ); 43 } 44 45 public void testCloseForSet() 46 { 47 testCloseForSet( new Object [] {} ); 48 testCloseForSet( new Object [] {"one"} ); 49 testCloseForSet( new Object [] {"to", "free", "for"} ); 50 testCloseForSet( new Object [] {"another", "one", "plus", Boolean.FALSE} ); 51 testCloseForSet( new Object [] {"the", "king", "is", "in", "his", "counting", "house"} ); 52 } 53 54 protected void testCloseForSet( Object [] objects ) 55 { 56 final boolean [] closed = {false}; 57 Iterator iterator = new WrappedIterator( Arrays.asList( objects ).iterator() ) 58 { public void close() { super.close(); closed[0] = true; } }; 59 iteratorToSet( iterator ); 60 assertTrue( closed[0] ); 61 } 62 63 public void testEmptyToEmptyList() 64 { 65 assertEquals( new ArrayList(), IteratorCollection.iteratorToList( NullIterator.instance ) ); 66 } 67 68 public void testSingletonToSingletonList() 69 { 70 assertEquals( oneList( "just one" ), IteratorCollection.iteratorToList( new SingletonIterator( "just one" ) ) ); 71 } 72 73 public void testLotsToList() 74 { 75 List list = Arrays.asList( new Object [] {"to", "be", "or", "not", "to", "be"} ); 76 assertEquals( list, IteratorCollection.iteratorToList( list.iterator() ) ); 77 } 78 79 public void testCloseForList() 80 { 81 testCloseForList( new Object [] {} ); 82 testCloseForList( new Object [] {"one"} ); 83 testCloseForList( new Object [] {"to", "free", "for"} ); 84 testCloseForList( new Object [] {"another", "one", "plus", Boolean.FALSE} ); 85 testCloseForList( new Object [] {"the", "king", "is", "in", "his", "counting", "house"} ); 86 } 87 88 protected void testCloseForList( Object [] objects ) 89 { 90 final boolean [] closed = {false}; 91 Iterator iterator = new WrappedIterator( Arrays.asList( objects ).iterator() ) 92 { public void close() { super.close(); closed[0] = true; } }; 93 iteratorToList( iterator ); 94 assertTrue( closed[0] ); 95 } 96 97 protected Set oneSet( Object x ) 98 { 99 Set result = new HashSet(); 100 result.add( x ); 101 return result; 102 } 103 104 protected Set setLots( Object [] elements ) 105 { 106 Set result = new HashSet(); 107 for (int i = 0; i < elements.length; i += 1) result.add( elements[i] ); 108 return result; 109 } 110 111 protected List oneList( Object x ) 112 { 113 List result = new ArrayList(); 114 result.add( x ); 115 return result; 116 } 117 } 118 119 | Popular Tags |