KickJava   Java API By Example, From Geeks To Geeks.

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


1 package uk.co.jezuk.mango;
2
3 import junit.framework.*;
4
5 public class SelectingIteratorTest extends TestCase
6 {
7   java.util.List JavaDoc list;
8
9   public SelectingIteratorTest(String JavaDoc 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 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.SelectingIterator(list.iterator(), new LessThanFive());
31     int i = 0;
32     while(iter.hasNext())
33     {
34       ++i;
35       iter.next();
36     } // while ...
37

38     assertEquals(5, i);
39   } // test1
40

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

73   public void test3()
74   {
75     list = new java.util.ArrayList JavaDoc();
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 JavaDoc iter = Iterators.SelectingIterator(list.iterator(),
89                                        new Predicate() {
90                                            public boolean test(Object JavaDoc o) {
91                                              String JavaDoc s = (String JavaDoc)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   } // test3
108
} //
109
Popular Tags