1 /*
   2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6273541
  27  * @summary Test that the counter/gauge/string monitors emit a
  28  *          jmx.monitor.error.type notification when the attribute
  29  *          being monitored returns a non comparable value.
  30  * @author Luis-Miguel Alventosa
  31  * @run clean NonComparableAttributeValueTest
  32  * @run build NonComparableAttributeValueTest
  33  * @run main NonComparableAttributeValueTest
  34  */
  35 
  36 import javax.management.*;
  37 import javax.management.monitor.*;
  38 
  39 public class NonComparableAttributeValueTest implements NotificationListener {
  40 
  41     // Flag to notify that a message has been received
  42     private boolean messageReceived = false;
  43 
  44     // MBean class
  45     public class ObservedObject implements ObservedObjectMBean {
  46         public Object getIntegerAttribute() {
  47             return new Object();
  48         }
  49         public Object getStringAttribute() {
  50             return new Object();
  51         }
  52     }
  53 
  54     // MBean interface
  55     public interface ObservedObjectMBean {
  56         public Object getIntegerAttribute();
  57         public Object getStringAttribute();
  58     }
  59 
  60     // Notification handler
  61     public void handleNotification(Notification notification,
  62                                    Object handback) {
  63         MonitorNotification n = (MonitorNotification) notification;
  64         echo("\tInside handleNotification...");
  65         String type = n.getType();
  66         try {
  67             if (type.equals(
  68                     MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {
  69                 echo("\t\t" + n.getObservedAttribute() + " is null");
  70                 echo("\t\tDerived Gauge = " + n.getDerivedGauge());
  71                 echo("\t\tTrigger = " + n.getTrigger());
  72                 messageReceived = true;
  73             } else {
  74                 echo("\t\tSkipping notification of type: " + type);
  75             }
  76         } catch (Exception e) {
  77             echo("\tError in handleNotification!");
  78             e.printStackTrace(System.out);
  79         }
  80     }
  81 
  82     /**
  83      * Update the counter and check for notifications
  84      */
  85     public int counterMonitorNotification() throws Exception {
  86 
  87         CounterMonitor counterMonitor = null;
  88         try {
  89             MBeanServer server = MBeanServerFactory.newMBeanServer();
  90 
  91             String domain = server.getDefaultDomain();
  92 
  93             // Create a new CounterMonitor MBean and add it to the MBeanServer.
  94             //
  95             echo(">>> CREATE a new CounterMonitor MBean");
  96             ObjectName counterMonitorName = new ObjectName(
  97                             domain + ":type=" + CounterMonitor.class.getName());
  98             counterMonitor = new CounterMonitor();
  99             server.registerMBean(counterMonitor, counterMonitorName);
 100 
 101             echo(">>> ADD a listener to the CounterMonitor");
 102             counterMonitor.addNotificationListener(this, null, null);
 103 
 104             //
 105             // MANAGEMENT OF A STANDARD MBEAN
 106             //
 107 
 108             echo(">>> CREATE a new ObservedObject MBean");
 109 
 110             ObjectName obsObjName =
 111                 ObjectName.getInstance(domain + ":type=ObservedObject");
 112             ObservedObject obsObj = new ObservedObject();
 113             server.registerMBean(obsObj, obsObjName);
 114 
 115             echo(">>> SET the attributes of the CounterMonitor:");
 116 
 117             counterMonitor.addObservedObject(obsObjName);
 118             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
 119 
 120             counterMonitor.setObservedAttribute("IntegerAttribute");
 121             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 122 
 123             counterMonitor.setNotify(true);
 124             echo("\tATTRIBUTE \"NotifyFlag\"        = true");
 125 
 126             Integer threshold = 2;
 127             counterMonitor.setInitThreshold(threshold);
 128             echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
 129 
 130             int granularityperiod = 500;
 131             counterMonitor.setGranularityPeriod(granularityperiod);
 132             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 133 
 134             echo(">>> START the CounterMonitor");
 135             counterMonitor.start();
 136 
 137             // Wait for granularity period (multiplied by 2 for sure)
 138             //
 139             Thread.sleep(granularityperiod * 2);
 140 
 141             // Check if notification was received
 142             //
 143             if (messageReceived) {
 144                 echo("\tOK: CounterMonitor notification received");
 145             } else {
 146                 echo("\tKO: CounterMonitor notification missed or not emitted");
 147                 return 1;
 148             }
 149         } finally {
 150             if (counterMonitor != null)
 151                 counterMonitor.stop();
 152         }
 153 
 154         return 0;
 155     }
 156 
 157     /**
 158      * Update the gauge and check for notifications
 159      */
 160     public int gaugeMonitorNotification() throws Exception {
 161 
 162         GaugeMonitor gaugeMonitor = null;
 163         try {
 164             MBeanServer server = MBeanServerFactory.newMBeanServer();
 165 
 166             String domain = server.getDefaultDomain();
 167 
 168             // Create a new GaugeMonitor MBean and add it to the MBeanServer.
 169             //
 170             echo(">>> CREATE a new GaugeMonitor MBean");
 171             ObjectName gaugeMonitorName = new ObjectName(
 172                             domain + ":type=" + GaugeMonitor.class.getName());
 173             gaugeMonitor = new GaugeMonitor();
 174             server.registerMBean(gaugeMonitor, gaugeMonitorName);
 175 
 176             echo(">>> ADD a listener to the GaugeMonitor");
 177             gaugeMonitor.addNotificationListener(this, null, null);
 178 
 179             //
 180             // MANAGEMENT OF A STANDARD MBEAN
 181             //
 182 
 183             echo(">>> CREATE a new ObservedObject MBean");
 184 
 185             ObjectName obsObjName =
 186                 ObjectName.getInstance(domain + ":type=ObservedObject");
 187             ObservedObject obsObj = new ObservedObject();
 188             server.registerMBean(obsObj, obsObjName);
 189 
 190             echo(">>> SET the attributes of the GaugeMonitor:");
 191 
 192             gaugeMonitor.addObservedObject(obsObjName);
 193             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
 194 
 195             gaugeMonitor.setObservedAttribute("IntegerAttribute");
 196             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 197 
 198             gaugeMonitor.setNotifyLow(false);
 199             gaugeMonitor.setNotifyHigh(true);
 200             echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
 201             echo("\tATTRIBUTE \"Notify High Flag\"  = true");
 202 
 203             Double highThreshold = 3.0, lowThreshold = 2.5;
 204             gaugeMonitor.setThresholds(highThreshold, lowThreshold);
 205             echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
 206             echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
 207 
 208             int granularityperiod = 500;
 209             gaugeMonitor.setGranularityPeriod(granularityperiod);
 210             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 211 
 212             echo(">>> START the GaugeMonitor");
 213             gaugeMonitor.start();
 214 
 215             // Wait for granularity period (multiplied by 2 for sure)
 216             //
 217             Thread.sleep(granularityperiod * 2);
 218 
 219             // Check if notification was received
 220             //
 221             if (messageReceived) {
 222                 echo("\tOK: GaugeMonitor notification received");
 223             } else {
 224                 echo("\tKO: GaugeMonitor notification missed or not emitted");
 225                 return 1;
 226             }
 227         } finally {
 228             if (gaugeMonitor != null)
 229                 gaugeMonitor.stop();
 230         }
 231 
 232         return 0;
 233     }
 234 
 235     /**
 236      * Update the string and check for notifications
 237      */
 238     public int stringMonitorNotification() throws Exception {
 239 
 240         StringMonitor stringMonitor = null;
 241         try {
 242             MBeanServer server = MBeanServerFactory.newMBeanServer();
 243 
 244             String domain = server.getDefaultDomain();
 245 
 246             // Create a new StringMonitor MBean and add it to the MBeanServer.
 247             //
 248             echo(">>> CREATE a new StringMonitor MBean");
 249             ObjectName stringMonitorName = new ObjectName(
 250                             domain + ":type=" + StringMonitor.class.getName());
 251             stringMonitor = new StringMonitor();
 252             server.registerMBean(stringMonitor, stringMonitorName);
 253 
 254             echo(">>> ADD a listener to the StringMonitor");
 255             stringMonitor.addNotificationListener(this, null, null);
 256 
 257             //
 258             // MANAGEMENT OF A STANDARD MBEAN
 259             //
 260 
 261             echo(">>> CREATE a new ObservedObject MBean");
 262 
 263             ObjectName obsObjName =
 264                 ObjectName.getInstance(domain + ":type=ObservedObject");
 265             ObservedObject obsObj = new ObservedObject();
 266             server.registerMBean(obsObj, obsObjName);
 267 
 268             echo(">>> SET the attributes of the StringMonitor:");
 269 
 270             stringMonitor.addObservedObject(obsObjName);
 271             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
 272 
 273             stringMonitor.setObservedAttribute("StringAttribute");
 274             echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
 275 
 276             stringMonitor.setNotifyMatch(true);
 277             echo("\tATTRIBUTE \"NotifyMatch\"       = true");
 278 
 279             stringMonitor.setNotifyDiffer(false);
 280             echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
 281 
 282             stringMonitor.setStringToCompare("do_match_now");
 283             echo("\tATTRIBUTE \"StringToCompare\"   = \"do_match_now\"");
 284 
 285             int granularityperiod = 500;
 286             stringMonitor.setGranularityPeriod(granularityperiod);
 287             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 288 
 289             echo(">>> START the StringMonitor");
 290             stringMonitor.start();
 291 
 292             // Wait for granularity period (multiplied by 2 for sure)
 293             //
 294             Thread.sleep(granularityperiod * 2);
 295 
 296             // Check if notification was received
 297             //
 298             if (messageReceived) {
 299                 echo("\tOK: StringMonitor notification received");
 300             } else {
 301                 echo("\tKO: StringMonitor notification missed or not emitted");
 302                 return 1;
 303             }
 304         } finally {
 305             if (stringMonitor != null)
 306                 stringMonitor.stop();
 307         }
 308 
 309         return 0;
 310     }
 311 
 312     /**
 313      * Test the monitor notifications.
 314      */
 315     public int monitorNotifications() throws Exception {
 316         echo(">>> ----------------------------------------");
 317         messageReceived = false;
 318         int error = counterMonitorNotification();
 319         echo(">>> ----------------------------------------");
 320         messageReceived = false;
 321         error += gaugeMonitorNotification();
 322         echo(">>> ----------------------------------------");
 323         messageReceived = false;
 324         error += stringMonitorNotification();
 325         echo(">>> ----------------------------------------");
 326         return error;
 327     }
 328 
 329     /*
 330      * Print message
 331      */
 332     private static void echo(String message) {
 333         System.out.println(message);
 334     }
 335 
 336     /*
 337      * Standalone entry point.
 338      *
 339      * Run the test and report to stdout.
 340      */
 341     public static void main (String args[]) throws Exception {
 342         NonComparableAttributeValueTest test = new NonComparableAttributeValueTest();
 343         int error = test.monitorNotifications();
 344         if (error > 0) {
 345             echo(">>> Unhappy Bye, Bye!");
 346             throw new IllegalStateException("Test FAILED: Didn't get all " +
 347                                             "the notifications that were " +
 348                                             "expected by the test!");
 349         } else {
 350             echo(">>> Happy Bye, Bye!");
 351         }
 352     }
 353 }