001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.set;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Comparator;
021: import java.util.Set;
022: import java.util.TreeSet;
023:
024: import junit.framework.Test;
025:
026: import org.apache.commons.collections.BulkTest;
027:
028: /**
029: * Extension of {@link AbstractTestSortedSet} for exercising the
030: * {@link UnmodifiableSortedSet} implementation.
031: *
032: * @since Commons Collections 3.0
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Phil Steitz
036: */
037: public class TestUnmodifiableSortedSet extends AbstractTestSortedSet {
038:
039: public TestUnmodifiableSortedSet(String testName) {
040: super (testName);
041: }
042:
043: public static Test suite() {
044: return BulkTest.makeSuite(TestUnmodifiableSortedSet.class);
045: }
046:
047: public static void main(String args[]) {
048: String[] testCaseName = { TestUnmodifiableSortedSet.class
049: .getName() };
050: junit.textui.TestRunner.main(testCaseName);
051: }
052:
053: //-------------------------------------------------------------------
054: public Set makeEmptySet() {
055: return UnmodifiableSortedSet.decorate(new TreeSet());
056: }
057:
058: public Set makeFullSet() {
059: TreeSet set = new TreeSet();
060: set.addAll(Arrays.asList(getFullElements()));
061: return UnmodifiableSortedSet.decorate(set);
062: }
063:
064: public boolean isAddSupported() {
065: return false;
066: }
067:
068: public boolean isRemoveSupported() {
069: return false;
070: }
071:
072: //--------------------------------------------------------------------
073: protected UnmodifiableSortedSet set = null;
074: protected ArrayList array = null;
075:
076: protected void setupSet() {
077: set = (UnmodifiableSortedSet) makeFullSet();
078: array = new ArrayList();
079: array.add(new Integer(1));
080: }
081:
082: /**
083: * Verify that base set and subsets are not modifiable
084: */
085: public void testUnmodifiable() {
086: setupSet();
087: verifyUnmodifiable(set);
088: verifyUnmodifiable(set.headSet(new Integer(1)));
089: verifyUnmodifiable(set.tailSet(new Integer(1)));
090: verifyUnmodifiable(set.subSet(new Integer(1), new Integer(3)));
091: }
092:
093: /**
094: * Verifies that a set is not modifiable
095: */
096: public void verifyUnmodifiable(Set set) {
097: try {
098: set.add("value");
099: fail("Expecting UnsupportedOperationException.");
100: } catch (UnsupportedOperationException e) {
101: // expected
102: }
103: try {
104: set.addAll(new TreeSet());
105: fail("Expecting UnsupportedOperationException.");
106: } catch (UnsupportedOperationException e) {
107: // expected
108: }
109: try {
110: set.clear();
111: fail("Expecting UnsupportedOperationException.");
112: } catch (UnsupportedOperationException e) {
113: // expected
114: }
115: try {
116: set.remove("x");
117: fail("Expecting UnsupportedOperationException.");
118: } catch (UnsupportedOperationException e) {
119: // expected
120: }
121: try {
122: set.removeAll(array);
123: fail("Expecting UnsupportedOperationException.");
124: } catch (UnsupportedOperationException e) {
125: // expected
126: }
127: try {
128: set.retainAll(array);
129: fail("Expecting UnsupportedOperationException.");
130: } catch (UnsupportedOperationException e) {
131: // expected
132: }
133: }
134:
135: public void testComparator() {
136: setupSet();
137: Comparator c = set.comparator();
138: assertTrue("natural order, so comparator should be null",
139: c == null);
140: }
141:
142: public String getCompatibilityVersion() {
143: return "3.1";
144: }
145:
146: // public void testCreate() throws Exception {
147: // resetEmpty();
148: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/UnmodifiableSortedSet.emptyCollection.version3.1.obj");
149: // resetFull();
150: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/UnmodifiableSortedSet.fullCollection.version3.1.obj");
151: // }
152:
153: }
|