1 package uk.co.jezuk.mango; 2 3 import junit.framework.*; 4 5 public class AdaptTest extends TestCase 6 { 7 java.util.List list; 8 9 public AdaptTest(String 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 (); 15 for(int i = 0; i < 10; ++i) 16 list.add(new Integer (i)); 17 } 19 public void test1() 20 { 21 System.out.println("test1"); 22 Algorithms.forEach(list, Adapt.Method(System.out, "println")); 23 } 25 public void test2() 26 { 27 try { 28 Algorithms.forEach(list, Adapt.Method(System.out, "charles")); 30 fail(); 31 } catch(RuntimeException e) { 33 } } 36 public void test3() 37 { 38 try { 39 Algorithms.forEach(list, Adapt.Method(System.out, "close")); 41 fail(); 42 } catch(RuntimeException e) { 44 } } 47 public void test4() 48 { 49 try { 50 Algorithms.forEach(list, Adapt.Method(System.in, "bongo")); 52 fail(); 53 } catch(RuntimeException e) { 55 } } 58 public void test5() 59 { 60 System.out.println("test5"); 61 Algorithms.forEach(list, Adapt.Method(this, "staticMethod")); 62 } 64 public void test6() 65 { 66 System.out.println("test6"); 67 Algorithms.forEach(list, Adapt.Method(this.getClass(), "staticMethod")); 68 } 70 class Something 71 { 72 Something(int i) { i_ = i; } 73 public void print() { System.out.println(i_); } 74 private int i_; 75 } 77 public void test7() 78 { 79 System.out.println("test7"); 80 java.util.List l = new java.util.ArrayList (); 81 for(int i = 0; i < 10000; ++i) 82 l.add(new Something(i)); 83 84 Algorithms.forEach(l, Adapt.ArgumentMethod("print")); 85 } 87 public void test8() 88 { 89 UnaryFunction fn = Adapt.Compose(new AppendX(), new AppendX()); 90 assertEquals("helloXX", fn.fn("hello")); 91 } 93 public void test9() 94 { 95 BinaryFunction fn = Adapt.Compose(new Concat(), new AppendX(), new AppendX()); 96 assertEquals("helloXworldX", fn.fn("hello", "world")); 97 } 99 static public class Concat implements BinaryFunction 100 { 101 public Object fn(Object x, Object y) 102 { 103 String s1 = (String )x; 104 String s2 = (String )y; 105 return s1+s2; 106 } } 109 110 static public class AppendX implements UnaryFunction 111 { 112 public Object fn(Object x) 113 { 114 String s = (String )x; 115 return s+'X'; 116 } 117 } 119 120 static public void staticMethod(Object o) 121 { 122 System.out.println(o); 123 } } | Popular Tags |