1 package uk.co.jezuk.mango; 2 3 import junit.framework.*; 4 5 public class SelectingIteratorTest extends TestCase 6 { 7 java.util.List list; 8 9 public SelectingIteratorTest(String name) { super(name); } 10 public static Test suite() { return new TestSuite(SelectingIteratorTest.class); } 11 12 protected void setUp() 13 { 14 list = new java.util.ArrayList (); 15 for(int i = 0; i < 10; ++i) 16 list.add(new Integer (i)); 17 } 19 class LessThanFive implements Predicate 20 { 21 public boolean test(Object obj) 22 { 23 Integer i = (Integer )obj; 24 return i.intValue() < 5; 25 } } 27 28 public void test1() 29 { 30 java.util.Iterator iter = Iterators.SelectingIterator(list.iterator(), new LessThanFive()); 31 int i = 0; 32 while(iter.hasNext()) 33 { 34 ++i; 35 iter.next(); 36 } 38 assertEquals(5, i); 39 } 41 public void test2() 42 { 43 list = new java.util.ArrayList (); 44 list.add("hawkeye pierce"); 45 list.add("sacremento"); 46 list.add("GOBBLE"); 47 list.add("SINGLETON"); 48 list.add("BILBO"); 49 list.add("ERNEST"); 50 list.add("DAVID"); 51 list.add("BILLY"); 52 list.add("SCAGGS"); 53 list.add("CHARLES"); 54 list.add("SIMEON"); 55 56 java.util.Iterator iter = Iterators.SelectingIterator(list.iterator(), 57 new Predicate() { 58 public boolean test(Object o) { 59 String s = (String )o; 60 return s.charAt(0) == 'S'; 61 } 62 }); 63 int i = 0; 64 while(iter.hasNext()) 65 { 66 ++i; 67 String s = (String )iter.next(); 68 assertEquals(true, s.startsWith("S")); 69 } assertEquals(3, i); 71 } 73 public void test3() 74 { 75 list = new java.util.ArrayList (); 76 list.add("hawkeye pierce"); 77 list.add("sacremento"); 78 list.add("GOBBLE"); 79 list.add("SINGLETON"); 80 list.add("BILBO"); 81 list.add("ERNEST"); 82 list.add("DAVID"); 83 list.add("BILLY"); 84 list.add("SCAGGS"); 85 list.add("CHARLES"); 86 list.add("SIMEON"); 87 88 java.util.Iterator iter = Iterators.SelectingIterator(list.iterator(), 89 new Predicate() { 90 public boolean test(Object o) { 91 String s = (String )o; 92 return s.charAt(0) == 'S'; 93 } 94 }); 95 while(iter.hasNext()) 96 iter.remove(); 97 98 assertEquals(8, list.size()); 99 assertEquals("hawkeye pierce", list.get(0)); 100 assertEquals("sacremento", list.get(1)); 101 assertEquals("GOBBLE", list.get(2)); 102 assertEquals("BILBO", list.get(3)); 103 assertEquals("ERNEST", list.get(4)); 104 assertEquals("DAVID", list.get(5)); 105 assertEquals("BILLY", list.get(6)); 106 assertEquals("CHARLES", list.get(7)); 107 } } | Popular Tags |