1 /*
   2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4152295
  26  * @summary trivial optimization in RemoteProxy.extendsRemote
  27  *
  28  * @author Laird Dornin
  29  *
  30  * @library ../../testlibrary
  31  * @modules java.rmi/sun.rmi.registry
  32  *          java.rmi/sun.rmi.server
  33  *          java.rmi/sun.rmi.transport
  34  *          java.rmi/sun.rmi.transport.tcp
  35  * @build TestLibrary
  36  * @run main/othervm GetRemoteClass
  37  */
  38 
  39 import java.io.Serializable;
  40 import java.rmi.Remote;
  41 import sun.rmi.server.Util;
  42 import java.lang.reflect.*;
  43 
  44 
  45 /**
  46  * Simple test to make sure that Util.getRemoteClass throws
  47  * exception in proper circumstances.  Pass two different classes, one
  48  * that extends Remote and one that only extends Serializable.  Should
  49  * throw exception for Serializable.
  50  */
  51 public class GetRemoteClass {
  52     public interface ExtendRemote extends Remote {
  53     }
  54 
  55     public static class TestRemote implements ExtendRemote {
  56     }
  57 
  58     public static class OnlySerializable implements Serializable {
  59     }
  60 
  61     public static void main(String[] argv) {
  62 
  63         System.err.println("\nregression test for 4152295\n");
  64 
  65         Method utilGetRemoteClass = null;
  66 
  67         try {
  68             /**
  69              * Use reflection to set access overrides so that the test
  70              * can call protected method, getRemoteClass
  71              */
  72             Class[] args = new Class[1];
  73             args[0] = Class.class;
  74             utilGetRemoteClass =
  75                 Util.class.getDeclaredMethod("getRemoteClass", args);
  76             utilGetRemoteClass.setAccessible(true);
  77 
  78             // getRemoteClass can be invoked without exception
  79             utilGetRemoteClass.invoke
  80                  (null , new Object [] {TestRemote.class});
  81             System.err.println("remote class flagged as remote");
  82 
  83             ClassNotFoundException cnfe = null;
  84             try {
  85                 // getRemoteClass can be invoked without exception
  86                 utilGetRemoteClass.invoke
  87                     (null , new Object [] {OnlySerializable.class});
  88             } catch (InvocationTargetException e) {
  89                 System.err.println("got ClassNotFoundException; remote " +
  90                                    "class flagged as nonremote");
  91                 cnfe = (ClassNotFoundException) e.getTargetException();
  92             }
  93             if (cnfe == null) {
  94                 TestLibrary.bomb("Serializable class flagged as remote?");
  95             }
  96 
  97             System.err.println("Test passed.");
  98         } catch (Exception e) {
  99             TestLibrary.bomb("Unexpected exception", e);
 100         }
 101     }
 102 }