001: /*
002: * Copyright 2001-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;
017:
018: import java.util.ArrayList;
019: import java.util.ConcurrentModificationException;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.ListIterator;
023:
024: import junit.framework.Test;
025:
026: /**
027: * Test FastArrayList implementation in <strong>fast</strong> mode.
028: *
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Jason van Zyl
032: */
033: public class TestFastArrayList1 extends TestFastArrayList {
034:
035: public TestFastArrayList1(String testName) {
036: super (testName);
037: }
038:
039: public static Test suite() {
040: return BulkTest.makeSuite(TestFastArrayList1.class);
041: }
042:
043: public static void main(String args[]) {
044: String[] testCaseName = { TestFastArrayList1.class.getName() };
045: junit.textui.TestRunner.main(testCaseName);
046: }
047:
048: public void setUp() {
049: list = (ArrayList) makeEmptyList();
050: }
051:
052: public List makeEmptyList() {
053: FastArrayList fal = new FastArrayList();
054: fal.setFast(true);
055: return (fal);
056: }
057:
058: public void testIterateModify1() {
059: List list = makeEmptyList();
060: list.add("A");
061: list.add("B");
062: list.add("C");
063: assertEquals(3, list.size());
064:
065: Iterator it = list.iterator();
066: assertEquals("A", it.next());
067: assertEquals(3, list.size());
068: list.add(1, "Z");
069: assertEquals(4, list.size());
070: assertEquals("B", it.next());
071: assertEquals("C", it.next());
072: assertEquals(false, it.hasNext());
073: }
074:
075: public void testIterateModify2() {
076: List list = makeEmptyList();
077: list.add("A");
078: list.add("B");
079: list.add("C");
080: assertEquals(3, list.size());
081:
082: ListIterator it = list.listIterator();
083: assertEquals("A", it.next());
084: it.add("M"); // change via Iterator interface
085: assertEquals(4, list.size());
086: list.add(2, "Z"); // change via List interface
087: assertEquals(5, list.size());
088: assertEquals("B", it.next());
089: try {
090: it.set("N"); // fails as previously changed via List interface
091: fail();
092: } catch (ConcurrentModificationException ex) {
093: }
094: try {
095: it.remove();
096: fail();
097: } catch (ConcurrentModificationException ex) {
098: }
099: try {
100: it.add("N");
101: fail();
102: } catch (ConcurrentModificationException ex) {
103: }
104: assertEquals("C", it.next());
105: assertEquals(false, it.hasNext());
106: }
107:
108: }
|