1 /*
   2  * Copyright (c) 2016, 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 /*
  25  * @test
  26  * @bug 8146975
  27  * @summary test RMI-IIOP with value object return
  28  * @modules java.corba
  29  * @library /lib/testlibrary
  30  * @build jdk.testlibrary.*
  31  * @compile Test.java Test3.java Test4.java
  32  *    HelloInterface.java HelloServer.java
  33  *    HelloClient.java HelloImpl.java _HelloImpl_Tie.java _HelloInterface_Stub.java
  34  *    RmiIiopReturnValueTest.java
  35  * @run main/othervm
  36  *    -Djava.naming.provider.url=iiop://localhost:5050
  37  *    -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
  38  *    RmiIiopReturnValueTest -port 5049
  39  * @run main/othervm/secure=java.lang.SecurityManager/policy=jtreg.test.policy
  40  *    -Djava.naming.provider.url=iiop://localhost:5050
  41  *    -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
  42  *    RmiIiopReturnValueTest -port 5049
  43  */
  44 
  45 
  46 import java.util.ArrayList;
  47 import java.util.Arrays;
  48 import java.util.List;
  49 import jdk.testlibrary.JDKToolFinder;
  50 import jdk.testlibrary.JDKToolLauncher;
  51 
  52 public class RmiIiopReturnValueTest {
  53 
  54     static final String ORBD = JDKToolFinder.getTestJDKTool("orbd");
  55     static final String JAVA = JDKToolFinder.getTestJDKTool("java");
  56     static final JDKToolLauncher orbdLauncher = JDKToolLauncher.createUsingTestJDK("orbd");
  57     static final String CLASSPATH = System.getProperty("java.class.path");
  58     static final int FIVE_SECONDS = 5000;
  59 
  60     private static Throwable clientException;
  61     private static boolean exceptionInClient;
  62     private static Process orbdProcess;
  63     private static Process rmiServerProcess;
  64 
  65     public static void main(String[] args) throws Exception {
  66         startTestComponents();
  67         stopTestComponents();
  68         System.err.println("Test completed OK ");
  69     }
  70 
  71     static void startTestComponents () throws Exception {
  72         startOrbd();
  73         Thread.sleep(FIVE_SECONDS);
  74         startRmiIiopServer();
  75         Thread.sleep(FIVE_SECONDS);
  76         executeRmiIiopClient();
  77     }
  78 
  79     private static void stopTestComponents() throws Exception {
  80         stopRmiIiopServer();
  81         stopOrbd();
  82         if (exceptionInClient) {
  83             throw new RuntimeException(clientException);
  84         } else if (!isResponseReceived()) {
  85             throw new RuntimeException("Expected Response not received");
  86         }
  87     }
  88 
  89     static void startOrbd() throws Exception {
  90         System.out.println("\nStarting orbd with NS port 5050 and activation port 5049 ");
  91 
  92         //orbd -ORBInitialHost localhost -ORBInitialPort 5050 -port 5049
  93         orbdLauncher.addToolArg("-ORBInitialHost").addToolArg("localhost")
  94             .addToolArg("-ORBInitialPort").addToolArg("5050")
  95             .addToolArg("-port").addToolArg("5049");
  96 
  97         System.out.println("RmiIiopReturnValueTest: Executing: " + Arrays.asList(orbdLauncher.getCommand()));
  98         ProcessBuilder pb = new ProcessBuilder(orbdLauncher.getCommand());
  99         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
 100         orbdProcess = pb.start();
 101     }
 102 
 103 
 104     static void startRmiIiopServer() throws Exception {
 105         System.out.println("\nStarting RmiIiopServer");
 106         // java --add-modules java.corba -cp .
 107         // -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
 108         // -Djava.naming.provider.url=iiop://localhost:5050 HelloServer -port 5049
 109         List<String> commands = new ArrayList<>();
 110         commands.add(RmiIiopReturnValueTest.JAVA);
 111         commands.add("--add-modules");
 112         commands.add("java.corba");
 113         commands.add("-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory");
 114         commands.add("-Djava.naming.provider.url=iiop://localhost:5050");
 115         commands.add("-cp");
 116         commands.add(RmiIiopReturnValueTest.CLASSPATH);
 117         commands.add("HelloServer");
 118         commands.add("-port");
 119         commands.add("5049");
 120 
 121         System.out.println("RmiIiopReturnValueTest: Executing: " + commands);
 122         ProcessBuilder pb = new ProcessBuilder(commands);
 123         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
 124         rmiServerProcess = pb.start();
 125     }
 126 
 127     static boolean isResponseReceived() {
 128         return HelloClient.isResponseReceived();
 129     }
 130 
 131     static void stopRmiIiopServer() throws Exception {
 132         if (rmiServerProcess != null) {
 133             System.out.println("RmiIiopReturnValueTest.stopRmiIiopServer: destroy rmiServerProcess");
 134             rmiServerProcess.destroyForcibly();
 135             rmiServerProcess.waitFor();
 136             System.out.println("serverProcess exitCode:"
 137                 + rmiServerProcess.exitValue());
 138         }
 139     }
 140 
 141     static void stopOrbd() throws Exception {
 142         System.out.println("RmiIiopReturnValueTest.stopOrbd: destroy orbdProcess ");
 143         orbdProcess.destroyForcibly();
 144         orbdProcess.waitFor();
 145         System.out.println("orbd exitCode:"
 146             + orbdProcess.exitValue());
 147     }
 148 
 149     static void executeRmiIiopClient() throws Exception {
 150         System.out.println("RmiIiopReturnValueTest.executeRmiIiopClient: HelloClient.executeRmiClientCall");
 151         try {
 152             HelloClient.executeRmiClientCall();
 153         } catch (Throwable t) {
 154             clientException = t;
 155             exceptionInClient = true;
 156         }
 157     }
 158 }