Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/NonComparableAttributeValueTest.java
38839 views
/*1* Copyright (c) 2005, 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 627354126* @summary Test that the counter/gauge/string monitors emit a27* jmx.monitor.error.type notification when the attribute28* being monitored returns a non comparable value.29* @author Luis-Miguel Alventosa30* @run clean NonComparableAttributeValueTest31* @run build NonComparableAttributeValueTest32* @run main NonComparableAttributeValueTest33*/3435import javax.management.*;36import javax.management.monitor.*;3738public class NonComparableAttributeValueTest implements NotificationListener {3940// Flag to notify that a message has been received41private volatile boolean messageReceived = false;4243// MBean class44public class ObservedObject implements ObservedObjectMBean {45public Object getIntegerAttribute() {46return new Object();47}48public Object getStringAttribute() {49return new Object();50}51}5253// MBean interface54public interface ObservedObjectMBean {55public Object getIntegerAttribute();56public Object getStringAttribute();57}5859// Notification handler60public void handleNotification(Notification notification,61Object handback) {62MonitorNotification n = (MonitorNotification) notification;63echo("\tInside handleNotification...");64String type = n.getType();65try {66if (type.equals(67MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {68echo("\t\t" + n.getObservedAttribute() + " is null");69echo("\t\tDerived Gauge = " + n.getDerivedGauge());70echo("\t\tTrigger = " + n.getTrigger());7172synchronized (this) {73messageReceived = true;74notifyAll();75}76} else {77echo("\t\tSkipping notification of type: " + type);78}79} catch (Exception e) {80echo("\tError in handleNotification!");81e.printStackTrace(System.out);82}83}8485/**86* Update the counter and check for notifications87*/88public int counterMonitorNotification() throws Exception {8990CounterMonitor counterMonitor = null;91try {92MBeanServer server = MBeanServerFactory.newMBeanServer();9394String domain = server.getDefaultDomain();9596// Create a new CounterMonitor MBean and add it to the MBeanServer.97//98echo(">>> CREATE a new CounterMonitor MBean");99ObjectName counterMonitorName = new ObjectName(100domain + ":type=" + CounterMonitor.class.getName());101counterMonitor = new CounterMonitor();102server.registerMBean(counterMonitor, counterMonitorName);103104echo(">>> ADD a listener to the CounterMonitor");105counterMonitor.addNotificationListener(this, null, null);106107//108// MANAGEMENT OF A STANDARD MBEAN109//110111echo(">>> CREATE a new ObservedObject MBean");112113ObjectName obsObjName =114ObjectName.getInstance(domain + ":type=ObservedObject");115ObservedObject obsObj = new ObservedObject();116server.registerMBean(obsObj, obsObjName);117118echo(">>> SET the attributes of the CounterMonitor:");119120counterMonitor.addObservedObject(obsObjName);121echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);122123counterMonitor.setObservedAttribute("IntegerAttribute");124echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");125126counterMonitor.setNotify(true);127echo("\tATTRIBUTE \"NotifyFlag\" = true");128129Integer threshold = 2;130counterMonitor.setInitThreshold(threshold);131echo("\tATTRIBUTE \"Threshold\" = " + threshold);132133int granularityperiod = 500;134counterMonitor.setGranularityPeriod(granularityperiod);135echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);136137echo(">>> START the CounterMonitor");138counterMonitor.start();139140// Check if notification was received141//142doWait();143if (messageReceived) {144echo("\tOK: CounterMonitor notification received");145} else {146echo("\tKO: CounterMonitor notification missed or not emitted");147return 1;148}149} finally {150if (counterMonitor != null)151counterMonitor.stop();152}153154return 0;155}156157/**158* Update the gauge and check for notifications159*/160public int gaugeMonitorNotification() throws Exception {161162GaugeMonitor gaugeMonitor = null;163try {164MBeanServer server = MBeanServerFactory.newMBeanServer();165166String domain = server.getDefaultDomain();167168// Create a new GaugeMonitor MBean and add it to the MBeanServer.169//170echo(">>> CREATE a new GaugeMonitor MBean");171ObjectName gaugeMonitorName = new ObjectName(172domain + ":type=" + GaugeMonitor.class.getName());173gaugeMonitor = new GaugeMonitor();174server.registerMBean(gaugeMonitor, gaugeMonitorName);175176echo(">>> ADD a listener to the GaugeMonitor");177gaugeMonitor.addNotificationListener(this, null, null);178179//180// MANAGEMENT OF A STANDARD MBEAN181//182183echo(">>> CREATE a new ObservedObject MBean");184185ObjectName obsObjName =186ObjectName.getInstance(domain + ":type=ObservedObject");187ObservedObject obsObj = new ObservedObject();188server.registerMBean(obsObj, obsObjName);189190echo(">>> SET the attributes of the GaugeMonitor:");191192gaugeMonitor.addObservedObject(obsObjName);193echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);194195gaugeMonitor.setObservedAttribute("IntegerAttribute");196echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");197198gaugeMonitor.setNotifyLow(false);199gaugeMonitor.setNotifyHigh(true);200echo("\tATTRIBUTE \"Notify Low Flag\" = false");201echo("\tATTRIBUTE \"Notify High Flag\" = true");202203Double highThreshold = 3.0, lowThreshold = 2.5;204gaugeMonitor.setThresholds(highThreshold, lowThreshold);205echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);206echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);207208int granularityperiod = 500;209gaugeMonitor.setGranularityPeriod(granularityperiod);210echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);211212echo(">>> START the GaugeMonitor");213gaugeMonitor.start();214215// Check if notification was received216//217doWait();218if (messageReceived) {219echo("\tOK: GaugeMonitor notification received");220} else {221echo("\tKO: GaugeMonitor notification missed or not emitted");222return 1;223}224} finally {225if (gaugeMonitor != null)226gaugeMonitor.stop();227}228229return 0;230}231232/**233* Update the string and check for notifications234*/235public int stringMonitorNotification() throws Exception {236237StringMonitor stringMonitor = null;238try {239MBeanServer server = MBeanServerFactory.newMBeanServer();240241String domain = server.getDefaultDomain();242243// Create a new StringMonitor MBean and add it to the MBeanServer.244//245echo(">>> CREATE a new StringMonitor MBean");246ObjectName stringMonitorName = new ObjectName(247domain + ":type=" + StringMonitor.class.getName());248stringMonitor = new StringMonitor();249server.registerMBean(stringMonitor, stringMonitorName);250251echo(">>> ADD a listener to the StringMonitor");252stringMonitor.addNotificationListener(this, null, null);253254//255// MANAGEMENT OF A STANDARD MBEAN256//257258echo(">>> CREATE a new ObservedObject MBean");259260ObjectName obsObjName =261ObjectName.getInstance(domain + ":type=ObservedObject");262ObservedObject obsObj = new ObservedObject();263server.registerMBean(obsObj, obsObjName);264265echo(">>> SET the attributes of the StringMonitor:");266267stringMonitor.addObservedObject(obsObjName);268echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);269270stringMonitor.setObservedAttribute("StringAttribute");271echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");272273stringMonitor.setNotifyMatch(true);274echo("\tATTRIBUTE \"NotifyMatch\" = true");275276stringMonitor.setNotifyDiffer(false);277echo("\tATTRIBUTE \"NotifyDiffer\" = false");278279stringMonitor.setStringToCompare("do_match_now");280echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\"");281282int granularityperiod = 500;283stringMonitor.setGranularityPeriod(granularityperiod);284echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);285286echo(">>> START the StringMonitor");287stringMonitor.start();288289// Check if notification was received290//291doWait();292if (messageReceived) {293echo("\tOK: StringMonitor notification received");294} else {295echo("\tKO: StringMonitor notification missed or not emitted");296return 1;297}298} finally {299if (stringMonitor != null)300stringMonitor.stop();301}302303return 0;304}305306/**307* Test the monitor notifications.308*/309public int monitorNotifications() throws Exception {310echo(">>> ----------------------------------------");311messageReceived = false;312int error = counterMonitorNotification();313echo(">>> ----------------------------------------");314messageReceived = false;315error += gaugeMonitorNotification();316echo(">>> ----------------------------------------");317messageReceived = false;318error += stringMonitorNotification();319echo(">>> ----------------------------------------");320return error;321}322323/*324* Print message325*/326private static void echo(String message) {327System.out.println(message);328}329330/*331* Wait messageReceived to be true332*/333synchronized void doWait() {334while (!messageReceived) {335try {336wait();337} catch (InterruptedException e) {338System.err.println("Got unexpected exception: " + e);339e.printStackTrace();340break;341}342}343}344345/*346* Standalone entry point.347*348* Run the test and report to stdout.349*/350public static void main (String args[]) throws Exception {351NonComparableAttributeValueTest test = new NonComparableAttributeValueTest();352int error = test.monitorNotifications();353if (error > 0) {354echo(">>> Unhappy Bye, Bye!");355throw new IllegalStateException("Test FAILED: Didn't get all " +356"the notifications that were " +357"expected by the test!");358} else {359echo(">>> Happy Bye, Bye!");360}361}362}363364365