Path: blob/master/test/jdk/javax/management/monitor/NonComparableAttributeValueTest.java
41149 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 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*31* @run clean NonComparableAttributeValueTest32* @run build NonComparableAttributeValueTest33* @run main NonComparableAttributeValueTest34*/3536import javax.management.*;37import javax.management.monitor.*;3839public class NonComparableAttributeValueTest implements NotificationListener {4041// Flag to notify that a message has been received42private volatile boolean messageReceived = false;4344// MBean class45public class ObservedObject implements ObservedObjectMBean {46public Object getIntegerAttribute() {47return new Object();48}49public Object getStringAttribute() {50return new Object();51}52}5354// MBean interface55public interface ObservedObjectMBean {56public Object getIntegerAttribute();57public Object 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());7273synchronized (this) {74messageReceived = true;75notifyAll();76}77} else {78echo("\t\tSkipping notification of type: " + type);79}80} catch (Exception e) {81echo("\tError in handleNotification!");82e.printStackTrace(System.out);83}84}8586/**87* Update the counter and check for notifications88*/89public int counterMonitorNotification() throws Exception {9091CounterMonitor counterMonitor = null;92try {93MBeanServer server = MBeanServerFactory.newMBeanServer();9495String domain = server.getDefaultDomain();9697// Create a new CounterMonitor MBean and add it to the MBeanServer.98//99echo(">>> CREATE a new CounterMonitor MBean");100ObjectName counterMonitorName = new ObjectName(101domain + ":type=" + CounterMonitor.class.getName());102counterMonitor = new CounterMonitor();103server.registerMBean(counterMonitor, counterMonitorName);104105echo(">>> ADD a listener to the CounterMonitor");106counterMonitor.addNotificationListener(this, null, null);107108//109// MANAGEMENT OF A STANDARD MBEAN110//111112echo(">>> CREATE a new ObservedObject MBean");113114ObjectName obsObjName =115ObjectName.getInstance(domain + ":type=ObservedObject");116ObservedObject obsObj = new ObservedObject();117server.registerMBean(obsObj, obsObjName);118119echo(">>> SET the attributes of the CounterMonitor:");120121counterMonitor.addObservedObject(obsObjName);122echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);123124counterMonitor.setObservedAttribute("IntegerAttribute");125echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");126127counterMonitor.setNotify(true);128echo("\tATTRIBUTE \"NotifyFlag\" = true");129130Integer threshold = 2;131counterMonitor.setInitThreshold(threshold);132echo("\tATTRIBUTE \"Threshold\" = " + threshold);133134int granularityperiod = 500;135counterMonitor.setGranularityPeriod(granularityperiod);136echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);137138echo(">>> START the CounterMonitor");139counterMonitor.start();140141// Check if notification was received142//143doWait();144if (messageReceived) {145echo("\tOK: CounterMonitor notification received");146} else {147echo("\tKO: CounterMonitor notification missed or not emitted");148return 1;149}150} finally {151if (counterMonitor != null)152counterMonitor.stop();153}154155return 0;156}157158/**159* Update the gauge and check for notifications160*/161public int gaugeMonitorNotification() throws Exception {162163GaugeMonitor gaugeMonitor = null;164try {165MBeanServer server = MBeanServerFactory.newMBeanServer();166167String domain = server.getDefaultDomain();168169// Create a new GaugeMonitor MBean and add it to the MBeanServer.170//171echo(">>> CREATE a new GaugeMonitor MBean");172ObjectName gaugeMonitorName = new ObjectName(173domain + ":type=" + GaugeMonitor.class.getName());174gaugeMonitor = new GaugeMonitor();175server.registerMBean(gaugeMonitor, gaugeMonitorName);176177echo(">>> ADD a listener to the GaugeMonitor");178gaugeMonitor.addNotificationListener(this, null, null);179180//181// MANAGEMENT OF A STANDARD MBEAN182//183184echo(">>> CREATE a new ObservedObject MBean");185186ObjectName obsObjName =187ObjectName.getInstance(domain + ":type=ObservedObject");188ObservedObject obsObj = new ObservedObject();189server.registerMBean(obsObj, obsObjName);190191echo(">>> SET the attributes of the GaugeMonitor:");192193gaugeMonitor.addObservedObject(obsObjName);194echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);195196gaugeMonitor.setObservedAttribute("IntegerAttribute");197echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");198199gaugeMonitor.setNotifyLow(false);200gaugeMonitor.setNotifyHigh(true);201echo("\tATTRIBUTE \"Notify Low Flag\" = false");202echo("\tATTRIBUTE \"Notify High Flag\" = true");203204Double highThreshold = 3.0, lowThreshold = 2.5;205gaugeMonitor.setThresholds(highThreshold, lowThreshold);206echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);207echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);208209int granularityperiod = 500;210gaugeMonitor.setGranularityPeriod(granularityperiod);211echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);212213echo(">>> START the GaugeMonitor");214gaugeMonitor.start();215216// Check if notification was received217//218doWait();219if (messageReceived) {220echo("\tOK: GaugeMonitor notification received");221} else {222echo("\tKO: GaugeMonitor notification missed or not emitted");223return 1;224}225} finally {226if (gaugeMonitor != null)227gaugeMonitor.stop();228}229230return 0;231}232233/**234* Update the string and check for notifications235*/236public int stringMonitorNotification() throws Exception {237238StringMonitor stringMonitor = null;239try {240MBeanServer server = MBeanServerFactory.newMBeanServer();241242String domain = server.getDefaultDomain();243244// Create a new StringMonitor MBean and add it to the MBeanServer.245//246echo(">>> CREATE a new StringMonitor MBean");247ObjectName stringMonitorName = new ObjectName(248domain + ":type=" + StringMonitor.class.getName());249stringMonitor = new StringMonitor();250server.registerMBean(stringMonitor, stringMonitorName);251252echo(">>> ADD a listener to the StringMonitor");253stringMonitor.addNotificationListener(this, null, null);254255//256// MANAGEMENT OF A STANDARD MBEAN257//258259echo(">>> CREATE a new ObservedObject MBean");260261ObjectName obsObjName =262ObjectName.getInstance(domain + ":type=ObservedObject");263ObservedObject obsObj = new ObservedObject();264server.registerMBean(obsObj, obsObjName);265266echo(">>> SET the attributes of the StringMonitor:");267268stringMonitor.addObservedObject(obsObjName);269echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);270271stringMonitor.setObservedAttribute("StringAttribute");272echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");273274stringMonitor.setNotifyMatch(true);275echo("\tATTRIBUTE \"NotifyMatch\" = true");276277stringMonitor.setNotifyDiffer(false);278echo("\tATTRIBUTE \"NotifyDiffer\" = false");279280stringMonitor.setStringToCompare("do_match_now");281echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\"");282283int granularityperiod = 500;284stringMonitor.setGranularityPeriod(granularityperiod);285echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);286287echo(">>> START the StringMonitor");288stringMonitor.start();289290// Check if notification was received291//292doWait();293if (messageReceived) {294echo("\tOK: StringMonitor notification received");295} else {296echo("\tKO: StringMonitor notification missed or not emitted");297return 1;298}299} finally {300if (stringMonitor != null)301stringMonitor.stop();302}303304return 0;305}306307/**308* Test the monitor notifications.309*/310public int monitorNotifications() throws Exception {311echo(">>> ----------------------------------------");312messageReceived = false;313int error = counterMonitorNotification();314echo(">>> ----------------------------------------");315messageReceived = false;316error += gaugeMonitorNotification();317echo(">>> ----------------------------------------");318messageReceived = false;319error += stringMonitorNotification();320echo(">>> ----------------------------------------");321return error;322}323324/*325* Print message326*/327private static void echo(String message) {328System.out.println(message);329}330331/*332* Wait messageReceived to be true333*/334synchronized void doWait() {335while (!messageReceived) {336try {337wait();338} catch (InterruptedException e) {339System.err.println("Got unexpected exception: " + e);340e.printStackTrace();341break;342}343}344}345346/*347* Standalone entry point.348*349* Run the test and report to stdout.350*/351public static void main (String args[]) throws Exception {352NonComparableAttributeValueTest test = new NonComparableAttributeValueTest();353int error = test.monitorNotifications();354if (error > 0) {355echo(">>> Unhappy Bye, Bye!");356throw new IllegalStateException("Test FAILED: Didn't get all " +357"the notifications that were " +358"expected by the test!");359} else {360echo(">>> Happy Bye, Bye!");361}362}363}364365366