001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestIteratorCollection.java,v 1.5 2008/01/02 12:08:35 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.util.test;
008:
009: import java.util.*;
010:
011: import junit.framework.TestSuite;
012:
013: import com.hp.hpl.jena.graph.test.GraphTestBase;
014: import com.hp.hpl.jena.util.*;
015: import com.hp.hpl.jena.util.iterator.*;
016:
017: /**
018: @author hedgehog
019: */
020: public class TestIteratorCollection extends GraphTestBase {
021: public TestIteratorCollection(String name) {
022: super (name);
023: }
024:
025: public static TestSuite suite() {
026: return new TestSuite(TestIteratorCollection.class);
027: }
028:
029: public void testEmptyToEmptySet() {
030: assertEquals(CollectionFactory.createHashedSet(),
031: IteratorCollection.iteratorToSet(NullIterator.instance));
032: }
033:
034: public void testSingletonToSingleSet() {
035: assertEquals(oneSet("single"),
036: iteratorToSet(new SingletonIterator("single")));
037: }
038:
039: public void testLotsToSet() {
040: Object[] elements = new Object[] { "now", "is", "the", "time" };
041: Iterator it = Arrays.asList(elements).iterator();
042: assertEquals(setLots(elements), IteratorCollection
043: .iteratorToSet(it));
044: }
045:
046: public void testCloseForSet() {
047: testCloseForSet(new Object[] {});
048: testCloseForSet(new Object[] { "one" });
049: testCloseForSet(new Object[] { "to", "free", "for" });
050: testCloseForSet(new Object[] { "another", "one", "plus",
051: Boolean.FALSE });
052: testCloseForSet(new Object[] { "the", "king", "is", "in",
053: "his", "counting", "house" });
054: }
055:
056: protected void testCloseForSet(Object[] objects) {
057: final boolean[] closed = { false };
058: Iterator iterator = new WrappedIterator(Arrays.asList(objects)
059: .iterator()) {
060: public void close() {
061: super .close();
062: closed[0] = true;
063: }
064: };
065: iteratorToSet(iterator);
066: assertTrue(closed[0]);
067: }
068:
069: public void testEmptyToEmptyList() {
070: assertEquals(new ArrayList(), IteratorCollection
071: .iteratorToList(NullIterator.instance));
072: }
073:
074: public void testSingletonToSingletonList() {
075: assertEquals(oneList("just one"), IteratorCollection
076: .iteratorToList(new SingletonIterator("just one")));
077: }
078:
079: public void testLotsToList() {
080: List list = Arrays.asList(new Object[] { "to", "be", "or",
081: "not", "to", "be" });
082: assertEquals(list, IteratorCollection.iteratorToList(list
083: .iterator()));
084: }
085:
086: public void testCloseForList() {
087: testCloseForList(new Object[] {});
088: testCloseForList(new Object[] { "one" });
089: testCloseForList(new Object[] { "to", "free", "for" });
090: testCloseForList(new Object[] { "another", "one", "plus",
091: Boolean.FALSE });
092: testCloseForList(new Object[] { "the", "king", "is", "in",
093: "his", "counting", "house" });
094: }
095:
096: protected void testCloseForList(Object[] objects) {
097: final boolean[] closed = { false };
098: Iterator iterator = new WrappedIterator(Arrays.asList(objects)
099: .iterator()) {
100: public void close() {
101: super .close();
102: closed[0] = true;
103: }
104: };
105: iteratorToList(iterator);
106: assertTrue(closed[0]);
107: }
108:
109: protected Set oneSet(Object x) {
110: Set result = new HashSet();
111: result.add(x);
112: return result;
113: }
114:
115: protected Set setLots(Object[] elements) {
116: Set result = new HashSet();
117: for (int i = 0; i < elements.length; i += 1)
118: result.add(elements[i]);
119: return result;
120: }
121:
122: protected List oneList(Object x) {
123: List result = new ArrayList();
124: result.add(x);
125: return result;
126: }
127: }
128:
129: /*
130: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
131: All rights reserved.
132:
133: Redistribution and use in source and binary forms, with or without
134: modification, are permitted provided that the following conditions
135: are met:
136:
137: 1. Redistributions of source code must retain the above copyright
138: notice, this list of conditions and the following disclaimer.
139:
140: 2. Redistributions in binary form must reproduce the above copyright
141: notice, this list of conditions and the following disclaimer in the
142: documentation and/or other materials provided with the distribution.
143:
144: 3. The name of the author may not be used to endorse or promote products
145: derived from this software without specific prior written permission.
146:
147: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
148: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
149: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
150: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
151: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
152: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
153: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
154: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
155: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
156: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
157: */
|