jdk/test/javax/management/monitor/CounterMonitorThresholdTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 20759 4e48d082f720
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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 6229368
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Wrong threshold value in CounterMonitor with offset and modulus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean CounterMonitorThresholdTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build CounterMonitorThresholdTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main CounterMonitorThresholdTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.MBeanServerInvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.monitor.CounterMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.monitor.CounterMonitorMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.monitor.MonitorNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class CounterMonitorThresholdTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // Offset = 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static int counter1[]      = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static int derivedGauge1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static int threshold1[]    = { 1, 2, 3, 4, 5, 5, 1, 1, 1, 2, 3, 4, 5, 1, 1, 2 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Offset = 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static int counter2[]      = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static int derivedGauge2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static int threshold2[]    = { 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 4 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public interface TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public int getCounter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public void setCounter(int count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static class Test implements TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public int getCounter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public void setCounter(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            this.count = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        private int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static class Listener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public void handleNotification(Notification n, Object hb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.println("\tReceived notification: " + n.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (n instanceof MonitorNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                MonitorNotification mn = (MonitorNotification) n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                System.out.println("\tSource: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    mn.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                System.out.println("\tType: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    mn.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                System.out.println("\tTimeStamp: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    mn.getTimeStamp());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                System.out.println("\tObservedObject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    mn.getObservedObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                System.out.println("\tObservedAttribute: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    mn.getObservedAttribute());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                System.out.println("\tDerivedGauge: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    mn.getDerivedGauge());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                System.out.println("\tTrigger: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    mn.getTrigger());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void runTest(int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                               int counter[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                               int derivedGauge[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                               int threshold[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // Retrieve the platform MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.out.println("\nRetrieve the platform MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String domain = mbs.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // Create and register TestMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ObjectName name =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            new ObjectName(domain +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                           ":type=" + Test.class.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                           ",offset=" + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        mbs.createMBean(Test.class.getName(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        TestMBean mbean = (TestMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                mbs, name, TestMBean.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Create and register CounterMonitorMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ObjectName cmn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            new ObjectName(domain +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                           ":type=" + CounterMonitor.class.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                           ",offset=" + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        CounterMonitor m = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        mbs.registerMBean(m, cmn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        CounterMonitorMBean cm = (CounterMonitorMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                mbs, cmn, CounterMonitorMBean.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ((NotificationEmitter) cm).addNotificationListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            new Listener(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        cm.addObservedObject(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        cm.setObservedAttribute("Counter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        cm.setGranularityPeriod(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        cm.setInitThreshold(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        cm.setOffset(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        cm.setModulus(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        cm.setNotify(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // Start the monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        System.out.println("\nStart monitoring...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        cm.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Play with counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        for (int i = 0; i < counter.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            mbean.setCounter(counter[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("\nCounter = " + mbean.getCounter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Thread.sleep(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Integer derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            System.out.println("Derived Gauge = " + derivedGaugeValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (derivedGaugeValue.intValue() != derivedGauge[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                System.out.println("Wrong derived gauge! Current value = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    derivedGaugeValue + " Expected value = " + derivedGauge[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                System.out.println("\nStop monitoring...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                cm.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new IllegalArgumentException("wrong derived gauge");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            Number thresholdValue = cm.getThreshold(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.out.println("Threshold = " + thresholdValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (thresholdValue.intValue() != threshold[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                System.out.println("Wrong threshold! Current value = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    thresholdValue + " Expected value = " + threshold[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                System.out.println("\nStop monitoring...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                cm.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new IllegalArgumentException("wrong threshold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            Thread.sleep(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // Stop the monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        System.out.println("\nStop monitoring...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        cm.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        runTest(1, counter1, derivedGauge1, threshold1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        runTest(3, counter2, derivedGauge2, threshold2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}