jdk/test/javax/management/mxbean/JMXServiceURLTest.java
author emcmanus
Mon, 01 Sep 2008 17:11:58 +0200
changeset 1153 6b88c071a015
parent 715 f16baef3a20e
permissions -rw-r--r--
6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 38
diff changeset
     2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     4
 *
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     8
 *
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    13
 * accompanied this code).
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    14
 *
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    18
 *
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    21
 * have any questions.
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    22
 */
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    23
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    24
/*
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    25
 * @test JMXServiceURLTest
1153
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    26
 * @bug 6607114 6670375 6731410
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    27
 * @summary Test that JMXServiceURL works correctly in MXBeans
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    28
 * @author Eamonn McManus
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    29
 */
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    30
1153
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    31
import java.io.InvalidObjectException;
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    32
import java.lang.management.ManagementFactory;
1153
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    33
import java.util.ArrayList;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    34
import java.util.HashMap;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    35
import java.util.List;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    36
import java.util.Map;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    37
import javax.management.Attribute;
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    38
import javax.management.JMX;
1153
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    39
import javax.management.MBeanException;
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    40
import javax.management.MBeanServer;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    41
import javax.management.ObjectName;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    42
import javax.management.openmbean.CompositeData;
1153
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    43
import javax.management.openmbean.CompositeDataSupport;
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    44
import javax.management.openmbean.CompositeType;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    45
import javax.management.openmbean.OpenType;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    46
import javax.management.openmbean.SimpleType;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    47
import javax.management.remote.JMXServiceURL;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    48
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    49
public class JMXServiceURLTest {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    50
    public static interface UrlMXBean {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    51
        public JMXServiceURL getUrl();
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    52
        public void setUrl(JMXServiceURL url);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    53
    }
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    54
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    55
    public static class UrlImpl implements UrlMXBean {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    56
        volatile JMXServiceURL url;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    57
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    58
        public JMXServiceURL getUrl() {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    59
            return url;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    60
        }
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    61
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    62
        public void setUrl(JMXServiceURL url) {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    63
            this.url = url;
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    64
        }
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    65
    }
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    66
1153
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    67
    private static enum Part {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    68
        PROTOCOL("protocol", SimpleType.STRING, "rmi", 25, "", "a:b", "/", "?", "#"),
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    69
        HOST("host", SimpleType.STRING, "a.b.c", 25, "a..b", ".a.b", "a.b."),
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    70
        PORT("port", SimpleType.INTEGER, 25, "25", -25),
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    71
        PATH("URLPath", SimpleType.STRING, "/tiddly", 25, "tiddly");
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    72
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    73
        Part(String name, OpenType openType, Object validValue, Object... bogusValues) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    74
            this.name = name;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    75
            this.openType = openType;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    76
            this.validValue = validValue;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    77
            this.bogusValues = bogusValues;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    78
        }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    79
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    80
        final String name;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    81
        final OpenType openType;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    82
        final Object validValue;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    83
        final Object[] bogusValues;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    84
    }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
    85
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    86
    public static void main(String[] args) throws Exception {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    87
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    88
        ObjectName name = new ObjectName("a:b=c");
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    89
        UrlImpl urlImpl = new UrlImpl();
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    90
        mbs.registerMBean(urlImpl, name);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    91
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    92
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://host:8000/noddy");
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    93
        UrlMXBean proxy = JMX.newMXBeanProxy(mbs, name, UrlMXBean.class);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    94
        proxy.setUrl(url);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    95
        assertEquals(url, urlImpl.url);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    96
        JMXServiceURL url2 = proxy.getUrl();
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    97
        assertEquals(url, url2);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    98
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
    99
        CompositeData cd = (CompositeData) mbs.getAttribute(name, "Url");
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   100
        CompositeType ct = cd.getCompositeType();
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   101
        // Make sure it looks like what we expect.  This will have to be
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   102
        // changed if ever we add new properties to CompositeType.  In that
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   103
        // case this test should also check interoperability between the
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   104
        // current version and the new version.
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   105
        assertEquals(4, ct.keySet().size());
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   106
        Object[][] expectedItems = {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   107
            {"protocol", SimpleType.STRING, "rmi"},
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   108
            {"host", SimpleType.STRING, "host"},
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   109
            {"port", SimpleType.INTEGER, 8000},
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   110
            {"URLPath", SimpleType.STRING, "/noddy"},
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   111
        };
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   112
        for (Object[] expectedItem : expectedItems) {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   113
            String itemName = (String) expectedItem[0];
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   114
            OpenType expectedType = (OpenType) expectedItem[1];
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   115
            Object expectedValue = expectedItem[2];
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   116
            OpenType actualType = ct.getType(itemName);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   117
            assertEquals(expectedType, actualType);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   118
            Object actualValue = cd.get(itemName);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   119
            assertEquals(expectedValue, actualValue);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   120
        }
1153
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   121
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   122
        // Now make sure we reject any bogus-looking CompositeData items.
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   123
        // We first try every combination of omitted items (items can be
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   124
        // null but cannot be omitted), then we try every combination of
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   125
        // valid and bogus items.
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   126
        final Part[] parts = Part.values();
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   127
        final int nParts = parts.length;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   128
        final int maxPartMask = (1 << nParts) - 1;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   129
        // Iterate over all possibilities of included and omitted, except
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   130
        // 0, because a CompositeDataSupport must have at least one element,
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   131
        // and maxPartMask, where all items are included and the result is valid.
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   132
        for (int mask = 1; mask < maxPartMask; mask++) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   133
            Map<String, Object> cdMap = new HashMap<String, Object>();
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   134
            List<String> names = new ArrayList<String>();
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   135
            List<OpenType> types = new ArrayList<OpenType>();
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   136
            for (int i = 0; i < nParts; i++) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   137
                if ((mask & (1 << i)) != 0) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   138
                    Part part = parts[i];
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   139
                    cdMap.put(part.name, part.validValue);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   140
                    names.add(part.name);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   141
                    types.add(openTypeForValue(part.validValue));
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   142
                }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   143
            }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   144
            String[] nameArray = names.toArray(new String[0]);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   145
            OpenType[] typeArray = types.toArray(new OpenType[0]);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   146
            CompositeType badct = new CompositeType(
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   147
                    "bad", "descr", nameArray, nameArray, typeArray);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   148
            CompositeData badcd = new CompositeDataSupport(badct, cdMap);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   149
            checkBad(mbs, name, badcd);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   150
        }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   151
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   152
        int nBogus = 1;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   153
        for (Part part : parts)
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   154
            nBogus *= (part.bogusValues.length + 1);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   155
        // Iterate over all combinations of bogus values.  We are basically
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   156
        // treating each Part as a digit while counting up from 1.  A digit
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   157
        // value of 0 stands for the valid value of that Part, and 1 on
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   158
        // stand for the bogus values.  Hence an integer where all the digits
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   159
        // are 0 would represent a valid CompositeData, which is why we
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   160
        // start from 1.
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   161
        for (int bogusCount = 1; bogusCount < nBogus; bogusCount++) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   162
            List<String> names = new ArrayList<String>();
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   163
            List<OpenType> types = new ArrayList<OpenType>();
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   164
            int x = bogusCount;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   165
            Map<String, Object> cdMap = new HashMap<String, Object>();
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   166
            for (Part part : parts) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   167
                int digitMax = part.bogusValues.length + 1;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   168
                int digit = x % digitMax;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   169
                Object value = (digit == 0) ?
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   170
                    part.validValue : part.bogusValues[digit - 1];
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   171
                cdMap.put(part.name, value);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   172
                names.add(part.name);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   173
                types.add(openTypeForValue(value));
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   174
                x /= digitMax;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   175
            }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   176
            String[] nameArray = names.toArray(new String[0]);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   177
            OpenType[] typeArray = types.toArray(new OpenType[0]);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   178
            CompositeType badct = new CompositeType(
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   179
                    "bad", "descr", nameArray, nameArray, typeArray);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   180
            CompositeData badcd = new CompositeDataSupport(badct, cdMap);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   181
            checkBad(mbs, name, badcd);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   182
        }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   183
    }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   184
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   185
    private static OpenType openTypeForValue(Object value) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   186
        if (value instanceof String)
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   187
            return SimpleType.STRING;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   188
        else if (value instanceof Integer)
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   189
            return SimpleType.INTEGER;
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   190
        else
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   191
            throw new AssertionError("Value has invalid type: " + value);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   192
    }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   193
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   194
    private static void checkBad(
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   195
            MBeanServer mbs, ObjectName name, CompositeData badcd)
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   196
            throws Exception {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   197
        try {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   198
            mbs.setAttribute(name, new Attribute("Url", badcd));
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   199
            throw new Exception("Expected exception for: " + badcd);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   200
        } catch (MBeanException e) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   201
            if (!(e.getCause() instanceof InvalidObjectException)) {
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   202
                throw new Exception(
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   203
                        "Wrapped exception should be InvalidObjectException", e);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   204
            }
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   205
            System.out.println("OK: rejected " + badcd);
6b88c071a015 6731410: JMXServiceURL cannot use @ConstructorProperties for compatibility reasons
emcmanus
parents: 715
diff changeset
   206
        }
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   207
    }
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   208
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   209
    private static void assertEquals(Object expect, Object actual)
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   210
            throws Exception {
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   211
        if (expect.equals(actual))
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   212
            System.out.println("Equal: " + expect);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   213
        else
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   214
            throw new Exception("Expected " + expect + ", got " + actual);
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   215
    }
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
diff changeset
   216
}