jdk/test/javax/management/mxbean/MXBeanTest.java
author dfuchs
Thu, 02 Feb 2017 16:50:46 +0000
changeset 43503 bc7f8619ab70
parent 33499 d4f084ec39eb
permissions -rw-r--r--
8173607: JMX RMI connector should be in its own module Summary: The JMX RMI connector is moved to a new java.management.rmi module. Reviewed-by: mchung, erikj
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: 18805
diff changeset
     2
 * Copyright (c) 2005, 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: 1247
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    26
 * @bug 6175517 6278707 6318827 6305746 6392303 6600709 8010285
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary General MXBean test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    29
 * @author Jaroslav Bachorik
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 33499
diff changeset
    30
 * @modules java.management.rmi
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean MXBeanTest MerlinMXBean TigerMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build MXBeanTest MerlinMXBean TigerMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main MXBeanTest
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.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Iterator;
831
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
    45
import java.util.Map;
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
    46
import java.util.SortedMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.JMX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.MBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.MBeanParameterInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.MBeanServerInvocationHandler;
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    56
import javax.management.NotCompliantMBeanException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.management.StandardMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.management.openmbean.ArrayType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.management.openmbean.CompositeDataInvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.management.openmbean.OpenType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.management.openmbean.SimpleType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.management.openmbean.TabularData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.management.openmbean.TabularType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public class MXBeanTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        testInterface(MerlinMXBean.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        testInterface(TigerMXBean.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        testInterface(MerlinMXBean.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        testInterface(TigerMXBean.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        testExplicitMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        testSubclassMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        testIndirectMXBean();
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    81
        testNonCompliantMXBean("Private", new Private());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    82
        testNonCompliantMXBean("NonCompliant", new NonCompliant());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (failures == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.out.println("Test passed");
831
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
    86
        else
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
    87
            throw new Exception("TEST FAILURES: " + failures);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    92
    private static interface PrivateMXBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    93
        public int[] getInts();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    94
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    95
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    96
    public static class Private implements PrivateMXBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    97
        public int[] getInts() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    98
            return new int[]{1,2,3};
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
    99
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   100
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   101
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   102
    public static interface NonCompliantMXBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   103
        public boolean getInt();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   104
        public boolean isInt();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   105
        public void setInt(int a);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   106
        public void setInt(long b);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   107
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   108
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   109
    public static class NonCompliant implements NonCompliantMXBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   110
        public boolean getInt() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   111
            return false;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   112
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   113
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   114
        public boolean isInt() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   115
            return true;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   116
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   117
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   118
        public void setInt(int a) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   119
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   120
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   121
        public void setInt(long b) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   122
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   123
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   124
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static interface ExplicitMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        public int[] getInts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static class Explicit implements ExplicitMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public int[] getInts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return new int[] {1, 2, 3};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static class Subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        extends StandardMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        implements ExplicitMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public Subclass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            super(ExplicitMXBean.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public int[] getInts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return new int[] {1, 2, 3};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static interface IndirectInterface extends ExplicitMXBean {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static class Indirect implements IndirectInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public int[] getInts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return new int[] {1, 2, 3};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   151
    private static void testNonCompliantMXBean(String type, Object bean) throws Exception {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   152
        System.out.println(type + " MXBean test...");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   153
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   154
        ObjectName on = new ObjectName("test:type=" + type);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   155
        try {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   156
            mbs.registerMBean(bean, on);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   157
            failure(bean.getClass().getInterfaces()[0].getName() + " is not a compliant "
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   158
                + "MXBean interface");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   159
        } catch (NotCompliantMBeanException e) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   160
            success("Non-compliant MXBean not registered");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   161
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   162
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 5506
diff changeset
   163
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static void testExplicitMXBean() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        System.out.println("Explicit MXBean test...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        ObjectName on = new ObjectName("test:type=Explicit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Explicit explicit = new Explicit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        mbs.registerMBean(explicit, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        testMXBean(mbs, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static void testSubclassMXBean() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        System.out.println("Subclass MXBean test...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ObjectName on = new ObjectName("test:type=Subclass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Subclass subclass = new Subclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        mbs.registerMBean(subclass, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        testMXBean(mbs, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static void testIndirectMXBean() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        System.out.println("Indirect MXBean test...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ObjectName on = new ObjectName("test:type=Indirect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Indirect indirect = new Indirect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        mbs.registerMBean(indirect, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        testMXBean(mbs, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static void testMXBean(MBeanServer mbs, ObjectName on)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        MBeanInfo mbi = mbs.getMBeanInfo(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        MBeanAttributeInfo[] attrs = mbi.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int nattrs = attrs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (mbi.getAttributes().length != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            failure("wrong number of attributes: " + attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            MBeanAttributeInfo mbai = attrs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (mbai.getName().equals("Ints")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                && mbai.isReadable() && !mbai.isWritable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                && mbai.getDescriptor().getFieldValue("openType")
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 831
diff changeset
   203
                    .equals(new ArrayType<int[]>(SimpleType.INTEGER, true))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                && attrs[0].getType().equals("[I"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                success("MBeanAttributeInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                failure("MBeanAttributeInfo: " + mbai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int[] ints = (int[]) mbs.getAttribute(on, "Ints");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (equal(ints, new int[] {1, 2, 3}, null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            success("getAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            failure("getAttribute: " + Arrays.toString(ints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        ExplicitMXBean proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int[] pints = proxy.getInts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (equal(pints, new int[] {1, 2, 3}, null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            success("getAttribute through proxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            failure("getAttribute through proxy: " + Arrays.toString(pints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private static class NamedMXBeans extends HashMap<ObjectName, Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        private static final long serialVersionUID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        NamedMXBeans(MBeanServerConnection mbsc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            this.mbsc = mbsc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        MBeanServerConnection getMBeanServerConnection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return mbsc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        private final MBeanServerConnection mbsc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /* This is the core of the test.  Given the MXBean interface c, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
       make an MXBean object that implements that interface by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
       constructing a dynamic proxy.  If the interface defines an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
       attribute Foo (with getFoo and setFoo methods), then it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
       also contain a field (constant) Foo of the same type, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
       field (constant) FooType that is an OpenType.  The field Foo is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
       a reference value for this case.  We check that the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
       does indeed have the given OpenType.  The dynamically-created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
       MXBean will return the reference value from the getFoo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
       method, and we check that that value survives the mapping to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
       open values and back when the attribute is accessed through an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
       MXBean proxy.  The MXBean will also check in its setFoo method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
       that the value being set is equal to the reference value, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
       tests that the mapping and unmapping also works in the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
       direction.  The interface should define an operation opFoo with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
       two parameters and a return value all of the same type as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
       attribute.  The MXBean will check that the two parameters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
       equal to the reference value, and will return that value.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
       test checks that calling the operation through an MXBean proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
       returns the reference value, again after mapping to and back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
       from open values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
       If any field (constant) in the MXBean interface has a name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
       ends with ObjectName, say FooObjectName, then its value must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
       a String containing an ObjectName value.  There must be a field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
       (constant) called Foo that is a valid MXBean, and that MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
       will be registered in the MBean Server with the given name before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
       the test starts.  This enables us to test that inter-MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
       references are correctly converted to ObjectNames and back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private static <T> void testInterface(Class<T> c, boolean nullTest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        System.out.println("Testing " + c.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                           (nullTest ? " for null values" : "") + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        JMXConnectorServer cs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        JMXServiceURL addr = cs.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        JMXConnector cc = JMXConnectorFactory.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        InvocationHandler ih =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                       new MXBeanImplInvocationHandler(c, namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                               new Class[] {c},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                               ih));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        ObjectName on = new ObjectName("test:type=" + c.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        mbs.registerMBean(impl, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        System.out.println("Register any MXBeans...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Field[] fields = c.getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        for (Field field : fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            String n = field.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (n.endsWith("ObjectName")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                String objectNameString = (String) field.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                String base = n.substring(0, n.length() - 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                Field f = c.getField(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                Object mxbean = f.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                ObjectName objectName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    ObjectName.getInstance(objectNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                mbs.registerMBean(mxbean, objectName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                namedMXBeans.put(objectName, mxbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            testInterface(c, mbsc, on, namedMXBeans, nullTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                cc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private static <T> void testInterface(Class<T> c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                          MBeanServerConnection mbsc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                          ObjectName on,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                          NamedMXBeans namedMXBeans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                          boolean nullTest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        System.out.println("Type check...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        MBeanInfo mbi = mbsc.getMBeanInfo(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        MBeanAttributeInfo[] mbais = mbi.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        for (int i = 0; i < mbais.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            MBeanAttributeInfo mbai = mbais[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            String name = mbai.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            Field typeField = c.getField(name + "Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            OpenType typeValue = (OpenType) typeField.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            OpenType openType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                (OpenType) mbai.getDescriptor().getFieldValue("openType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (typeValue.equals(openType))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                success("attribute " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    "Wrong type attribute " + name + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    openType + " should be " + typeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                failure(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        MBeanOperationInfo[] mbois = mbi.getOperations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (int i = 0; i < mbois.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            MBeanOperationInfo mboi = mbois[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            String oname = mboi.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (!oname.startsWith("op"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            OpenType retType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                (OpenType) mboi.getDescriptor().getFieldValue("openType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            MBeanParameterInfo[] params = mboi.getSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            MBeanParameterInfo p1i = params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            MBeanParameterInfo p2i = params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            OpenType p1Type =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                (OpenType) p1i.getDescriptor().getFieldValue("openType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            OpenType p2Type =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                (OpenType) p2i.getDescriptor().getFieldValue("openType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (!retType.equals(p1Type) || !p1Type.equals(p2Type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    "Parameter and return open types should all be same " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    "but are not: " + retType + " " + oname + "(" + p1Type +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    ", " + p2Type + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                failure(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            String name = oname.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            Field typeField = c.getField(name + "Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            OpenType typeValue = (OpenType) typeField.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (typeValue.equals(retType))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                success("operation " + oname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    "Wrong type operation " + oname + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    retType + " should be " + typeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                failure(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        System.out.println("Mapping check...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        Object proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            JMX.newMXBeanProxy(mbsc, on, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        Method[] methods = c.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        for (int i = 0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            final Method method = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (method.getDeclaringClass() != c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                continue; // skip hashCode() etc inherited from Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            final String mname = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            final int what = getType(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            final String name = getName(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            final Field refField = c.getField(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (nullTest && refField.getType().isPrimitive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            final Field openTypeField = c.getField(name + "Type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            final OpenType openType = (OpenType) openTypeField.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            final Object refValue = nullTest ? null : refField.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            Object setValue = refValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                Field onField = c.getField(name + "ObjectName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                String refName = (String) onField.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                ObjectName refObjName = ObjectName.getInstance(refName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                Class<?> mxbeanInterface = refField.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                setValue = nullTest ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    JMX.newMXBeanProxy(mbsc, refObjName, mxbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // no xObjectName field, setValue == refValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                switch (what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                case GET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    final Object gotOpen = mbsc.getAttribute(on, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (nullTest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        if (gotOpen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            failure(mname + " got non-null value " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                    gotOpen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    } else if (!openType.isValue(gotOpen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        if (gotOpen instanceof TabularData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                            // detail the mismatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            TabularData gotTabular = (TabularData) gotOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            compareTabularType((TabularType) openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                               gotTabular.getTabularType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        failure(mname + " got open data " + gotOpen +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                " not valid for open type " + openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    final Object got = method.invoke(proxy, (Object[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    if (!equal(refValue, got, namedMXBeans)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        failure(mname + " got " + string(got) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                ", should be " + string(refValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                case SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    method.invoke(proxy, new Object[] {setValue});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                case OP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    final Object opped =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        method.invoke(proxy, new Object[] {setValue, setValue});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    if (!equal(refValue, opped, namedMXBeans)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        failure(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                mname + " got " + string(opped) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                ", should be " + string(refValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    success(mname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                failure(mname, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private static void success(String what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        System.out.println("OK: " + what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private static void failure(String what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        System.out.println("FAILED: " + what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static void failure(String what, Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        System.out.println("FAILED WITH EXCEPTION: " + what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private static class MXBeanImplInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        MXBeanImplInvocationHandler(Class intf, NamedMXBeans namedMXBeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            this.intf = intf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            this.namedMXBeans = namedMXBeans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            final String mname = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            final int what = getType(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            final String name = getName(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            final Field refField = intf.getField(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            final Object refValue = getRefValue(refField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            switch (what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            case GET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                assert args == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                return refValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            case SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                assert args.length == 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                Object setValue = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                if (!equal(refValue, setValue, namedMXBeans)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        mname + "(" + string(setValue) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        ") does not match ref: " + string(refValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            case OP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                assert args.length == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                Object arg1 = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                Object arg2 = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                if (!equal(arg1, arg2, namedMXBeans)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        mname + "(" + string(arg1) + ", " + string(arg2) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        "): args not equal";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                if (!equal(refValue, arg1, namedMXBeans)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        mname + "(" + string(arg1) + ", " + string(arg2) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        "): args do not match ref: " + string(refValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                return refValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        Object getRefValue(Field refField) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return refField.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        private final Class intf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        private final NamedMXBeans namedMXBeans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    private static class MXBeanNullImplInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            extends MXBeanImplInvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        MXBeanNullImplInvocationHandler(Class intf, NamedMXBeans namedMXBeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            super(intf, namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        Object getRefValue(Field refField) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            Class<?> type = refField.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (type.isPrimitive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                return super.getRefValue(refField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    private static final String[] prefixes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        "get", "set", "op",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    private static final int GET = 0, SET = 1, OP = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    private static String getName(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return getName(m.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private static String getName(String n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        for (int i = 0; i < prefixes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (n.startsWith(prefixes[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                return n.substring(prefixes[i].length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private static int getType(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return getType(m.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    private static int getType(String n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        for (int i = 0; i < prefixes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (n.startsWith(prefixes[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    static boolean equal(Object o1, Object o2, NamedMXBeans namedMXBeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (o1 == o2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (o1 == null || o2 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (o1.getClass().isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (!o2.getClass().isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return deepEqual(o1, o2, namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
831
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   610
        if (o1 instanceof Map) {
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   611
            if (!(o2 instanceof Map))
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   612
                return false;
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   613
            return equalMap((Map) o1, (Map) o2, namedMXBeans);
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   614
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (o1 instanceof CompositeData && o2 instanceof CompositeData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            return compositeDataEqual((CompositeData) o1, (CompositeData) o2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                      namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (Proxy.isProxyClass(o1.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (Proxy.isProxyClass(o2.getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                return proxyEqual(o1, o2, namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            InvocationHandler ih = Proxy.getInvocationHandler(o1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
//            if (ih instanceof MXBeanInvocationHandler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
//                return proxyEqualsObject((MXBeanInvocationHandler) ih,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
//                                         o2, namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (ih instanceof MBeanServerInvocationHandler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            } else if (ih instanceof CompositeDataInvocationHandler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                return o2.equals(o1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                // We assume the other object has a reasonable equals method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        } else if (Proxy.isProxyClass(o2.getClass()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return equal(o2, o1, namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return o1.equals(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    // We'd use Arrays.deepEquals except we want the test to work on 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    // Note this code assumes no selfreferential arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // (as does Arrays.deepEquals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    private static boolean deepEqual(Object a1, Object a2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                     NamedMXBeans namedMXBeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int len = Array.getLength(a1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (len != Array.getLength(a2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            Object e1 = Array.get(a1, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            Object e2 = Array.get(a2, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (!equal(e1, e2, namedMXBeans))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
831
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   654
    private static boolean equalMap(Map<?,?> m1, Map<?,?> m2,
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   655
                                    NamedMXBeans namedMXBeans) {
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   656
        if (m1.size() != m2.size())
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   657
            return false;
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   658
        if ((m1 instanceof SortedMap) != (m2 instanceof SortedMap))
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   659
            return false;
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   660
        for (Object k1 : m1.keySet()) {
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   661
            if (!m2.containsKey(k1))
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   662
                return false;
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   663
            if (!equal(m1.get(k1), m2.get(k1), namedMXBeans))
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   664
                return false;
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   665
        }
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   666
        return true;
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   667
    }
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   668
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    // This is needed to work around a bug (5095277)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    // in CompositeDataSupport.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    private static boolean compositeDataEqual(CompositeData cd1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                              CompositeData cd2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                              NamedMXBeans namedMXBeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (cd1 == cd2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (!cd1.getCompositeType().equals(cd2.getCompositeType()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        Collection v1 = cd1.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        Collection v2 = cd2.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (v1.size() != v2.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return false; // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        for (Iterator i1 = v1.iterator(), i2 = v2.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
             i1.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (!equal(i1.next(), i2.next(), namedMXBeans))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    // Also needed for 5095277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    private static boolean proxyEqual(Object proxy1, Object proxy2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                      NamedMXBeans namedMXBeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (proxy1.getClass() != proxy2.getClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return proxy1.equals(proxy2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        InvocationHandler ih1 = Proxy.getInvocationHandler(proxy1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        InvocationHandler ih2 = Proxy.getInvocationHandler(proxy2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (!(ih1 instanceof CompositeDataInvocationHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            || !(ih2 instanceof CompositeDataInvocationHandler))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            return proxy1.equals(proxy2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        CompositeData cd1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            ((CompositeDataInvocationHandler) ih1).getCompositeData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        CompositeData cd2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            ((CompositeDataInvocationHandler) ih2).getCompositeData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return compositeDataEqual(cd1, cd2, namedMXBeans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
//    private static boolean proxyEqualsObject(MXBeanInvocationHandler ih,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
//                                             Object o,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
//                                             NamedMXBeans namedMXBeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
//        if (namedMXBeans.getMBeanServerConnection() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
//            ih.getMBeanServerConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
//            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
//        ObjectName on = ih.getObjectName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
//        Object named = namedMXBeans.get(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
//        if (named == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
//            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
//        return (o == named && ih.getMXBeanInterface().isInstance(named));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    /* I wanted to call this method toString(Object), but oddly enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
       this meant that I couldn't call it from the inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
       MXBeanImplInvocationHandler, because the inherited Object.toString()
831
50f701930577 6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents: 2
diff changeset
   724
       prevented that.  */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    static String string(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (o instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            return '"' + (String) o + '"';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (o instanceof Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return deepToString((Collection) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (o.getClass().isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            return deepToString(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        return o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private static String deepToString(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        buf.append("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        int len = Array.getLength(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            Object e = Array.get(o, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            buf.append(string(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        buf.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    private static String deepToString(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return deepToString(c.toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    private static void compareTabularType(TabularType t1, TabularType t2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (t1.equals(t2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            System.out.println("same tabular type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (t1.getClassName().equals(t2.getClassName()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            System.out.println("same class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (t1.getDescription().equals(t2.getDescription()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            System.out.println("same description");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            System.out.println("t1 description: " + t1.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            System.out.println("t2 description: " + t2.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (t1.getIndexNames().equals(t2.getIndexNames()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            System.out.println("same index names");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (t1.getRowType().equals(t2.getRowType()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            System.out.println("same row type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
}