jdk/test/javax/management/ObjectInstance/MBeanInfoFailTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 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 5001857
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test queryNames() and queryMBeans() with a buggy DynamicMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Daniel Fuchs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean MBeanInfoFailTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build MBeanInfoFailTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main MBeanInfoFailTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class MBeanInfoFailTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static class UnspeakableException extends RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        public UnspeakableException(String unspeakableMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            super(unspeakableMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public interface ThornyDevilMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public boolean isDormant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        public void setDormant(boolean sleep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static class ThornyDevil
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        extends StandardMBean implements ThornyDevilMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        private boolean sleep=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public ThornyDevil() throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            super(ThornyDevilMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public boolean isDormant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            return this.sleep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public void setDormant(boolean sleep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.sleep = sleep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public MBeanInfo getMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (isDormant()) return super.getMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new UnspeakableException("The Thorny Devil has awoken!");
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 void printInstances(Set instances) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (Iterator it1 = instances.iterator(); it1.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            final ObjectInstance oi = (ObjectInstance)it1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            final ObjectName     on = oi.getObjectName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            final String         cn = oi.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.err.println(String.valueOf(on) + ": class is " + cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.out.println("Test queryNames() and queryMBeans() with a "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                           "buggy DynamicMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            final MBeanServer server = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            final ObjectName  troubleKeeper =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                new ObjectName("ThornyDevil:name=TroubleKeeper");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            final ObjectName  troubleMaker =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                new ObjectName("ThornyDevil:name=TroubleMaker");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            final ObjectName  thornyPattern =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                new ObjectName("ThornyDevil:*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            server.createMBean(ThornyDevil.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                               troubleKeeper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            server.createMBean(ThornyDevil.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                               troubleMaker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            final MBeanInfo info1 = server.getMBeanInfo(troubleKeeper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.out.println(String.valueOf(troubleKeeper)+" is a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                               info1.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            final MBeanInfo info2 = server.getMBeanInfo(troubleMaker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.out.println(String.valueOf(troubleMaker)+" is a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                               info2.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            final Set thorny1 = server.queryNames(thornyPattern,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (thorny1.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                System.err.println("queryNames(): " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                   "Expected to find 2 ThornyDevils before" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                   " trouble started! ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                System.err.println("Found "+thorny1.size()+" instead: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                   thorny1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            final Set thornyM1 = server.queryMBeans(thornyPattern,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (thornyM1.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                System.err.println("queryMBeans(): " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                   "Expected to find 2 ThornyDevils before" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                   " trouble started! ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                System.err.println("Found "+thornyM1.size()+" instead: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                printInstances(thornyM1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            for (Iterator it1 = thornyM1.iterator(); it1.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                final ObjectInstance oi = (ObjectInstance)it1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                final ObjectName     on = oi.getObjectName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                final String         cn = oi.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                if (cn == null || !ThornyDevil.class.getName().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    equals(cn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    System.err.println("Expected no trouble yet!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    System.err.println(String.valueOf(on) + ": class is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                       cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    System.exit(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println(String.valueOf(on) + ": class is " + cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.println("Starting trouble with "+troubleMaker+" ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            ThornyDevilMBean troubleMakerproxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                (ThornyDevilMBean) MBeanServerInvocationHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                newProxyInstance(server, troubleMaker, ThornyDevilMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                 false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            troubleMakerproxy.setDormant(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                final MBeanInfo mbi = server.getMBeanInfo(troubleMaker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.err.println("No trouble started!: " + mbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (x.getCause() instanceof UnspeakableException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    System.out.println("Trouble started as expected: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                       + x.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    System.err.println("Unexpected trouble: " + x.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    System.exit(3);
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
            final Set thorny2 = server.queryNames(thornyPattern,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (thorny2.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                System.err.println("Expected to find 2 ThornyDevils after" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                   " trouble started! ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                System.err.println("Found "+thorny2.size()+" instead: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                   thorny2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                System.exit(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            final Set thornyM2 = server.queryMBeans(thornyPattern,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (thornyM2.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                System.err.println("queryMBeans(): " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                   "Expected to find 2 ThornyDevils after" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                   " trouble started! ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                System.err.println("Found "+thornyM2.size()+" instead: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                printInstances(thornyM2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                System.exit(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            for (Iterator it1 = thornyM2.iterator(); it1.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                final ObjectInstance oi = (ObjectInstance)it1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                final ObjectName     on = oi.getObjectName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                final String         cn = oi.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (!on.equals(troubleMaker)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    if (cn == null || !ThornyDevil.class.getName().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        equals(cn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        System.err.println("Expected no trouble for " + on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        System.err.println(String.valueOf(on) + ": class is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                           cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        System.exit(6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    if (cn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        System.err.println("Expected trouble for " + on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        System.err.println(String.valueOf(on) + ": class is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                           cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        System.exit(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                System.out.println(String.valueOf(on) + ": class is " + cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.out.println("Ahahah! " + troubleMaker +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                               " successfully thwarted!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch( Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            System.err.println("Unexpected exception: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            System.exit(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}