KickJava   Java API By Example, From Geeks To Geeks.

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


1 package uk.co.jezuk.mango;
2
3 import junit.framework.*;
4
5 public class TransformIteratorTest extends TestCase
6 {
7   java.util.List JavaDoc list;
8
9   public TransformIteratorTest(String JavaDoc name) { super(name); }
10   public static Test suite() { return new TestSuite(TransformIteratorTest.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 DoubleUp implements UnaryFunction
20   {
21     public Object JavaDoc fn(Object JavaDoc obj)
22     {
23       Integer JavaDoc i = (Integer JavaDoc)obj;
24       return new Integer JavaDoc(i.intValue() * 2);
25     } // test
26
}
27   
28   public void test1()
29   {
30     java.util.Iterator JavaDoc iter = Iterators.TransformIterator(list.iterator(), new DoubleUp());
31
32     int i = 0;
33     while(iter.hasNext())
34     {
35       Integer JavaDoc n = (Integer JavaDoc)iter.next();
36       assertEquals(n.intValue(), i);
37       i += 2;
38     } // while ...
39
} // test1
40

41   class NameObject
42   {
43     public NameObject(String JavaDoc name) { name_ = name; }
44     
45     public String JavaDoc getName() { return name_; }
46
47     private String JavaDoc name_;
48   } // NameObject
49

50   public void test2()
51   {
52     list = new java.util.ArrayList JavaDoc();
53     list.add(new NameObject("hawkeye pierce"));
54     list.add(new NameObject("sacremento"));
55     list.add(new NameObject("GOBBLE"));
56     list.add(new NameObject("SINGLETON"));
57     list.add(new NameObject("BILBO"));
58     NameObject theOneIWant = new NameObject("CORGAN");
59     list.add(theOneIWant);
60     list.add(new NameObject("ERNEST"));
61     list.add(new NameObject("DAVID"));
62     list.add(new NameObject("BILLY"));
63     list.add(new NameObject("SCAGGS"));
64     list.add(new NameObject("CHARLES"));
65     list.add(new NameObject("SIMEON"));
66
67     // find the object called CORGAN
68
String JavaDoc found = (String JavaDoc)Algorithms.find(Iterators.TransformIterator(list.iterator(),
69                           Adapt.ArgumentMethod("getName")),
70                           new String JavaDoc("CORGAN"));
71     assertEquals("CORGAN", found);
72   } // test2
73
} //
74

75
76
Popular Tags