jdk/test/javax/management/modelmbean/UnserializableTargetObjectTest.java
author stefank
Fri, 17 Apr 2015 17:10:38 +0000
changeset 30266 ef82cd1f2db3
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
Merge
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) 2005, 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 6332962
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that a RequiredModelMBean operation can have a targetObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * that is not serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean UnserializableTargetObjectTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build UnserializableTargetObjectTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main UnserializableTargetObjectTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/* This test and DescriptorSupportSerialTest basically cover the same thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
   I wrote them at different times and forgot that I had written the earlier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
   one.  However the coverage is slightly different so I'm keeping both.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.modelmbean.DescriptorSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.modelmbean.ModelMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.modelmbean.ModelMBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.modelmbean.ModelMBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.modelmbean.ModelMBeanInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.modelmbean.ModelMBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.modelmbean.RequiredModelMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class UnserializableTargetObjectTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static class Resource { // not serializable!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int operationCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public void operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            operationCount++;
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 int getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public void setCount(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            this.count = count;
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
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ObjectName name = new ObjectName("a:b=c");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Resource resource1 = new Resource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Resource resource2 = new Resource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Resource resource3 = new Resource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Method operationMethod = Resource.class.getMethod("operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Method getCountMethod = Resource.class.getMethod("getCount");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Method setCountMethod = Resource.class.getMethod("setCount", int.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Descriptor operationDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            new DescriptorSupport(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                    "descriptorType", "name", "targetObject"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                  }, new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                    "operation", "operation", resource1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                  });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Descriptor getCountDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            new DescriptorSupport(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                    "descriptorType", "name", "targetObject"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                  }, new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                    "operation", "getCount", resource2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                  });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Descriptor setCountDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            new DescriptorSupport(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                    "descriptorType", "name", "targetObject"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                  }, new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                    "operation", "setCount", resource2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                  });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Descriptor countDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            new DescriptorSupport(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                    "descriptorType", "name", "getMethod", "setMethod"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                  }, new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                    "attribute", "Count", "getCount", "setCount"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                  });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ModelMBeanOperationInfo operationInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            new ModelMBeanOperationInfo("operation description",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                        operationMethod, operationDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ModelMBeanOperationInfo getCountInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            new ModelMBeanOperationInfo("getCount description",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                        getCountMethod, getCountDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        ModelMBeanOperationInfo setCountInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            new ModelMBeanOperationInfo("setCount description",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                        setCountMethod, setCountDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ModelMBeanAttributeInfo countInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            new ModelMBeanAttributeInfo("Count", "Count description",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                        getCountMethod, setCountMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                        countDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        ModelMBeanInfo mmbi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            new ModelMBeanInfoSupport(Resource.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                      "ModelMBean to test targetObject",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                      new ModelMBeanAttributeInfo[] {countInfo},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                      null,  // no constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                      new ModelMBeanOperationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                          operationInfo, getCountInfo, setCountInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                      },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                      null); // no notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        ModelMBean mmb = new RequiredModelMBean(mmbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        mmb.setManagedResource(resource3, "ObjectReference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        mbs.registerMBean(mmb, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        mbs.invoke(name, "operation", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        mbs.setAttribute(name, new Attribute("Count", 53));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (resource1.operationCount != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new Exception("operationCount: " + resource1.operationCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (resource2.count != 53)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new Exception("count: " + resource2.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int got = (Integer) mbs.getAttribute(name, "Count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (got != 53)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new Exception("got count: " + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        JMXConnectorServer cs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        JMXServiceURL addr = cs.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        JMXConnector cc = JMXConnectorFactory.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Above gets NotSerializableException if resource included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // serialized form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        cc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.out.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}