jdk/test/javax/management/context/ContextTest.java
author twisti
Tue, 21 Apr 2009 11:16:30 -0700 (2009-04-21)
changeset 2571 d602ad6538bd
parent 1639 a97859015238
permissions -rw-r--r--
6822110: Add AddressLiteral class on SPARC Summary: The Address class on SPARC currently handles both, addresses and address literals, what makes the Address class more complicated than it has to be. Reviewed-by: never, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1570
diff changeset
     2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     4
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     8
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    13
 * accompanied this code).
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    14
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    18
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    21
 * have any questions.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    22
 */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    23
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    24
/*
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    25
 * @test ContextTest
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    26
 * @bug 5072267
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    27
 * @summary Test client contexts.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    28
 * @author Eamonn McManus
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    29
 * TODO: Try registering with a null name replaced by preRegister (for example
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    30
 * from the MLet class) and see if it now works.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    31
 */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    32
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    33
import java.lang.management.ManagementFactory;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    34
import java.lang.reflect.InvocationHandler;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    35
import java.lang.reflect.InvocationTargetException;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    36
import java.lang.reflect.Method;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    37
import java.lang.reflect.Proxy;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    38
import java.util.Arrays;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    39
import java.util.Collections;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    40
import java.util.HashMap;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    41
import java.util.HashSet;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    42
import java.util.LinkedList;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    43
import java.util.List;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    44
import java.util.Map;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    45
import java.util.Queue;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    46
import java.util.Set;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    47
import java.util.TreeMap;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    48
import java.util.concurrent.Callable;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    49
import javax.management.Attribute;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    50
import javax.management.AttributeList;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    51
import javax.management.ClientContext;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    52
import javax.management.DynamicMBean;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    53
import javax.management.JMX;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    54
import javax.management.ListenerNotFoundException;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    55
import javax.management.MBeanNotificationInfo;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    56
import javax.management.MBeanRegistration;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    57
import javax.management.MBeanServer;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    58
import javax.management.MBeanServerDelegate;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    59
import javax.management.Notification;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    60
import javax.management.NotificationBroadcasterSupport;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    61
import javax.management.NotificationFilter;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    62
import javax.management.NotificationListener;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    63
import javax.management.ObjectInstance;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    64
import javax.management.ObjectName;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    65
import javax.management.StandardMBean;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    66
import javax.management.loading.MLet;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    67
import javax.management.namespace.JMXNamespace;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    68
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    69
import javax.management.remote.MBeanServerForwarder;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    70
import static java.util.Collections.emptyMap;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    71
import static java.util.Collections.singletonMap;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    72
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    73
public class ContextTest {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    74
    private static String failure;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    75
    private static final Map<String, String> emptyContext = emptyMap();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    76
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    77
    public static interface ShowContextMBean {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    78
        public Map<String, String> getContext();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    79
        public Map<String, String> getCreationContext();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    80
        public Set<String> getCalledOps();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    81
        public String getThing();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    82
        public void setThing(String x);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    83
        public int add(int x, int y);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    84
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    85
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    86
    public static class ShowContext
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    87
            extends NotificationBroadcasterSupport
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    88
            implements ShowContextMBean, MBeanRegistration {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    89
        private final Map<String, String> creationContext;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    90
        private final Set<String> calledOps = new HashSet<String>();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    91
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    92
        public ShowContext() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    93
            creationContext = getContext();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    94
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    95
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    96
        public Map<String, String> getContext() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    97
            return ClientContext.getContext();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    98
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    99
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   100
        public Map<String, String> getCreationContext() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   101
            return creationContext;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   102
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   103
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   104
        public Set<String> getCalledOps() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   105
            return calledOps;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   106
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   107
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   108
        public String getThing() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   109
            return "x";
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   110
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   111
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   112
        public void setThing(String x) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   113
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   114
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   115
        public int add(int x, int y) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   116
            return x + y;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   117
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   118
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   119
        public ObjectName preRegister(MBeanServer server, ObjectName name) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   120
            assertEquals("preRegister context", creationContext, getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   121
            calledOps.add("preRegister");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   122
            return name;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   123
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   124
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   125
        public void postRegister(Boolean registrationDone) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   126
            assertEquals("postRegister context", creationContext, getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   127
            calledOps.add("postRegister");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   128
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   129
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   130
        // The condition checked here is not guaranteed universally true,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   131
        // but is true every time we unregister an instance of this MBean
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   132
        // in this test.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   133
        public void preDeregister() throws Exception {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   134
            assertEquals("preDeregister context", creationContext, getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   135
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   136
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   137
        public void postDeregister() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   138
            assertEquals("postDeregister context", creationContext, getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   139
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   140
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   141
        // Same remark as for preDeregister
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   142
        @Override
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   143
        public MBeanNotificationInfo[] getNotificationInfo() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   144
            calledOps.add("getNotificationInfo");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   145
            return super.getNotificationInfo();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   146
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   147
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   148
        @Override
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   149
        public void addNotificationListener(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   150
                NotificationListener listener, NotificationFilter filter, Object handback) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   151
            calledOps.add("addNotificationListener");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   152
            super.addNotificationListener(listener, filter, handback);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   153
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   154
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   155
        @Override
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   156
        public void removeNotificationListener(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   157
                NotificationListener listener)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   158
        throws ListenerNotFoundException {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   159
            calledOps.add("removeNL1");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   160
            super.removeNotificationListener(listener);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   161
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   162
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   163
        @Override
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   164
        public void removeNotificationListener(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   165
                NotificationListener listener, NotificationFilter filter, Object handback)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   166
        throws ListenerNotFoundException {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   167
            calledOps.add("removeNL3");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   168
            super.removeNotificationListener(listener, filter, handback);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   169
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   170
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   171
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   172
    private static class LogRecord {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   173
        final String op;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   174
        final Object[] params;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   175
        final Map<String, String> context;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   176
        LogRecord(String op, Object[] params, Map<String, String> context) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   177
            this.op = op;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   178
            this.params = params;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   179
            this.context = context;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   180
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   181
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   182
        @Override
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   183
        public String toString() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   184
            return op + Arrays.deepToString(params) + " " + context;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   185
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   186
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   187
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   188
    /*
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   189
     * InvocationHandler that forwards all methods to a contained object
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   190
     * but also records each forwarded method.  This allows us to check
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   191
     * that the appropriate methods were called with the appropriate
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   192
     * parameters.  It's similar to what's typically available in
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   193
     * Mock Object frameworks.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   194
     */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   195
    private static class LogIH implements InvocationHandler {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   196
        private final Object wrapped;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   197
        Queue<LogRecord> log = new LinkedList<LogRecord>();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   198
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   199
        LogIH(Object wrapped) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   200
            this.wrapped = wrapped;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   201
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   202
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   203
        public Object invoke(Object proxy, Method method, Object[] args)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   204
        throws Throwable {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   205
            if (method.getDeclaringClass() != Object.class) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   206
                LogRecord lr =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   207
                    new LogRecord(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   208
                        method.getName(), args, ClientContext.getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   209
                log.add(lr);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   210
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   211
            try {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   212
                return method.invoke(wrapped, args);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   213
            } catch (InvocationTargetException e) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   214
                throw e.getCause();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   215
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   216
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   217
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   218
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   219
    private static <T> T newSnoop(Class<T> wrappedClass, LogIH logIH) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   220
        return wrappedClass.cast(Proxy.newProxyInstance(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   221
                wrappedClass.getClassLoader(),
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   222
                new Class<?>[] {wrappedClass},
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   223
                logIH));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   224
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   225
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   226
    public static void main(String[] args) throws Exception {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   227
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   228
        System.out.println(mbs.queryNames(null, null));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   229
        ObjectName name = new ObjectName("a:b=c");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   230
        mbs.registerMBean(new ShowContext(), name);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   231
        final ShowContextMBean show =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   232
                JMX.newMBeanProxy(mbs, name, ShowContextMBean.class);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   233
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   234
        // Test local setting and getting within the MBeanServer
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   235
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   236
        assertEquals("initial context", emptyContext, show.getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   237
        ClientContext.doWithContext(singletonMap("foo", "bar"), new Callable<Void>() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   238
            public Void call() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   239
                assertEquals("context in doWithContext",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   240
                        singletonMap("foo", "bar"), show.getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   241
                return null;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   242
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   243
        });
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   244
        assertEquals("initial context after doWithContext",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   245
                emptyContext, show.getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   246
        String got = ClientContext.doWithContext(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   247
                singletonMap("foo", "baz"), new Callable<String>() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   248
            public String call() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   249
                return ClientContext.getContext().get("foo");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   250
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   251
        });
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   252
        assertEquals("value extracted from context", "baz", got);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   253
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   254
        Map<String, String> combined = ClientContext.doWithContext(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   255
                singletonMap("foo", "baz"), new Callable<Map<String, String>>() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   256
            public Map<String, String> call() throws Exception {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   257
                return ClientContext.doWithContext(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   258
                        singletonMap("fred", "jim"),
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   259
                        new Callable<Map<String, String>>() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   260
                    public Map<String, String> call() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   261
                        return ClientContext.getContext();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   262
                    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   263
                });
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   264
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   265
        });
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   266
        assertEquals("nested doWithContext context",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   267
                singletonMap("fred", "jim"), combined);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   268
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   269
        final String ugh = "a!\u00c9//*=:\"% ";
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   270
        ClientContext.doWithContext(singletonMap(ugh, ugh), new Callable<Void>() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   271
            public Void call() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   272
                assertEquals("context with tricky encoding",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   273
                        singletonMap(ugh, ugh), show.getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   274
                return null;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   275
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   276
        });
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   277
        Map<String, String> ughMap = new TreeMap<String, String>();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   278
        ughMap.put(ugh, ugh);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   279
        ughMap.put("fred", "jim");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   280
        // Since this is a TreeMap and "fred" is greater than ugh (which begins
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   281
        // with "a"), we will see the encoding of ugh first in the output string.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   282
        String encoded = ClientContext.encode(ughMap);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   283
        String expectedUghCoding = "a%21%C3%89%2F%2F%2A%3D%3A%22%25+";
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   284
        String expectedUghMapCoding =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   285
                ClientContext.NAMESPACE + "//" + expectedUghCoding + "=" +
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   286
                expectedUghCoding + ";fred=jim";
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   287
        assertEquals("hairy context encoded as string",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   288
                expectedUghMapCoding, encoded);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   289
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   290
        // Wrap the MBeanServer with a context MBSF so we can test withContext.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   291
        // Also check the simulated namespace directly.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   292
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   293
        LogIH mbsIH = new LogIH(mbs);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   294
        MBeanServer snoopMBS = newSnoop(MBeanServer.class, mbsIH);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   295
        MBeanServerForwarder ctxMBS =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   296
                ClientContext.newContextForwarder(snoopMBS, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   297
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   298
        // The MBSF returned by ClientContext is actually a compound of two
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   299
        // forwarders, but that is supposed to be hidden by changing the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   300
        // behaviour of get/setMBeanServer.  Check that it is indeed so.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   301
        assertEquals("next MBS of context forwarder",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   302
                snoopMBS, ctxMBS.getMBeanServer());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   303
        // If the above assertion fails you may get a confusing message
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   304
        // because the toString() of the two objects is likely to be the same
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   305
        // so it will look as if they should be equal.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   306
        ctxMBS.setMBeanServer(null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   307
        assertEquals("next MBS of context forwarder after setting it null",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   308
                null, ctxMBS.getMBeanServer());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   309
        ctxMBS.setMBeanServer(snoopMBS);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   310
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   311
        // The MBSF should look the same as the original MBeanServer except
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   312
        // that it has the JMXNamespace for the simulated namespace.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   313
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   314
        Set<ObjectName> origNames = mbs.queryNames(null, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   315
        Set<ObjectName> mbsfNames = ctxMBS.queryNames(null, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   316
        assertEquals("number of MBeans returned by queryNames within forwarder",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   317
                origNames.size() + 1, mbsfNames.size());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   318
        assertEquals("MBeanCount within forwarder",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   319
                mbsfNames.size(), ctxMBS.getMBeanCount());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   320
        assertCalled(mbsIH, "queryNames", emptyContext);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   321
        assertCalled(mbsIH, "getMBeanCount", emptyContext);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   322
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   323
        ObjectName ctxNamespaceName = new ObjectName(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   324
                ClientContext.NAMESPACE + "//:" + JMXNamespace.TYPE_ASSIGNMENT);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   325
        origNames.add(ctxNamespaceName);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   326
        assertEquals("MBeans within forwarder", origNames, mbsfNames);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   327
        Set<String> domains = new HashSet<String>(Arrays.asList(ctxMBS.getDomains()));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   328
        assertEquals("domains include context namespace MBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   329
                true, domains.contains(ClientContext.NAMESPACE + "//"));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   330
        assertCalled(mbsIH, "getDomains", emptyContext);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   331
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   332
        // Now test ClientContext.withContext.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   333
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   334
        MBeanServer ughMBS = ClientContext.withContext(ctxMBS, ugh, ugh);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   335
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   336
        ShowContextMBean ughshow =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   337
                JMX.newMBeanProxy(ughMBS, name, ShowContextMBean.class);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   338
        Map<String, String> ughCtx = ughshow.getContext();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   339
        Map<String, String> ughExpect = singletonMap(ugh, ugh);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   340
        assertEquals("context seen by MBean accessed within namespace",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   341
                ughExpect, ughCtx);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   342
        assertCalled(mbsIH, "getAttribute", ughExpect, name, "Context");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   343
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   344
        MBeanServer cmbs = ClientContext.withContext(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   345
                ctxMBS, "mickey", "mouse");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   346
        ShowContextMBean cshow =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   347
                JMX.newMBeanProxy(cmbs, name, ShowContextMBean.class);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   348
        assertEquals("context seen by MBean accessed within namespace",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   349
                singletonMap("mickey", "mouse"), cshow.getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   350
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   351
        MBeanServer ccmbs = ClientContext.withContext(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   352
                cmbs, "donald", "duck");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   353
        ShowContextMBean ccshow =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   354
                JMX.newMBeanProxy(ccmbs, name, ShowContextMBean.class);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   355
        Map<String, String> disney = new HashMap<String, String>();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   356
        disney.put("mickey", "mouse");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   357
        disney.put("donald", "duck");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   358
        assertEquals("context seen by MBean in nested namespace",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   359
                disney, ccshow.getContext());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   360
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   361
        // Test that all MBS ops produce reasonable results
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   362
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   363
        ObjectName logger = new ObjectName("a:type=Logger");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   364
        DynamicMBean showMBean =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   365
                new StandardMBean(new ShowContext(), ShowContextMBean.class);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   366
        LogIH mbeanLogIH = new LogIH(showMBean);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   367
        DynamicMBean logMBean = newSnoop(DynamicMBean.class, mbeanLogIH);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   368
        ObjectInstance loggerOI = ccmbs.registerMBean(logMBean, logger);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   369
        assertEquals("ObjectName returned by createMBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   370
                logger, loggerOI.getObjectName());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   371
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   372
        // We get an getMBeanInfo call to determine the className in the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   373
        // ObjectInstance to return from registerMBean.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   374
        assertCalled(mbeanLogIH, "getMBeanInfo", disney);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   375
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   376
        ccmbs.getAttribute(logger, "Thing");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   377
        assertCalled(mbeanLogIH, "getAttribute", disney);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   378
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   379
        ccmbs.getAttributes(logger, new String[] {"Thing", "Context"});
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   380
        assertCalled(mbeanLogIH, "getAttributes", disney);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   381
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   382
        ccmbs.setAttribute(logger, new Attribute("Thing", "bar"));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   383
        assertCalled(mbeanLogIH, "setAttribute", disney);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   384
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   385
        ccmbs.setAttributes(logger, new AttributeList(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   386
                Arrays.asList(new Attribute("Thing", "baz"))));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   387
        assertCalled(mbeanLogIH, "setAttributes", disney);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   388
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   389
        ccmbs.getMBeanInfo(logger);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   390
        assertCalled(mbeanLogIH, "getMBeanInfo", disney);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   391
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   392
        Set<ObjectName> names = ccmbs.queryNames(null, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   393
        Set<ObjectName> expectedNames = new HashSet<ObjectName>(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   394
                Collections.singleton(MBeanServerDelegate.DELEGATE_NAME));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   395
        assertEquals("context namespace query includes expected names",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   396
                true, names.containsAll(expectedNames));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   397
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   398
        Set<ObjectName> nsNames = ccmbs.queryNames(new ObjectName("*//:*"), null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   399
        Set<ObjectName> expectedNsNames = new HashSet<ObjectName>(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   400
                Arrays.asList(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   401
                new ObjectName(ClientContext.NAMESPACE +
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   402
                ObjectName.NAMESPACE_SEPARATOR + ":" +
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   403
                JMXNamespace.TYPE_ASSIGNMENT)));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   404
        assertEquals("context namespace query includes namespace MBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   405
                true, nsNames.containsAll(expectedNsNames));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   406
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   407
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   408
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   409
        Set<ObjectInstance> insts = ccmbs.queryMBeans(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   410
                MBeanServerDelegate.DELEGATE_NAME, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   411
        assertEquals("size of set from MBeanServerDelegate query", 1, insts.size());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   412
        assertEquals("ObjectName from MBeanServerDelegate query",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   413
                MBeanServerDelegate.DELEGATE_NAME,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   414
                insts.iterator().next().getObjectName());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   415
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   416
        ObjectName createdName = new ObjectName("a:type=Created");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   417
        ObjectInstance createdOI =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   418
                ccmbs.createMBean(ShowContext.class.getName(), createdName);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   419
        assertEquals("class name from createMBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   420
                ShowContext.class.getName(), createdOI.getClassName());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   421
        assertEquals("ObjectName from createMBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   422
                createdName, createdOI.getObjectName());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   423
        assertEquals("context within createMBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   424
                disney, ccmbs.getAttribute(createdName, "CreationContext"));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   425
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   426
        NotificationListener nothingListener = new NotificationListener() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   427
            public void handleNotification(Notification n, Object h) {}
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   428
        };
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   429
        ccmbs.addNotificationListener(createdName, nothingListener, null, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   430
        ccmbs.removeNotificationListener(createdName, nothingListener, null, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   431
        ccmbs.addNotificationListener(createdName, nothingListener, null, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   432
        ccmbs.removeNotificationListener(createdName, nothingListener);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   433
        Set<String> expectedOps = new HashSet<String>(Arrays.asList(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   434
                "preRegister", "postRegister", "addNotificationListener",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   435
                "removeNL1", "removeNL3", "getNotificationInfo"));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   436
        assertEquals("operations called on MBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   437
                expectedOps, ccmbs.getAttribute(createdName, "CalledOps"));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   438
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   439
        assertEquals("ClassLoader for MBean",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   440
                ShowContext.class.getClassLoader(),
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   441
                ccmbs.getClassLoaderFor(createdName));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   442
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   443
        assertEquals("isRegistered", true, ccmbs.isRegistered(createdName));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   444
        assertEquals("isInstanceOf", true, ccmbs.isInstanceOf(createdName,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   445
                ShowContext.class.getName()));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   446
        assertEquals("isInstanceOf", false, ccmbs.isInstanceOf(createdName,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   447
                DynamicMBean.class.getName()));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   448
        ccmbs.unregisterMBean(createdName);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   449
        assertEquals("isRegistered after unregister",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   450
                false, ccmbs.isRegistered(createdName));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   451
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   452
        MLet mlet = new MLet();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   453
        ObjectName defaultMLetName = new ObjectName("DefaultDomain:type=MLet");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   454
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   455
        ccmbs.registerMBean(mlet, defaultMLetName);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   456
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   457
        assertEquals("getClassLoader", mlet, ccmbs.getClassLoader(defaultMLetName));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   458
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   459
        assertEquals("number of MBean operations", 0, mbeanLogIH.log.size());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   460
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   461
        // Test that contexts still work when we can't combine two encoded contexts.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   462
        // Here, we wrap cmbs (mickey=mouse) so that ccmbs2 (donald=duck) cannot
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   463
        // see that it already contains a context and therefore cannot combine
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   464
        // into mickey=mouse;donald=duck.  We don't actually use the snoop
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   465
        // capabilities of the returned object -- we just want an opaque
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   466
        // MBeanServer wrapper
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   467
        MBeanServer cmbs2 = newSnoop(MBeanServer.class, new LogIH(cmbs));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   468
        MBeanServer ccmbs2 = ClientContext.withContext(cmbs2, "donald", "duck");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   469
        assertEquals("context when combination is impossible",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   470
                disney, ccmbs2.getAttribute(name, "Context"));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   471
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   472
        // Test failure cases of ClientContext.encode
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   473
        final List<Map<String, String>> badEncodeArgs =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   474
                Arrays.asList(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   475
                    null,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   476
                    Collections.<String,String>singletonMap(null, "foo"),
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   477
                    Collections.<String,String>singletonMap("foo", null));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   478
        for (Map<String, String> bad : badEncodeArgs) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   479
            try {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   480
                String oops = ClientContext.encode(bad);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   481
                failed("ClientContext.encode(" + bad + ") should have failed: "
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   482
                        + oops);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   483
            } catch (Exception e) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   484
                assertEquals("Exception for ClientContext.encode(" + bad + ")",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   485
                        IllegalArgumentException.class, e.getClass());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   486
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   487
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   488
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   489
        // ADD NEW TESTS HERE ^^^
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   490
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   491
        if (failure != null)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   492
            throw new Exception(failure);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   493
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   494
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   495
    private static void assertEquals(String what, Object x, Object y) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   496
        if (!equal(x, y))
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   497
            failed(what + ": expected " + string(x) + "; got " + string(y));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   498
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   499
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   500
    private static boolean equal(Object x, Object y) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   501
        if (x == y)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   502
            return true;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   503
        if (x == null || y == null)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   504
            return false;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   505
        if (x.getClass().isArray())
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   506
            return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   507
        return x.equals(y);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   508
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   509
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   510
    private static String string(Object x) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   511
        String s = Arrays.deepToString(new Object[] {x});
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   512
        return s.substring(1, s.length() - 1);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   513
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   514
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   515
    private static void assertCalled(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   516
            LogIH logIH, String op, Map<String, String> expectedContext) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   517
        assertCalled(logIH, op, expectedContext, (Object[]) null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   518
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   519
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   520
    private static void assertCalled(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   521
            LogIH logIH, String op, Map<String, String> expectedContext,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   522
            Object... params) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   523
        LogRecord lr = logIH.log.remove();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   524
        assertEquals("called operation", op, lr.op);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   525
        if (params != null)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   526
            assertEquals("operation parameters", params, lr.params);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   527
        assertEquals("operation context", expectedContext, lr.context);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   528
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   529
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   530
    private static void failed(String why) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   531
        failure = why;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   532
        new Throwable("FAILED: " + why).printStackTrace(System.out);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   533
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   534
}