KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import net.sf.cglib.proxysample.ProxySampleInterface_ReturnsBasic;
24
25 import junit.framework.*;
26
27 /**
28  * @author Chris Nokleberg <a HREF="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
29  * @version $Id: TestProxy.java,v 1.5 2004/06/24 21:15:16 herbyderby Exp $
30  */

31 public class TestProxy extends CodeGenTestCase {
32
33     private class SimpleInvocationHandler implements InvocationHandler {
34         Object JavaDoc o = null;
35         public SimpleInvocationHandler(Object JavaDoc o) {
36             this.o = o;
37         }
38         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc m, Object JavaDoc[] args) throws Throwable JavaDoc {
39             System.out.println("invoking " + m + " on " + o + " with " + args);
40             Object JavaDoc r = m.invoke(o, args);
41             System.out.println("done: " + m + " on " + o + " with " + args + ", result is " + r);
42             return r;
43         }
44      }
45
46     public void testGetProxyClassAndConstructor() throws Exception JavaDoc {
47         HashMap JavaDoc map = new HashMap JavaDoc();
48         map.put("test", "test");
49         InvocationHandler handler = new SimpleInvocationHandler(map);
50         Class JavaDoc proxyClass = Proxy.getProxyClass(TestProxy.class.getClassLoader(), new Class JavaDoc[] { Map JavaDoc.class });
51         Map JavaDoc proxyMap = (Map JavaDoc) proxyClass.getConstructor(new Class JavaDoc[] { InvocationHandler.class }).
52             newInstance(new Object JavaDoc[] { handler });
53         assertEquals("proxy delegation not correct",
54                             map.get("test"), proxyMap.get("test"));
55     }
56
57     public void testGetProxyInstance() throws Exception JavaDoc {
58         HashMap JavaDoc map = new HashMap JavaDoc();
59         map.put("test", "test");
60         InvocationHandler handler = new SimpleInvocationHandler(map);
61         Map JavaDoc proxyMap = (Map JavaDoc) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class JavaDoc[] { Map JavaDoc.class }, handler);
62         assertEquals("proxy delegation not correct", map.get("test"), proxyMap.get("test"));
63     }
64
65     public void testIsProxyClass() throws Exception JavaDoc {
66         HashMap JavaDoc map = new HashMap JavaDoc();
67         map.put("test", "test");
68         InvocationHandler handler = new SimpleInvocationHandler(map);
69         Map JavaDoc proxyMap = (Map JavaDoc) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class JavaDoc[] { Map JavaDoc.class }, handler);
70         assertTrue("real proxy not accepted", Proxy.isProxyClass(proxyMap.getClass()));
71     }
72
73     private class FakeProxy extends Proxy {
74         public FakeProxy(InvocationHandler ih) {
75             super(ih);
76         }
77     }
78
79     public void testIsNotProxyClass() throws Exception JavaDoc {
80         assertTrue("fake proxy accepted as real",
81                         !Proxy.isProxyClass(FakeProxy.class));
82     }
83
84     private static class ReturnNullHandler implements InvocationHandler {
85         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
86             return null;
87         }
88     }
89
90     public void testReturnNull() throws Exception JavaDoc {
91         System.err.println("hello");
92         ProxySampleInterface_ReturnsBasic rb =
93             (ProxySampleInterface_ReturnsBasic)
94             Proxy.newProxyInstance(null,
95                                    new Class JavaDoc[]{ ProxySampleInterface_ReturnsBasic.class },
96                                    new ReturnNullHandler());
97         try {
98             int result = rb.getKala(11);
99             fail("must throw an exception, but returned " + result);
100         } catch (NullPointerException JavaDoc ignore) { }
101     }
102
103     public void testGetInvocationHandler() throws Exception JavaDoc {
104         HashMap JavaDoc map = new HashMap JavaDoc();
105         map.put("test", "test");
106         InvocationHandler handler = new InvocationHandler() {
107             public Object JavaDoc invoke(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc {
108                 throw new Exception JavaDoc("test!");
109             }
110         };
111         Map JavaDoc proxyMap = (Map JavaDoc) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class JavaDoc[] { Map JavaDoc.class }, handler);
112         assertSame("should be the same handler", handler, Proxy.getInvocationHandler(proxyMap));
113     }
114
115     public void testException() throws Exception JavaDoc {
116         HashMap JavaDoc map = new HashMap JavaDoc();
117         map.put("test", "test");
118         InvocationHandler handler = new InvocationHandler() {
119             public Object JavaDoc invoke(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc {
120                 throw new Exception JavaDoc("test!");
121             }
122         };
123         Map JavaDoc proxyMap = (Map JavaDoc) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class JavaDoc[] { Map JavaDoc.class }, handler);
124         try {
125             proxyMap.get("test"); //should throw exception
126
fail("proxy exception handling not correct, should throw exception");
127         } catch (UndeclaredThrowableException e) {
128             System.out.println("exception: " + e);
129         } catch (Exception JavaDoc e) {
130             fail("proxy exception handling not correct, threw wrong exception: " + e);
131         }
132     }
133
134     public void testEquals() throws Exception JavaDoc {
135         final Object JavaDoc k1 = new Object JavaDoc();
136         final Object JavaDoc k2 = new Object JavaDoc();
137         InvocationHandler handler = new InvocationHandler() {
138             public Object JavaDoc invoke(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc {
139                 if (method.getName().equals("equals")) {
140                     return (args[0] == k1) ? Boolean.TRUE : Boolean.FALSE;
141                 }
142                 return null;
143             }
144         };
145         Object JavaDoc proxy = Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class JavaDoc[] { Map JavaDoc.class }, handler);
146         assertTrue(proxy.equals(k1));
147         assertTrue(!proxy.equals(k2));
148     }
149
150     public TestProxy(String JavaDoc testName) {
151         super(testName);
152     }
153     
154     public static void main(String JavaDoc[] args) {
155         junit.textui.TestRunner.run(suite());
156     }
157     
158     public static Test suite() {
159         return new TestSuite(TestProxy.class);
160     }
161     
162     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
163          InvocationHandler handler = new InvocationHandler() {
164             public Object JavaDoc invoke(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc {
165                 throw new Exception JavaDoc("test!");
166             }
167         };
168         Proxy.newProxyInstance(loader, new Class JavaDoc[] { Map JavaDoc.class }, handler);
169        
170     }
171     
172     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
173         if(leaks()){
174          fail("Memory Leak in Proxy");
175         }
176     }
177     
178 }
179
Popular Tags