blob: d8ef3d65b1620b965777c6f946c3e1c1ae4986b8 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @summary Verifies basic correct functioning of proxy serialization.
26 */
27
28import java.io.*;
29import java.lang.reflect.*;
30import java.util.Random;
31
32// proxy interfaces
33interface Foo { int foo(); }
34interface Bar { float bar(); }
35
36// dummy invocation handler
37class Handler implements InvocationHandler, Serializable {
38
39 static Method fooMethod, barMethod;
40 static {
41 try {
42 fooMethod = Foo.class.getDeclaredMethod("foo", new Class[0]);
43 barMethod = Bar.class.getDeclaredMethod("bar", new Class[0]);
44 } catch (NoSuchMethodException ex) {
45 throw new Error();
46 }
47 }
48
49 int foo;
50 float bar;
51
52 Handler(int foo, float bar) {
53 this.foo = foo;
54 this.bar = bar;
55 }
56
57 public Object invoke(Object proxy, Method method, Object[] args)
58 throws Throwable
59 {
60 if (method.equals(fooMethod)) {
61 return new Integer(foo);
62 } else if (method.equals(barMethod)) {
63 return new Float(bar);
64 } else {
65 throw new UnsupportedOperationException();
66 }
67 }
68}
69
70// ObjectInputStream incapable of resolving proxy classes
71class ProxyBlindInputStream extends ObjectInputStream {
72
73 ProxyBlindInputStream(InputStream in) throws IOException { super(in); }
74
75 protected Class resolveProxyClass(String[] interfaces)
76 throws IOException, ClassNotFoundException
77 {
78 throw new ClassNotFoundException();
79 }
80}
81
82public class Basic {
83 public static void main(String[] args) throws Exception {
84 ClassLoader loader = ClassLoader.getSystemClassLoader();
85 Class[] interfaces = new Class[] { Foo.class, Bar.class };
86 Random rand = new Random();
87 int foo = rand.nextInt();
88 float bar = rand.nextFloat();
89 ObjectOutputStream oout;
90 ObjectInputStream oin;
91 ByteArrayOutputStream bout;
92 Object proxy;
93
94
95 // test simple proxy write + read
96 bout = new ByteArrayOutputStream();
97 oout = new ObjectOutputStream(bout);
98 oout.writeObject(Proxy.newProxyInstance(
99 loader, interfaces, new Handler(foo, bar)));
100 oout.close();
101
102 oin = new ObjectInputStream(
103 new ByteArrayInputStream(bout.toByteArray()));
104 proxy = oin.readObject();
105 if ((((Foo) proxy).foo() != foo) || (((Bar) proxy).bar() != bar)) {
106 throw new Error();
107 }
108
109
110 // test missing proxy class ClassNotFoundException
111 oin = new ProxyBlindInputStream(
112 new ByteArrayInputStream(bout.toByteArray()));
113 try {
114 oin.readObject();
115 throw new Error();
116 } catch (ClassNotFoundException ex) {
117 }
118 }
119}