KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > primitives > decorators > BaseUnmodifiableIntListTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.commons.collections.primitives.decorators;
18
19 import junit.framework.TestCase;
20
21 import org.apache.commons.collections.primitives.ArrayIntList;
22 import org.apache.commons.collections.primitives.IntIterator;
23 import org.apache.commons.collections.primitives.IntList;
24 import org.apache.commons.collections.primitives.IntListIterator;
25
26 /**
27  * @version $Revision: 480451 $ $Date: 2006-11-28 23:45:08 -0800 (Tue, 28 Nov 2006) $
28  * @author Rodney Waldhoff
29  */

30 public abstract class BaseUnmodifiableIntListTest extends TestCase {
31
32     // conventional
33
// ------------------------------------------------------------------------
34

35     public BaseUnmodifiableIntListTest(String JavaDoc testName) {
36         super(testName);
37     }
38     
39     // framework
40
// ------------------------------------------------------------------------
41

42     protected abstract IntList makeUnmodifiableIntList();
43
44     protected IntList makeIntList() {
45         IntList list = new ArrayIntList();
46         for(int i=0;i<10;i++) {
47             list.add(i);
48         }
49         return list;
50     }
51
52     // tests
53
// ------------------------------------------------------------------------
54

55     public final void testNotModifiable() throws Exception JavaDoc {
56         assertListNotModifiable(makeUnmodifiableIntList());
57     }
58
59     public final void testSublistNotModifiable() throws Exception JavaDoc {
60         IntList list = makeUnmodifiableIntList();
61         assertListNotModifiable(list.subList(0,list.size()-2));
62     }
63     
64     public final void testIteratorNotModifiable() throws Exception JavaDoc {
65         IntList list = makeUnmodifiableIntList();
66         assertIteratorNotModifiable(list.iterator());
67         assertIteratorNotModifiable(list.subList(0,list.size()-2).iterator());
68     }
69     
70     public final void testListIteratorNotModifiable() throws Exception JavaDoc {
71         IntList list = makeUnmodifiableIntList();
72         assertListIteratorNotModifiable(list.listIterator());
73         assertListIteratorNotModifiable(list.subList(0,list.size()-2).listIterator());
74         assertListIteratorNotModifiable(list.listIterator(1));
75         assertListIteratorNotModifiable(list.subList(0,list.size()-2).listIterator(1));
76     }
77
78     // util
79
// ------------------------------------------------------------------------
80

81     private void assertListIteratorNotModifiable(IntListIterator iter) throws Exception JavaDoc {
82         assertIteratorNotModifiable(iter);
83         
84         assertTrue(iter.hasPrevious());
85         
86         try {
87             iter.set(2);
88             fail("Expected UnsupportedOperationException");
89         } catch(UnsupportedOperationException JavaDoc e) {
90             // expected
91
}
92         
93         try {
94             iter.add(2);
95             fail("Expected UnsupportedOperationException");
96         } catch(UnsupportedOperationException JavaDoc e) {
97             // expected
98
}
99     }
100
101     private void assertIteratorNotModifiable(IntIterator iter) throws Exception JavaDoc {
102         assertTrue(iter.hasNext());
103         iter.next();
104         
105         try {
106             iter.remove();
107             fail("Expected UnsupportedOperationException");
108         } catch(UnsupportedOperationException JavaDoc e) {
109             // expected
110
}
111     }
112
113     private void assertListNotModifiable(IntList list) throws Exception JavaDoc {
114         try {
115             list.add(1);
116             fail("Expected UnsupportedOperationException");
117         } catch(UnsupportedOperationException JavaDoc e) {
118             // expected
119
}
120         
121         try {
122             list.add(1,2);
123             fail("Expected UnsupportedOperationException");
124         } catch(UnsupportedOperationException JavaDoc e) {
125             // expected
126
}
127         
128         try {
129             list.addAll(makeIntList());
130             fail("Expected UnsupportedOperationException");
131         } catch(UnsupportedOperationException JavaDoc e) {
132             // expected
133
}
134         
135         try {
136             list.addAll(1,makeIntList());
137             fail("Expected UnsupportedOperationException");
138         } catch(UnsupportedOperationException JavaDoc e) {
139             // expected
140
}
141         
142         try {
143             list.removeElementAt(1);
144             fail("Expected UnsupportedOperationException");
145         } catch(UnsupportedOperationException JavaDoc e) {
146             // expected
147
}
148         
149         try {
150             list.removeElement(1);
151             fail("Expected UnsupportedOperationException");
152         } catch(UnsupportedOperationException JavaDoc e) {
153             // expected
154
}
155         
156         try {
157             list.removeAll(makeIntList());
158             fail("Expected UnsupportedOperationException");
159         } catch(UnsupportedOperationException JavaDoc e) {
160             // expected
161
}
162                 
163         try {
164             list.retainAll(makeIntList());
165             fail("Expected UnsupportedOperationException");
166         } catch(UnsupportedOperationException JavaDoc e) {
167             // expected
168
}
169
170         try {
171             list.clear();
172             fail("Expected UnsupportedOperationException");
173         } catch(UnsupportedOperationException JavaDoc e) {
174             // expected
175
}
176         
177         try {
178             list.set(1,2);
179             fail("Expected UnsupportedOperationException");
180         } catch(UnsupportedOperationException JavaDoc e) {
181             // expected
182
}
183     }
184 }
Popular Tags