KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > proxy > TestDispatcher


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.proxy;
17
18 import net.sf.cglib.CodeGenTestCase;
19 import java.beans.*;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.*;
22 import junit.framework.*;
23
24 /**
25  * @author Chris Nokleberg
26  * @version $Id: TestDispatcher.java,v 1.6 2004/06/24 21:15:17 herbyderby Exp $
27  */

28 public class TestDispatcher extends CodeGenTestCase {
29     interface Foo {
30         String JavaDoc foo();
31     }
32
33     interface Bar {
34         String JavaDoc bar();
35     }
36     
37     public void testSimple() throws Exception JavaDoc {
38         final Object JavaDoc[] impls = new Object JavaDoc[]{
39             new Foo() {
40                 public String JavaDoc foo() {
41                     return "foo1";
42                 }
43             },
44             new Bar() {
45                 public String JavaDoc bar() {
46                     return "bar1";
47                 }
48             }
49         };
50
51         Callback[] callbacks = new Callback[]{
52             new Dispatcher() {
53                 public Object JavaDoc loadObject() {
54                     return impls[0];
55                 }
56             },
57             new Dispatcher() {
58                 public Object JavaDoc loadObject() {
59                     return impls[1];
60                 }
61             }
62         };
63
64         Enhancer e = new Enhancer();
65         e.setInterfaces(new Class JavaDoc[]{ Foo.class, Bar.class });
66         e.setCallbacks(callbacks);
67         e.setCallbackFilter(new CallbackFilter() {
68             public int accept(Method JavaDoc method) {
69                 return (method.getDeclaringClass().equals(Foo.class)) ? 0 : 1;
70             }
71         });
72         Object JavaDoc obj = e.create();
73         
74         assertTrue(((Foo)obj).foo().equals("foo1"));
75         assertTrue(((Bar)obj).bar().equals("bar1"));
76
77         impls[0] = new Foo() {
78             public String JavaDoc foo() {
79                 return "foo2";
80             }
81         };
82         assertTrue(((Foo)obj).foo().equals("foo2"));
83     }
84
85     public TestDispatcher(String JavaDoc testName) {
86         super(testName);
87     }
88     
89     public static void main(String JavaDoc[] args) {
90         junit.textui.TestRunner.run(suite());
91     }
92     
93     public static Test suite() {
94         return new TestSuite(TestDispatcher.class);
95     }
96     
97     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
98     }
99     
100     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
101     }
102     
103 }
104
Popular Tags