KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class TestTypedSortedSet extends AbstractTestSortedSet{
38     
39     public TestTypedSortedSet(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     public static Test suite() {
44         return BulkTest.makeSuite(TestTypedSortedSet.class);
45     }
46     
47     public static void main(String JavaDoc args[]) {
48         String JavaDoc[] testCaseName = { TestTypedSortedSet.class.getName()};
49         junit.textui.TestRunner.main(testCaseName);
50     }
51     
52  //-------------------------------------------------------------------
53
protected Class JavaDoc integerType = new Integer JavaDoc(0).getClass();
54     
55     public Set JavaDoc makeEmptySet() {
56         return TypedSortedSet.decorate(new TreeSet JavaDoc(), integerType);
57     }
58     
59     public Set JavaDoc makeFullSet() {
60         TreeSet JavaDoc set = new TreeSet JavaDoc();
61         set.addAll(Arrays.asList(getFullElements()));
62         return TypedSortedSet.decorate(set, integerType);
63     }
64    
65     
66 //--------------------------------------------------------------------
67
protected Long JavaDoc getNextAsLong() {
68         SortedSet JavaDoc set = (SortedSet JavaDoc) makeFullSet();
69         int nextValue = ((Integer JavaDoc)set.last()).intValue() + 1;
70         return new Long JavaDoc(nextValue);
71     }
72     
73     protected Integer JavaDoc getNextAsInt() {
74         SortedSet JavaDoc set = (SortedSet JavaDoc) makeFullSet();
75         int nextValue = ((Integer JavaDoc)set.last()).intValue() + 1;
76         return new Integer JavaDoc(nextValue);
77     }
78            
79     public void testIllegalAdd() {
80         Set JavaDoc set = makeFullSet();
81         try {
82             set.add(getNextAsLong());
83             fail("Should fail type test.");
84         } catch (IllegalArgumentException JavaDoc e) {
85             // expected
86
}
87         assertTrue("Collection shouldn't convert long to int",
88          !set.contains(getNextAsInt()));
89     }
90
91     public void testIllegalAddAll() {
92         Set JavaDoc set = makeFullSet();
93         Set JavaDoc elements = new TreeSet JavaDoc();
94         elements.add(getNextAsLong());
95         try {
96             set.addAll(elements);
97             fail("Should fail type test.");
98         } catch (IllegalArgumentException JavaDoc e) {
99             // expected
100
}
101         assertTrue("Collection shouldn't convert long to int",
102          !set.contains(getNextAsInt()));
103     }
104
105     public boolean skipSerializedCanonicalTests() {
106         return true; // Typed and Predicated get confused
107
}
108
109 }
110
Popular Tags