Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/NullAttributeValueTest.java
38839 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6200031 802520626* @summary Test that the counter/gauge/string monitors emit a27* jmx.monitor.error.type notification when the attribute28* being monitored returns a null value.29* @author Luis-Miguel Alventosa30* @author Shanliang JIANG31* @run clean NullAttributeValueTest32* @run build NullAttributeValueTest33* @run main NullAttributeValueTest34*/3536import javax.management.*;37import javax.management.monitor.*;3839public class NullAttributeValueTest implements NotificationListener {4041// Flag to notify that a message has been received42private volatile boolean messageReceived = false;4344// MBean class45public class ObservedObject implements ObservedObjectMBean {46public Integer getIntegerAttribute() {47return null;48}49public String getStringAttribute() {50return null;51}52}5354// MBean interface55public interface ObservedObjectMBean {56public Integer getIntegerAttribute();57public String getStringAttribute();58}5960// Notification handler61public void handleNotification(Notification notification,62Object handback) {63MonitorNotification n = (MonitorNotification) notification;64echo("\tInside handleNotification...");65String type = n.getType();66try {67if (type.equals(68MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {69echo("\t\t" + n.getObservedAttribute() + " is null");70echo("\t\tDerived Gauge = " + n.getDerivedGauge());71echo("\t\tTrigger = " + n.getTrigger());72messageReceived = true;73} else {74echo("\t\tSkipping notification of type: " + type);75}76} catch (Exception e) {77echo("\tError in handleNotification!");78e.printStackTrace(System.out);79}80}8182/**83* Update the counter and check for notifications84*/85public int counterMonitorNotification() throws Exception {86CounterMonitor counterMonitor = null;87try {88MBeanServer server = MBeanServerFactory.newMBeanServer();8990String domain = server.getDefaultDomain();9192// Create a new CounterMonitor MBean and add it to the MBeanServer.93//94echo(">>> CREATE a new CounterMonitor MBean");95ObjectName counterMonitorName = new ObjectName(96domain + ":type=" + CounterMonitor.class.getName());97counterMonitor = new CounterMonitor();98server.registerMBean(counterMonitor, counterMonitorName);99100echo(">>> ADD a listener to the CounterMonitor");101counterMonitor.addNotificationListener(this, null, null);102103//104// MANAGEMENT OF A STANDARD MBEAN105//106107echo(">>> CREATE a new ObservedObject MBean");108109ObjectName obsObjName =110ObjectName.getInstance(domain + ":type=ObservedObject");111ObservedObject obsObj = new ObservedObject();112server.registerMBean(obsObj, obsObjName);113114echo(">>> SET the attributes of the CounterMonitor:");115116counterMonitor.addObservedObject(obsObjName);117echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);118119counterMonitor.setObservedAttribute("IntegerAttribute");120echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");121122counterMonitor.setNotify(true);123echo("\tATTRIBUTE \"NotifyFlag\" = true");124125Integer threshold = 2;126counterMonitor.setInitThreshold(threshold);127echo("\tATTRIBUTE \"Threshold\" = " + threshold);128129int granularityperiod = 500;130counterMonitor.setGranularityPeriod(granularityperiod);131echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);132133echo(">>> START the CounterMonitor");134counterMonitor.start();135136return checkReceived(granularityperiod, "CounterMonitor");137} finally {138if (counterMonitor != null)139counterMonitor.stop();140}141}142143/**144* Update the gauge and check for notifications145*/146public int gaugeMonitorNotification() throws Exception {147GaugeMonitor gaugeMonitor = null;148try {149MBeanServer server = MBeanServerFactory.newMBeanServer();150151String domain = server.getDefaultDomain();152153// Create a new GaugeMonitor MBean and add it to the MBeanServer.154//155echo(">>> CREATE a new GaugeMonitor MBean");156ObjectName gaugeMonitorName = new ObjectName(157domain + ":type=" + GaugeMonitor.class.getName());158gaugeMonitor = new GaugeMonitor();159server.registerMBean(gaugeMonitor, gaugeMonitorName);160161echo(">>> ADD a listener to the GaugeMonitor");162gaugeMonitor.addNotificationListener(this, null, null);163164//165// MANAGEMENT OF A STANDARD MBEAN166//167168echo(">>> CREATE a new ObservedObject MBean");169170ObjectName obsObjName =171ObjectName.getInstance(domain + ":type=ObservedObject");172ObservedObject obsObj = new ObservedObject();173server.registerMBean(obsObj, obsObjName);174175echo(">>> SET the attributes of the GaugeMonitor:");176177gaugeMonitor.addObservedObject(obsObjName);178echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);179180gaugeMonitor.setObservedAttribute("IntegerAttribute");181echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");182183gaugeMonitor.setNotifyLow(false);184gaugeMonitor.setNotifyHigh(true);185echo("\tATTRIBUTE \"Notify Low Flag\" = false");186echo("\tATTRIBUTE \"Notify High Flag\" = true");187188Double highThreshold = 3.0, lowThreshold = 2.5;189gaugeMonitor.setThresholds(highThreshold, lowThreshold);190echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);191echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);192193int granularityperiod = 500;194gaugeMonitor.setGranularityPeriod(granularityperiod);195echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);196197echo(">>> START the GaugeMonitor");198gaugeMonitor.start();199200return checkReceived(granularityperiod, "GaugeMonitor");201} finally {202if (gaugeMonitor != null)203gaugeMonitor.stop();204}205}206207/**208* Update the string and check for notifications209*/210public int stringMonitorNotification() throws Exception {211StringMonitor stringMonitor = null;212try {213MBeanServer server = MBeanServerFactory.newMBeanServer();214215String domain = server.getDefaultDomain();216217// Create a new StringMonitor MBean and add it to the MBeanServer.218//219echo(">>> CREATE a new StringMonitor MBean");220ObjectName stringMonitorName = new ObjectName(221domain + ":type=" + StringMonitor.class.getName());222stringMonitor = new StringMonitor();223server.registerMBean(stringMonitor, stringMonitorName);224225echo(">>> ADD a listener to the StringMonitor");226stringMonitor.addNotificationListener(this, null, null);227228//229// MANAGEMENT OF A STANDARD MBEAN230//231232echo(">>> CREATE a new ObservedObject MBean");233234ObjectName obsObjName =235ObjectName.getInstance(domain + ":type=ObservedObject");236ObservedObject obsObj = new ObservedObject();237server.registerMBean(obsObj, obsObjName);238239echo(">>> SET the attributes of the StringMonitor:");240241stringMonitor.addObservedObject(obsObjName);242echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);243244stringMonitor.setObservedAttribute("StringAttribute");245echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");246247stringMonitor.setNotifyMatch(true);248echo("\tATTRIBUTE \"NotifyMatch\" = true");249250stringMonitor.setNotifyDiffer(false);251echo("\tATTRIBUTE \"NotifyDiffer\" = false");252253stringMonitor.setStringToCompare("do_match_now");254echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\"");255256int granularityperiod = 500;257stringMonitor.setGranularityPeriod(granularityperiod);258echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);259260echo(">>> START the StringMonitor");261stringMonitor.start();262263return checkReceived(granularityperiod, "StringMonitor");264} finally {265if (stringMonitor != null)266stringMonitor.stop();267}268}269270/**271* Test the monitor notifications.272*/273public int monitorNotifications() throws Exception {274echo(">>> ----------------------------------------");275messageReceived = false;276int error = counterMonitorNotification();277echo(">>> ----------------------------------------");278messageReceived = false;279error += gaugeMonitorNotification();280echo(">>> ----------------------------------------");281messageReceived = false;282error += stringMonitorNotification();283echo(">>> ----------------------------------------");284return error;285}286287private int checkReceived(long granularityperiod, String caller) throws InterruptedException {288int i = 100;289do {290Thread.sleep(granularityperiod);291} while (!messageReceived && i-- > 0);292293if (messageReceived) {294echo("\tOK: " + caller + " notification received");295} else {296echo("\tKO: " + caller + " notification missed or not emitted");297}298299return messageReceived ? 0 : 1;300}301302/*303* Print message304*/305private static void echo(String message) {306System.out.println(message);307}308309/*310* Standalone entry point.311*312* Run the test and report to stdout.313*/314public static void main (String args[]) throws Exception {315NullAttributeValueTest test = new NullAttributeValueTest();316int error = test.monitorNotifications();317if (error > 0) {318echo(">>> Unhappy Bye, Bye!");319throw new IllegalStateException("Test FAILED: Didn't get all " +320"the notifications that were " +321"expected by the test!");322} else {323echo(">>> Happy Bye, Bye!");324}325}326}327328329