1 16 package org.apache.commons.collections.list; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 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 TestPredicatedList extends AbstractTestList { 37 38 public TestPredicatedList(String testName) { 39 super(testName); 40 } 41 42 public static Test suite() { 43 return new TestSuite(TestPredicatedList.class); 44 } 45 46 public static void main(String args[]) { 47 String [] testCaseName = { TestPredicatedList.class.getName()}; 48 junit.textui.TestRunner.main(testCaseName); 49 } 50 51 53 protected Predicate truePredicate = PredicateUtils.truePredicate(); 54 55 protected List decorateList(List list, Predicate predicate) { 56 return PredicatedList.decorate(list, predicate); 57 } 58 59 public List makeEmptyList() { 60 return decorateList(new ArrayList (), 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 public List makeTestList() { 77 return decorateList(new ArrayList (), testPredicate); 78 } 79 80 public void testIllegalAdd() { 81 List list = makeTestList(); 82 Integer i = new Integer (3); 83 try { 84 list.add(i); 85 fail("Integer should fail string predicate."); 86 } catch (IllegalArgumentException e) { 87 } 89 assertTrue("Collection shouldn't contain illegal element", 90 !list.contains(i)); 91 } 92 93 public void testIllegalAddAll() { 94 List list = makeTestList(); 95 List elements = new ArrayList (); 96 elements.add("one"); 97 elements.add("two"); 98 elements.add(new Integer (3)); 99 elements.add("four"); 100 try { 101 list.addAll(0,elements); 102 fail("Integer should fail string predicate."); 103 } catch (IllegalArgumentException e) { 104 } 106 assertTrue("List shouldn't contain illegal element", 107 !list.contains("one")); 108 assertTrue("List shouldn't contain illegal element", 109 !list.contains("two")); 110 assertTrue("List shouldn't contain illegal element", 111 !list.contains(new Integer (3))); 112 assertTrue("List shouldn't contain illegal element", 113 !list.contains("four")); 114 } 115 116 public void testIllegalSet() { 117 List list = makeTestList(); 118 try { 119 list.set(0,new Integer (3)); 120 fail("Integer should fail string predicate."); 121 } catch (IllegalArgumentException e) { 122 } 124 } 125 126 public void testLegalAddAll() { 127 List list = makeTestList(); 128 list.add("zero"); 129 List elements = new ArrayList (); 130 elements.add("one"); 131 elements.add("two"); 132 elements.add("three"); 133 list.addAll(1,elements); 134 assertTrue("List should contain legal element", 135 list.contains("zero")); 136 assertTrue("List should contain legal element", 137 list.contains("one")); 138 assertTrue("List should contain legal element", 139 list.contains("two")); 140 assertTrue("List should contain legal element", 141 list.contains("three")); 142 } 143 144 public String getCompatibilityVersion() { 145 return "3.1"; 146 } 147 148 155 } 156 | Popular Tags |