KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestSetUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import junit.framework.Test;
24
25 import org.apache.commons.collections.set.PredicatedSet;
26
27 /**
28  * Tests for SetUtils.
29  *
30  * @version $Revision: 1.16 $ $Date: 2004/06/02 22:12:14 $
31  *
32  * @author Stephen Colebourne
33  * @author Neil O'Toole
34  * @author Matthew Hawthorne
35  */

36 public class TestSetUtils extends BulkTest {
37
38     public TestSetUtils(String JavaDoc name) {
39         super(name);
40     }
41
42     public static Test suite() {
43         return BulkTest.makeSuite(TestSetUtils.class);
44     }
45
46     public void testNothing() {
47     }
48     
49     public void testpredicatedSet() {
50         Predicate predicate = new Predicate() {
51             public boolean evaluate(Object JavaDoc o) {
52                 return o instanceof String JavaDoc;
53             }
54         };
55         Set JavaDoc set = SetUtils.predicatedSet(new HashSet JavaDoc(), predicate);
56         assertTrue("returned object should be a PredicatedSet",
57             set instanceof PredicatedSet);
58         try {
59             set = SetUtils.predicatedSet(new HashSet JavaDoc(), null);
60             fail("Expecting IllegalArgumentException for null predicate.");
61         } catch (IllegalArgumentException JavaDoc ex) {
62             // expected
63
}
64         try {
65             set = SetUtils.predicatedSet(null, predicate);
66             fail("Expecting IllegalArgumentException for null set.");
67         } catch (IllegalArgumentException JavaDoc ex) {
68             // expected
69
}
70     }
71
72     public void testEquals() {
73         Collection JavaDoc data = Arrays.asList( new String JavaDoc[] { "a", "b", "c" });
74         
75         Set JavaDoc a = new HashSet JavaDoc( data );
76         Set JavaDoc b = new HashSet JavaDoc( data );
77         
78         assertEquals(true, a.equals(b));
79         assertEquals(true, SetUtils.isEqualSet(a, b));
80         a.clear();
81         assertEquals(false, SetUtils.isEqualSet(a, b));
82         assertEquals(false, SetUtils.isEqualSet(a, null));
83         assertEquals(false, SetUtils.isEqualSet(null, b));
84         assertEquals(true, SetUtils.isEqualSet(null, null));
85     }
86     
87     public void testHashCode() {
88         Collection JavaDoc data = Arrays.asList( new String JavaDoc[] { "a", "b", "c" });
89             
90         Set JavaDoc a = new HashSet JavaDoc( data );
91         Set JavaDoc b = new HashSet JavaDoc( data );
92         
93         assertEquals(true, a.hashCode() == b.hashCode());
94         assertEquals(true, a.hashCode() == SetUtils.hashCodeForSet(a));
95         assertEquals(true, b.hashCode() == SetUtils.hashCodeForSet(b));
96         assertEquals(true, SetUtils.hashCodeForSet(a) == SetUtils.hashCodeForSet(b));
97         a.clear();
98         assertEquals(false, SetUtils.hashCodeForSet(a) == SetUtils.hashCodeForSet(b));
99         assertEquals(0, SetUtils.hashCodeForSet(null));
100     }
101
102 }
103
Popular Tags