Path: blob/master/test/jdk/javax/management/monitor/NullAttributeValueTest.java
41152 views
/*1* Copyright (c) 2005, 2015, 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*32* @run clean NullAttributeValueTest33* @run build NullAttributeValueTest34* @run main NullAttributeValueTest35*/3637import javax.management.*;38import javax.management.monitor.*;3940public class NullAttributeValueTest implements NotificationListener {4142// Flag to notify that a message has been received43private volatile boolean messageReceived = false;4445// MBean class46public class ObservedObject implements ObservedObjectMBean {47public Integer getIntegerAttribute() {48return null;49}50public String getStringAttribute() {51return null;52}53}5455// MBean interface56public interface ObservedObjectMBean {57public Integer getIntegerAttribute();58public String getStringAttribute();59}6061// Notification handler62public void handleNotification(Notification notification,63Object handback) {64MonitorNotification n = (MonitorNotification) notification;65echo("\tInside handleNotification...");66String type = n.getType();67try {68if (type.equals(69MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {70echo("\t\t" + n.getObservedAttribute() + " is null");71echo("\t\tDerived Gauge = " + n.getDerivedGauge());72echo("\t\tTrigger = " + n.getTrigger());73messageReceived = true;74} else {75echo("\t\tSkipping notification of type: " + type);76}77} catch (Exception e) {78echo("\tError in handleNotification!");79e.printStackTrace(System.out);80}81}8283/**84* Update the counter and check for notifications85*/86public int counterMonitorNotification() throws Exception {87CounterMonitor counterMonitor = null;88try {89MBeanServer server = MBeanServerFactory.newMBeanServer();9091String domain = server.getDefaultDomain();9293// Create a new CounterMonitor MBean and add it to the MBeanServer.94//95echo(">>> CREATE a new CounterMonitor MBean");96ObjectName counterMonitorName = new ObjectName(97domain + ":type=" + CounterMonitor.class.getName());98counterMonitor = new CounterMonitor();99server.registerMBean(counterMonitor, counterMonitorName);100101echo(">>> ADD a listener to the CounterMonitor");102counterMonitor.addNotificationListener(this, null, null);103104//105// MANAGEMENT OF A STANDARD MBEAN106//107108echo(">>> CREATE a new ObservedObject MBean");109110ObjectName obsObjName =111ObjectName.getInstance(domain + ":type=ObservedObject");112ObservedObject obsObj = new ObservedObject();113server.registerMBean(obsObj, obsObjName);114115echo(">>> SET the attributes of the CounterMonitor:");116117counterMonitor.addObservedObject(obsObjName);118echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);119120counterMonitor.setObservedAttribute("IntegerAttribute");121echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");122123counterMonitor.setNotify(true);124echo("\tATTRIBUTE \"NotifyFlag\" = true");125126Integer threshold = 2;127counterMonitor.setInitThreshold(threshold);128echo("\tATTRIBUTE \"Threshold\" = " + threshold);129130int granularityperiod = 500;131counterMonitor.setGranularityPeriod(granularityperiod);132echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);133134echo(">>> START the CounterMonitor");135counterMonitor.start();136137return checkReceived(granularityperiod, "CounterMonitor");138} finally {139if (counterMonitor != null)140counterMonitor.stop();141}142}143144/**145* Update the gauge and check for notifications146*/147public int gaugeMonitorNotification() throws Exception {148GaugeMonitor gaugeMonitor = null;149try {150MBeanServer server = MBeanServerFactory.newMBeanServer();151152String domain = server.getDefaultDomain();153154// Create a new GaugeMonitor MBean and add it to the MBeanServer.155//156echo(">>> CREATE a new GaugeMonitor MBean");157ObjectName gaugeMonitorName = new ObjectName(158domain + ":type=" + GaugeMonitor.class.getName());159gaugeMonitor = new GaugeMonitor();160server.registerMBean(gaugeMonitor, gaugeMonitorName);161162echo(">>> ADD a listener to the GaugeMonitor");163gaugeMonitor.addNotificationListener(this, null, null);164165//166// MANAGEMENT OF A STANDARD MBEAN167//168169echo(">>> CREATE a new ObservedObject MBean");170171ObjectName obsObjName =172ObjectName.getInstance(domain + ":type=ObservedObject");173ObservedObject obsObj = new ObservedObject();174server.registerMBean(obsObj, obsObjName);175176echo(">>> SET the attributes of the GaugeMonitor:");177178gaugeMonitor.addObservedObject(obsObjName);179echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);180181gaugeMonitor.setObservedAttribute("IntegerAttribute");182echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");183184gaugeMonitor.setNotifyLow(false);185gaugeMonitor.setNotifyHigh(true);186echo("\tATTRIBUTE \"Notify Low Flag\" = false");187echo("\tATTRIBUTE \"Notify High Flag\" = true");188189Double highThreshold = 3.0, lowThreshold = 2.5;190gaugeMonitor.setThresholds(highThreshold, lowThreshold);191echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);192echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);193194int granularityperiod = 500;195gaugeMonitor.setGranularityPeriod(granularityperiod);196echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);197198echo(">>> START the GaugeMonitor");199gaugeMonitor.start();200201return checkReceived(granularityperiod, "GaugeMonitor");202} finally {203if (gaugeMonitor != null)204gaugeMonitor.stop();205}206}207208/**209* Update the string and check for notifications210*/211public int stringMonitorNotification() throws Exception {212StringMonitor stringMonitor = null;213try {214MBeanServer server = MBeanServerFactory.newMBeanServer();215216String domain = server.getDefaultDomain();217218// Create a new StringMonitor MBean and add it to the MBeanServer.219//220echo(">>> CREATE a new StringMonitor MBean");221ObjectName stringMonitorName = new ObjectName(222domain + ":type=" + StringMonitor.class.getName());223stringMonitor = new StringMonitor();224server.registerMBean(stringMonitor, stringMonitorName);225226echo(">>> ADD a listener to the StringMonitor");227stringMonitor.addNotificationListener(this, null, null);228229//230// MANAGEMENT OF A STANDARD MBEAN231//232233echo(">>> CREATE a new ObservedObject MBean");234235ObjectName obsObjName =236ObjectName.getInstance(domain + ":type=ObservedObject");237ObservedObject obsObj = new ObservedObject();238server.registerMBean(obsObj, obsObjName);239240echo(">>> SET the attributes of the StringMonitor:");241242stringMonitor.addObservedObject(obsObjName);243echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);244245stringMonitor.setObservedAttribute("StringAttribute");246echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");247248stringMonitor.setNotifyMatch(true);249echo("\tATTRIBUTE \"NotifyMatch\" = true");250251stringMonitor.setNotifyDiffer(false);252echo("\tATTRIBUTE \"NotifyDiffer\" = false");253254stringMonitor.setStringToCompare("do_match_now");255echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\"");256257int granularityperiod = 500;258stringMonitor.setGranularityPeriod(granularityperiod);259echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);260261echo(">>> START the StringMonitor");262stringMonitor.start();263264return checkReceived(granularityperiod, "StringMonitor");265} finally {266if (stringMonitor != null)267stringMonitor.stop();268}269}270271/**272* Test the monitor notifications.273*/274public int monitorNotifications() throws Exception {275echo(">>> ----------------------------------------");276messageReceived = false;277int error = counterMonitorNotification();278echo(">>> ----------------------------------------");279messageReceived = false;280error += gaugeMonitorNotification();281echo(">>> ----------------------------------------");282messageReceived = false;283error += stringMonitorNotification();284echo(">>> ----------------------------------------");285return error;286}287288private int checkReceived(long granularityperiod, String caller) throws InterruptedException {289int i = 100;290do {291Thread.sleep(granularityperiod);292} while (!messageReceived && i-- > 0);293294if (messageReceived) {295echo("\tOK: " + caller + " notification received");296} else {297echo("\tKO: " + caller + " notification missed or not emitted");298}299300return messageReceived ? 0 : 1;301}302303/*304* Print message305*/306private static void echo(String message) {307System.out.println(message);308}309310/*311* Standalone entry point.312*313* Run the test and report to stdout.314*/315public static void main (String args[]) throws Exception {316NullAttributeValueTest test = new NullAttributeValueTest();317int error = test.monitorNotifications();318if (error > 0) {319echo(">>> Unhappy Bye, Bye!");320throw new IllegalStateException("Test FAILED: Didn't get all " +321"the notifications that were " +322"expected by the test!");323} else {324echo(">>> Happy Bye, Bye!");325}326}327}328329330