1 16 package org.apache.commons.collections; 17 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 23 import junit.framework.Test; 24 25 import org.apache.commons.collections.set.PredicatedSet; 26 27 36 public class TestSetUtils extends BulkTest { 37 38 public TestSetUtils(String 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 o) { 52 return o instanceof String ; 53 } 54 }; 55 Set set = SetUtils.predicatedSet(new HashSet (), predicate); 56 assertTrue("returned object should be a PredicatedSet", 57 set instanceof PredicatedSet); 58 try { 59 set = SetUtils.predicatedSet(new HashSet (), null); 60 fail("Expecting IllegalArgumentException for null predicate."); 61 } catch (IllegalArgumentException ex) { 62 } 64 try { 65 set = SetUtils.predicatedSet(null, predicate); 66 fail("Expecting IllegalArgumentException for null set."); 67 } catch (IllegalArgumentException ex) { 68 } 70 } 71 72 public void testEquals() { 73 Collection data = Arrays.asList( new String [] { "a", "b", "c" }); 74 75 Set a = new HashSet ( data ); 76 Set b = new HashSet ( 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 data = Arrays.asList( new String [] { "a", "b", "c" }); 89 90 Set a = new HashSet ( data ); 91 Set b = new HashSet ( 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 |