001: /*
002: * Copyright 2001-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;
017:
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.HashSet;
021: import java.util.Set;
022:
023: import junit.framework.Test;
024:
025: import org.apache.commons.collections.set.PredicatedSet;
026:
027: /**
028: * Tests for SetUtils.
029: *
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author Stephen Colebourne
033: * @author Neil O'Toole
034: * @author Matthew Hawthorne
035: */
036: public class TestSetUtils extends BulkTest {
037:
038: public TestSetUtils(String name) {
039: super (name);
040: }
041:
042: public static Test suite() {
043: return BulkTest.makeSuite(TestSetUtils.class);
044: }
045:
046: public void testNothing() {
047: }
048:
049: public void testpredicatedSet() {
050: Predicate predicate = new Predicate() {
051: public boolean evaluate(Object o) {
052: return o instanceof String;
053: }
054: };
055: Set set = SetUtils.predicatedSet(new HashSet(), predicate);
056: assertTrue("returned object should be a PredicatedSet",
057: set instanceof PredicatedSet);
058: try {
059: set = SetUtils.predicatedSet(new HashSet(), null);
060: fail("Expecting IllegalArgumentException for null predicate.");
061: } catch (IllegalArgumentException ex) {
062: // expected
063: }
064: try {
065: set = SetUtils.predicatedSet(null, predicate);
066: fail("Expecting IllegalArgumentException for null set.");
067: } catch (IllegalArgumentException ex) {
068: // expected
069: }
070: }
071:
072: public void testEquals() {
073: Collection data = Arrays.asList(new String[] { "a", "b", "c" });
074:
075: Set a = new HashSet(data);
076: Set b = new HashSet(data);
077:
078: assertEquals(true, a.equals(b));
079: assertEquals(true, SetUtils.isEqualSet(a, b));
080: a.clear();
081: assertEquals(false, SetUtils.isEqualSet(a, b));
082: assertEquals(false, SetUtils.isEqualSet(a, null));
083: assertEquals(false, SetUtils.isEqualSet(null, b));
084: assertEquals(true, SetUtils.isEqualSet(null, null));
085: }
086:
087: public void testHashCode() {
088: Collection data = Arrays.asList(new String[] { "a", "b", "c" });
089:
090: Set a = new HashSet(data);
091: Set b = new HashSet(data);
092:
093: assertEquals(true, a.hashCode() == b.hashCode());
094: assertEquals(true, a.hashCode() == SetUtils.hashCodeForSet(a));
095: assertEquals(true, b.hashCode() == SetUtils.hashCodeForSet(b));
096: assertEquals(true, SetUtils.hashCodeForSet(a) == SetUtils
097: .hashCodeForSet(b));
098: a.clear();
099: assertEquals(false, SetUtils.hashCodeForSet(a) == SetUtils
100: .hashCodeForSet(b));
101: assertEquals(0, SetUtils.hashCodeForSet(null));
102: }
103:
104: }
|