KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > test > TestIteratorCollection


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestIteratorCollection.java,v 1.2 2005/02/21 12:19:22 andy_seaborne Exp $
5 */

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 /**
18  @author hedgehog
19  */

20 public class TestIteratorCollection extends GraphTestBase
21     {
22     public TestIteratorCollection( String JavaDoc 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 JavaDoc [] elements = new Object JavaDoc[] {"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 JavaDoc[] {} );
48         testCloseForSet( new Object JavaDoc[] {"one"} );
49         testCloseForSet( new Object JavaDoc[] {"to", "free", "for"} );
50         testCloseForSet( new Object JavaDoc[] {"another", "one", "plus", Boolean.FALSE} );
51         testCloseForSet( new Object JavaDoc[] {"the", "king", "is", "in", "his", "counting", "house"} );
52         }
53     
54     protected void testCloseForSet( Object JavaDoc[] 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 JavaDoc[] {"to", "be", "or", "not", "to", "be"} );
76         assertEquals( list, IteratorCollection.iteratorToList( list.iterator() ) );
77         }
78         
79     public void testCloseForList()
80         {
81         testCloseForList( new Object JavaDoc[] {} );
82         testCloseForList( new Object JavaDoc[] {"one"} );
83         testCloseForList( new Object JavaDoc[] {"to", "free", "for"} );
84         testCloseForList( new Object JavaDoc[] {"another", "one", "plus", Boolean.FALSE} );
85         testCloseForList( new Object JavaDoc[] {"the", "king", "is", "in", "his", "counting", "house"} );
86         }
87     
88     protected void testCloseForList( Object JavaDoc[] 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 JavaDoc x )
98         {
99         Set result = new HashSet();
100         result.add( x );
101         return result;
102         }
103     
104     protected Set setLots( Object JavaDoc [] 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 JavaDoc x )
112         {
113         List result = new ArrayList();
114         result.add( x );
115         return result;
116         }
117     }
118
119 /*
120     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
121     All rights reserved.
122
123     Redistribution and use in source and binary forms, with or without
124     modification, are permitted provided that the following conditions
125     are met:
126
127     1. Redistributions of source code must retain the above copyright
128        notice, this list of conditions and the following disclaimer.
129
130     2. Redistributions in binary form must reproduce the above copyright
131        notice, this list of conditions and the following disclaimer in the
132        documentation and/or other materials provided with the distribution.
133
134     3. The name of the author may not be used to endorse or promote products
135        derived from this software without specific prior written permission.
136
137     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
138     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
139     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
140     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
141     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
142     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
143     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
144     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
145     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
146     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
147 */
Popular Tags