KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 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 java.lang.reflect.*;
19 import junit.framework.*;
20 import net.sf.cglib.CodeGenTestCase;
21
22 public class TestInterfaceMaker extends CodeGenTestCase
23 {
24     public void testStandalone() throws Exception JavaDoc {
25         InterfaceMaker im = new InterfaceMaker();
26         im.add(D1.class);
27         im.add(D2.class);
28         Class JavaDoc iface = im.create();
29         Method[] methods = iface.getMethods();
30         assertTrue(methods.length == 2);
31         String JavaDoc name1 = methods[0].getName();
32         String JavaDoc name2 = methods[1].getName();
33         assertTrue(("herby".equals(name1) && "derby".equals(name2)) ||
34                    ("herby".equals(name2) && "derby".equals(name1)));
35     }
36
37     public void testEnhancer() throws Exception JavaDoc {
38         InterfaceMaker im = new InterfaceMaker();
39         im.add(D1.class);
40         im.add(D2.class);
41         Class JavaDoc iface = im.create();
42         Object JavaDoc obj = Enhancer.create(Object JavaDoc.class, new Class JavaDoc[]{ iface }, new MethodInterceptor() {
43             public Object JavaDoc intercept(Object JavaDoc obj, Method method, Object JavaDoc[] args, MethodProxy proxy) {
44                 return "test";
45             }
46         });
47         Method method = obj.getClass().getMethod("herby", null);
48         assertTrue("test".equals(method.invoke(obj, null)));
49     }
50
51     public TestInterfaceMaker(String JavaDoc testName) {
52         super(testName);
53     }
54     
55     public static Test suite() {
56         return new TestSuite(TestInterfaceMaker.class);
57     }
58     
59     public static void main(String JavaDoc args[]) {
60         String JavaDoc[] testCaseName = {TestInterfaceMaker.class.getName()};
61         junit.textui.TestRunner.main(testCaseName);
62     }
63     
64     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
65     }
66     
67     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
68     }
69     
70 }
71
Popular Tags