KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class TestUnmodifiableSortedSet extends AbstractTestSortedSet{
38     
39     public TestUnmodifiableSortedSet(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     public static Test suite() {
44         return BulkTest.makeSuite(TestUnmodifiableSortedSet.class);
45     }
46     
47     public static void main(String JavaDoc args[]) {
48         String JavaDoc[] testCaseName = { TestUnmodifiableSortedSet.class.getName()};
49         junit.textui.TestRunner.main(testCaseName);
50     }
51     
52     //-------------------------------------------------------------------
53
public Set JavaDoc makeEmptySet() {
54         return UnmodifiableSortedSet.decorate(new TreeSet JavaDoc());
55     }
56     
57     public Set JavaDoc makeFullSet() {
58         TreeSet JavaDoc set = new TreeSet JavaDoc();
59         set.addAll(Arrays.asList(getFullElements()));
60         return UnmodifiableSortedSet.decorate(set);
61     }
62     
63     public boolean isAddSupported() {
64         return false;
65     }
66     
67     public boolean isRemoveSupported() {
68         return false;
69     }
70            
71     //--------------------------------------------------------------------
72
protected UnmodifiableSortedSet set = null;
73     protected ArrayList JavaDoc array = null;
74     
75     protected void setupSet() {
76         set = (UnmodifiableSortedSet) makeFullSet();
77         array = new ArrayList JavaDoc();
78         array.add(new Integer JavaDoc(1));
79     }
80     
81     /**
82      * Verify that base set and subsets are not modifiable
83      */

84     public void testUnmodifiable() {
85         setupSet();
86         verifyUnmodifiable(set);
87         verifyUnmodifiable(set.headSet(new Integer JavaDoc(1)));
88         verifyUnmodifiable(set.tailSet(new Integer JavaDoc(1)));
89         verifyUnmodifiable(set.subSet(new Integer JavaDoc(1), new Integer JavaDoc(3)));
90     }
91     
92     /**
93      * Verifies that a set is not modifiable
94      */

95     public void verifyUnmodifiable(Set JavaDoc set) {
96         try {
97             set.add("value");
98             fail("Expecting UnsupportedOperationException.");
99         } catch (UnsupportedOperationException JavaDoc e) {
100             // expected
101
}
102         try {
103             set.addAll(new TreeSet JavaDoc());
104             fail("Expecting UnsupportedOperationException.");
105         } catch (UnsupportedOperationException JavaDoc e) {
106             // expected
107
}
108         try {
109             set.clear();
110             fail("Expecting UnsupportedOperationException.");
111         } catch (UnsupportedOperationException JavaDoc e) {
112             // expected
113
}
114         try {
115             set.remove("x");
116             fail("Expecting UnsupportedOperationException.");
117         } catch (UnsupportedOperationException JavaDoc e) {
118             // expected
119
}
120         try {
121             set.removeAll(array);
122             fail("Expecting UnsupportedOperationException.");
123         } catch (UnsupportedOperationException JavaDoc e) {
124             // expected
125
}
126         try {
127             set.retainAll(array);
128             fail("Expecting UnsupportedOperationException.");
129         } catch (UnsupportedOperationException JavaDoc e) {
130             // expected
131
}
132     }
133     
134     public void testComparator() {
135         setupSet();
136         Comparator JavaDoc c = set.comparator();
137         assertTrue("natural order, so comparator should be null", c == null);
138     }
139
140     public String JavaDoc getCompatibilityVersion() {
141         return "3.1";
142     }
143
144 // public void testCreate() throws Exception {
145
// resetEmpty();
146
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/UnmodifiableSortedSet.emptyCollection.version3.1.obj");
147
// resetFull();
148
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/UnmodifiableSortedSet.fullCollection.version3.1.obj");
149
// }
150

151 }
152
Popular Tags