1 /*
   2  * Copyright (c) 2005, 2015, 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 6229368 8025207
  27  * @summary Wrong threshold value in CounterMonitor with offset and modulus.
  28  * @author Luis-Miguel Alventosa
  29  *
  30  * @run clean CounterMonitorThresholdTest
  31  * @run build CounterMonitorThresholdTest
  32  * @run main CounterMonitorThresholdTest
  33  */
  34 
  35 import java.lang.management.ManagementFactory;
  36 import javax.management.MBeanServer;
  37 import javax.management.MBeanServerInvocationHandler;
  38 import javax.management.Notification;
  39 import javax.management.NotificationEmitter;
  40 import javax.management.NotificationListener;
  41 import javax.management.ObjectName;
  42 import javax.management.monitor.CounterMonitor;
  43 import javax.management.monitor.CounterMonitorMBean;
  44 import javax.management.monitor.MonitorNotification;
  45 
  46 public class CounterMonitorThresholdTest {
  47 
  48     // Offset = 1
  49     private static int counter1[]      = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
  50     private static int derivedGauge1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
  51     private static int threshold1[]    = { 1, 2, 3, 4, 5, 5, 1, 1, 1, 2, 3, 4, 5, 1, 1, 2 };
  52 
  53     // Offset = 3
  54     private static int counter2[]      = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
  55     private static int derivedGauge2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
  56     private static int threshold2[]    = { 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 4 };
  57 
  58     public interface TestMBean {
  59         public int getCounter();
  60         public void setCounter(int count);
  61     }
  62 
  63     public static class Test implements TestMBean {
  64         public int getCounter() {
  65             return count;
  66         }
  67         public void setCounter(int count) {
  68             this.count = count;
  69         }
  70         private int count = 0;
  71     }
  72 
  73     public static class Listener implements NotificationListener {
  74         public void handleNotification(Notification n, Object hb) {
  75             System.out.println("\tReceived notification: " + n.getType());
  76             if (n instanceof MonitorNotification) {
  77                 MonitorNotification mn = (MonitorNotification) n;
  78                 System.out.println("\tSource: " +
  79                     mn.getSource());
  80                 System.out.println("\tType: " +
  81                     mn.getType());
  82                 System.out.println("\tTimeStamp: " +
  83                     mn.getTimeStamp());
  84                 System.out.println("\tObservedObject: " +
  85                     mn.getObservedObject());
  86                 System.out.println("\tObservedAttribute: " +
  87                     mn.getObservedAttribute());
  88                 System.out.println("\tDerivedGauge: " +
  89                     mn.getDerivedGauge());
  90                 System.out.println("\tTrigger: " +
  91                     mn.getTrigger());
  92             }
  93         }
  94     }
  95 
  96     public static void runTest(int offset,
  97                                int counter[],
  98                                int derivedGauge[],
  99                                int threshold[]) throws Exception {
 100         // Retrieve the platform MBean server
 101         //
 102         System.out.println("\nRetrieve the platform MBean server");
 103         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 104         String domain = mbs.getDefaultDomain();
 105 
 106         // Create and register TestMBean
 107         //
 108         ObjectName name =
 109             new ObjectName(domain +
 110                            ":type=" + Test.class.getName() +
 111                            ",offset=" + offset);
 112         mbs.createMBean(Test.class.getName(), name);
 113         TestMBean mbean = (TestMBean)
 114             MBeanServerInvocationHandler.newProxyInstance(
 115                 mbs, name, TestMBean.class, false);
 116 
 117         // Create and register CounterMonitorMBean
 118         //
 119         ObjectName cmn =
 120             new ObjectName(domain +
 121                            ":type=" + CounterMonitor.class.getName() +
 122                            ",offset=" + offset);
 123         CounterMonitor m = new CounterMonitor();
 124         mbs.registerMBean(m, cmn);
 125         CounterMonitorMBean cm = (CounterMonitorMBean)
 126             MBeanServerInvocationHandler.newProxyInstance(
 127                 mbs, cmn, CounterMonitorMBean.class, true);
 128         ((NotificationEmitter) cm).addNotificationListener(
 129             new Listener(), null, null);
 130         cm.addObservedObject(name);
 131         cm.setObservedAttribute("Counter");
 132         cm.setGranularityPeriod(100);
 133         cm.setInitThreshold(1);
 134         cm.setOffset(offset);
 135         cm.setModulus(5);
 136         cm.setNotify(true);
 137 
 138         // Start the monitor
 139         //
 140         System.out.println("\nStart monitoring...");
 141         cm.start();
 142 
 143         // Play with counter
 144         //
 145         for (int i = 0; i < counter.length; i++) {
 146             mbean.setCounter(counter[i]);
 147             System.out.println("\nCounter = " + mbean.getCounter());
 148             Integer derivedGaugeValue;
 149             // either pass or test timeout (killed by test harness)
 150             // see 8025207
 151             do {
 152                 Thread.sleep(150);
 153                 derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
 154             } while (derivedGaugeValue.intValue() != derivedGauge[i]);
 155 
 156             Number thresholdValue = cm.getThreshold(name);
 157             System.out.println("Threshold = " + thresholdValue);
 158             if (thresholdValue.intValue() != threshold[i]) {
 159                 System.out.println("Wrong threshold! Current value = " +
 160                     thresholdValue + " Expected value = " + threshold[i]);
 161                 System.out.println("\nStop monitoring...");
 162                 cm.stop();
 163                 throw new IllegalArgumentException("wrong threshold");
 164             }
 165         }
 166 
 167         // Stop the monitor
 168         //
 169         System.out.println("\nStop monitoring...");
 170         cm.stop();
 171     }
 172 
 173     public static void main(String[] args) throws Exception {
 174         runTest(1, counter1, derivedGauge1, threshold1);
 175         runTest(3, counter2, derivedGauge2, threshold2);
 176     }
 177 }