KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > reflect > TestDelegates


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.reflect;
17
18 import java.lang.reflect.Method JavaDoc;
19 import junit.framework.*;
20
21 /**
22  * @version $Id: TestDelegates.java,v 1.4 2004/06/24 21:15:16 herbyderby Exp $
23  */

24 public class TestDelegates extends net.sf.cglib.CodeGenTestCase {
25
26     public interface StringMaker {
27         Object JavaDoc newInstance(char[] buf, int offset, int count);
28     }
29
30     public void testConstructor() throws Throwable JavaDoc {
31         StringMaker maker = (StringMaker)ConstructorDelegate.create(String JavaDoc.class, StringMaker.class);
32         assertTrue("nil".equals(maker.newInstance("vanilla".toCharArray(), 2, 3)));
33     }
34
35     public interface Substring {
36         String JavaDoc substring(int start, int end);
37     }
38
39     public interface Substring2 {
40         Object JavaDoc anyNameAllowed(int start, int end);
41     }
42
43     public interface IndexOf {
44         int indexOf(String JavaDoc str, int fromIndex);
45     }
46
47     public void testFancy() throws Throwable JavaDoc {
48         Substring delegate = (Substring)MethodDelegate.create("CGLIB", "substring", Substring.class);
49         assertTrue("LI".equals(delegate.substring(2, 4)));
50     }
51
52     public void testFancyNames() throws Throwable JavaDoc {
53         Substring2 delegate = (Substring2)MethodDelegate.create("CGLIB", "substring", Substring2.class);
54         assertTrue("LI".equals(delegate.anyNameAllowed(2, 4)));
55     }
56
57     public void testFancyTypes() throws Throwable JavaDoc {
58         String JavaDoc test = "abcabcabc";
59         IndexOf delegate = (IndexOf)MethodDelegate.create(test, "indexOf", IndexOf.class);
60         assertTrue(delegate.indexOf("ab", 1) == test.indexOf("ab", 1));
61     }
62
63     public void testEquals() throws Throwable JavaDoc {
64         String JavaDoc test = "abc";
65         MethodDelegate mc1 = MethodDelegate.create(test, "indexOf", IndexOf.class);
66         MethodDelegate mc2 = MethodDelegate.create(test, "indexOf", IndexOf.class);
67         MethodDelegate mc3 = MethodDelegate.create("other", "indexOf", IndexOf.class);
68         MethodDelegate mc4 = MethodDelegate.create(test, "substring", Substring.class);
69         MethodDelegate mc5 = MethodDelegate.create(test, "substring", Substring2.class);
70         assertTrue(mc1.equals(mc2));
71         assertTrue(!mc1.equals(mc3));
72         assertTrue(!mc1.equals(mc4));
73         assertTrue(mc4.equals(mc5));
74     }
75
76     public static interface MainDelegate {
77         int main(String JavaDoc[] args);
78     }
79
80     public static class MainTest {
81         public static int alternateMain(String JavaDoc[] args) {
82             return 7;
83         }
84     }
85
86     public void testStaticDelegate() throws Throwable JavaDoc {
87         MainDelegate start = (MainDelegate)MethodDelegate.createStatic(MainTest.class,
88                                                                        "alternateMain",
89                                                                        MainDelegate.class);
90         assertTrue(start.main(null) == 7);
91     }
92
93     public static interface Listener {
94         public void onEvent();
95     }
96
97     public static class Publisher {
98         public int test = 0;
99         private MulticastDelegate event = MulticastDelegate.create(Listener.class);
100         public void addListener(Listener listener) {
101             event = event.add(listener);
102         }
103         public void removeListener(Listener listener) {
104             event = event.remove(listener);
105         }
106         public void fireEvent() {
107             ((Listener)event).onEvent();
108         }
109     }
110
111     public void testPublisher() throws Throwable JavaDoc {
112         final Publisher p = new Publisher();
113         Listener l1 = new Listener() {
114                 public void onEvent() {
115                     p.test++;
116                 }
117             };
118         p.addListener(l1);
119         p.addListener(l1);
120         p.fireEvent();
121         assertTrue(p.test == 2);
122         p.removeListener(l1);
123         p.fireEvent();
124         assertTrue(p.test == 3);
125     }
126
127     public static interface SuperSimple {
128         public int execute();
129     }
130
131     public void testMulticastReturnValue() throws Throwable JavaDoc {
132         SuperSimple ss1 = new SuperSimple() {
133                 public int execute() {
134                     return 1;
135                 }
136             };
137         SuperSimple ss2 = new SuperSimple() {
138                 public int execute() {
139                     return 2;
140                 }
141             };
142         MulticastDelegate multi = MulticastDelegate.create(SuperSimple.class);
143         multi = multi.add(ss1);
144         multi = multi.add(ss2);
145         assertTrue(((SuperSimple)multi).execute() == 2);
146         multi = multi.remove(ss1);
147         multi = multi.add(ss1);
148         assertTrue(((SuperSimple)multi).execute() == 1);
149     }
150
151     public TestDelegates(String JavaDoc testName) {
152         super(testName);
153     }
154     
155     public static void main(String JavaDoc[] args) {
156         junit.textui.TestRunner.run(suite());
157     }
158     
159     public static Test suite() {
160         return new TestSuite(TestDelegates.class);
161     }
162
163     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
164     }
165     
166     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
167     }
168     
169 }
170
Popular Tags