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
24import java.io.BufferedWriter;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.io.PrintWriter;
28
29import 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 * @run main/othervm/secure=java.lang.SecurityManager/policy=jtreg.test.policy
44 *    -Djava.naming.provider.url=iiop://localhost:3050
45 *    -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
46 *    OrbPropertiesTest -port 3049
47 */
48public class OrbPropertiesTest {
49
50    public static void main(String[] args) throws Exception {
51        updateOrbPropertiesFile();
52        // create and initialize the ORB
53        ORB orb = ORB.init(args, null);
54        if (!(orb instanceof TestOrbImpl)) {
55            throw new RuntimeException("org.omg.CORBA.ORBClass property not set as expected");
56        }
57        ORB singletonOrb = ORB.init();
58        System.out.println("singletonOrb class == " + singletonOrb.getClass().getName());
59        if (!(singletonOrb instanceof TestSingletonOrbImpl)) {
60            throw new RuntimeException("org.omg.CORBA.ORBSingletonClass property not set as expected");
61        }
62
63    }
64
65    private static void updateOrbPropertiesFile() throws Exception {
66        String orbPropertiesFile = System.getProperty("java.home", ".") + "/conf/orb.properties";
67        String orbClassMapping = "org.omg.CORBA.ORBClass TestOrbImpl";
68        String orbSingletonClassMapping = "org.omg.CORBA.ORBSingletonClass TestSingletonOrbImpl";
69        String orbPropertiesMappings = orbClassMapping + "\n" + orbSingletonClassMapping +"\n";
70        try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
71                new FileWriter(orbPropertiesFile, false)))) {
72            hfPWriter.println(orbPropertiesMappings);
73        } catch (IOException ioEx) {
74            ioEx.printStackTrace();
75            throw ioEx;
76        }
77    }
78}
79