Basic.java revision 0
0N/A/*
0N/A * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary Verifies basic correct functioning of proxy serialization.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.lang.reflect.*;
0N/Aimport java.util.Random;
0N/A
0N/A// proxy interfaces
0N/Ainterface Foo { int foo(); }
0N/Ainterface Bar { float bar(); }
0N/A
0N/A// dummy invocation handler
0N/Aclass Handler implements InvocationHandler, Serializable {
0N/A
0N/A static Method fooMethod, barMethod;
0N/A static {
0N/A try {
0N/A fooMethod = Foo.class.getDeclaredMethod("foo", new Class[0]);
0N/A barMethod = Bar.class.getDeclaredMethod("bar", new Class[0]);
0N/A } catch (NoSuchMethodException ex) {
0N/A throw new Error();
0N/A }
0N/A }
0N/A
0N/A int foo;
0N/A float bar;
0N/A
0N/A Handler(int foo, float bar) {
0N/A this.foo = foo;
0N/A this.bar = bar;
0N/A }
0N/A
0N/A public Object invoke(Object proxy, Method method, Object[] args)
0N/A throws Throwable
0N/A {
0N/A if (method.equals(fooMethod)) {
0N/A return new Integer(foo);
0N/A } else if (method.equals(barMethod)) {
0N/A return new Float(bar);
0N/A } else {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A }
0N/A}
0N/A
0N/A// ObjectInputStream incapable of resolving proxy classes
0N/Aclass ProxyBlindInputStream extends ObjectInputStream {
0N/A
0N/A ProxyBlindInputStream(InputStream in) throws IOException { super(in); }
0N/A
0N/A protected Class resolveProxyClass(String[] interfaces)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A throw new ClassNotFoundException();
0N/A }
0N/A}
0N/A
0N/Apublic class Basic {
0N/A public static void main(String[] args) throws Exception {
0N/A ClassLoader loader = ClassLoader.getSystemClassLoader();
0N/A Class[] interfaces = new Class[] { Foo.class, Bar.class };
0N/A Random rand = new Random();
0N/A int foo = rand.nextInt();
0N/A float bar = rand.nextFloat();
0N/A ObjectOutputStream oout;
0N/A ObjectInputStream oin;
0N/A ByteArrayOutputStream bout;
0N/A Object proxy;
0N/A
0N/A
0N/A // test simple proxy write + read
0N/A bout = new ByteArrayOutputStream();
0N/A oout = new ObjectOutputStream(bout);
0N/A oout.writeObject(Proxy.newProxyInstance(
0N/A loader, interfaces, new Handler(foo, bar)));
0N/A oout.close();
0N/A
0N/A oin = new ObjectInputStream(
0N/A new ByteArrayInputStream(bout.toByteArray()));
0N/A proxy = oin.readObject();
0N/A if ((((Foo) proxy).foo() != foo) || (((Bar) proxy).bar() != bar)) {
0N/A throw new Error();
0N/A }
0N/A
0N/A
0N/A // test missing proxy class ClassNotFoundException
0N/A oin = new ProxyBlindInputStream(
0N/A new ByteArrayInputStream(bout.toByteArray()));
0N/A try {
0N/A oin.readObject();
0N/A throw new Error();
0N/A } catch (ClassNotFoundException ex) {
0N/A }
0N/A }
0N/A}