KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > TestUnmodifiableList


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

16 package org.apache.commons.collections.list;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 /**
27  * Extension of {@link AbstractTestList} for exercising the
28  * {@link UnmodifiableList} implementation.
29  *
30  * @since Commons Collections 3.0
31  * @version $Revision: 1.7 $ $Date: 2004/06/02 22:07:53 $
32  *
33  * @author Phil Steitz
34  */

35 public class TestUnmodifiableList extends AbstractTestList {
36     
37     public TestUnmodifiableList(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     public static Test suite() {
42         return new TestSuite(TestUnmodifiableList.class);
43     }
44     
45     public static void main(String JavaDoc args[]) {
46         String JavaDoc[] testCaseName = { TestUnmodifiableList.class.getName()};
47         junit.textui.TestRunner.main(testCaseName);
48     }
49
50     //-----------------------------------------------------------------------
51
public List JavaDoc makeEmptyList() {
52         return UnmodifiableList.decorate(new ArrayList JavaDoc());
53     }
54     
55     public List JavaDoc makeFullList() {
56         ArrayList JavaDoc list = new ArrayList JavaDoc();
57         list.addAll(Arrays.asList(getFullElements()));
58         return UnmodifiableList.decorate(list);
59     }
60     
61     public boolean isSetSupported() {
62         return false;
63     }
64     
65     public boolean isAddSupported() {
66         return false;
67     }
68     
69     public boolean isRemoveSupported() {
70         return false;
71     }
72     
73     //-----------------------------------------------------------------------
74
protected UnmodifiableList list = null;
75     protected ArrayList JavaDoc array = null;
76     
77     protected void setupList() {
78         list = (UnmodifiableList) makeFullList();
79         array = new ArrayList JavaDoc();
80         array.add(new Integer JavaDoc(1));
81     }
82     
83     /**
84      * Verify that base list and sublists are not modifiable
85      */

86     public void testUnmodifiable() {
87         setupList();
88         verifyUnmodifiable(list);
89         verifyUnmodifiable(list.subList(0, 2));
90     }
91         
92     protected void verifyUnmodifiable(List JavaDoc list) {
93         try {
94             list.add(0, new Integer JavaDoc(0));
95             fail("Expecting UnsupportedOperationException.");
96         } catch (UnsupportedOperationException JavaDoc e) {
97             // expected
98
}
99         try {
100             list.add(new Integer JavaDoc(0));
101              fail("Expecting UnsupportedOperationException.");
102         } catch (UnsupportedOperationException JavaDoc e) {
103             // expected
104
}
105         try {
106             list.addAll(0, array);
107              fail("Expecting UnsupportedOperationException.");
108         } catch (UnsupportedOperationException JavaDoc e) {
109             // expected
110
}
111         try {
112             list.addAll(array);
113              fail("Expecting UnsupportedOperationException.");
114         } catch (UnsupportedOperationException JavaDoc e) {
115             // expected
116
}
117         try {
118             list.clear();
119              fail("Expecting UnsupportedOperationException.");
120         } catch (UnsupportedOperationException JavaDoc e) {
121             // expected
122
}
123         try {
124             list.remove(0);
125              fail("Expecting UnsupportedOperationException.");
126         } catch (UnsupportedOperationException JavaDoc e) {
127             // expected
128
}
129         try {
130             list.remove(new Integer JavaDoc(0));
131              fail("Expecting UnsupportedOperationException.");
132         } catch (UnsupportedOperationException JavaDoc e) {
133             // expected
134
}
135         try {
136             list.removeAll(array);
137              fail("Expecting UnsupportedOperationException.");
138         } catch (UnsupportedOperationException JavaDoc e) {
139             // expected
140
}
141         try {
142             list.retainAll(array);
143              fail("Expecting UnsupportedOperationException.");
144         } catch (UnsupportedOperationException JavaDoc e) {
145             // expected
146
}
147         try {
148             list.set(0, new Integer JavaDoc(0));
149              fail("Expecting UnsupportedOperationException.");
150         } catch (UnsupportedOperationException JavaDoc e) {
151             // expected
152
}
153     }
154     
155     /**
156      * Verify that iterator is not modifiable
157      */

158     public void testUnmodifiableIterator() {
159         setupList();
160         Iterator JavaDoc iterator = list.iterator();
161         try {
162             Object JavaDoc obj = iterator.next();
163             iterator.remove();
164             fail("Expecting UnsupportedOperationException.");
165         } catch (UnsupportedOperationException JavaDoc e) {
166             // expected
167
}
168     }
169
170     public String JavaDoc getCompatibilityVersion() {
171         return "3.1";
172     }
173
174 // public void testCreate() throws Exception {
175
// resetEmpty();
176
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/UnmodifiableList.emptyCollection.version3.1.obj");
177
// resetFull();
178
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/UnmodifiableList.fullCollection.version3.1.obj");
179
// }
180

181 }
182
Popular Tags