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.decorators;
018
019import org.apache.commons.collections.primitives.ArrayCharList;
020import org.apache.commons.collections.primitives.CharIterator;
021import org.apache.commons.collections.primitives.CharList;
022import org.apache.commons.collections.primitives.CharListIterator;
023
024/**
025 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
026 * @author Rodney Waldhoff
027 */
028public abstract class BaseUnmodifiableCharListIteratorTest extends BaseUnmodifiableCharIteratorTest {
029
030    // conventional
031    // ------------------------------------------------------------------------
032
033    public BaseUnmodifiableCharListIteratorTest(String testName) {
034        super(testName);
035    }
036    
037
038    // framework
039    // ------------------------------------------------------------------------
040
041    protected abstract CharListIterator makeUnmodifiableCharListIterator();
042
043    protected CharIterator makeUnmodifiableCharIterator() {
044        return makeUnmodifiableCharListIterator();
045    }
046
047    protected CharIterator makeCharIterator() {
048        return makeCharListIterator();
049    }
050    
051    protected CharListIterator makeCharListIterator() {
052        CharList list = new ArrayCharList();
053        for(char i=0;i<10;i++) {
054            list.add(i);
055        }
056        return list.listIterator();
057    }
058
059    // tests
060    // ------------------------------------------------------------------------
061
062    public final void testCharListIteratorNotModifiable() {
063        CharListIterator iter = makeUnmodifiableCharListIterator();
064        assertTrue(iter.hasNext());
065        iter.next();
066        try {
067            iter.remove();
068            fail("Expected UnsupportedOperationException");
069        } catch(UnsupportedOperationException e) {
070            // expected
071        }
072        try {
073            iter.add((char)1);
074            fail("Expected UnsupportedOperationException");
075        } catch(UnsupportedOperationException e) {
076            // expected
077        }
078        try {
079            iter.set((char)3);
080            fail("Expected UnsupportedOperationException");
081        } catch(UnsupportedOperationException e) {
082            // expected
083        }
084    }
085
086    public final void testIterateCharListIterator() {        
087        CharListIterator iter = makeUnmodifiableCharListIterator();
088        CharListIterator expected = makeCharListIterator();
089        
090        assertTrue(! iter.hasPrevious());
091        
092        while( expected.hasNext() ) {
093            assertTrue(iter.hasNext());
094            assertEquals(expected.nextIndex(),iter.nextIndex());
095            assertEquals(expected.previousIndex(),iter.previousIndex());
096            assertEquals(expected.next(),iter.next());
097            assertTrue(iter.hasPrevious());
098            assertEquals(expected.nextIndex(),iter.nextIndex());
099            assertEquals(expected.previousIndex(),iter.previousIndex());
100        }
101
102        assertTrue(! iter.hasNext() );
103
104        while( expected.hasPrevious() ) {
105            assertTrue(iter.hasPrevious());
106            assertEquals(expected.nextIndex(),iter.nextIndex());
107            assertEquals(expected.previousIndex(),iter.previousIndex());
108            assertEquals(expected.previous(),iter.previous());
109            assertTrue(iter.hasNext());
110            assertEquals(expected.nextIndex(),iter.nextIndex());
111            assertEquals(expected.previousIndex(),iter.previousIndex());
112        }
113        assertTrue(! iter.hasPrevious() );
114    }
115
116}