Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/Activatable/notSerializable/NotSerializable.java
38829 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 446098325* @summary This test verifies that an instance of Activatable cannot26* be serialized (without implicit impl-to-stub replacement), because27* it cannot be meaningfully deserialized anyway.28* See also test/java/rmi/server/RemoteObject/unrecognizedRefType.29* @author Peter Jones30*31* @run main/othervm NotSerializable32*/3334import java.io.ByteArrayOutputStream;35import java.io.NotSerializableException;36import java.io.ObjectInput;37import java.io.ObjectOutputStream;38import java.io.ObjectOutput;39import java.lang.reflect.Method;40import java.rmi.MarshalledObject;41import java.rmi.NoSuchObjectException;42import java.rmi.Remote;43import java.rmi.RemoteException;44import java.rmi.server.Operation;45import java.rmi.server.RemoteCall;46import java.rmi.server.RemoteObject;47import java.rmi.server.RemoteRef;48import java.rmi.server.RemoteStub;49import java.rmi.activation.Activatable;50import java.rmi.activation.ActivationID;51import java.rmi.activation.Activator;5253public class NotSerializable {5455public static void main(String[] args) throws Exception {56System.err.println("\nRegression test for bug 4460983\n");5758Activatable act = new FakeActivatable();59try {60ObjectOutputStream out =61new ObjectOutputStream(new ByteArrayOutputStream());62try {63out.writeObject(act);64throw new RuntimeException("TEST FAILED: " +65"Activatable instance successfully serialized");66} catch (NotSerializableException e) {67System.err.println("NotSerializableException as expected:");68e.printStackTrace();69} // other exceptions cause test failure7071System.err.println("TEST PASSED");72} finally {73try {74Activatable.unexportObject(act, true);75} catch (NoSuchObjectException e) {76}77}78}7980private static class FakeActivatable extends Activatable {81FakeActivatable() throws RemoteException {82super(new ActivationID(new FakeActivator()), 0);83}84}8586private static class FakeActivator87extends RemoteStub implements Activator88{89FakeActivator() {90super(new FakeRemoteRef("FakeRef"));91}9293public MarshalledObject activate(ActivationID id, boolean force) {94return null;95}96}9798private static class FakeRemoteRef implements RemoteRef {99private final String refType;100101FakeRemoteRef(String refType) {102this.refType = refType;103}104105public Object invoke(Remote obj,106Method method,107Object[] params,108long opnum)109{110throw new UnsupportedOperationException();111}112113public RemoteCall newCall(RemoteObject obj,114Operation[] op,115int opnum,116long hash)117{118throw new UnsupportedOperationException();119}120121public void invoke(RemoteCall call) {122throw new UnsupportedOperationException();123}124125public void done(RemoteCall call) {126throw new UnsupportedOperationException();127}128129public String getRefClass(java.io.ObjectOutput out) {130return refType;131}132133public int remoteHashCode() { return hashCode(); }134public boolean remoteEquals(RemoteRef obj) { return equals(obj); }135public String remoteToString() { return toString(); }136137public void readExternal(ObjectInput in) {138throw new UnsupportedOperationException();139}140141public void writeExternal(ObjectOutput out) {142// no data to write143}144}145}146147148