001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.util;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: public class IteratorChainTest extends TestCase {
025:
026: private Iterator<String> makeIterator(String marker, int howMany) {
027: final List<String> c = new ArrayList<String>();
028: for (int i = 1; i <= howMany; i++) {
029: c.add(marker + i);
030: }
031: return c.iterator();
032: }
033:
034: public void testNoIterator() {
035: final IteratorChain<String> c = new IteratorChain<String>();
036: assertFalse("Empty IteratorChain.hastNext() is false", c
037: .hasNext());
038: assertEquals("", getString(c));
039: }
040:
041: public void testCallNextTooEarly() {
042: final IteratorChain<String> c = new IteratorChain<String>();
043: c.addIterator(makeIterator("a", 3));
044: try {
045: c.next();
046: fail("Calling next() before hasNext() should throw RuntimeException");
047: } catch (RuntimeException asExpected) {
048: // we're fine
049: }
050: }
051:
052: public void testCallAddTooLate() {
053: final IteratorChain<String> c = new IteratorChain<String>();
054: c.hasNext();
055: try {
056: c.addIterator(makeIterator("a", 3));
057: fail("Calling addIterator after hasNext() should throw RuntimeException");
058: } catch (RuntimeException asExpected) {
059: // we're fine
060: }
061: }
062:
063: public void testRemove() {
064: final IteratorChain<String> c = new IteratorChain<String>();
065: try {
066: c.remove();
067: fail("Calling remove should throw UnsupportedOperationException");
068: } catch (UnsupportedOperationException asExpected) {
069: // we're fine
070: }
071: }
072:
073: public void testOneIterator() {
074: final IteratorChain<String> c = new IteratorChain<String>();
075: c.addIterator(makeIterator("a", 3));
076: assertEquals("a1a2a3", getString(c));
077: }
078:
079: public void testTwoIterators() {
080: final IteratorChain<String> c = new IteratorChain<String>();
081: c.addIterator(makeIterator("a", 3));
082: c.addIterator(makeIterator("b", 2));
083: assertEquals("a1a2a3b1b2", getString(c));
084: }
085:
086: public void testEmptyIteratorsInTheMiddle() {
087: final IteratorChain<String> c = new IteratorChain<String>();
088: c.addIterator(makeIterator("a", 3));
089: c.addIterator(makeIterator("b", 0));
090: c.addIterator(makeIterator("c", 1));
091: assertEquals("a1a2a3c1", getString(c));
092: }
093:
094: /** dump the contents of it to a String */
095: private String getString(Iterator<String> it) {
096: final StringBuffer sb = new StringBuffer();
097: sb.append("");
098: while (it.hasNext()) {
099: sb.append(it.next());
100: }
101: return sb.toString();
102: }
103: }
|