KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import org.apache.commons.collections.Predicate;
25 import org.apache.commons.collections.PredicateUtils;
26
27 /**
28  * Extension of {@link TestList} for exercising the
29  * {@link PredicatedList} implementation.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.7 $ $Date: 2004/06/02 22:07:53 $
33  *
34  * @author Phil Steitz
35  */

36 public class TestPredicatedList extends AbstractTestList {
37     
38     public TestPredicatedList(String JavaDoc testName) {
39         super(testName);
40     }
41     
42     public static Test suite() {
43         return new TestSuite(TestPredicatedList.class);
44     }
45     
46     public static void main(String JavaDoc args[]) {
47         String JavaDoc[] testCaseName = { TestPredicatedList.class.getName()};
48         junit.textui.TestRunner.main(testCaseName);
49     }
50     
51  //-------------------------------------------------------------------
52

53     protected Predicate truePredicate = PredicateUtils.truePredicate();
54     
55     protected List JavaDoc decorateList(List JavaDoc list, Predicate predicate) {
56         return PredicatedList.decorate(list, predicate);
57     }
58     
59     public List JavaDoc makeEmptyList() {
60         return decorateList(new ArrayList JavaDoc(), truePredicate);
61     }
62     
63     public Object JavaDoc[] getFullElements() {
64         return new Object JavaDoc[] {"1", "3", "5", "7", "2", "4", "6"};
65     }
66     
67 //--------------------------------------------------------------------
68

69      protected Predicate testPredicate =
70         new Predicate() {
71             public boolean evaluate(Object JavaDoc o) {
72                 return o instanceof String JavaDoc;
73             }
74         };
75     
76     public List JavaDoc makeTestList() {
77         return decorateList(new ArrayList JavaDoc(), testPredicate);
78     }
79     
80     public void testIllegalAdd() {
81         List JavaDoc list = makeTestList();
82         Integer JavaDoc i = new Integer JavaDoc(3);
83         try {
84             list.add(i);
85             fail("Integer should fail string predicate.");
86         } catch (IllegalArgumentException JavaDoc e) {
87             // expected
88
}
89         assertTrue("Collection shouldn't contain illegal element",
90          !list.contains(i));
91     }
92
93     public void testIllegalAddAll() {
94         List JavaDoc list = makeTestList();
95         List JavaDoc elements = new ArrayList JavaDoc();
96         elements.add("one");
97         elements.add("two");
98         elements.add(new Integer JavaDoc(3));
99         elements.add("four");
100         try {
101             list.addAll(0,elements);
102             fail("Integer should fail string predicate.");
103         } catch (IllegalArgumentException JavaDoc e) {
104             // expected
105
}
106         assertTrue("List shouldn't contain illegal element",
107          !list.contains("one"));
108         assertTrue("List shouldn't contain illegal element",
109          !list.contains("two"));
110         assertTrue("List shouldn't contain illegal element",
111          !list.contains(new Integer JavaDoc(3)));
112         assertTrue("List shouldn't contain illegal element",
113          !list.contains("four"));
114     }
115     
116     public void testIllegalSet() {
117         List JavaDoc list = makeTestList();
118         try {
119             list.set(0,new Integer JavaDoc(3));
120             fail("Integer should fail string predicate.");
121         } catch (IllegalArgumentException JavaDoc e) {
122             // expected
123
}
124     }
125     
126     public void testLegalAddAll() {
127         List JavaDoc list = makeTestList();
128         list.add("zero");
129         List JavaDoc elements = new ArrayList JavaDoc();
130         elements.add("one");
131         elements.add("two");
132         elements.add("three");
133         list.addAll(1,elements);
134         assertTrue("List should contain legal element",
135          list.contains("zero"));
136         assertTrue("List should contain legal element",
137          list.contains("one"));
138         assertTrue("List should contain legal element",
139          list.contains("two"));
140         assertTrue("List should contain legal element",
141          list.contains("three"));
142     }
143
144     public String JavaDoc getCompatibilityVersion() {
145         return "3.1";
146     }
147
148 // public void testCreate() throws Exception {
149
// resetEmpty();
150
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedList.emptyCollection.version3.1.obj");
151
// resetFull();
152
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedList.fullCollection.version3.1.obj");
153
// }
154

155 }
156
Popular Tags