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.adapters;
018
019import java.util.Iterator;
020import java.util.ListIterator;
021import java.util.NoSuchElementException;
022
023import junit.framework.Test;
024import junit.framework.TestSuite;
025
026import org.apache.commons.collections.iterators.AbstractTestListIterator;
027import org.apache.commons.collections.primitives.ArrayIntList;
028import org.apache.commons.collections.primitives.IntList;
029
030/**
031 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
032 * @author Rodney Waldhoff
033 */
034public class TestIntListIteratorListIterator extends AbstractTestListIterator {
035
036    // conventional
037    // ------------------------------------------------------------------------
038
039    public TestIntListIteratorListIterator(String testName) {
040        super(testName);
041    }
042
043    public static Test suite() {
044        return new TestSuite(TestIntListIteratorListIterator.class);
045    }
046
047    // collections testing framework
048    // ------------------------------------------------------------------------
049
050    public ListIterator makeEmptyListIterator() {
051        return IntListIteratorListIterator.wrap(makeEmptyIntList().listIterator());
052    }
053    
054    public ListIterator makeFullListIterator() {
055        return IntListIteratorListIterator.wrap(makeFullIntList().listIterator());
056    }
057
058    protected IntList makeEmptyIntList() {
059        return new ArrayIntList();
060    }
061    
062    protected IntList makeFullIntList() {
063        IntList list = makeEmptyIntList();
064        int[] elts = getFullElements();
065        for(int i=0;i<elts.length;i++) {
066            list.add(elts[i]);
067        }
068        return list;
069    }
070    
071    public int[] getFullElements() {
072        return new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
073    }
074    
075    public Object addSetValue() {
076        return new Integer(1);
077    }
078
079    // tests
080    // ------------------------------------------------------------------------
081
082    
083    public void testNextHasNextRemove() {
084        int[] elements = getFullElements();
085        Iterator iter = makeFullIterator();
086        for(int i=0;i<elements.length;i++) {
087            assertTrue(iter.hasNext());
088            assertEquals(new Integer(elements[i]),iter.next());
089            if(supportsRemove()) {
090                iter.remove();
091            }
092        }        
093        assertTrue(! iter.hasNext() );
094    }
095
096    public void testEmptyIterator() {
097        assertTrue( ! makeEmptyIterator().hasNext() );
098        try {
099            makeEmptyIterator().next();
100            fail("Expected NoSuchElementException");
101        } catch(NoSuchElementException e) {
102            // expected
103        }
104        if(supportsRemove()) {
105            try {
106                makeEmptyIterator().remove();
107                fail("Expected IllegalStateException");
108            } catch(IllegalStateException e) {
109                // expected
110            }
111        }        
112    }
113
114    public void testRemoveBeforeNext() {
115        if(supportsRemove()) {
116            try {
117                makeFullIterator().remove();
118                fail("Expected IllegalStateException");
119            } catch(IllegalStateException e) {
120                // expected
121            }
122        }        
123    }
124
125    public void testRemoveAfterRemove() {
126        if(supportsRemove()) {
127            Iterator iter = makeFullIterator();
128            iter.next();
129            iter.remove();
130            try {
131                iter.remove();
132                fail("Expected IllegalStateException");
133            } catch(IllegalStateException e) {
134                // expected
135            }
136        }        
137    }
138
139}