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