KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > TestTransformedList


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.list;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.collections.collection.TestTransformedCollection;
28
29 /**
30  * Extension of {@link TestList} for exercising the {@link TransformedList}
31  * implementation.
32  *
33  * @since Commons Collections 3.0
34  * @version $Revision: 1.6 $ $Date: 2004/06/02 22:07:53 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TestTransformedList extends AbstractTestList {
39     
40     public TestTransformedList(String JavaDoc testName) {
41         super(testName);
42     }
43
44     public static Test suite() {
45         return new TestSuite(TestTransformedList.class);
46     }
47
48     public static void main(String JavaDoc args[]) {
49         String JavaDoc[] testCaseName = { TestTransformedList.class.getName()};
50         junit.textui.TestRunner.main(testCaseName);
51     }
52
53     public Collection JavaDoc makeConfirmedCollection() {
54         return new ArrayList JavaDoc();
55     }
56
57     public Collection JavaDoc makeConfirmedFullCollection() {
58         List JavaDoc list = new ArrayList JavaDoc();
59         list.addAll(Arrays.asList(getFullElements()));
60         return list;
61     }
62     
63     public List JavaDoc makeEmptyList() {
64         return TransformedList.decorate(new ArrayList JavaDoc(), TestTransformedCollection.NOOP_TRANSFORMER);
65     }
66
67     public List JavaDoc makeFullList() {
68         List JavaDoc list = new ArrayList JavaDoc();
69         list.addAll(Arrays.asList(getFullElements()));
70         return TransformedList.decorate(list, TestTransformedCollection.NOOP_TRANSFORMER);
71     }
72     
73     public void testTransformedList() {
74         List JavaDoc list = TransformedList.decorate(new ArrayList JavaDoc(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
75         assertEquals(0, list.size());
76         Object JavaDoc[] els = new Object JavaDoc[] {"1", "3", "5", "7", "2", "4", "6"};
77         for (int i = 0; i < els.length; i++) {
78             list.add(els[i]);
79             assertEquals(i + 1, list.size());
80             assertEquals(true, list.contains(new Integer JavaDoc((String JavaDoc) els[i])));
81             assertEquals(false, list.contains(els[i]));
82         }
83         
84         assertEquals(false, list.remove(els[0]));
85         assertEquals(true, list.remove(new Integer JavaDoc((String JavaDoc) els[0])));
86         
87         list.clear();
88         for (int i = 0; i < els.length; i++) {
89             list.add(0, els[i]);
90             assertEquals(i + 1, list.size());
91             assertEquals(new Integer JavaDoc((String JavaDoc) els[i]), list.get(0));
92         }
93         
94         list.set(0, "22");
95         assertEquals(new Integer JavaDoc(22), list.get(0));
96         
97         ListIterator JavaDoc it = list.listIterator();
98         it.next();
99         it.set("33");
100         assertEquals(new Integer JavaDoc(33), list.get(0));
101         it.add("44");
102         assertEquals(new Integer JavaDoc(44), list.get(1));
103         
104         List JavaDoc adds = new ArrayList JavaDoc();
105         adds.add("1");
106         adds.add("2");
107         list.clear();
108         list.addAll(adds);
109         assertEquals(new Integer JavaDoc(1), list.get(0));
110         assertEquals(new Integer JavaDoc(2), list.get(1));
111         
112         adds.clear();
113         adds.add("3");
114         list.addAll(1, adds);
115         assertEquals(new Integer JavaDoc(1), list.get(0));
116         assertEquals(new Integer JavaDoc(3), list.get(1));
117         assertEquals(new Integer JavaDoc(2), list.get(2));
118     }
119
120     public String JavaDoc getCompatibilityVersion() {
121         return "3.1";
122     }
123
124 // public void testCreate() throws Exception {
125
// resetEmpty();
126
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedList.emptyCollection.version3.1.obj");
127
// resetFull();
128
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedList.fullCollection.version3.1.obj");
129
// }
130

131 }
132
Popular Tags