test/javax/management/monitor/NonComparableAttributeValueTest.java

Print this page




  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 


 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;


 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;


 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 }


  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 volatile 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                 
  73                 synchronized (this) {
  74                     messageReceived = true;
  75                     notifyAll();
  76                 }
  77             } else {
  78                 echo("\t\tSkipping notification of type: " + type);
  79             }
  80         } catch (Exception e) {
  81             echo("\tError in handleNotification!");
  82             e.printStackTrace(System.out);
  83         }
  84     }
  85 
  86     /**
  87      * Update the counter and check for notifications
  88      */
  89     public int counterMonitorNotification() throws Exception {
  90 
  91         CounterMonitor counterMonitor = null;
  92         try {
  93             MBeanServer server = MBeanServerFactory.newMBeanServer();
  94 
  95             String domain = server.getDefaultDomain();
  96 


 121             counterMonitor.addObservedObject(obsObjName);
 122             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
 123 
 124             counterMonitor.setObservedAttribute("IntegerAttribute");
 125             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 126 
 127             counterMonitor.setNotify(true);
 128             echo("\tATTRIBUTE \"NotifyFlag\"        = true");
 129 
 130             Integer threshold = 2;
 131             counterMonitor.setInitThreshold(threshold);
 132             echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
 133 
 134             int granularityperiod = 500;
 135             counterMonitor.setGranularityPeriod(granularityperiod);
 136             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 137 
 138             echo(">>> START the CounterMonitor");
 139             counterMonitor.start();
 140 




 141             // Check if notification was received
 142             //
 143             doWait();
 144             if (messageReceived) {
 145                 echo("\tOK: CounterMonitor notification received");
 146             } else {
 147                 echo("\tKO: CounterMonitor notification missed or not emitted");
 148                 return 1;
 149             }
 150         } finally {
 151             if (counterMonitor != null)
 152                 counterMonitor.stop();
 153         }
 154 
 155         return 0;
 156     }
 157 
 158     /**
 159      * Update the gauge and check for notifications
 160      */
 161     public int gaugeMonitorNotification() throws Exception {
 162 
 163         GaugeMonitor gaugeMonitor = null;


 196             gaugeMonitor.setObservedAttribute("IntegerAttribute");
 197             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 198 
 199             gaugeMonitor.setNotifyLow(false);
 200             gaugeMonitor.setNotifyHigh(true);
 201             echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
 202             echo("\tATTRIBUTE \"Notify High Flag\"  = true");
 203 
 204             Double highThreshold = 3.0, lowThreshold = 2.5;
 205             gaugeMonitor.setThresholds(highThreshold, lowThreshold);
 206             echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
 207             echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
 208 
 209             int granularityperiod = 500;
 210             gaugeMonitor.setGranularityPeriod(granularityperiod);
 211             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 212 
 213             echo(">>> START the GaugeMonitor");
 214             gaugeMonitor.start();
 215 




 216             // Check if notification was received
 217             //
 218             doWait();
 219             if (messageReceived) {
 220                 echo("\tOK: GaugeMonitor notification received");
 221             } else {
 222                 echo("\tKO: GaugeMonitor notification missed or not emitted");
 223                 return 1;
 224             }
 225         } finally {
 226             if (gaugeMonitor != null)
 227                 gaugeMonitor.stop();
 228         }
 229 
 230         return 0;
 231     }
 232 
 233     /**
 234      * Update the string and check for notifications
 235      */
 236     public int stringMonitorNotification() throws Exception {
 237 
 238         StringMonitor stringMonitor = null;


 268             stringMonitor.addObservedObject(obsObjName);
 269             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
 270 
 271             stringMonitor.setObservedAttribute("StringAttribute");
 272             echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
 273 
 274             stringMonitor.setNotifyMatch(true);
 275             echo("\tATTRIBUTE \"NotifyMatch\"       = true");
 276 
 277             stringMonitor.setNotifyDiffer(false);
 278             echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
 279 
 280             stringMonitor.setStringToCompare("do_match_now");
 281             echo("\tATTRIBUTE \"StringToCompare\"   = \"do_match_now\"");
 282 
 283             int granularityperiod = 500;
 284             stringMonitor.setGranularityPeriod(granularityperiod);
 285             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 286 
 287             echo(">>> START the StringMonitor");





 288 
 289             // Check if notification was received
 290             //
 291             doWait();
 292             if (messageReceived) {
 293                 echo("\tOK: StringMonitor notification received");
 294             } else {
 295                 echo("\tKO: StringMonitor notification missed or not emitted");
 296                 return 1;
 297             }
 298         } finally {
 299             if (stringMonitor != null)
 300                 stringMonitor.stop();
 301         }
 302 
 303         return 0;
 304     }
 305 
 306     /**
 307      * Test the monitor notifications.
 308      */
 309     public int monitorNotifications() throws Exception {
 310         echo(">>> ----------------------------------------");
 311         messageReceived = false;
 312         int error = counterMonitorNotification();
 313         echo(">>> ----------------------------------------");
 314         messageReceived = false;
 315         error += gaugeMonitorNotification();
 316         echo(">>> ----------------------------------------");
 317         messageReceived = false;
 318         error += stringMonitorNotification();
 319         echo(">>> ----------------------------------------");
 320         return error;
 321     }
 322 
 323     /*
 324      * Print message
 325      */
 326     private static void echo(String message) {
 327         System.out.println(message);
 328     }
 329 
 330     /*
 331      * Wait messageReceived to be true 
 332      */
 333     synchronized void doWait() {
 334         while (!messageReceived) {
 335             try {
 336                 wait();
 337             } catch (InterruptedException e) {
 338                 System.err.println("Got unexpected exception: " + e);
 339                 e.printStackTrace();
 340                 break;
 341             }
 342         }
 343     }
 344     
 345     /*
 346      * Standalone entry point.
 347      *
 348      * Run the test and report to stdout.
 349      */
 350     public static void main (String args[]) throws Exception {
 351         NonComparableAttributeValueTest test = new NonComparableAttributeValueTest();
 352         int error = test.monitorNotifications();
 353         if (error > 0) {
 354             echo(">>> Unhappy Bye, Bye!");
 355             throw new IllegalStateException("Test FAILED: Didn't get all " +
 356                                             "the notifications that were " +
 357                                             "expected by the test!");
 358         } else {
 359             echo(">>> Happy Bye, Bye!");
 360         }
 361     }
 362 }