KickJava   Java API By Example, From Geeks To Geeks.

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


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, Bob Lee
26  * @version $Id: TestProxyRefDispatcher.java,v 1.1 2004/12/10 08:48:43 herbyderby Exp $
27  */

28 public class TestProxyRefDispatcher 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         final Object JavaDoc[] proxyReference = new Object JavaDoc[1];
52         Callback[] callbacks = new Callback[]{
53             new ProxyRefDispatcher() {
54                 public Object JavaDoc loadObject(Object JavaDoc proxy) {
55                     proxyReference[0] = proxy;
56                     return impls[0];
57                 }
58             },
59             new ProxyRefDispatcher() {
60                 public Object JavaDoc loadObject(Object JavaDoc proxy) {
61                     proxyReference[0] = proxy;
62                     return impls[1];
63                 }
64             }
65         };
66
67         Enhancer e = new Enhancer();
68         e.setInterfaces(new Class JavaDoc[]{ Foo.class, Bar.class });
69         e.setCallbacks(callbacks);
70         e.setCallbackFilter(new CallbackFilter() {
71             public int accept(Method JavaDoc method) {
72                 return (method.getDeclaringClass().equals(Foo.class)) ? 0 : 1;
73             }
74         });
75         Object JavaDoc obj = e.create();
76         
77         assertNull(proxyReference[0]);
78         assertTrue(((Foo)obj).foo().equals("foo1"));
79         assertSame(obj, proxyReference[0]);
80         proxyReference[0] = null;
81         assertTrue(((Bar)obj).bar().equals("bar1"));
82         assertSame(obj, proxyReference[0]);
83         proxyReference[0] = null;
84
85         impls[0] = new Foo() {
86             public String JavaDoc foo() {
87                 return "foo2";
88             }
89         };
90         assertTrue(((Foo)obj).foo().equals("foo2"));
91         assertSame(obj, proxyReference[0]);
92     }
93
94     public TestProxyRefDispatcher(String JavaDoc testName) {
95         super(testName);
96     }
97     
98     public static void main(String JavaDoc[] args) {
99         junit.textui.TestRunner.run(suite());
100     }
101     
102     public static Test suite() {
103         return new TestSuite(TestProxyRefDispatcher.class);
104     }
105     
106     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
107     }
108     
109     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
110     }
111     
112 }
113
Popular Tags