KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > TestPredicatedSet


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.set;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Set 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 TestSet} for exercising the
29  * {@link PredicatedSet} implementation.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.6 $ $Date: 2004/06/02 22:12:14 $
33  *
34  * @author Phil Steitz
35  */

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

53     protected Predicate truePredicate = PredicateUtils.truePredicate();
54     
55     protected Set JavaDoc decorateSet(Set JavaDoc set, Predicate predicate) {
56         return PredicatedSet.decorate(set, predicate);
57     }
58     
59     public Set JavaDoc makeEmptySet() {
60         return decorateSet(new HashSet 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     protected Set JavaDoc makeTestSet() {
77         return decorateSet(new HashSet JavaDoc(), testPredicate);
78     }
79     
80     public void testGetSet() {
81          Set JavaDoc set = makeTestSet();
82         assertTrue("returned set should not be null",
83             ((PredicatedSet) set).getSet() != null);
84     }
85     
86     public void testIllegalAdd() {
87         Set JavaDoc set = makeTestSet();
88         Integer JavaDoc i = new Integer JavaDoc(3);
89         try {
90             set.add(i);
91             fail("Integer should fail string predicate.");
92         } catch (IllegalArgumentException JavaDoc e) {
93             // expected
94
}
95         assertTrue("Collection shouldn't contain illegal element",
96          !set.contains(i));
97     }
98
99     public void testIllegalAddAll() {
100         Set JavaDoc set = makeTestSet();
101         Set JavaDoc elements = new HashSet JavaDoc();
102         elements.add("one");
103         elements.add("two");
104         elements.add(new Integer JavaDoc(3));
105         elements.add("four");
106         try {
107             set.addAll(elements);
108             fail("Integer should fail string predicate.");
109         } catch (IllegalArgumentException JavaDoc e) {
110             // expected
111
}
112         assertTrue("Set shouldn't contain illegal element",
113          !set.contains("one"));
114         assertTrue("Set shouldn't contain illegal element",
115          !set.contains("two"));
116         assertTrue("Set shouldn't contain illegal element",
117          !set.contains(new Integer JavaDoc(3)));
118         assertTrue("Set shouldn't contain illegal element",
119          !set.contains("four"));
120     }
121
122     public String JavaDoc getCompatibilityVersion() {
123         return "3.1";
124     }
125
126 // public void testCreate() throws Exception {
127
// resetEmpty();
128
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSet.emptyCollection.version3.1.obj");
129
// resetFull();
130
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSet.fullCollection.version3.1.obj");
131
// }
132

133 }
Popular Tags