KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > collection > TestTransformedCollection


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.collection;
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
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.collections.Transformer;
27 import org.apache.commons.collections.TransformerUtils;
28
29 /**
30  * Extension of {@link TestCollection} for exercising the {@link TransformedCollection}
31  * implementation.
32  *
33  * @since Commons Collections 3.0
34  * @version $Revision: 1.7 $ $Date: 2004/06/02 22:06:33 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TestTransformedCollection extends AbstractTestCollection {
39     
40     private static class StringToInteger implements Transformer {
41         public Object JavaDoc transform(Object JavaDoc input) {
42             return new Integer JavaDoc((String JavaDoc) input);
43         }
44     }
45     
46     public static final Transformer NOOP_TRANSFORMER = TransformerUtils.nopTransformer();
47     public static final Transformer STRING_TO_INTEGER_TRANSFORMER = new StringToInteger();
48
49     public TestTransformedCollection(String JavaDoc testName) {
50         super(testName);
51     }
52
53     public static Test suite() {
54         return new TestSuite(TestTransformedCollection.class);
55     }
56
57     public static void main(String JavaDoc args[]) {
58         String JavaDoc[] testCaseName = { TestTransformedCollection.class.getName()};
59         junit.textui.TestRunner.main(testCaseName);
60     }
61
62     //-----------------------------------------------------------------------
63
public Collection JavaDoc makeConfirmedCollection() {
64         return new ArrayList JavaDoc();
65     }
66
67     public Collection JavaDoc makeConfirmedFullCollection() {
68         List JavaDoc list = new ArrayList JavaDoc();
69         list.addAll(Arrays.asList(getFullElements()));
70         return list;
71     }
72     
73     public Collection JavaDoc makeCollection() {
74         return TransformedCollection.decorate(new ArrayList JavaDoc(), NOOP_TRANSFORMER);
75     }
76
77     public Collection JavaDoc makeFullCollection() {
78         List JavaDoc list = new ArrayList JavaDoc();
79         list.addAll(Arrays.asList(getFullElements()));
80         return TransformedCollection.decorate(list, NOOP_TRANSFORMER);
81     }
82     
83     //-----------------------------------------------------------------------
84
public Object JavaDoc[] getFullElements() {
85         return new Object JavaDoc[] {"1", "3", "5", "7", "2", "4", "6"};
86     }
87
88     public Object JavaDoc[] getOtherElements() {
89         return new Object JavaDoc[] {"9", "88", "678", "87", "98", "78", "99"};
90     }
91
92     //-----------------------------------------------------------------------
93
public void testTransformedCollection() {
94         Collection JavaDoc coll = TransformedCollection.decorate(new ArrayList JavaDoc(), STRING_TO_INTEGER_TRANSFORMER);
95         assertEquals(0, coll.size());
96         Object JavaDoc[] els = getFullElements();
97         for (int i = 0; i < els.length; i++) {
98             coll.add(els[i]);
99             assertEquals(i + 1, coll.size());
100             assertEquals(true, coll.contains(new Integer JavaDoc((String JavaDoc) els[i])));
101             assertEquals(false, coll.contains(els[i]));
102         }
103         
104         assertEquals(true, coll.remove(new Integer JavaDoc((String JavaDoc) els[0])));
105     }
106
107     public String JavaDoc getCompatibilityVersion() {
108         return "3.1";
109     }
110
111 // public void testCreate() throws Exception {
112
// resetEmpty();
113
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedCollection.emptyCollection.version3.1.obj");
114
// resetFull();
115
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/TransformedCollection.fullCollection.version3.1.obj");
116
// }
117

118 }
119
Popular Tags