test/jdk/javax/management/mxbean/PreRegisterNameTest.java
author jwilhelm
Thu, 24 Jan 2019 21:24:29 +0100
changeset 53479 31e5c82f59e9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8217580: Remove tests from problemList as bugs has been closed Reviewed-by: iignatyev, mseledtsov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2006, 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 6448042
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that MXBeans can define their own names in preRegister
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.ObjectInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.StandardMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Test that an MXBean can decide its name by returning a value from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the preRegister method.  Also test the same thing for Standard MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * for good measure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class PreRegisterNameTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static interface SpumeMXBean {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static class Spume implements SpumeMXBean, MBeanRegistration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        private ObjectName realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        public Spume(ObjectName realName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            this.realName = realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public void preDeregister() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public ObjectName preRegister(MBeanServer server, ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static interface ThingMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public boolean getNoddy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static class Thing implements ThingMBean, MBeanRegistration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        private ObjectName realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public Thing(ObjectName realName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            this.realName = realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public ObjectName preRegister(MBeanServer mbs, ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public void postRegister(Boolean done) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public void preDeregister() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public void postDeregister() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public boolean getNoddy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static class XThing extends StandardMBean implements ThingMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        private ObjectName realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public XThing(ObjectName realName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            super(ThingMBean.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            this.realName = realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        public ObjectName preRegister(MBeanServer server, ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public boolean getNoddy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static class XSpume extends StandardMBean implements SpumeMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        private ObjectName realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public XSpume(ObjectName realName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            super(SpumeMXBean.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            this.realName = realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public ObjectName preRegister(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            super.preRegister(server, realName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return realName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (Class<?> c : new Class<?>[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                Spume.class, Thing.class, XSpume.class, XThing.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             }) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                System.out.println("Class " + c.getName() + " with name " + n +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ObjectName realName = new ObjectName("a:type=" + c.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                Constructor<?> constr = c.getConstructor(ObjectName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                Object mbean = constr.newInstance(realName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                ObjectInstance oi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                String what =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    "Registering MBean of type " + c.getName() + " under name " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    "<" + n + ">: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    oi = mbs.registerMBean(mbean, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    fail(what + " got " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                ObjectName registeredName = oi.getObjectName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (!registeredName.equals(realName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    fail(what + " registered as " + registeredName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (!mbs.isRegistered(realName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    fail(what + " not registered as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                mbs.unregisterMBean(registeredName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (failures == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            System.out.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new Exception("TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static void fail(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.err.println("FAILED: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        failure = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static int failures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}