001: /*
002: * Copyright 2001-2005 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;
017:
018: import java.util.ArrayList;
019: import java.util.ConcurrentModificationException;
020: import java.util.List;
021: import java.util.ListIterator;
022:
023: import junit.framework.Test;
024:
025: /**
026: * Test FastArrayList.
027: *
028: * @version $Revision: 171360 $ $Date: 2005-05-22 20:23:04 +0100 (Sun, 22 May 2005) $
029: *
030: * @author Jason van Zyl
031: */
032: public class TestFastArrayList extends TestArrayList {
033:
034: public TestFastArrayList(String testName) {
035: super (testName);
036: }
037:
038: public static Test suite() {
039: return BulkTest.makeSuite(TestFastArrayList.class);
040: }
041:
042: public static void main(String args[]) {
043: String[] testCaseName = { TestFastArrayList.class.getName() };
044: junit.textui.TestRunner.main(testCaseName);
045: }
046:
047: public void setUp() {
048: list = (ArrayList) makeEmptyList();
049: }
050:
051: public List makeEmptyList() {
052: FastArrayList fal = new FastArrayList();
053: fal.setFast(false);
054: return (fal);
055: }
056:
057: public void testConcurrentModification_alwaysFast() {
058: FastArrayList list = new FastArrayList();
059: list.setFast(true);
060: list.add("a");
061: list.add("b");
062: list.add("c");
063: ListIterator iter = list.listIterator();
064: assertEquals("a", iter.next());
065: assertEquals("b", iter.next());
066: iter.remove(); // checking for no ConcurrentModificationException
067: assertEquals("c", iter.next());
068: assertEquals(false, iter.hasNext());
069: assertEquals("c", iter.previous());
070: assertEquals("a", iter.previous());
071: assertEquals(false, iter.hasPrevious());
072: }
073:
074: public void testConcurrentModification_alwaysFastModError() {
075: FastArrayList list = new FastArrayList();
076: list.setFast(true);
077: list.add("a");
078: list.add("b");
079: list.add("c");
080: ListIterator iter = list.listIterator();
081: assertEquals("a", iter.next());
082: assertEquals("b", iter.next());
083: list.remove(1);
084: try {
085: iter.remove();
086: } catch (ConcurrentModificationException ex) {
087: // expected
088: }
089: // iterator state now invalid
090: }
091:
092: public void testConcurrentModification_delayedFast() {
093: FastArrayList list = new FastArrayList();
094: list.add("a");
095: list.add("b");
096: list.add("c");
097: ListIterator iter = list.listIterator();
098: assertEquals("a", iter.next());
099: assertEquals("b", iter.next());
100: list.setFast(true);
101: iter.remove(); // checking for no ConcurrentModificationException
102: assertEquals("c", iter.next());
103: assertEquals(false, iter.hasNext());
104: assertEquals("c", iter.previous());
105: assertEquals("a", iter.previous());
106: assertEquals(false, iter.hasPrevious());
107: }
108:
109: public void testConcurrentModification_delayedFastModError() {
110: FastArrayList list = new FastArrayList();
111: list.add("a");
112: list.add("b");
113: list.add("c");
114: ListIterator iter = list.listIterator();
115: assertEquals("a", iter.next());
116: assertEquals("b", iter.next());
117: list.setFast(true);
118: list.remove(1);
119: try {
120: iter.remove();
121: } catch (ConcurrentModificationException ex) {
122: // expected
123: }
124: // iterator state now invalid
125: }
126:
127: public void testConcurrentModification_alwaysFastPrevious() {
128: FastArrayList list = new FastArrayList();
129: list.setFast(true);
130: list.add("a");
131: list.add("b");
132: list.add("c");
133: ListIterator iter = list.listIterator();
134: assertEquals("a", iter.next());
135: assertEquals("b", iter.next());
136: assertEquals("b", iter.previous());
137: iter.remove(); // checking for no ConcurrentModificationException
138: assertEquals("c", iter.next());
139: assertEquals(false, iter.hasNext());
140: assertEquals("c", iter.previous());
141: assertEquals("a", iter.previous());
142: assertEquals(false, iter.hasPrevious());
143: }
144:
145: public void testConcurrentModification_alwaysFastModErrorPrevious() {
146: FastArrayList list = new FastArrayList();
147: list.setFast(true);
148: list.add("a");
149: list.add("b");
150: list.add("c");
151: ListIterator iter = list.listIterator();
152: assertEquals("a", iter.next());
153: assertEquals("b", iter.next());
154: assertEquals("b", iter.previous());
155: list.remove(1);
156: try {
157: iter.remove();
158: } catch (ConcurrentModificationException ex) {
159: // expected
160: }
161: // iterator state now invalid
162: }
163:
164: }
|