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.HashSet;
019: import java.util.Set;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.collections.Predicate;
025: import org.apache.commons.collections.PredicateUtils;
026:
027: /**
028: * Extension of {@link TestSet} for exercising the
029: * {@link PredicatedSet} 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 TestPredicatedSet extends AbstractTestSet {
037:
038: public TestPredicatedSet(String testName) {
039: super (testName);
040: }
041:
042: public static Test suite() {
043: return new TestSuite(TestPredicatedSet.class);
044: }
045:
046: public static void main(String args[]) {
047: String[] testCaseName = { TestPredicatedSet.class.getName() };
048: junit.textui.TestRunner.main(testCaseName);
049: }
050:
051: //-------------------------------------------------------------------
052:
053: protected Predicate truePredicate = PredicateUtils.truePredicate();
054:
055: protected Set decorateSet(Set set, Predicate predicate) {
056: return PredicatedSet.decorate(set, predicate);
057: }
058:
059: public Set makeEmptySet() {
060: return decorateSet(new HashSet(), truePredicate);
061: }
062:
063: public Object[] getFullElements() {
064: return new Object[] { "1", "3", "5", "7", "2", "4", "6" };
065: }
066:
067: //--------------------------------------------------------------------
068:
069: protected Predicate testPredicate = new Predicate() {
070: public boolean evaluate(Object o) {
071: return o instanceof String;
072: }
073: };
074:
075: protected Set makeTestSet() {
076: return decorateSet(new HashSet(), testPredicate);
077: }
078:
079: public void testGetSet() {
080: Set set = makeTestSet();
081: assertTrue("returned set should not be null",
082: ((PredicatedSet) set).getSet() != null);
083: }
084:
085: public void testIllegalAdd() {
086: Set set = makeTestSet();
087: Integer i = new Integer(3);
088: try {
089: set.add(i);
090: fail("Integer should fail string predicate.");
091: } catch (IllegalArgumentException e) {
092: // expected
093: }
094: assertTrue("Collection shouldn't contain illegal element", !set
095: .contains(i));
096: }
097:
098: public void testIllegalAddAll() {
099: Set set = makeTestSet();
100: Set elements = new HashSet();
101: elements.add("one");
102: elements.add("two");
103: elements.add(new Integer(3));
104: elements.add("four");
105: try {
106: set.addAll(elements);
107: fail("Integer should fail string predicate.");
108: } catch (IllegalArgumentException e) {
109: // expected
110: }
111: assertTrue("Set shouldn't contain illegal element", !set
112: .contains("one"));
113: assertTrue("Set shouldn't contain illegal element", !set
114: .contains("two"));
115: assertTrue("Set shouldn't contain illegal element", !set
116: .contains(new Integer(3)));
117: assertTrue("Set shouldn't contain illegal element", !set
118: .contains("four"));
119: }
120:
121: public String getCompatibilityVersion() {
122: return "3.1";
123: }
124:
125: // public void testCreate() throws Exception {
126: // resetEmpty();
127: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSet.emptyCollection.version3.1.obj");
128: // resetFull();
129: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/PredicatedSet.fullCollection.version3.1.obj");
130: // }
131:
132: }
|