1 /*
   2  * Copyright (c) 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 import java.io.BufferedWriter;
  25 import java.io.FileWriter;
  26 import java.io.IOException;
  27 import java.io.PrintWriter;
  28 
  29 import org.omg.CORBA.ORB;
  30 
  31 /*
  32  * @test
  33  * @bug 8049375
  34  * @summary Extend how the org.omg.CORBA.ORB handles the search for orb.properties
  35  * @library /lib/testlibrary
  36  * @build jdk.testlibrary.*
  37  * @modules java.corba
  38  * @compile OrbPropertiesTest.java TestOrbImpl.java TestSingletonOrbImpl.java
  39  * @run main/othervm
  40  *    -Djava.naming.provider.url=iiop://localhost:1050
  41  *    -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
  42  *    OrbPropertiesTest -port 1049
  43  */
  44 public class OrbPropertiesTest {
  45 
  46     public static void main(String[] args) throws Exception {
  47         updateOrbPropertiesFile();
  48         // create and initialize the ORB
  49         ORB orb = ORB.init(args, null);
  50         if (!(orb instanceof TestOrbImpl)) {
  51             throw new RuntimeException("org.omg.CORBA.ORBClass property not set as expected");
  52         }
  53         ORB singletonOrb = ORB.init();
  54         System.out.println("singletonOrb class == " + singletonOrb.getClass().getName());
  55         if (!(singletonOrb instanceof TestSingletonOrbImpl)) {
  56             throw new RuntimeException("org.omg.CORBA.ORBSingletonClass property not set as expected");
  57         }
  58 
  59     }
  60 
  61     private static void updateOrbPropertiesFile() throws Exception {
  62         String orbPropertiesFile = System.getProperty("java.home", ".") + "/conf/orb.properties";
  63         String orbClassMapping = "org.omg.CORBA.ORBClass TestOrbImpl";
  64         String orbSingletonClassMapping = "org.omg.CORBA.ORBSingletonClass TestSingletonOrbImpl";
  65         String orbPropertiesMappings = orbClassMapping + "\n" + orbSingletonClassMapping +"\n";
  66         try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
  67                 new FileWriter(orbPropertiesFile, false)))) {
  68             hfPWriter.println(orbPropertiesMappings);
  69         } catch (IOException ioEx) {
  70             ioEx.printStackTrace();
  71             throw ioEx;
  72         }
  73     }
  74 }