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 */
017package org.apache.commons.collections.primitives;
018
019import java.util.Iterator;
020import java.util.NoSuchElementException;
021
022import org.apache.commons.collections.iterators.AbstractTestIterator;
023import org.apache.commons.collections.primitives.adapters.ShortIteratorIterator;
024
025/**
026 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
027 * @author Rodney Waldhoff
028 */
029public abstract class TestShortIterator extends AbstractTestIterator {
030
031    // conventional
032    // ------------------------------------------------------------------------
033
034    public TestShortIterator(String testName) {
035        super(testName);
036    }
037
038    // collections testing framework
039    // ------------------------------------------------------------------------
040
041    public Iterator makeEmptyIterator() {
042        return ShortIteratorIterator.wrap(makeEmptyShortIterator());
043    }
044
045    public Iterator makeFullIterator() {
046        return ShortIteratorIterator.wrap(makeFullShortIterator());
047    }
048
049
050    protected abstract ShortIterator makeEmptyShortIterator();
051    protected abstract ShortIterator makeFullShortIterator();
052    protected abstract short[] getFullElements();
053
054    // tests
055    // ------------------------------------------------------------------------
056    
057    public void testNextHasNextRemove() {
058        short[] elements = getFullElements();
059        ShortIterator iter = makeFullShortIterator();
060        for(int i=0;i<elements.length;i++) {
061            assertTrue(iter.hasNext());
062            assertEquals(elements[i],iter.next());
063            if(supportsRemove()) {
064                iter.remove();
065            }
066        }        
067        assertTrue(! iter.hasNext() );
068    }
069
070    public void testEmptyShortIterator() {
071        assertTrue( ! makeEmptyShortIterator().hasNext() );
072        try {
073            makeEmptyShortIterator().next();
074            fail("Expected NoSuchElementException");
075        } catch(NoSuchElementException e) {
076            // expected
077        }
078        if(supportsRemove()) {
079            try {
080                makeEmptyShortIterator().remove();
081                fail("Expected IllegalStateException");
082            } catch(IllegalStateException e) {
083                // expected
084            }
085        }        
086    }
087
088    public void testRemoveBeforeNext() {
089        if(supportsRemove()) {
090            try {
091                makeFullShortIterator().remove();
092                fail("Expected IllegalStateException");
093            } catch(IllegalStateException e) {
094                // expected
095            }
096        }        
097    }
098
099    public void testRemoveAfterRemove() {
100        if(supportsRemove()) {
101            ShortIterator iter = makeFullShortIterator();
102            iter.next();
103            iter.remove();
104            try {
105                iter.remove();
106                fail("Expected IllegalStateException");
107            } catch(IllegalStateException e) {
108                // expected
109            }
110        }        
111    }
112}