1 16 package org.apache.commons.collections.set; 17 18 import java.util.HashSet ; 19 import java.util.Set ; 20 21 import junit.framework.Test; 22 import junit.framework.TestSuite; 23 24 import org.apache.commons.collections.Predicate; 25 import org.apache.commons.collections.PredicateUtils; 26 27 36 public class TestPredicatedSet extends AbstractTestSet{ 37 38 public TestPredicatedSet(String testName) { 39 super(testName); 40 } 41 42 public static Test suite() { 43 return new TestSuite(TestPredicatedSet.class); 44 } 45 46 public static void main(String args[]) { 47 String [] testCaseName = { TestPredicatedSet.class.getName()}; 48 junit.textui.TestRunner.main(testCaseName); 49 } 50 51 53 protected Predicate truePredicate = PredicateUtils.truePredicate(); 54 55 protected Set decorateSet(Set set, Predicate predicate) { 56 return PredicatedSet.decorate(set, predicate); 57 } 58 59 public Set makeEmptySet() { 60 return decorateSet(new HashSet (), truePredicate); 61 } 62 63 public Object [] getFullElements() { 64 return new Object [] {"1", "3", "5", "7", "2", "4", "6"}; 65 } 66 67 69 protected Predicate testPredicate = 70 new Predicate() { 71 public boolean evaluate(Object o) { 72 return o instanceof String ; 73 } 74 }; 75 76 protected Set makeTestSet() { 77 return decorateSet(new HashSet (), testPredicate); 78 } 79 80 public void testGetSet() { 81 Set set = makeTestSet(); 82 assertTrue("returned set should not be null", 83 ((PredicatedSet) set).getSet() != null); 84 } 85 86 public void testIllegalAdd() { 87 Set set = makeTestSet(); 88 Integer i = new Integer (3); 89 try { 90 set.add(i); 91 fail("Integer should fail string predicate."); 92 } catch (IllegalArgumentException e) { 93 } 95 assertTrue("Collection shouldn't contain illegal element", 96 !set.contains(i)); 97 } 98 99 public void testIllegalAddAll() { 100 Set set = makeTestSet(); 101 Set elements = new HashSet (); 102 elements.add("one"); 103 elements.add("two"); 104 elements.add(new Integer (3)); 105 elements.add("four"); 106 try { 107 set.addAll(elements); 108 fail("Integer should fail string predicate."); 109 } catch (IllegalArgumentException e) { 110 } 112 assertTrue("Set shouldn't contain illegal element", 113 !set.contains("one")); 114 assertTrue("Set shouldn't contain illegal element", 115 !set.contains("two")); 116 assertTrue("Set shouldn't contain illegal element", 117 !set.contains(new Integer (3))); 118 assertTrue("Set shouldn't contain illegal element", 119 !set.contains("four")); 120 } 121 122 public String getCompatibilityVersion() { 123 return "3.1"; 124 } 125 126 133 } | Popular Tags |