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.Collection;
021: import java.util.List;
022: import java.util.ListIterator;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.commons.collections.collection.TestTransformedCollection;
028:
029: /**
030: * Extension of {@link TestList} for exercising the {@link TransformedList}
031: * implementation.
032: *
033: * @since Commons Collections 3.0
034: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
035: *
036: * @author Stephen Colebourne
037: */
038: public class TestTransformedList extends AbstractTestList {
039:
040: public TestTransformedList(String testName) {
041: super (testName);
042: }
043:
044: public static Test suite() {
045: return new TestSuite(TestTransformedList.class);
046: }
047:
048: public static void main(String args[]) {
049: String[] testCaseName = { TestTransformedList.class.getName() };
050: junit.textui.TestRunner.main(testCaseName);
051: }
052:
053: public Collection makeConfirmedCollection() {
054: return new ArrayList();
055: }
056:
057: public Collection makeConfirmedFullCollection() {
058: List list = new ArrayList();
059: list.addAll(Arrays.asList(getFullElements()));
060: return list;
061: }
062:
063: public List makeEmptyList() {
064: return TransformedList.decorate(new ArrayList(),
065: TestTransformedCollection.NOOP_TRANSFORMER);
066: }
067:
068: public List makeFullList() {
069: List list = new ArrayList();
070: list.addAll(Arrays.asList(getFullElements()));
071: return TransformedList.decorate(list,
072: TestTransformedCollection.NOOP_TRANSFORMER);
073: }
074:
075: public void testTransformedList() {
076: List list = TransformedList
077: .decorate(
078: new ArrayList(),
079: TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
080: assertEquals(0, list.size());
081: Object[] els = new Object[] { "1", "3", "5", "7", "2", "4", "6" };
082: for (int i = 0; i < els.length; i++) {
083: list.add(els[i]);
084: assertEquals(i + 1, list.size());
085: assertEquals(true, list.contains(new Integer(
086: (String) els[i])));
087: assertEquals(false, list.contains(els[i]));
088: }
089:
090: assertEquals(false, list.remove(els[0]));
091: assertEquals(true, list.remove(new Integer((String) els[0])));
092:
093: list.clear();
094: for (int i = 0; i < els.length; i++) {
095: list.add(0, els[i]);
096: assertEquals(i + 1, list.size());
097: assertEquals(new Integer((String) els[i]), list.get(0));
098: }
099:
100: list.set(0, "22");
101: assertEquals(new Integer(22), list.get(0));
102:
103: ListIterator it = list.listIterator();
104: it.next();
105: it.set("33");
106: assertEquals(new Integer(33), list.get(0));
107: it.add("44");
108: assertEquals(new Integer(44), list.get(1));
109:
110: List adds = new ArrayList();
111: adds.add("1");
112: adds.add("2");
113: list.clear();
114: list.addAll(adds);
115: assertEquals(new Integer(1), list.get(0));
116: assertEquals(new Integer(2), list.get(1));
117:
118: adds.clear();
119: adds.add("3");
120: list.addAll(1, adds);
121: assertEquals(new Integer(1), list.get(0));
122: assertEquals(new Integer(3), list.get(1));
123: assertEquals(new Integer(2), list.get(2));
124: }
125:
126: public String getCompatibilityVersion() {
127: return "3.1";
128: }
129:
130: // public void testCreate() throws Exception {
131: // resetEmpty();
132: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedList.emptyCollection.version3.1.obj");
133: // resetFull();
134: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedList.fullCollection.version3.1.obj");
135: // }
136:
137: }
|