KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.SortedSet JavaDoc;
22 import java.util.TreeSet JavaDoc;
23
24 import junit.framework.Test;
25
26 import org.apache.commons.collections.BulkTest;
27 import org.apache.commons.collections.Predicate;
28 import org.apache.commons.collections.PredicateUtils;
29 import org.apache.commons.collections.map.TestPredicatedSortedMap;
30
31 /**
32  * Extension of {@link AbstractTestSortedSet} for exercising the
33  * {@link PredicatedSortedSet} implementation.
34  *
35  * @since Commons Collections 3.0
36  * @version $Revision: 1.5 $ $Date: 2004/06/02 22:12:14 $
37  *
38  * @author Phil Steitz
39  */

40 public class TestPredicatedSortedSet extends AbstractTestSortedSet{
41     
42     public TestPredicatedSortedSet(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public static Test suite() {
47         return BulkTest.makeSuite(TestPredicatedSortedSet.class);
48     }
49     
50     public static void main(String JavaDoc args[]) {
51         String JavaDoc[] testCaseName = { TestPredicatedSortedMap.class.getName()};
52         junit.textui.TestRunner.main(testCaseName);
53     }
54     
55  //-------------------------------------------------------------------
56

57     protected Predicate truePredicate = PredicateUtils.truePredicate();
58     
59     public Set JavaDoc makeEmptySet() {
60         return PredicatedSortedSet.decorate(new TreeSet JavaDoc(), truePredicate);
61     }
62     
63     public Set JavaDoc makeFullSet() {
64         TreeSet JavaDoc set = new TreeSet JavaDoc();
65         set.addAll(Arrays.asList(getFullElements()));
66         return PredicatedSortedSet.decorate(set, truePredicate);
67     }
68    
69     
70 //--------------------------------------------------------------------
71
protected Predicate testPredicate =
72         new Predicate() {
73             public boolean evaluate(Object JavaDoc o) {
74                 return (o instanceof String JavaDoc) && (((String JavaDoc) o).startsWith("A"));
75             }
76         };
77      
78     
79     protected SortedSet JavaDoc makeTestSet() {
80         return PredicatedSortedSet.decorate(new TreeSet JavaDoc(), testPredicate);
81     }
82     
83     public void testGetSet() {
84         SortedSet JavaDoc set = makeTestSet();
85         assertTrue("returned set should not be null",
86             ((PredicatedSortedSet) set).getSet() != null);
87     }
88     
89     public void testIllegalAdd() {
90         SortedSet JavaDoc set = makeTestSet();
91         String JavaDoc testString = "B";
92         try {
93             set.add(testString);
94             fail("Should fail string predicate.");
95         } catch (IllegalArgumentException JavaDoc e) {
96             // expected
97
}
98         assertTrue("Collection shouldn't contain illegal element",
99          !set.contains(testString));
100     }
101
102     public void testIllegalAddAll() {
103         SortedSet JavaDoc set = makeTestSet();
104         Set JavaDoc elements = new TreeSet JavaDoc();
105         elements.add("Aone");
106         elements.add("Atwo");
107         elements.add("Bthree");
108         elements.add("Afour");
109         try {
110             set.addAll(elements);
111             fail("Should fail string predicate.");
112         } catch (IllegalArgumentException JavaDoc e) {
113             // expected
114
}
115         assertTrue("Set shouldn't contain illegal element",
116          !set.contains("Aone"));
117         assertTrue("Set shouldn't contain illegal element",
118          !set.contains("Atwo"));
119         assertTrue("Set shouldn't contain illegal element",
120          !set.contains("Bthree"));
121         assertTrue("Set shouldn't contain illegal element",
122          !set.contains("Afour"));
123     }
124     
125     public void testComparator() {
126         SortedSet JavaDoc set = makeTestSet();
127         Comparator JavaDoc c = set.comparator();
128         assertTrue("natural order, so comparator should be null", c == null);
129     }
130
131     public String JavaDoc getCompatibilityVersion() {
132         return "3.1";
133     }
134
135 // public void testCreate() throws Exception {
136
// resetEmpty();
137
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSortedSet.emptyCollection.version3.1.obj");
138
// resetFull();
139
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSortedSet.fullCollection.version3.1.obj");
140
// }
141

142 }
143
Popular Tags