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.Comparator;
020: import java.util.Set;
021: import java.util.SortedSet;
022: import java.util.TreeSet;
023:
024: import junit.framework.Test;
025:
026: import org.apache.commons.collections.BulkTest;
027: import org.apache.commons.collections.Predicate;
028: import org.apache.commons.collections.PredicateUtils;
029: import org.apache.commons.collections.map.TestPredicatedSortedMap;
030:
031: /**
032: * Extension of {@link AbstractTestSortedSet} for exercising the
033: * {@link PredicatedSortedSet} implementation.
034: *
035: * @since Commons Collections 3.0
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Phil Steitz
039: */
040: public class TestPredicatedSortedSet extends AbstractTestSortedSet {
041:
042: public TestPredicatedSortedSet(String testName) {
043: super (testName);
044: }
045:
046: public static Test suite() {
047: return BulkTest.makeSuite(TestPredicatedSortedSet.class);
048: }
049:
050: public static void main(String args[]) {
051: String[] testCaseName = { TestPredicatedSortedMap.class
052: .getName() };
053: junit.textui.TestRunner.main(testCaseName);
054: }
055:
056: //-------------------------------------------------------------------
057:
058: protected Predicate truePredicate = PredicateUtils.truePredicate();
059:
060: public Set makeEmptySet() {
061: return PredicatedSortedSet.decorate(new TreeSet(),
062: truePredicate);
063: }
064:
065: public Set makeFullSet() {
066: TreeSet set = new TreeSet();
067: set.addAll(Arrays.asList(getFullElements()));
068: return PredicatedSortedSet.decorate(set, truePredicate);
069: }
070:
071: //--------------------------------------------------------------------
072: protected Predicate testPredicate = new Predicate() {
073: public boolean evaluate(Object o) {
074: return (o instanceof String)
075: && (((String) o).startsWith("A"));
076: }
077: };
078:
079: protected SortedSet makeTestSet() {
080: return PredicatedSortedSet.decorate(new TreeSet(),
081: testPredicate);
082: }
083:
084: public void testGetSet() {
085: SortedSet set = makeTestSet();
086: assertTrue("returned set should not be null",
087: ((PredicatedSortedSet) set).getSet() != null);
088: }
089:
090: public void testIllegalAdd() {
091: SortedSet set = makeTestSet();
092: String testString = "B";
093: try {
094: set.add(testString);
095: fail("Should fail string predicate.");
096: } catch (IllegalArgumentException e) {
097: // expected
098: }
099: assertTrue("Collection shouldn't contain illegal element", !set
100: .contains(testString));
101: }
102:
103: public void testIllegalAddAll() {
104: SortedSet set = makeTestSet();
105: Set elements = new TreeSet();
106: elements.add("Aone");
107: elements.add("Atwo");
108: elements.add("Bthree");
109: elements.add("Afour");
110: try {
111: set.addAll(elements);
112: fail("Should fail string predicate.");
113: } catch (IllegalArgumentException e) {
114: // expected
115: }
116: assertTrue("Set shouldn't contain illegal element", !set
117: .contains("Aone"));
118: assertTrue("Set shouldn't contain illegal element", !set
119: .contains("Atwo"));
120: assertTrue("Set shouldn't contain illegal element", !set
121: .contains("Bthree"));
122: assertTrue("Set shouldn't contain illegal element", !set
123: .contains("Afour"));
124: }
125:
126: public void testComparator() {
127: SortedSet set = makeTestSet();
128: Comparator c = set.comparator();
129: assertTrue("natural order, so comparator should be null",
130: c == null);
131: }
132:
133: public String getCompatibilityVersion() {
134: return "3.1";
135: }
136:
137: // public void testCreate() throws Exception {
138: // resetEmpty();
139: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSortedSet.emptyCollection.version3.1.obj");
140: // resetFull();
141: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSortedSet.fullCollection.version3.1.obj");
142: // }
143:
144: }
|