KickJava   Java API By Example, From Geeks To Geeks.

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


1 package uk.co.jezuk.mango;
2
3 import junit.framework.*;
4
5 public class AdaptTest extends TestCase
6 {
7   java.util.List JavaDoc list;
8
9   public AdaptTest(String JavaDoc name) { super(name); }
10   public static Test suite() { return new TestSuite(AdaptTest.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   public void test1()
20   {
21     System.out.println("test1");
22     Algorithms.forEach(list, Adapt.Method(System.out, "println"));
23   } //
24

25   public void test2()
26   {
27     try {
28       // should throw
29
Algorithms.forEach(list, Adapt.Method(System.out, "charles"));
30       fail();
31     } // try
32
catch(RuntimeException JavaDoc e) {
33     } // RuntimeException
34
} // test2
35

36   public void test3()
37   {
38     try {
39       // should throw
40
Algorithms.forEach(list, Adapt.Method(System.out, "close"));
41       fail();
42     } // try
43
catch(RuntimeException JavaDoc e) {
44     } // RuntimeException
45
} // test3
46

47   public void test4()
48   {
49     try {
50       // should throw
51
Algorithms.forEach(list, Adapt.Method(System.in, "bongo"));
52       fail();
53     } // try
54
catch(RuntimeException JavaDoc e) {
55     } // RuntimeException
56
} // test4
57

58   public void test5()
59   {
60     System.out.println("test5");
61     Algorithms.forEach(list, Adapt.Method(this, "staticMethod"));
62   } // test5
63

64   public void test6()
65   {
66     System.out.println("test6");
67     Algorithms.forEach(list, Adapt.Method(this.getClass(), "staticMethod"));
68   } // test6
69

70   class Something
71   {
72       Something(int i) { i_ = i; }
73       public void print() { System.out.println(i_); }
74       private int i_;
75   } // Something
76

77   public void test7()
78   {
79     System.out.println("test7");
80     java.util.List JavaDoc l = new java.util.ArrayList JavaDoc();
81     for(int i = 0; i < 10000; ++i)
82     l.add(new Something(i));
83
84     Algorithms.forEach(l, Adapt.ArgumentMethod("print"));
85   } // test7
86

87   public void test8()
88   {
89     UnaryFunction fn = Adapt.Compose(new AppendX(), new AppendX());
90     assertEquals("helloXX", fn.fn("hello"));
91   } // test8
92

93   public void test9()
94   {
95     BinaryFunction fn = Adapt.Compose(new Concat(), new AppendX(), new AppendX());
96     assertEquals("helloXworldX", fn.fn("hello", "world"));
97   } // test9
98

99   static public class Concat implements BinaryFunction
100   {
101     public Object JavaDoc fn(Object JavaDoc x, Object JavaDoc y)
102     {
103       String JavaDoc s1 = (String JavaDoc)x;
104       String JavaDoc s2 = (String JavaDoc)y;
105       return s1+s2;
106     } // fn
107
} // Concat
108

109
110   static public class AppendX implements UnaryFunction
111   {
112     public Object JavaDoc fn(Object JavaDoc x)
113     {
114       String JavaDoc s = (String JavaDoc)x;
115       return s+'X';
116     }
117   } // AppendX
118

119
120   static public void staticMethod(Object JavaDoc o)
121   {
122     System.out.println(o);
123   } // staticMethod
124
} // AdaptTest
125
Popular Tags