001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.list;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Extension of {@link AbstractTestList} for exercising the
028: * {@link UnmodifiableList} implementation.
029: *
030: * @since Commons Collections 3.0
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Phil Steitz
034: */
035: public class TestUnmodifiableList extends AbstractTestList {
036:
037: public TestUnmodifiableList(String testName) {
038: super (testName);
039: }
040:
041: public static Test suite() {
042: return new TestSuite(TestUnmodifiableList.class);
043: }
044:
045: public static void main(String args[]) {
046: String[] testCaseName = { TestUnmodifiableList.class.getName() };
047: junit.textui.TestRunner.main(testCaseName);
048: }
049:
050: //-----------------------------------------------------------------------
051: public List makeEmptyList() {
052: return UnmodifiableList.decorate(new ArrayList());
053: }
054:
055: public List makeFullList() {
056: ArrayList list = new ArrayList();
057: list.addAll(Arrays.asList(getFullElements()));
058: return UnmodifiableList.decorate(list);
059: }
060:
061: public boolean isSetSupported() {
062: return false;
063: }
064:
065: public boolean isAddSupported() {
066: return false;
067: }
068:
069: public boolean isRemoveSupported() {
070: return false;
071: }
072:
073: //-----------------------------------------------------------------------
074: protected UnmodifiableList list = null;
075: protected ArrayList array = null;
076:
077: protected void setupList() {
078: list = (UnmodifiableList) makeFullList();
079: array = new ArrayList();
080: array.add(new Integer(1));
081: }
082:
083: /**
084: * Verify that base list and sublists are not modifiable
085: */
086: public void testUnmodifiable() {
087: setupList();
088: verifyUnmodifiable(list);
089: verifyUnmodifiable(list.subList(0, 2));
090: }
091:
092: protected void verifyUnmodifiable(List list) {
093: try {
094: list.add(0, new Integer(0));
095: fail("Expecting UnsupportedOperationException.");
096: } catch (UnsupportedOperationException e) {
097: // expected
098: }
099: try {
100: list.add(new Integer(0));
101: fail("Expecting UnsupportedOperationException.");
102: } catch (UnsupportedOperationException e) {
103: // expected
104: }
105: try {
106: list.addAll(0, array);
107: fail("Expecting UnsupportedOperationException.");
108: } catch (UnsupportedOperationException e) {
109: // expected
110: }
111: try {
112: list.addAll(array);
113: fail("Expecting UnsupportedOperationException.");
114: } catch (UnsupportedOperationException e) {
115: // expected
116: }
117: try {
118: list.clear();
119: fail("Expecting UnsupportedOperationException.");
120: } catch (UnsupportedOperationException e) {
121: // expected
122: }
123: try {
124: list.remove(0);
125: fail("Expecting UnsupportedOperationException.");
126: } catch (UnsupportedOperationException e) {
127: // expected
128: }
129: try {
130: list.remove(new Integer(0));
131: fail("Expecting UnsupportedOperationException.");
132: } catch (UnsupportedOperationException e) {
133: // expected
134: }
135: try {
136: list.removeAll(array);
137: fail("Expecting UnsupportedOperationException.");
138: } catch (UnsupportedOperationException e) {
139: // expected
140: }
141: try {
142: list.retainAll(array);
143: fail("Expecting UnsupportedOperationException.");
144: } catch (UnsupportedOperationException e) {
145: // expected
146: }
147: try {
148: list.set(0, new Integer(0));
149: fail("Expecting UnsupportedOperationException.");
150: } catch (UnsupportedOperationException e) {
151: // expected
152: }
153: }
154:
155: /**
156: * Verify that iterator is not modifiable
157: */
158: public void testUnmodifiableIterator() {
159: setupList();
160: Iterator iterator = list.iterator();
161: try {
162: Object obj = iterator.next();
163: iterator.remove();
164: fail("Expecting UnsupportedOperationException.");
165: } catch (UnsupportedOperationException e) {
166: // expected
167: }
168: }
169:
170: public String 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: }
|