jdk/test/javax/management/monitor/CounterMonitorInitThresholdTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
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: 5506
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6333528
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that the initial threshold is properly used by the observed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          objects added before the counter monitor is started as well as by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          the observed objects which are added once the monitor is started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Luis-Miguel Alventosa
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    31
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run clean CounterMonitorInitThresholdTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run build CounterMonitorInitThresholdTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main CounterMonitorInitThresholdTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServerInvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.monitor.CounterMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.monitor.CounterMonitorMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.monitor.MonitorNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class CounterMonitorInitThresholdTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public interface TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public int getCounter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public void setCounter(int count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static class Test implements TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public int getCounter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public void setCounter(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.count = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        private int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static class Listener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public void handleNotification(Notification n, Object hb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.out.println("\tReceived notification: " + n.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (n instanceof MonitorNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                MonitorNotification mn = (MonitorNotification) n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                System.out.println("\tSource: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    mn.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                System.out.println("\tType: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    mn.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                System.out.println("\tTimeStamp: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    mn.getTimeStamp());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                System.out.println("\tObservedObject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    mn.getObservedObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                System.out.println("\tObservedAttribute: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    mn.getObservedAttribute());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                System.out.println("\tDerivedGauge: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    mn.getDerivedGauge());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                System.out.println("\tTrigger: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    mn.getTrigger());
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static void runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // Retrieve the platform MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        System.out.println("\nRetrieve the platform MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        String domain = mbs.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // Create and register TestMBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ObjectName name1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            new ObjectName(domain +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                           ":type=" + Test.class.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                           ",name=1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        mbs.createMBean(Test.class.getName(), name1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        TestMBean mbean1 = (TestMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                mbs, name1, TestMBean.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        ObjectName name2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            new ObjectName(domain +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                           ":type=" + Test.class.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                           ",name=2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        mbs.createMBean(Test.class.getName(), name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        TestMBean mbean2 = (TestMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                mbs, name2, TestMBean.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Create and register CounterMonitorMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        ObjectName cmn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            new ObjectName(domain +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                           ":type=" + CounterMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        CounterMonitor m = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        mbs.registerMBean(m, cmn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        CounterMonitorMBean cm = (CounterMonitorMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                mbs, cmn, CounterMonitorMBean.class, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ((NotificationEmitter) cm).addNotificationListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            new Listener(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        cm.setObservedAttribute("Counter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        cm.setGranularityPeriod(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        cm.setInitThreshold(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        cm.setNotify(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Add observed object name1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println("\nObservedObject \"" + name1 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            "\" registered before starting the monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        cm.addObservedObject(name1);
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 for name1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.out.println("\nTest ObservedObject \"" + name1 + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            mbean1.setCounter(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.out.println("\nCounter = " + mbean1.getCounter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Thread.sleep(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            Number thresholdValue = cm.getThreshold(name1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            System.out.println("Threshold = " + thresholdValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (thresholdValue.intValue() != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                System.out.println("Wrong threshold! Current value = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    thresholdValue + " Expected value = 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                System.out.println("\nStop monitoring...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                cm.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                throw new IllegalArgumentException("wrong threshold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            Thread.sleep(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Add observed object name2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        System.out.println("\nObservedObject \"" + name2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            "\" registered after starting the monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        cm.addObservedObject(name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // Play with counter for name2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        System.out.println("\nTest ObservedObject \"" + name2 + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            mbean2.setCounter(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            System.out.println("\nCounter = " + mbean2.getCounter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            Thread.sleep(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Number thresholdValue = cm.getThreshold(name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            System.out.println("Threshold = " + thresholdValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (thresholdValue.intValue() != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                System.out.println("Wrong threshold! Current value = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    thresholdValue + " Expected value = 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                System.out.println("\nStop monitoring...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                cm.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                throw new IllegalArgumentException("wrong threshold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            Thread.sleep(300);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // Stop the monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        System.out.println("\nStop monitoring...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        cm.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}