jdk/test/javax/management/monitor/NonComparableAttributeValueTest.java
author stefank
Fri, 17 Apr 2015 17:10:38 +0000
changeset 30266 ef82cd1f2db3
parent 26623 11289cf5c8fc
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6273541
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that the counter/gauge/string monitors emit a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          jmx.monitor.error.type notification when the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          being monitored returns a non comparable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean NonComparableAttributeValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build NonComparableAttributeValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main NonComparableAttributeValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class NonComparableAttributeValueTest implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // Flag to notify that a message has been received
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    42
    private volatile boolean messageReceived = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // MBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public class ObservedObject implements ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public Object getIntegerAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        public Object getStringAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public interface ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public Object getIntegerAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        public Object getStringAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Notification handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void handleNotification(Notification notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                   Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        MonitorNotification n = (MonitorNotification) notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        echo("\tInside handleNotification...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String type = n.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (type.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                echo("\t\t" + n.getObservedAttribute() + " is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                echo("\t\tDerived Gauge = " + n.getDerivedGauge());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                echo("\t\tTrigger = " + n.getTrigger());
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    72
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    73
                synchronized (this) {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    74
                    messageReceived = true;
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    75
                    notifyAll();
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    76
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                echo("\t\tSkipping notification of type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            echo("\tError in handleNotification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Update the counter and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public int counterMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        CounterMonitor counterMonitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            // Create a new CounterMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            echo(">>> CREATE a new CounterMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ObjectName counterMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                            domain + ":type=" + CounterMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            counterMonitor = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            server.registerMBean(counterMonitor, counterMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            echo(">>> ADD a listener to the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            counterMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            echo(">>> CREATE a new ObservedObject MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            ObjectName obsObjName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ObservedObject obsObj = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            server.registerMBean(obsObj, obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            echo(">>> SET the attributes of the CounterMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            counterMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            counterMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            counterMonitor.setNotify(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            echo("\tATTRIBUTE \"NotifyFlag\"        = true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            Integer threshold = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            counterMonitor.setInitThreshold(threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            counterMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            echo(">>> START the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            counterMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            //
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   143
            doWait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                echo("\tOK: CounterMonitor notification received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                echo("\tKO: CounterMonitor notification missed or not emitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (counterMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                counterMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Update the gauge and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int gaugeMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        GaugeMonitor gaugeMonitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // Create a new GaugeMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            echo(">>> CREATE a new GaugeMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            ObjectName gaugeMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            domain + ":type=" + GaugeMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            gaugeMonitor = new GaugeMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            server.registerMBean(gaugeMonitor, gaugeMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            echo(">>> ADD a listener to the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            gaugeMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            echo(">>> CREATE a new ObservedObject MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            ObjectName obsObjName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            ObservedObject obsObj = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            server.registerMBean(obsObj, obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            echo(">>> SET the attributes of the GaugeMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            gaugeMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            gaugeMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            gaugeMonitor.setNotifyLow(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            gaugeMonitor.setNotifyHigh(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            echo("\tATTRIBUTE \"Notify High Flag\"  = true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            Double highThreshold = 3.0, lowThreshold = 2.5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            gaugeMonitor.setThresholds(highThreshold, lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            gaugeMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            echo(">>> START the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            gaugeMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            //
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   218
            doWait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                echo("\tOK: GaugeMonitor notification received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                echo("\tKO: GaugeMonitor notification missed or not emitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (gaugeMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                gaugeMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Update the string and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public int stringMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        StringMonitor stringMonitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // Create a new StringMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            echo(">>> CREATE a new StringMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            ObjectName stringMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            domain + ":type=" + StringMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            stringMonitor = new StringMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            server.registerMBean(stringMonitor, stringMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            echo(">>> ADD a listener to the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            stringMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            echo(">>> CREATE a new ObservedObject MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            ObjectName obsObjName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            ObservedObject obsObj = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            server.registerMBean(obsObj, obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            echo(">>> SET the attributes of the StringMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            stringMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            stringMonitor.setObservedAttribute("StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            stringMonitor.setNotifyMatch(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            echo("\tATTRIBUTE \"NotifyMatch\"       = true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            stringMonitor.setNotifyDiffer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            stringMonitor.setStringToCompare("do_match_now");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            echo("\tATTRIBUTE \"StringToCompare\"   = \"do_match_now\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            stringMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            echo(">>> START the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            stringMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            //
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   292
            doWait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                echo("\tOK: StringMonitor notification received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                echo("\tKO: StringMonitor notification missed or not emitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (stringMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                stringMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Test the monitor notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public int monitorNotifications() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int error = counterMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        error += gaugeMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        error += stringMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /*
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   332
     * Wait messageReceived to be true
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   333
     */
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   334
    synchronized void doWait() {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   335
        while (!messageReceived) {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   336
            try {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   337
                wait();
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   338
            } catch (InterruptedException e) {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   339
                System.err.println("Got unexpected exception: " + e);
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   340
                e.printStackTrace();
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   341
                break;
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   342
            }
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   343
        }
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   344
    }
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   345
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   346
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Standalone entry point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Run the test and report to stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        NonComparableAttributeValueTest test = new NonComparableAttributeValueTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int error = test.monitorNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            echo(">>> Unhappy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new IllegalStateException("Test FAILED: Didn't get all " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                            "the notifications that were " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                            "expected by the test!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            echo(">>> Happy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}