test/jdk/javax/management/monitor/NullAttributeValueTest.java
author lfoltan
Tue, 20 Feb 2018 07:46:40 -0500
changeset 49026 844bf1deff1a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196884: VS2017 Multiple Type Cast Conversion Compilation Errors Summary: Change the type of symbolic constant badAddressVal and introduce specific casts to fix multiple type cast conversion compilation errors. Reviewed-by: coleenp, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
20760
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
    26
 * @bug 6200031 8025206
2
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 null value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Luis-Miguel Alventosa
20760
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
    31
 * @author Shanliang JIANG
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    32
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run clean NullAttributeValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run build NullAttributeValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @run main NullAttributeValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class NullAttributeValueTest implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // Flag to notify that a message has been received
20760
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
    44
    private volatile boolean messageReceived = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // MBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public class ObservedObject implements ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        public Integer getIntegerAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public String getStringAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public interface ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public Integer getIntegerAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public String getStringAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Notification handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void handleNotification(Notification notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                   Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        MonitorNotification n = (MonitorNotification) notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        echo("\tInside handleNotification...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        String type = n.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (type.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                echo("\t\t" + n.getObservedAttribute() + " is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                echo("\t\tDerived Gauge = " + n.getDerivedGauge());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                echo("\t\tTrigger = " + n.getTrigger());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                messageReceived = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                echo("\t\tSkipping notification of type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            echo("\tError in handleNotification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Update the counter and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public int counterMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        CounterMonitor counterMonitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // Create a new CounterMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            echo(">>> CREATE a new CounterMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ObjectName counterMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                            domain + ":type=" + CounterMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            counterMonitor = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            server.registerMBean(counterMonitor, counterMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            echo(">>> ADD a listener to the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            counterMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            echo(">>> CREATE a new ObservedObject MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            ObjectName obsObjName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            ObservedObject obsObj = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            server.registerMBean(obsObj, obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            echo(">>> SET the attributes of the CounterMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            counterMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            counterMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            counterMonitor.setNotify(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            echo("\tATTRIBUTE \"NotifyFlag\"        = true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            Integer threshold = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            counterMonitor.setInitThreshold(threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            counterMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            echo(">>> START the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            counterMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
20760
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   138
            return checkReceived(granularityperiod, "CounterMonitor");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (counterMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                counterMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Update the gauge and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public int gaugeMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        GaugeMonitor gaugeMonitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // Create a new GaugeMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            echo(">>> CREATE a new GaugeMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            ObjectName gaugeMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            domain + ":type=" + GaugeMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            gaugeMonitor = new GaugeMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            server.registerMBean(gaugeMonitor, gaugeMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            echo(">>> ADD a listener to the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            gaugeMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            echo(">>> CREATE a new ObservedObject MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            ObjectName obsObjName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ObservedObject obsObj = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            server.registerMBean(obsObj, obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            echo(">>> SET the attributes of the GaugeMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            gaugeMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            gaugeMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            gaugeMonitor.setNotifyLow(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            gaugeMonitor.setNotifyHigh(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            echo("\tATTRIBUTE \"Notify High Flag\"  = true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Double highThreshold = 3.0, lowThreshold = 2.5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            gaugeMonitor.setThresholds(highThreshold, lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            gaugeMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            echo(">>> START the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            gaugeMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
20760
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   202
            return checkReceived(granularityperiod, "GaugeMonitor");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (gaugeMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                gaugeMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Update the string and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public int stringMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        StringMonitor stringMonitor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // Create a new StringMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            echo(">>> CREATE a new StringMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            ObjectName stringMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            domain + ":type=" + StringMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            stringMonitor = new StringMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            server.registerMBean(stringMonitor, stringMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            echo(">>> ADD a listener to the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            stringMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            echo(">>> CREATE a new ObservedObject MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            ObjectName obsObjName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            ObservedObject obsObj = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            server.registerMBean(obsObj, obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            echo(">>> SET the attributes of the StringMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            stringMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            stringMonitor.setObservedAttribute("StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            stringMonitor.setNotifyMatch(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            echo("\tATTRIBUTE \"NotifyMatch\"       = true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            stringMonitor.setNotifyDiffer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            stringMonitor.setStringToCompare("do_match_now");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            echo("\tATTRIBUTE \"StringToCompare\"   = \"do_match_now\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            stringMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            echo(">>> START the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            stringMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
20760
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   265
            return checkReceived(granularityperiod, "StringMonitor");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (stringMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                stringMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Test the monitor notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public int monitorNotifications() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int error = counterMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        error += gaugeMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        error += stringMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
20760
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   289
    private int checkReceived(long granularityperiod, String caller) throws InterruptedException {
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   290
        int i = 100;
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   291
        do {
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   292
            Thread.sleep(granularityperiod);
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   293
        } while (!messageReceived && i-- > 0);
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   294
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   295
        if (messageReceived) {
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   296
            echo("\tOK: " + caller + " notification received");
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   297
        } else {
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   298
            echo("\tKO: " + caller + " notification missed or not emitted");
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   299
        }
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   300
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   301
        return messageReceived ? 0 : 1;
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   302
    }
50de706b1565 8025206: Intermittent test failure: javax/management/monitor/NullAttributeValueTest.java
sjiang
parents: 5506
diff changeset
   303
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Standalone entry point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Run the test and report to stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        NullAttributeValueTest test = new NullAttributeValueTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        int error = test.monitorNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            echo(">>> Unhappy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw new IllegalStateException("Test FAILED: Didn't get all " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                            "the notifications that were " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                            "expected by the test!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            echo(">>> Happy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
}