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.Arrays;
019: import java.util.Set;
020: import java.util.SortedSet;
021: import java.util.TreeSet;
022:
023: import junit.framework.Test;
024:
025: import org.apache.commons.collections.BulkTest;
026:
027: /**
028: * Extension of {@link AbstractTestSortedSet} for exercising the
029: * {@link TypedSortedSet} implementation.
030: *
031: * @since Commons Collections 3.0
032: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
033: *
034: * @author Phil Steitz
035: */
036: public class TestTypedSortedSet extends AbstractTestSortedSet {
037:
038: public TestTypedSortedSet(String testName) {
039: super (testName);
040: }
041:
042: public static Test suite() {
043: return BulkTest.makeSuite(TestTypedSortedSet.class);
044: }
045:
046: public static void main(String args[]) {
047: String[] testCaseName = { TestTypedSortedSet.class.getName() };
048: junit.textui.TestRunner.main(testCaseName);
049: }
050:
051: //-------------------------------------------------------------------
052: protected Class integerType = new Integer(0).getClass();
053:
054: public Set makeEmptySet() {
055: return TypedSortedSet.decorate(new TreeSet(), integerType);
056: }
057:
058: public Set makeFullSet() {
059: TreeSet set = new TreeSet();
060: set.addAll(Arrays.asList(getFullElements()));
061: return TypedSortedSet.decorate(set, integerType);
062: }
063:
064: //--------------------------------------------------------------------
065: protected Long getNextAsLong() {
066: SortedSet set = (SortedSet) makeFullSet();
067: int nextValue = ((Integer) set.last()).intValue() + 1;
068: return new Long(nextValue);
069: }
070:
071: protected Integer getNextAsInt() {
072: SortedSet set = (SortedSet) makeFullSet();
073: int nextValue = ((Integer) set.last()).intValue() + 1;
074: return new Integer(nextValue);
075: }
076:
077: public void testIllegalAdd() {
078: Set set = makeFullSet();
079: try {
080: set.add(getNextAsLong());
081: fail("Should fail type test.");
082: } catch (IllegalArgumentException e) {
083: // expected
084: }
085: assertTrue("Collection shouldn't convert long to int", !set
086: .contains(getNextAsInt()));
087: }
088:
089: public void testIllegalAddAll() {
090: Set set = makeFullSet();
091: Set elements = new TreeSet();
092: elements.add(getNextAsLong());
093: try {
094: set.addAll(elements);
095: fail("Should fail type test.");
096: } catch (IllegalArgumentException e) {
097: // expected
098: }
099: assertTrue("Collection shouldn't convert long to int", !set
100: .contains(getNextAsInt()));
101: }
102:
103: public boolean skipSerializedCanonicalTests() {
104: return true; // Typed and Predicated get confused
105: }
106:
107: }
|