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 junit.framework.TestCase;
020
021import org.apache.commons.collections.primitives.ArrayShortList;
022import org.apache.commons.collections.primitives.ShortIterator;
023import org.apache.commons.collections.primitives.ShortList;
024import org.apache.commons.collections.primitives.ShortListIterator;
025
026/**
027 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
028 * @author Rodney Waldhoff
029 */
030public abstract class BaseUnmodifiableShortListTest extends TestCase {
031
032    // conventional
033    // ------------------------------------------------------------------------
034
035    public BaseUnmodifiableShortListTest(String testName) {
036        super(testName);
037    }
038    
039    // framework
040    // ------------------------------------------------------------------------
041
042    protected abstract ShortList makeUnmodifiableShortList();
043
044    protected ShortList makeShortList() {
045        ShortList list = new ArrayShortList();
046        for(short i=0;i<10;i++) {
047            list.add(i);
048        }
049        return list;
050    }
051
052    // tests
053    // ------------------------------------------------------------------------
054    
055    public final void testNotModifiable() throws Exception {
056        assertListNotModifiable(makeUnmodifiableShortList());
057    }
058
059    public final void testSublistNotModifiable() throws Exception {
060        ShortList list = makeUnmodifiableShortList();
061        assertListNotModifiable(list.subList(0,list.size()-2));
062    }
063    
064    public final void testIteratorNotModifiable() throws Exception {
065        ShortList list = makeUnmodifiableShortList();
066        assertIteratorNotModifiable(list.iterator());
067        assertIteratorNotModifiable(list.subList(0,list.size()-2).iterator());
068    }
069    
070    public final void testListIteratorNotModifiable() throws Exception {
071        ShortList list = makeUnmodifiableShortList();
072        assertListIteratorNotModifiable(list.listIterator());
073        assertListIteratorNotModifiable(list.subList(0,list.size()-2).listIterator());
074        assertListIteratorNotModifiable(list.listIterator(1));
075        assertListIteratorNotModifiable(list.subList(0,list.size()-2).listIterator(1));
076    }
077
078    // util
079    // ------------------------------------------------------------------------
080    
081    private void assertListIteratorNotModifiable(ShortListIterator iter) throws Exception {
082        assertIteratorNotModifiable(iter);
083        
084        assertTrue(iter.hasPrevious());
085        
086        try {
087            iter.set((short)2);
088            fail("Expected UnsupportedOperationException");
089        } catch(UnsupportedOperationException e) {
090            // expected
091        }
092        
093        try {
094            iter.add((short)2);
095            fail("Expected UnsupportedOperationException");
096        } catch(UnsupportedOperationException e) {
097            // expected
098        }
099    }
100
101    private void assertIteratorNotModifiable(ShortIterator iter) throws Exception {
102        assertTrue(iter.hasNext());
103        iter.next();
104        
105        try {
106            iter.remove();
107            fail("Expected UnsupportedOperationException");
108        } catch(UnsupportedOperationException e) {
109            // expected
110        }
111    }
112
113    private void assertListNotModifiable(ShortList list) throws Exception {
114        try {
115            list.add((short)1);
116            fail("Expected UnsupportedOperationException");
117        } catch(UnsupportedOperationException e) {
118            // expected
119        }
120        
121        try {
122            list.add(1,(short)2);
123            fail("Expected UnsupportedOperationException");
124        } catch(UnsupportedOperationException e) {
125            // expected
126        }
127        
128        try {
129            list.addAll(makeShortList());
130            fail("Expected UnsupportedOperationException");
131        } catch(UnsupportedOperationException e) {
132            // expected
133        }
134        
135        try {
136            list.addAll(1,makeShortList());
137            fail("Expected UnsupportedOperationException");
138        } catch(UnsupportedOperationException e) {
139            // expected
140        }
141        
142        try {
143            list.removeElementAt(1);
144            fail("Expected UnsupportedOperationException");
145        } catch(UnsupportedOperationException e) {
146            // expected
147        }
148        
149        try {
150            list.removeElement((short)1);
151            fail("Expected UnsupportedOperationException");
152        } catch(UnsupportedOperationException e) {
153            // expected
154        }
155        
156        try {
157            list.removeAll(makeShortList());
158            fail("Expected UnsupportedOperationException");
159        } catch(UnsupportedOperationException e) {
160            // expected
161        }
162                
163        try {
164            list.retainAll(makeShortList());
165            fail("Expected UnsupportedOperationException");
166        } catch(UnsupportedOperationException e) {
167            // expected
168        }
169
170        try {
171            list.clear();
172            fail("Expected UnsupportedOperationException");
173        } catch(UnsupportedOperationException e) {
174            // expected
175        }
176        
177        try {
178            list.set(1,(short)2);
179            fail("Expected UnsupportedOperationException");
180        } catch(UnsupportedOperationException e) {
181            // expected
182        }
183    }
184}