KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > SkippingIteratorTest


1 package uk.co.jezuk.mango;
2
3 import junit.framework.*;
4
5 public class SkippingIteratorTest extends TestCase
6 {
7   java.util.List JavaDoc list;
8
9   public SkippingIteratorTest(String JavaDoc 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 JavaDoc();
15     for(int i = 0; i < 10; ++i)
16       list.add(new Integer JavaDoc(i));
17   } // setUp
18

19   class LessThanFive implements Predicate
20   {
21     public boolean test(Object JavaDoc obj)
22     {
23       Integer JavaDoc i = (Integer JavaDoc)obj;
24       return i.intValue() < 5;
25     } // test
26
}
27   
28   public void test1()
29   {
30     java.util.Iterator JavaDoc iter = Iterators.SkippingIterator(list.iterator(), new LessThanFive());
31
32     assertEquals(new Integer JavaDoc(5), iter.next());
33     assertEquals(new Integer JavaDoc(6), iter.next());
34     assertEquals(new Integer JavaDoc(7), iter.next());
35     assertEquals(new Integer JavaDoc(8), iter.next());
36     assertEquals(new Integer JavaDoc(9), iter.next());
37   } // test1
38

39   public void test2()
40   {
41     list = new java.util.ArrayList JavaDoc();
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 JavaDoc iter = Iterators.SkippingIterator(list.iterator(),
55                                        new Predicate() {
56                                            public boolean test(Object JavaDoc o) {
57                                              String JavaDoc s = (String JavaDoc)o;
58                                              return s.charAt(0) == 'S';
59                                            }
60                                        });
61     int i = 0;
62     while(iter.hasNext())
63     {
64       ++i;
65       String JavaDoc s = (String JavaDoc)iter.next();
66       assertEquals(false, s.startsWith("S"));
67     } // while
68
assertEquals(8, i);
69   } // test2
70
} //
71
Popular Tags