1 /*
   2  * Copyright (c) 1998, 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 4010355
  26  * @summary RemoteException should have server's stack trace
  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 ClientStackTrace MyRemoteObject_Stub
  36  * @run main/othervm/policy=security.policy/timeout=120 ClientStackTrace
  37  */
  38 
  39 /*
  40  * This test ensures that the stack trace in a caught server side
  41  * remote exception contains the string "exceptionReceivedFromServer".
  42  */
  43 
  44 import java.rmi.*;
  45 import java.rmi.server.*;
  46 import sun.rmi.transport.StreamRemoteCall;
  47 import java.io.ByteArrayOutputStream;
  48 import java.io.PrintStream;
  49 
  50 interface MyRemoteInterface extends Remote {
  51     void ping() throws RemoteException;
  52 }
  53 
  54 class MyRemoteObject extends UnicastRemoteObject
  55     implements MyRemoteInterface {
  56 
  57     public MyRemoteObject () throws RemoteException {}
  58 
  59     public void ping () throws RemoteException {
  60         throw new RemoteException("This is a test remote exception");
  61     }
  62 }
  63 
  64 public class ClientStackTrace {
  65     Object dummy = new Object();
  66 
  67     public static void main(String[] args) {
  68 
  69         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  70 
  71         Object dummy = new Object();
  72         MyRemoteObject myRobj = null;
  73         MyRemoteInterface myStub = null;
  74 
  75         try {
  76             ByteArrayOutputStream bos = new ByteArrayOutputStream();
  77             PrintStream ps = new PrintStream(bos);
  78 
  79             System.err.println("\nRegression test for bug 4010355\n");
  80 
  81             myRobj = new MyRemoteObject();
  82 
  83             /* cause a remote exception to occur. */
  84             try {
  85                 myStub = (MyRemoteInterface) RemoteObject.toStub(myRobj);
  86                 myStub.ping();
  87 
  88             } catch (RemoteException re) {
  89                 re.printStackTrace(ps);
  90                 String trace = bos.toString();
  91 
  92                 if (trace.indexOf("exceptionReceivedFromServer") <0 ) {
  93                     throw new RuntimeException("No client stack trace on " +
  94                                                "thrown remote exception");
  95                 } else {
  96                     System.err.println("test passed with stack trace: " +
  97                                        trace);
  98                 }
  99             }
 100 
 101             deactivate(myRobj);
 102 
 103         } catch (Exception e) {
 104             e.printStackTrace();
 105             System.err.println("test failed");
 106             throw new RuntimeException(e.getMessage());
 107         } finally {
 108             myRobj = null;
 109             myStub = null;
 110         }
 111     }
 112 
 113     // make sure that the remote object goes away.
 114     static void deactivate(RemoteServer r) {
 115         // make sure that the object goes away
 116         try {
 117             System.err.println("deactivating object.");
 118             UnicastRemoteObject.unexportObject(r, true);
 119         } catch (Exception e) {
 120             e.getMessage();
 121             e.printStackTrace();
 122         }
 123     }
 124 }