1   /*
2    * CGLIBTest.java
3    * Created on 9 Май 2006 г., 1:09
4    */
5   
6   package org.weda;
7   
8   import java.lang.reflect.Method;
9   import junit.framework.TestCase;
10  import net.sf.cglib.proxy.Callback;
11  import net.sf.cglib.proxy.CallbackFilter;
12  import net.sf.cglib.proxy.Enhancer;
13  import net.sf.cglib.proxy.Factory;
14  import net.sf.cglib.proxy.MethodInterceptor;
15  import net.sf.cglib.proxy.MethodProxy;
16  import net.sf.cglib.proxy.NoOp;
17  
18  /**
19   *
20   * @author Mikhail Titov
21   */
22  public class CGLIBTest extends TestCase implements MethodInterceptor {
23      
24      public CGLIBTest(String name) {
25          super(name);
26      }
27      
28      public void test() throws Exception {
29          Enhancer en = new Enhancer();
30          en.setSuperclass(AbstractHelloWorld.class);
31          en.setCallbacks(new Callback[]{this, NoOp.INSTANCE});
32          en.setCallbackFilter(new CallbackFilter(){
33              public int accept(Method method) {                
34                  return method.getName().equals("getGreeting") ? 0 : 1;
35              }            
36          });
37          AbstractHelloWorld helloWorld = (AbstractHelloWorld)en.create();
38          assertEquals("hello world", helloWorld.getGreeting());
39          assertEquals("hello world2", helloWorld.getGreeting2());
40          //perfomance test
41          HelloWorld helloWorld2=new HelloWorld();
42          String res = null;
43          long len = 0;
44          long start = System.currentTimeMillis();
45          for (int i=0; i<1000000; i++){
46              res = helloWorld2.getGreeting2();
47              len=+res.length();
48          }
49          long finish = System.currentTimeMillis();
50          assertEquals("hello world2", res);
51          assertTrue(len>1);
52          System.out.println("Normal object execution time: "+(finish-start));
53          //
54          len = 0;
55          start = System.currentTimeMillis();
56          for (int i=0; i<1000000; i++){
57              res = helloWorld.getGreeting2();
58              len=+res.length();
59          }
60          finish = System.currentTimeMillis();
61          assertEquals("hello world2", res);
62          assertTrue(len>1);
63          System.out.println("Proxied object execution time: "+(finish-start));
64      }
65      
66      public void test2() throws Exception {
67          Enhancer en = new Enhancer();
68          en.setSuperclass(AbstractHelloWorld.class);
69          //en.setCallbacks(new Callback[]{this, NoOp.INSTANCE});
70          en.setCallbackFilter(new CallbackFilter(){
71              public int accept(Method method) {                
72                  return method.getName().equals("getGreeting") ? 0 : 1;
73              }            
74          });
75          en.setCallbackTypes(new Class[]{this.getClass(), NoOp.class});
76          Class clazz = en.createClass();
77          //assertEquals(AbstractHelloWorld.class, clazz);
78          AbstractHelloWorld helloWorld = (AbstractHelloWorld)clazz.newInstance();
79          ((Factory)helloWorld).setCallbacks(new Callback[]{this, NoOp.INSTANCE});
80          assertEquals("hello world", helloWorld.getGreeting());
81          assertEquals("hello world2", helloWorld.getGreeting2());
82          //perfomance test
83          HelloWorld helloWorld2=new HelloWorld();
84          String res = null;
85          long len = 0;
86          long start = System.currentTimeMillis();
87          for (int i=0; i<1000000; i++){
88              res = helloWorld2.getGreeting2();
89              len=+res.length();
90          }
91          long finish = System.currentTimeMillis();
92          assertEquals("hello world2", res);
93          assertTrue(len>1);
94          System.out.println("Normal object execution time: "+(finish-start));
95          //
96          len = 0;
97          start = System.currentTimeMillis();
98          for (int i=0; i<1000000; i++){
99              res = helloWorld.getGreeting2();
100             len=+res.length();
101         }
102         finish = System.currentTimeMillis();
103         assertEquals("hello world2", res);
104         assertTrue(len>1);
105         System.out.println("Proxied object execution time: "+(finish-start));
106     }
107 
108     public Object intercept(
109             Object object, Method method, Object[] object0
110             , MethodProxy methodProxy) 
111         throws Throwable 
112     {
113         return "hello world";
114     }
115     
116 }