aboutsummaryrefslogtreecommitdiff
path: root/src/test/testproxy/ProxyTester.java
blob: 64712ef3919248e985051f0cede77a25da59288e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package testproxy;

import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

import org.junit.Assert;

import java.lang.reflect.InvocationTargetException;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyObject;
import javassist.util.proxy.Proxy;
import junit.framework.TestCase;
import java.io.*;

@SuppressWarnings({"unchecked", "rawtypes","unused"})
public class ProxyTester extends TestCase {
    public ProxyTester(String s) {
        super(s);
    }

    public ProxyTester() {
        this("proxy");
    }

    static class Interceptor1 implements MethodHandler {
        int counter = 0;

        public Object invoke(Object self, Method m, Method proceed,
                Object[] args) throws Exception {
            System.out.println("intercept: " + m + ", proceed: " + proceed);
            System.out.println("     modifier: "
                               + Modifier.toString(proceed.getModifiers()));
            counter++;
            return proceed.invoke(self, args);
        }
    }

    static class Interceptor2 implements MethodHandler {
        int counter = 0;
        public Object invoke(Object self, Method m, Method proceed,
                             Object[] args) throws Exception {
            System.out.println("intercept: " + m + ", proceed: " + proceed);
            counter++;
            if (proceed != null)
                return proceed.invoke(self, args);
            else
                if (m.getReturnType() == int.class)
                    return Integer.valueOf(3);
                else
                    return "OK";
        }
    }

    static MethodFilter finalizeRemover = new MethodFilter() {
        public boolean isHandled(Method m) {
            return !m.getName().equals("finalize");
        }
    };

