jdk/test/javax/management/loading/LibraryLoader/LibraryLoaderTest.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 44423 306c020eb154
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 44423
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4969756
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that the same native library coming from the same jar file can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * be loaded twice by two different MLets on the same JVM without conflict.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    30
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean LibraryLoaderTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build LibraryLoaderTest
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    33
 * @run main/othervm LibraryLoaderTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.ObjectInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class LibraryLoaderTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final String mletInfo[][] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        {"testDomain:type=MLet,index=0", "UseNativeLib0.html"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        {"testDomain:type=MLet,index=1", "UseNativeLib1.html"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    52
    public static void main(String args[]) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.out.println("os.name=" + osName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        String osArch = System.getProperty("os.arch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.println("os.name=" + osArch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // Check for supported platforms:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // Solaris/SPARC and Windows/x86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if ((!(osName.equals("SunOS") && osArch.equals("sparc"))) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            (!(osName.startsWith("Windows") && osArch.equals("x86")))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
              "This test runs only on Solaris/SPARC and Windows/x86 platforms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.out.println("Bye! Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String libPath = System.getProperty("java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("java.library.path=" + libPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String testSrc = System.getProperty("test.src");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.out.println("test.src=" + testSrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String workingDir = System.getProperty("user.dir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.out.println("user.dir=" + workingDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        String urlCodebase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (testSrc.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            urlCodebase =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                "file:" + testSrc.replace(File.separatorChar, '/') + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            urlCodebase =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    87
        // Create MBeanServer
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    88
        //
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    89
        MBeanServer server = MBeanServerFactory.newMBeanServer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    91
        // Create MLet instances and call getRandom on the loaded MBeans
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    92
        //
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    93
        for (int i = 0; i < mletInfo.length; i++) {
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    94
            // Create ObjectName for MLet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            //
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    96
            ObjectName mlet = new ObjectName(mletInfo[i][0]);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    97
            server.createMBean("javax.management.loading.MLet", mlet);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
    98
            System.out.println("MLet = " + mlet);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   100
            // Display old library directory and set it to test.classes
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   101
            //
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   102
            String libraryDirectory =
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   103
                (String) server.getAttribute(mlet, "LibraryDirectory");
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   104
            System.out.println("Old Library Directory = " +
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   105
                               libraryDirectory);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   106
            Attribute attribute =
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   107
                new Attribute("LibraryDirectory", workingDir);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   108
            server.setAttribute(mlet, attribute);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   109
            libraryDirectory =
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   110
                (String) server.getAttribute(mlet, "LibraryDirectory");
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   111
            System.out.println("New Library Directory = " +
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   112
                               libraryDirectory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   114
            // Get MBeans from URL
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   115
            //
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   116
            String mletURL = urlCodebase + mletInfo[i][1];
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   117
            System.out.println("MLet URL = " + mletURL);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   118
            Object[] params = new Object[] { mletURL };
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   119
            String[] signature = new String[] {"java.lang.String"};
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   120
            Object res[] = ((Set<?>) server.invoke(mlet,
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   121
                                                   "getMBeansFromURL",
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   122
                                                   params,
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   123
                                                   signature)).toArray();
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   124
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   125
            // Iterate through all the loaded MBeans
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   126
            //
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   127
            for (int j = 0; j < res.length; j++) {
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   128
                // Now ensure none of the returned objects is a Throwable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                //
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   130
                if (res[j] instanceof Throwable) {
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   131
                    ((Throwable) res[j]).printStackTrace(System.out);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   132
                    throw new Exception("Failed to load the MBean #" + j
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   133
                        ,(Throwable)res[j]);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   134
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   136
                // On each of the loaded MBeans, try to invoke their
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   137
                // native operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                //
21661
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   139
                Object result = null;
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   140
                try {
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   141
                    ObjectName mbean =
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   142
                        ((ObjectInstance) res[j]).getObjectName();
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   143
                    result = server.getAttribute(mbean, "Random");
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   144
                    System.out.println("MBean #" + j + " = " + mbean);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   145
                    System.out.println("Random number = " + result);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   146
                } catch (ReflectionException e) {
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   147
                    e.getTargetException().printStackTrace(System.out);
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   148
                    throw new Exception ("A ReflectionException "
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   149
                            + "occured when attempting to invoke "
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   150
                            + "a native library based operation.",
e6b6a0b1fc26 6959636: testcase failing on windows javax/management/loading/LibraryLoader/LibraryLoaderTest.java
egahlin
parents: 5506
diff changeset
   151
                            e.getTargetException());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}