1 16 package org.apache.commons.collections.collection; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.List ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 import org.apache.commons.collections.Predicate; 27 import org.apache.commons.collections.PredicateUtils; 28 29 38 public class TestPredicatedCollection extends AbstractTestCollection { 39 40 public TestPredicatedCollection(String name) { 41 super(name); 42 } 43 44 public static Test suite() { 45 return new TestSuite(TestPredicatedCollection.class); 46 } 47 48 public static void main(String args[]) { 49 String [] testCaseName = { TestPredicatedCollection.class.getName()}; 50 junit.textui.TestRunner.main(testCaseName); 51 } 52 53 55 protected Predicate truePredicate = PredicateUtils.truePredicate(); 56 57 protected Collection decorateCollection(Collection collection, 58 Predicate predicate) { 59 return PredicatedCollection.decorate(collection, predicate); 60 } 61 62 public Collection makeCollection() { 63 return decorateCollection(new ArrayList (), truePredicate); 64 } 65 66 public Collection makeConfirmedCollection() { 67 return new ArrayList (); 68 } 69 70 public Object [] getFullElements() { 71 return new Object [] {"1", "3", "5", "7", "2", "4", "6"}; 72 } 73 74 public Collection makeFullCollection() { 75 List list = new ArrayList (); 76 list.addAll(Arrays.asList(getFullElements())); 77 return decorateCollection(list, truePredicate); 78 } 79 80 public Collection makeConfirmedFullCollection() { 81 List list = new ArrayList (); 82 list.addAll(Arrays.asList(getFullElements())); 83 return list; 84 } 85 86 protected Predicate testPredicate = 88 new Predicate() { 89 public boolean evaluate(Object o) { 90 return o instanceof String ; 91 } 92 }; 93 94 public Collection makeTestCollection() { 95 return decorateCollection(new ArrayList (), testPredicate); 96 } 97 98 public void testIllegalAdd() { 99 Collection c = makeTestCollection(); 100 Integer i = new Integer (3); 101 try { 102 c.add(i); 103 fail("Integer should fail string predicate."); 104 } catch (IllegalArgumentException e) { 105 } 107 assertTrue("Collection shouldn't contain illegal element", 108 !c.contains(i)); 109 } 110 111 public void testIllegalAddAll() { 112 Collection c = makeTestCollection(); 113 List elements = new ArrayList (); 114 elements.add("one"); 115 elements.add("two"); 116 elements.add(new Integer (3)); 117 elements.add("four"); 118 try { 119 c.addAll(elements); 120 fail("Integer should fail string predicate."); 121 } catch (IllegalArgumentException e) { 122 } 124 assertTrue("Collection shouldn't contain illegal element", 125 !c.contains("one")); 126 assertTrue("Collection shouldn't contain illegal element", 127 !c.contains("two")); 128 assertTrue("Collection shouldn't contain illegal element", 129 !c.contains(new Integer (3))); 130 assertTrue("Collection shouldn't contain illegal element", 131 !c.contains("four")); 132 } 133 134 public String getCompatibilityVersion() { 135 return "3.1"; 136 } 137 138 145 } 146 | Popular Tags |