KickJava   Java API By Example, From Geeks To Geeks.

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


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.SortedBag;
25
26 /**
27  * Extension of {@link TestBag} for exercising the {@link TypedSortedBag}
28  * implementation.
29  *
30  * @since Commons Collections 3.0
31  * @version $Revision: 1.8 $ $Date: 2004/06/02 22:05:03 $
32  *
33  * @author Phil Steitz
34  */

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

52     protected Class JavaDoc stringClass = this.getName().getClass();
53     private Object JavaDoc obj = new Object JavaDoc();
54     protected Class JavaDoc objectClass = obj.getClass();
55     protected SortedBag nullBag = null;
56     
57     protected SortedBag decorateBag(SortedBag bag, Class JavaDoc claz) {
58         return TypedSortedBag.decorate(bag, claz);
59     }
60
61     public Bag makeBag() {
62         return decorateBag(new TreeBag(), objectClass);
63     }
64     
65     protected Bag makeTestBag() {
66         return decorateBag(new TreeBag(), stringClass);
67     }
68     
69     //--------------------------------------------------------------------------
70

71     public void testDecorate() {
72         SortedBag bag = decorateBag(new TreeBag(), stringClass);
73         try {
74             SortedBag bag3 = decorateBag(new TreeBag(), null);
75             fail("Expecting IllegalArgumentException for null predicate");
76         } catch (IllegalArgumentException JavaDoc e) {}
77         try {
78             SortedBag bag4 = decorateBag(nullBag, stringClass);
79             fail("Expecting IllegalArgumentException for null bag");
80         } catch (IllegalArgumentException JavaDoc e) {}
81     }
82     
83     public void testSortOrder() {
84         SortedBag bag = decorateBag(new TreeBag(), stringClass);
85         String JavaDoc one = "one";
86         String JavaDoc two = "two";
87         String JavaDoc three = "three";
88         bag.add(one);
89         bag.add(two);
90         bag.add(three);
91         assertEquals("first element", bag.first(), one);
92         assertEquals("last element", bag.last(), two);
93         Comparator JavaDoc c = bag.comparator();
94         assertTrue("natural order, so comparator should be null", c == null);
95     }
96
97     protected boolean skipSerializedCanonicalTests() {
98         return true;
99     }
100
101 }
102
Popular Tags