1 /*
   2  * Copyright (c) 2014, 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 8065957
  26  * @library ../../../../java/rmi/testlibrary
  27  * @modules java.corba
  28  *          java.rmi/sun.rmi.registry
  29  *          java.rmi/sun.rmi.server
  30  *          java.rmi/sun.rmi.transport
  31  *          java.rmi/sun.rmi.transport.tcp
  32  * @build TestLibrary
  33  * @summary Compiles a PortableRemoteObject with rmic -iiop and ensures that stub and tie classes are generated.
  34  * @run main IIOPCompilation
  35  * @author Felix Yang
  36  *
  37  */
  38 import java.io.File;
  39 import java.io.FileNotFoundException;
  40 import java.io.IOException;
  41 import java.rmi.RemoteException;
  42 import java.util.Arrays;
  43 import java.util.List;
  44 
  45 import javax.rmi.PortableRemoteObject;
  46 
  47 public class IIOPCompilation {
  48 
  49     public static void main(String args[]) throws IOException, InterruptedException {
  50         IIOPCompilation test = new IIOPCompilation();
  51         test.doTest();
  52     }
  53 
  54     private void doTest() throws IOException, InterruptedException {
  55         String className = DummyImpl.class.getName();
  56         int exitCode = runRmic(className);
  57         if (exitCode != 0) {
  58             throw new RuntimeException("Rmic failed. The exit code is " + exitCode);
  59         }
  60 
  61         // Check the stub class generated correctly
  62         String stubFile = "_" + Dummy.class.getName() + "_Stub.class";
  63         assertFileExists(stubFile);
  64 
  65         // Check the tie class generated correctly
  66         String tieFile = "_" + className + "_Tie.class";
  67         assertFileExists(tieFile);
  68     }
  69 
  70     private void assertFileExists(String fileName) throws FileNotFoundException {
  71         if (!new File(fileName).exists()) {
  72             throw new FileNotFoundException(fileName + " doesn't exist!");
  73         }
  74     }
  75 
  76     private int runRmic(String classname) throws IOException, InterruptedException {
  77         String rmicProgramStr = TestLibrary.getProperty("java.home", "") + File.separator + "bin" + File.separator + "rmic";
  78         String testClasses = TestLibrary.getProperty("test.classes", "");
  79         List<String> command = Arrays.asList(rmicProgramStr, "-iiop", "-classpath", testClasses, classname);
  80         System.out.println("Running command: " + command);
  81 
  82         Process p = null;
  83         try {
  84             p = new ProcessBuilder(command).inheritIO().start();
  85             p.waitFor();
  86             return p.exitValue();
  87         } finally {
  88             if (p != null && p.isAlive()) {
  89                 p.destroy();
  90             }
  91         }
  92     }
  93 }
  94 
  95 interface Dummy extends java.rmi.Remote {
  96 }
  97 
  98 class DummyImpl extends PortableRemoteObject implements Dummy {
  99     public DummyImpl() throws RemoteException {
 100     }
 101 }