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 junit.framework.Test;
019: import junit.framework.TestSuite;
020:
021: import org.apache.commons.collections.collection.CompositeCollection;
022:
023: import java.util.Set;
024: import java.util.HashSet;
025: import java.util.Collection;
026:
027: /**
028: * Extension of {@link AbstractTestSet} for exercising the
029: * {@link CompositeSet} 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 Brian McCallister
035: * @author Phil Steitz
036: */
037:
038: public class TestCompositeSet extends AbstractTestSet {
039: public TestCompositeSet(String name) {
040: super (name);
041: }
042:
043: public static Test suite() {
044: return new TestSuite(TestCompositeSet.class);
045: }
046:
047: public Set makeEmptySet() {
048: final HashSet contained = new HashSet();
049: CompositeSet set = new CompositeSet(contained);
050: set.setMutator(new CompositeSet.SetMutator() {
051: public void resolveCollision(CompositeSet comp,
052: Set existing, Set added, Collection intersects) {
053: throw new IllegalArgumentException();
054: }
055:
056: public boolean add(CompositeCollection composite,
057: Collection[] collections, Object obj) {
058: return contained.add(obj);
059: }
060:
061: public boolean addAll(CompositeCollection composite,
062: Collection[] collections, Collection coll) {
063: return contained.addAll(coll);
064: }
065:
066: public boolean remove(CompositeCollection composite,
067: Collection[] collections, Object obj) {
068: return contained.remove(obj);
069: }
070: });
071: return set;
072: }
073:
074: public Set buildOne() {
075: HashSet set = new HashSet();
076: set.add("1");
077: set.add("2");
078: return set;
079: }
080:
081: public Set buildTwo() {
082: HashSet set = new HashSet();
083: set.add("3");
084: set.add("4");
085: return set;
086: }
087:
088: public void testContains() {
089: CompositeSet set = new CompositeSet(new Set[] { buildOne(),
090: buildTwo() });
091: assertTrue(set.contains("1"));
092: }
093:
094: public void testRemoveUnderlying() {
095: Set one = buildOne();
096: Set two = buildTwo();
097: CompositeSet set = new CompositeSet(new Set[] { one, two });
098: one.remove("1");
099: assertFalse(set.contains("1"));
100:
101: two.remove("3");
102: assertFalse(set.contains("3"));
103: }
104:
105: public void testRemoveComposited() {
106: Set one = buildOne();
107: Set two = buildTwo();
108: CompositeSet set = new CompositeSet(new Set[] { one, two });
109: set.remove("1");
110: assertFalse(one.contains("1"));
111:
112: set.remove("3");
113: assertFalse(one.contains("3"));
114: }
115:
116: public void testFailedCollisionResolution() {
117: Set one = buildOne();
118: Set two = buildTwo();
119: CompositeSet set = new CompositeSet(new Set[] { one, two });
120: set.setMutator(new CompositeSet.SetMutator() {
121: public void resolveCollision(CompositeSet comp,
122: Set existing, Set added, Collection intersects) {
123: }
124:
125: public boolean add(CompositeCollection composite,
126: Collection[] collections, Object obj) {
127: throw new UnsupportedOperationException();
128: }
129:
130: public boolean addAll(CompositeCollection composite,
131: Collection[] collections, Collection coll) {
132: throw new UnsupportedOperationException();
133: }
134:
135: public boolean remove(CompositeCollection composite,
136: Collection[] collections, Object obj) {
137: throw new UnsupportedOperationException();
138: }
139: });
140:
141: HashSet three = new HashSet();
142: three.add("1");
143: try {
144: set.addComposited(three);
145: fail("IllegalArgumentException should have been thrown");
146: } catch (IllegalArgumentException e) {
147: // expected
148: }
149: }
150:
151: public void testAddComposited() {
152: Set one = buildOne();
153: Set two = buildTwo();
154: CompositeSet set = new CompositeSet();
155: set.addComposited(one, two);
156: CompositeSet set2 = new CompositeSet(buildOne());
157: set2.addComposited(buildTwo());
158: assertTrue(set.equals(set2));
159: HashSet set3 = new HashSet();
160: set3.add("1");
161: set3.add("2");
162: set3.add("3");
163: HashSet set4 = new HashSet();
164: set4.add("4");
165: CompositeSet set5 = new CompositeSet(set3);
166: set5.addComposited(set4);
167: assertTrue(set.equals(set5));
168: try {
169: set.addComposited(set3);
170: fail("Expecting UnsupportedOperationException.");
171: } catch (UnsupportedOperationException ex) {
172: // expected
173: }
174: }
175: }
|