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.bag;
017:
018: import java.util.Set;
019:
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.collections.Bag;
024: import org.apache.commons.collections.Predicate;
025: import org.apache.commons.collections.PredicateUtils;
026:
027: /**
028: * Extension of {@link TestBag} for exercising the {@link PredicatedBag}
029: * 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 TestPredicatedBag extends AbstractTestBag {
037:
038: public TestPredicatedBag(String testName) {
039: super (testName);
040: }
041:
042: public static Test suite() {
043: return new TestSuite(TestPredicatedBag.class);
044: }
045:
046: public static void main(String args[]) {
047: String[] testCaseName = { TestPredicatedBag.class.getName() };
048: junit.textui.TestRunner.main(testCaseName);
049: }
050:
051: //--------------------------------------------------------------------------
052:
053: protected Predicate stringPredicate() {
054: return new Predicate() {
055: public boolean evaluate(Object o) {
056: return o instanceof String;
057: }
058: };
059: }
060:
061: protected Predicate truePredicate = PredicateUtils.truePredicate();
062:
063: protected Bag decorateBag(HashBag bag, Predicate predicate) {
064: return PredicatedBag.decorate(bag, predicate);
065: }
066:
067: public Bag makeBag() {
068: return decorateBag(new HashBag(), truePredicate);
069: }
070:
071: protected Bag makeTestBag() {
072: return decorateBag(new HashBag(), stringPredicate());
073: }
074:
075: //--------------------------------------------------------------------------
076:
077: public void testlegalAddRemove() {
078: Bag bag = makeTestBag();
079: assertEquals(0, bag.size());
080: Object[] els = new Object[] { "1", "3", "5", "7", "2", "4", "1" };
081: for (int i = 0; i < els.length; i++) {
082: bag.add(els[i]);
083: assertEquals(i + 1, bag.size());
084: assertEquals(true, bag.contains(els[i]));
085: }
086: Set set = ((PredicatedBag) bag).uniqueSet();
087: assertTrue("Unique set contains the first element", set
088: .contains(els[0]));
089: assertEquals(true, bag.remove(els[0]));
090: set = ((PredicatedBag) bag).uniqueSet();
091: assertTrue("Unique set now does not contain the first element",
092: !set.contains(els[0]));
093: }
094:
095: public void testIllegalAdd() {
096: Bag bag = makeTestBag();
097: Integer i = new Integer(3);
098: try {
099: bag.add(i);
100: fail("Integer should fail string predicate.");
101: } catch (IllegalArgumentException e) {
102: // expected
103: }
104: assertTrue("Collection shouldn't contain illegal element", !bag
105: .contains(i));
106: }
107:
108: public void testIllegalDecorate() {
109: HashBag elements = new HashBag();
110: elements.add("one");
111: elements.add("two");
112: elements.add(new Integer(3));
113: elements.add("four");
114: try {
115: Bag bag = decorateBag(elements, stringPredicate());
116: fail("Bag contains an element that should fail the predicate.");
117: } catch (IllegalArgumentException e) {
118: // expected
119: }
120: try {
121: Bag bag = decorateBag(new HashBag(), null);
122: fail("Expectiing IllegalArgumentException for null predicate.");
123: } catch (IllegalArgumentException e) {
124: // expected
125: }
126: }
127:
128: public String getCompatibilityVersion() {
129: return "3.1";
130: }
131:
132: // public void testCreate() throws Exception {
133: // Bag bag = makeBag();
134: // writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedBag.emptyCollection.version3.1.obj");
135: // bag = makeBag();
136: // bag.add("A");
137: // bag.add("A");
138: // bag.add("B");
139: // bag.add("B");
140: // bag.add("C");
141: // writeExternalFormToDisk((java.io.Serializable) bag, "D:/dev/collections/data/test/PredicatedBag.fullCollection.version3.1.obj");
142: // }
143:
144: }
|