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