    public void testTarget() throws Exception {
        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(Target.class);
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        f.writeDirectory = ".";
        Class c = f.createClass();
        Target obj = (Target)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor);
        obj.m();
        assertEquals(true, obj.m(true));
        assertEquals((byte)1, obj.m1((byte)1));
        assertEquals('a', obj.m2('a'));
        assertEquals((short)2, obj.m3((short)2));
        assertEquals(3, obj.m(3));
        assertEquals(4L, obj.m5(4L));
        assertTrue(5.0F == obj.m6(5.0F));
        assertTrue(6.0 == obj.m7(6.0));
        assertEquals("test", obj.m("test"));
        int[] ia = { 1, 2, 3 };
        assertEquals(ia, obj.m7(ia));
        String[] sa = { "1", "2" };
        assertEquals(sa, obj.m8(sa));
        assertEquals(obj, obj.m9(3, obj, null));
        assertEquals(14, interceptor.counter);
    }

    public void testTarget1() throws Exception {
        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(Target1.class);
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        Class c = f.createClass();
        Target1 obj = (Target1)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor);
        assertEquals(null, obj.m(null));
        assertEquals(1, interceptor.counter);
    }

    public void testObject() throws Exception {
        ProxyFactory f = new ProxyFactory();
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        Class c = f.createClass();
        Object obj = (Object)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor);
        System.out.println(obj.toString());
        assertEquals(2, interceptor.counter);
    }

    public void testSetter() throws Exception {
        ProxyFactory f = new ProxyFactory();
        f.writeDirectory = ".";
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        Class c = f.createClass();
        Object obj = (Object)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor);
        System.out.println("setter1: " + obj.toString());
        ((ProxyObject)obj).setHandler(new MethodHandler() {
            public Object invoke(Object self, Method m, Method proceed,
                    Object[] args) throws Exception {
                System.out.print("intercept: " + m);
                return "OK";
            }
        });
        assertEquals("OK", obj.toString());
    }

    public void testString() throws Exception {
        ProxyFactory f = new ProxyFactory();
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        f.setSuperclass(String.class);
        try {
            Class c = f.createClass();
            Assert.fail("String is final!");
        }
        catch (RuntimeException e) {
            System.out.println(e);
        }
    }

    public void testConstructor() throws Exception {
        ProxyFactory f = new ProxyFactory();
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        f.setSuperclass(Target2.class);
        Class c = f.createClass();
        Constructor[] cons = c.getDeclaredConstructors();
        assertEquals(3, cons.length);
        Constructor con1 = c.getDeclaredConstructor(new Class[] { int.class });
        Constructor con2 = c.getDeclaredConstructor(new Class[] { int.class, int.class });
        Method m1 = c.getDeclaredMethod("get", new Class[0]);
        Method m2 = c.getDeclaredMethod("foo", new Class[0]);
        assertEquals(0, m1.getExceptionTypes().length);
        assertEquals("java.io.IOException", m2.getExceptionTypes()[0].getName());

        Target2 t2 = (Target2)con1.newInstance(new Object[] { Integer.valueOf(1) });
        ((Proxy)t2).setHandler(interceptor);
        System.out.println(t2.toString());
        assertEquals(2, interceptor.counter);

        interceptor.counter = 0;
        assertEquals(2, t2.foo());
        assertEquals(4, t2._dfoo());
        assertEquals(2, interceptor.counter);
    }

    public void testInterface() throws Exception {
        ProxyFactory f = new ProxyFactory();
        Interceptor2 interceptor2 = new Interceptor2();
        // f.setHandler(interceptor2);
        f.setFilter(finalizeRemover);
        f.setInterfaces(new Class[] { Target3.class });
        Class c = f.createClass();
        Target3 obj = (Target3)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor2);
        assertEquals("OK", obj.m());
        System.out.println(obj.toString());
        assertEquals(3, interceptor2.counter);
    }

    public void test2Interfaces() throws Exception {
        ProxyFactory f = new ProxyFactory();
        Interceptor2 interceptor2 = new Interceptor2();
        // f.setHandler(interceptor2);
        f.setFilter(finalizeRemover);
        f.setInterfaces(new Class[] { Target3.class, Target4.class });
        Class c = f.createClass();
        Target3 obj = (Target3)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor2);
        assertEquals("OK", obj.m());
        System.out.println(obj.toString());
        assertEquals(3, interceptor2.counter);

        interceptor2.counter = 0;
        Target4 obj4 = (Target4)c.getConstructor().newInstance();
        ((Proxy)obj4).setHandler(interceptor2);
        assertEquals(3, obj4.bar4());
        assertEquals(3, obj4.foo4());
        assertEquals(2, interceptor2.counter);
    }

    public void testFilter() throws Exception {
        ProxyFactory f = new ProxyFactory();
        Interceptor2 interceptor2 = new Interceptor2();
        // f.setHandler(interceptor2);
        f.setFilter(finalizeRemover);
        f.setInterfaces(new Class[] { Target3.class });
        f.setFilter(new MethodFilter() {
                public boolean isHandled(Method m) {
                    return m.getDeclaringClass() != Object.class;
                }
            });
        Class c = f.createClass();
        Target3 obj = (Target3)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor2);
        assertEquals("OK", obj.m());
        System.out.println(obj.toString());
        assertEquals(1, interceptor2.counter);
    }

    public static boolean testInitFlag;

    public void testInit() throws Exception {
        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(TargetInit.class);
        MethodHandler handler = new MethodHandler() {
            public Object invoke(Object self, Method m,
                    Method proceed, Object[] args) throws Exception {
                System.out.println("testInit " + testInitFlag);
                return proceed.invoke(self, args);
            }
        };
        testInitFlag = false;
        Class c = f.createClass();
        assertTrue(testInitFlag); // since 3.12.  Before then, this line was assertFalse(testInitFlag);
        System.out.println("testInit createClass(): " + testInitFlag);
        TargetInit obj = (TargetInit)c.getConstructor().newInstance();
        assertTrue(testInitFlag);
        System.out.println("testInit newInstance(): " + testInitFlag);
        ((ProxyObject)obj).setHandler(handler);
        assertEquals("OK", obj.m());
    }

    public void testCreate() throws Exception {
        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(Target5.class);
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        Class c = f.createClass();
        Target5 obj = (Target5)f.create(new Class[] { int.class }, new Object[] { Integer.valueOf(3) });
        ((Proxy)obj).setHandler(interceptor);
        assertEquals(3, obj.get());
    }


    public void testBridgeMethod() throws Exception {
        ProxyFactory f = new ProxyFactory();
        f.writeDirectory = ".";
        f.setSuperclass(BridgeMethod.class);
        Interceptor1 interceptor = new Interceptor1();
        // f.setHandler(interceptor);
        f.setFilter(finalizeRemover);
        Class c = f.createClass();
        BridgeMethod obj = (BridgeMethod)c.getConstructor().newInstance();
        ((Proxy)obj).setHandler(interceptor);
        Integer value = obj.m1();
        assertEquals(7, value.intValue());
        BridgeMethodInf inf = (BridgeMethodInf)obj;
        Number num = inf.m1();
        assertEquals(7, num.intValue());
        BridgeMethodSuper sup = obj;
        try {
            Object x = sup.id(new Object());
            fail("not cast error");
        }
        catch (ClassCastException e) {}
        catch (Exception e) {
            if (e instanceof InvocationTargetException)
                if (e.getCause() instanceof ClassCastException)
                    return;

            throw e;
        }
    }

    public void testGetters() throws Exception {
        ProxyFactory f = new ProxyFactory();
        Class c = ProxyTester.class;
        f.setSuperclass(c);
        assertEquals(c, f.getSuperclass());
        Class i = java.io.Serializable.class;
        f.setInterfaces(new Class[] { i });
        assertEquals(i, f.getInterfaces()[0]);
    }

    static class ProxyFactory2 extends ProxyFactory {
        public ClassLoader getClassLoader2() {
            return getClassLoader();
        }
    }

    public void testProvider() throws Exception {
        ProxyFactory.ClassLoaderProvider cp = ProxyFactory.classLoaderProvider;
        try {
            final ClassLoader cl = Thread.currentThread().getContextClassLoader();
            ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() {
                public ClassLoader get(ProxyFactory pf) {
                    return Thread.currentThread().getContextClassLoader();
                }
            };

            ProxyFactory2 pf = new ProxyFactory2();
            assertEquals(cl, pf.getClassLoader2());
        }
        finally {
            ProxyFactory.classLoaderProvider = cp;
        }
    }

    @SuppressWarnings("deprecation")
	public void testCache() throws Exception {
        boolean prev = ProxyFactory.useCache;
        ProxyFactory.useCache = true;
        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(Cache1.class);
        Class c = f.createClass();
        ProxyFactory f2 = new ProxyFactory();
        f2.setSuperclass(Cache1.class);
        assertEquals(c, f2.createClass());
        ProxyFactory f3 = new ProxyFactory();
        f3.setSuperclass(Cache1.class);
        f3.setHandler(new Interceptor1());	// deprecated
        assertFalse(c == f3.createClass());
        ProxyFactory.useCache = true;
        ProxyFactory f4 = new ProxyFactory();
        f4.setSuperclass(Cache1.class);
        f4.setInterfaces(new Class[] { Cache2.class });
        Class c4 = f4.createClass();
        assertFalse(c == c4);
        ProxyFactory f5 = new ProxyFactory();
        f5.setSuperclass(Cache1.class);
        f5.setInterfaces(new Class[] { Cache2.class });
        assertEquals(c4, f5.createClass());
        ProxyFactory f6 = new ProxyFactory();
        f6.setInterfaces(new Class[] { Cache2.class });
        assertFalse(c4 == f6.createClass());
        ProxyFactory.useCache = prev;
    }

    public static class Cache1 {
        public int foo() { return 0; }
    }

    public static interface Cache2 {
        public int bar();
    }

    public void testReadWrite() throws Exception {
        final String fileName = "read-write.bin";
        ProxyFactory.ClassLoaderProvider cp = ProxyFactory.classLoaderProvider;
        try {
            ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() {
                public ClassLoader get(ProxyFactory pf) {
                    /* If javassist.Loader is returned, the super type of ReadWriteData class,
                     * which is Serializable, is loaded by javassist.Loader as well as ReadWriteData.
                     * This breaks the implementation of the object serializer.
                     */
                    // return new javassist.Loader();
                    return Thread.currentThread().getContextClassLoader();
                }
            };
            ProxyFactory pf = new ProxyFactory();
            pf.setSuperclass(ReadWriteData.class);
            Object data = pf.createClass().getConstructor().newInstance();
            //Object data = new ReadWriteData();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
            oos.writeObject(data);
            oos.close();
        }
        finally {
            ProxyFactory.classLoaderProvider = cp;
        }

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
        Object data2 = ois.readObject();
        ois.close();
        int i = ((ReadWriteData)data2).foo();
        assertEquals(4, i);
    }

    public static class ReadWriteData implements Serializable {
        /** default serialVersionUID */
        private static final long serialVersionUID = 1L;

        public int foo() { return 4; }
    }

    public void testWriteReplace() throws Exception {
        ProxyFactory pf = new ProxyFactory();
        pf.setSuperclass(WriteReplace.class);
        Object data = pf.createClass().getConstructor().newInstance();
        assertEquals(data, ((WriteReplace)data).writeReplace());

        ProxyFactory pf2 = new ProxyFactory();
        pf2.setSuperclass(WriteReplace2.class);
        Object data2 = pf2.createClass().getConstructor().newInstance();
        Method meth = data2.getClass().getDeclaredMethod("writeReplace", new Class[0]);
        assertEquals("javassist.util.proxy.SerializedProxy",
                    meth.invoke(data2, new Object[0]).getClass().getName());
    }

    public static class WriteReplace implements Serializable {
        /** default serialVersionUID */
        private static final long serialVersionUID = 1L;

        public Object writeReplace() { return this; }
    }

    public static class WriteReplace2 implements Serializable {
        /** default serialVersionUID */
        private static final long serialVersionUID = 1L;

        public Object writeReplace(int i) { return Integer.valueOf(i); }
    }

    public static void testJIRA189() throws Exception {
        Class persistentClass = Target189.PublishedArticle.class;
        ProxyFactory factory = new ProxyFactory();
        //factory.writeDirectory = ".";
        factory.setUseCache(false);
        factory.setSuperclass(persistentClass);
        factory.setInterfaces(new Class[] { Target189.TestProxy.class });
        Class cl = factory.createClass();
        Object obj = cl.getConstructor().newInstance();
        System.out.println("JIRA189:" + obj.getClass().getClassLoader() + ", " + obj.getClass().getSuperclass().getName()
                            + ", " + Target189.PublishedArticle.class.getClassLoader());
        Target189.TestProxy proxy = (Target189.TestProxy)cl.getConstructor().newInstance();
        Target189.TestMethodHandler methodHandler = new Target189.TestMethodHandler();
        ((ProxyObject)proxy).setHandler(methodHandler);
        ((Target189.Article)proxy).getIssue();
        assertTrue(methodHandler.wasInvokedOnce());
        methodHandler.reset();
        Target189.PublishedArticle article = (Target189.PublishedArticle)proxy;
        article.getIssue();
        assertTrue(methodHandler.wasInvokedOnce());
    }

    public void testJIRA127() throws Exception {
        ProxyFactory proxyFactory = new ProxyFactory();
        // proxyFactory.writeDirectory = ".";
        proxyFactory.setInterfaces(new Class[]{ Target127.Sub.class });
        Target127.Sub proxy = (Target127.Sub)proxyFactory.create(new Class[0], new Object[0], new MethodHandler() {
            public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
                return null;
            }
        });
        ((Target127.Super)proxy).item();    // proxyFactory must generate a bridge method.
        ((Target127.Sub)proxy).item();
    }

    public static void main(String[] args) {
        // javassist.bytecode.ClassFile.MAJOR_VERSION = javassist.bytecode.ClassFile.JAVA_6;
        junit.textui.TestRunner.run(ProxyTester.class);
    }
}