summaryrefslogtreecommitdiffstats
path: root/src/test/javassist/proxyfactory/ProxyFactoryTest.java
blob: 7224eb506fd24d3f5d298388ba12d3b9c35d3a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package javassist.proxyfactory;

import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
import junit.framework.TestCase;

import java.io.*;
import java.lang.reflect.Method;

/**
 * <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
 */
public class ProxyFactoryTest extends TestCase {


    public void testMethodHandlers() throws Exception {
        ProxyFactory fact = new ProxyFactory();
        fact.setSuperclass(MyCls.class);

        Class proxyClass = fact.createClass();

        MyMethodHandler myHandler = new MyMethodHandler();
        myHandler.setX(4711);

        MyCls myCls = (MyCls) proxyClass.newInstance();
        ((ProxyObject) myCls).setHandler(myHandler);

        MethodHandler h2 = ((ProxyObject) myCls).getHandler();
        assertNotNull(h2);
        assertTrue(h2 instanceof MyMethodHandler);
    }

    public void testSerialize() throws Exception {
        ProxyFactory fact = new ProxyFactory();
        fact.setSuperclass(MyCls.class);

        Class proxyClass = fact.createClass();

        MyMethodHandler myHandler = new MyMethodHandler();
        myHandler.setX(4711);

        MyCls myCls = (MyCls) proxyClass.newInstance();
        ((ProxyObject) myCls).setHandler(myHandler);


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(myCls);
        byte[] ba = baos.toByteArray();

        ByteArrayInputStream bais = new ByteArrayInputStream(ba);
        ObjectInputStream ois = new ObjectInputStream(bais);
        MyCls myCls2 =  (MyCls) ois.readObject();

        MethodHandler h2 = ((ProxyObject) myCls2).getHandler();
        assertNotNull(h2);
        assertTrue(h2 instanceof MyMethodHandler);
    }

    
    public static class MyMethodHandler implements MethodHandler, Serializable {

        private int x;

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
            // actually do nothing!
            return null;
        }
    }

    public void testJira127() throws Exception {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setInterfaces(new Class[]{ JIRA127Sub.class });
        proxyFactory.createClass();
    }

    public interface JIRA127 {
        JIRA127 get();
    }
    public interface JIRA127Sub extends JIRA127 {
        JIRA127Sub get();
    }
    
}