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:
025: /**
026: * Extension of {@link TestBag} for exercising the {@link TypedBag}
027: * implementation.
028: *
029: * @since Commons Collections 3.0
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author Phil Steitz
033: */
034: public class TestTypedBag extends AbstractTestBag {
035:
036: public TestTypedBag(String testName) {
037: super (testName);
038: }
039:
040: public static Test suite() {
041: return new TestSuite(TestTypedBag.class);
042: }
043:
044: public static void main(String args[]) {
045: String[] testCaseName = { TestTypedBag.class.getName() };
046: junit.textui.TestRunner.main(testCaseName);
047: }
048:
049: //--------------------------------------------------------------------------
050:
051: protected Class stringClass = this .getName().getClass();
052: private Object obj = new Object();
053: protected Class objectClass = obj.getClass();
054:
055: protected Bag decorateBag(HashBag bag, Class claz) {
056: return TypedBag.decorate(bag, claz);
057: }
058:
059: public Bag makeBag() {
060: return decorateBag(new HashBag(), objectClass);
061: }
062:
063: protected Bag makeTestBag() {
064: return decorateBag(new HashBag(), stringClass);
065: }
066:
067: //--------------------------------------------------------------------------
068:
069: public void testlegalAddRemove() {
070: Bag bag = makeTestBag();
071: assertEquals(0, bag.size());
072: Object[] els = new Object[] { "1", "3", "5", "7", "2", "4", "1" };
073: for (int i = 0; i < els.length; i++) {
074: bag.add(els[i]);
075: assertEquals(i + 1, bag.size());
076: assertEquals(true, bag.contains(els[i]));
077: }
078: Set set = ((PredicatedBag) bag).uniqueSet();
079: assertTrue("Unique set contains the first element", set
080: .contains(els[0]));
081: assertEquals(true, bag.remove(els[0]));
082: set = ((PredicatedBag) bag).uniqueSet();
083: assertTrue("Unique set now does not contain the first element",
084: !set.contains(els[0]));
085: }
086:
087: public void testIllegalAdd() {
088: Bag bag = makeTestBag();
089: Integer i = new Integer(3);
090: try {
091: bag.add(i);
092: fail("Integer should fail type check.");
093: } catch (IllegalArgumentException e) {
094: // expected
095: }
096: assertTrue("Collection shouldn't contain illegal element", !bag
097: .contains(i));
098: }
099:
100: public void testIllegalDecorate() {
101: HashBag elements = new HashBag();
102: elements.add("one");
103: elements.add("two");
104: elements.add(new Integer(3));
105: elements.add("four");
106: try {
107: Bag bag = decorateBag(elements, stringClass);
108: fail("Bag contains an element that should fail the type test.");
109: } catch (IllegalArgumentException e) {
110: // expected
111: }
112: try {
113: Bag bag = decorateBag(new HashBag(), null);
114: fail("Expectiing IllegalArgumentException for null predicate.");
115: } catch (IllegalArgumentException e) {
116: // expected
117: }
118: }
119:
120: protected boolean skipSerializedCanonicalTests() {
121: return true;
122: }
123:
124: }
|