1 package uk.co.jezuk.mango; 2 3 import junit.framework.*; 4 5 public class SkippingIteratorTest extends TestCase 6 { 7 java.util.List list; 8 9 public SkippingIteratorTest(String name) { super(name); } 10 public static Test suite() { return new TestSuite(SkippingIteratorTest.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.SkippingIterator(list.iterator(), new LessThanFive()); 31 32 assertEquals(new Integer (5), iter.next()); 33 assertEquals(new Integer (6), iter.next()); 34 assertEquals(new Integer (7), iter.next()); 35 assertEquals(new Integer (8), iter.next()); 36 assertEquals(new Integer (9), iter.next()); 37 } 39 public void test2() 40 { 41 list = new java.util.ArrayList (); 42 list.add("hawkeye pierce"); 43 list.add("sacremento"); 44 list.add("GOBBLE"); 45 list.add("SINGLETON"); 46 list.add("BILBO"); 47 list.add("ERNEST"); 48 list.add("DAVID"); 49 list.add("BILLY"); 50 list.add("SCAGGS"); 51 list.add("CHARLES"); 52 list.add("SIMEON"); 53 54 java.util.Iterator iter = Iterators.SkippingIterator(list.iterator(), 55 new Predicate() { 56 public boolean test(Object o) { 57 String s = (String )o; 58 return s.charAt(0) == 'S'; 59 } 60 }); 61 int i = 0; 62 while(iter.hasNext()) 63 { 64 ++i; 65 String s = (String )iter.next(); 66 assertEquals(false, s.startsWith("S")); 67 } assertEquals(8, i); 69 } } | Popular Tags |