KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > TestPredicatedSortedBag


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.bag;
17
18 import java.util.Comparator JavaDoc;
19
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.apache.commons.collections.Bag;
24 import org.apache.commons.collections.Predicate;
25 import org.apache.commons.collections.PredicateUtils;
26 import org.apache.commons.collections.SortedBag;
27
28 /**
29  * Extension of {@link TestBag} for exercising the {@link PredicatedSortedBag}
30  * implementation.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.8 $ $Date: 2004/06/02 22:05:03 $
34  *
35  * @author Phil Steitz
36  */

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

56     protected Predicate stringPredicate() {
57         return new Predicate() {
58             public boolean evaluate(Object JavaDoc o) {
59                 return o instanceof String JavaDoc;
60             }
61         };
62     }
63     
64     protected Predicate truePredicate = PredicateUtils.truePredicate();
65     
66     protected SortedBag decorateBag(SortedBag bag, Predicate predicate) {
67         return PredicatedSortedBag.decorate(bag, predicate);
68     }
69     
70     public Bag makeBag() {
71         return decorateBag(new TreeBag(), truePredicate);
72     }
73     
74     protected Bag makeTestBag() {
75         return decorateBag(new TreeBag(), stringPredicate());
76     }
77     
78     //--------------------------------------------------------------------------
79

80     public void testDecorate() {
81         SortedBag bag = decorateBag(new TreeBag(), stringPredicate());
82         SortedBag bag2 = ((PredicatedSortedBag) bag).getSortedBag();
83         try {
84             SortedBag bag3 = decorateBag(new TreeBag(), null);
85             fail("Expecting IllegalArgumentException for null predicate");
86         } catch (IllegalArgumentException JavaDoc e) {}
87         try {
88             SortedBag bag4 = decorateBag(nullBag, stringPredicate());
89             fail("Expecting IllegalArgumentException for null bag");
90         } catch (IllegalArgumentException JavaDoc e) {}
91     }
92     
93     public void testSortOrder() {
94         SortedBag bag = decorateBag(new TreeBag(), stringPredicate());
95         String JavaDoc one = "one";
96         String JavaDoc two = "two";
97         String JavaDoc three = "three";
98         bag.add(one);
99         bag.add(two);
100         bag.add(three);
101         assertEquals("first element", bag.first(), one);
102         assertEquals("last element", bag.last(), two);
103         Comparator JavaDoc c = bag.comparator();
104         assertTrue("natural order, so comparator should be null", c == null);
105     }
106
107     public String JavaDoc getCompatibilityVersion() {
108         return "3.1";
109     }
110
111 // public void testCreate() throws Exception {
112
// Bag bag = makeBag();
113
// writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedSortedBag.emptyCollection.version3.1.obj");
114
// bag = makeBag();
115
// bag.add("A");
116
// bag.add("A");
117
// bag.add("B");
118
// bag.add("B");
119
// bag.add("C");
120
// writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedSortedBag.fullCollection.version3.1.obj");
121
// }
122

123 }
124
Popular Tags