KickJava   Java API By Example, From Geeks To Geeks.

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


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.Predicate;
27 import org.apache.commons.collections.PredicateUtils;
28
29 /**
30  * Extension of {@link TestCollection} for exercising the
31  * {@link PredicatedCollection} implementation.
32  *
33  * @since Commons Collections 3.0
34  * @version $Revision: 1.6 $ $Date: 2004/06/02 22:06:33 $
35  *
36  * @author Phil Steitz
37  */

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

55     protected Predicate truePredicate = PredicateUtils.truePredicate();
56     
57     protected Collection JavaDoc decorateCollection(Collection JavaDoc collection,
58         Predicate predicate) {
59         return PredicatedCollection.decorate(collection, predicate);
60     }
61     
62     public Collection JavaDoc makeCollection() {
63         return decorateCollection(new ArrayList JavaDoc(), truePredicate);
64     }
65     
66     public Collection JavaDoc makeConfirmedCollection() {
67         return new ArrayList JavaDoc();
68     }
69     
70     public Object JavaDoc[] getFullElements() {
71         return new Object JavaDoc[] {"1", "3", "5", "7", "2", "4", "6"};
72     }
73     
74     public Collection JavaDoc makeFullCollection() {
75         List JavaDoc list = new ArrayList JavaDoc();
76         list.addAll(Arrays.asList(getFullElements()));
77         return decorateCollection(list, truePredicate);
78     }
79     
80     public Collection JavaDoc makeConfirmedFullCollection() {
81         List JavaDoc list = new ArrayList JavaDoc();
82         list.addAll(Arrays.asList(getFullElements()));
83         return list;
84     }
85
86  //-----------------------------------------------------------------
87
protected Predicate testPredicate =
88         new Predicate() {
89             public boolean evaluate(Object JavaDoc o) {
90                 return o instanceof String JavaDoc;
91             }
92         };
93     
94     public Collection JavaDoc makeTestCollection() {
95         return decorateCollection(new ArrayList JavaDoc(), testPredicate);
96     }
97      
98     public void testIllegalAdd() {
99         Collection JavaDoc c = makeTestCollection();
100         Integer JavaDoc i = new Integer JavaDoc(3);
101         try {
102             c.add(i);
103             fail("Integer should fail string predicate.");
104         } catch (IllegalArgumentException JavaDoc e) {
105             // expected
106
}
107         assertTrue("Collection shouldn't contain illegal element",
108          !c.contains(i));
109     }
110
111     public void testIllegalAddAll() {
112         Collection JavaDoc c = makeTestCollection();
113         List JavaDoc elements = new ArrayList JavaDoc();
114         elements.add("one");
115         elements.add("two");
116         elements.add(new Integer JavaDoc(3));
117         elements.add("four");
118         try {
119             c.addAll(elements);
120             fail("Integer should fail string predicate.");
121         } catch (IllegalArgumentException JavaDoc e) {
122             // expected
123
}
124         assertTrue("Collection shouldn't contain illegal element",
125          !c.contains("one"));
126         assertTrue("Collection shouldn't contain illegal element",
127          !c.contains("two"));
128         assertTrue("Collection shouldn't contain illegal element",
129          !c.contains(new Integer JavaDoc(3)));
130         assertTrue("Collection shouldn't contain illegal element",
131          !c.contains("four"));
132     }
133
134     public String JavaDoc getCompatibilityVersion() {
135         return "3.1";
136     }
137
138 // public void testCreate() throws Exception {
139
// resetEmpty();
140
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedCollection.emptyCollection.version3.1.obj");
141
// resetFull();
142
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedCollection.fullCollection.version3.1.obj");
143
// }
144

145 }
146
Popular Tags