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