001: /*
002: * Copyright 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.list;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.List;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Extension of {@link TestList} for exercising the {@link GrowthList}.
028: *
029: * @since Commons Collections 3.2
030: * @version $Revision: 155406 $ $Date: 2005-05-15 19:30:49 +0100 (Sun, 15 May 2005) $
031: *
032: * @author Stephen Colebourne
033: */
034: public class TestGrowthList extends AbstractTestList {
035:
036: public TestGrowthList(String testName) {
037: super (testName);
038: }
039:
040: public static Test suite() {
041: return new TestSuite(TestGrowthList.class);
042: }
043:
044: public static void main(String args[]) {
045: String[] testCaseName = { TestGrowthList.class.getName() };
046: junit.textui.TestRunner.main(testCaseName);
047: }
048:
049: public List makeEmptyList() {
050: return new GrowthList();
051: }
052:
053: public List makeFullList() {
054: List list = new ArrayList();
055: list.addAll(Arrays.asList(getFullElements()));
056: return GrowthList.decorate(list);
057: }
058:
059: //-----------------------------------------------------------------------
060: public void testGrowthAdd() {
061: Integer one = new Integer(1);
062: GrowthList grower = new GrowthList();
063: assertEquals(0, grower.size());
064: grower.add(1, one);
065: assertEquals(2, grower.size());
066: assertEquals(null, grower.get(0));
067: assertEquals(one, grower.get(1));
068: }
069:
070: public void testGrowthAddAll() {
071: Integer one = new Integer(1);
072: Integer two = new Integer(2);
073: Collection coll = new ArrayList();
074: coll.add(one);
075: coll.add(two);
076: GrowthList grower = new GrowthList();
077: assertEquals(0, grower.size());
078: grower.addAll(1, coll);
079: assertEquals(3, grower.size());
080: assertEquals(null, grower.get(0));
081: assertEquals(one, grower.get(1));
082: assertEquals(two, grower.get(2));
083: }
084:
085: public void testGrowthSet1() {
086: Integer one = new Integer(1);
087: GrowthList grower = new GrowthList();
088: assertEquals(0, grower.size());
089: grower.set(1, one);
090: assertEquals(2, grower.size());
091: assertEquals(null, grower.get(0));
092: assertEquals(one, grower.get(1));
093: }
094:
095: public void testGrowthSet2() {
096: Integer one = new Integer(1);
097: GrowthList grower = new GrowthList();
098: assertEquals(0, grower.size());
099: grower.set(0, one);
100: assertEquals(1, grower.size());
101: assertEquals(one, grower.get(0));
102: }
103:
104: //-----------------------------------------------------------------------
105: /**
106: * Override.
107: */
108: public void testListAddByIndexBoundsChecking() {
109: List list;
110: Object element = getOtherElements()[0];
111: try {
112: list = makeEmptyList();
113: list.add(-1, element);
114: fail("List.add should throw IndexOutOfBoundsException [-1]");
115: } catch (IndexOutOfBoundsException e) {
116: // expected
117: }
118: }
119:
120: /**
121: * Override.
122: */
123: public void testListAddByIndexBoundsChecking2() {
124: List list;
125: Object element = getOtherElements()[0];
126: try {
127: list = makeFullList();
128: list.add(-1, element);
129: fail("List.add should throw IndexOutOfBoundsException [-1]");
130: } catch (IndexOutOfBoundsException e) {
131: // expected
132: }
133: }
134:
135: /**
136: * Override.
137: */
138: public void testListSetByIndexBoundsChecking() {
139: List list = makeEmptyList();
140: Object element = getOtherElements()[0];
141: try {
142: list.set(-1, element);
143: fail("List.set should throw IndexOutOfBoundsException [-1]");
144: } catch (IndexOutOfBoundsException e) {
145: // expected
146: }
147: }
148:
149: /**
150: * Override.
151: */
152: public void testListSetByIndexBoundsChecking2() {
153: List list = makeFullList();
154: Object element = getOtherElements()[0];
155: try {
156: list.set(-1, element);
157: fail("List.set should throw IndexOutOfBoundsException [-1]");
158: } catch (IndexOutOfBoundsException e) {
159: // expected
160: }
161: }
162:
163: //-----------------------------------------------------------------------
164: public String getCompatibilityVersion() {
165: return "3.2";
166: }
167:
168: // public void testCreate() throws Exception {
169: // resetEmpty();
170: // writeExternalFormToDisk((java.io.Serializable) collection, "C:/commons/collections/data/test/GrowthList.emptyCollection.version3.2.obj");
171: // resetFull();
172: // writeExternalFormToDisk((java.io.Serializable) collection, "C:/commons/collections/data/test/GrowthList.fullCollection.version3.2.obj");
173: // }
174:
175: }
|