710N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
710N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
710N/A *
710N/A * This code is free software; you can redistribute it and/or modify it
710N/A * under the terms of the GNU General Public License version 2 only, as
710N/A * published by the Free Software Foundation.
710N/A *
710N/A * This code is distributed in the hope that it will be useful, but WITHOUT
710N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
710N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
710N/A * version 2 for more details (a copy is included in the LICENSE file that
710N/A * accompanied this code).
710N/A *
710N/A * You should have received a copy of the GNU General Public License version
710N/A * 2 along with this work; if not, write to the Free Software Foundation,
710N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
710N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
710N/A */
710N/A
710N/A/*
710N/A * @test
710N/A * @bug 6683213
710N/A * @summary Test that the initial derived gauge is (Integer)0
710N/A * @author Daniel Fuchs
710N/A * @run clean DerivedGaugeMonitorTest
710N/A * @run build DerivedGaugeMonitorTest
710N/A * @run main DerivedGaugeMonitorTest
710N/A */
710N/A
710N/Aimport java.io.Serializable;
710N/Aimport java.util.concurrent.CountDownLatch;
710N/Aimport java.util.concurrent.TimeUnit;
710N/Aimport javax.management.MBeanServer;
710N/Aimport javax.management.MBeanServerFactory;
710N/Aimport javax.management.ObjectName;
710N/Aimport javax.management.monitor.CounterMonitor;
710N/Aimport javax.management.monitor.GaugeMonitor;
710N/A
710N/Apublic class DerivedGaugeMonitorTest {
710N/A
710N/A public static interface Things {
710N/A public long getALong();
710N/A public int getAnInt();
710N/A public double getADouble();
710N/A public short getAShort();
710N/A public byte getAByte();
710N/A public float getAFloat();
710N/A }
710N/A public static interface MyMBean extends Things {
710N/A public Things getAThing();
710N/A }
710N/A
710N/A public static class MyThings implements Things, Serializable {
710N/A private static final long serialVersionUID = -4333982919572564126L;
710N/A
710N/A private volatile long along = 0;
710N/A private volatile int anint = 0;
710N/A private volatile short ashort = 0;
710N/A private volatile byte abyte = 0;
710N/A private volatile float afloat = 0;
710N/A private volatile double adouble = 0;
710N/A
710N/A private volatile transient boolean mutable;
710N/A
710N/A public MyThings() {
710N/A this(false);
710N/A }
710N/A
710N/A protected MyThings(boolean mutable) {
710N/A this.mutable=mutable;
710N/A }
710N/A
710N/A public long getALong() {
710N/A return mutable?along++:along;
710N/A }
710N/A
710N/A public int getAnInt() {
710N/A return mutable?anint++:anint;
710N/A }
710N/A
710N/A public double getADouble() {
710N/A return mutable?adouble++:adouble;
710N/A }
710N/A
710N/A public short getAShort() {
710N/A return mutable?ashort++:ashort;
710N/A }
710N/A
710N/A public byte getAByte() {
710N/A return mutable?abyte++:abyte;
710N/A }
710N/A
710N/A public float getAFloat() {
710N/A return mutable?afloat++:afloat;
710N/A }
710N/A
710N/A @Override
710N/A public Object clone() throws CloneNotSupportedException {
710N/A final MyThings other = (MyThings)super.clone();
710N/A other.mutable=false;
710N/A return other;
710N/A }
710N/A }
710N/A
710N/A public static class My implements MyMBean {
710N/A
710N/A public final CountDownLatch cdl = new CountDownLatch(6);
710N/A
710N/A private final MyThings things = new MyThings(true);
710N/A private volatile int count = 0;
710N/A
710N/A public Things getAThing() {
710N/A count++;
710N/A cdl.countDown();
710N/A try {
710N/A return (Things) things.clone();
710N/A } catch (CloneNotSupportedException ex) {
710N/A return null;
710N/A }
710N/A }
710N/A
710N/A public long getALong() {
710N/A count++;
710N/A cdl.countDown();
710N/A return things.getALong();
710N/A }
710N/A
710N/A public int getAnInt() {
710N/A count++;
710N/A cdl.countDown();
710N/A return things.getAnInt();
710N/A }
710N/A
710N/A public double getADouble() {
710N/A count++;
710N/A cdl.countDown();
710N/A return things.getADouble();
710N/A }
710N/A
710N/A public short getAShort() {
710N/A count++;
710N/A cdl.countDown();
710N/A return things.getAShort();
710N/A }
710N/A
710N/A public byte getAByte() {
710N/A count++;
710N/A cdl.countDown();
710N/A return things.getAByte();
710N/A }
710N/A
710N/A public float getAFloat() {
710N/A count++;
710N/A cdl.countDown();
710N/A return things.getAFloat();
710N/A }
710N/A
710N/A }
710N/A
710N/A
710N/A public static String[] attributes = {
710N/A "AByte","AShort","AnInt","ALong","AFloat","ADouble"
710N/A };
710N/A public static String[] things = {
710N/A "AThing.AByte","AThing.AShort","AThing.AnInt","AThing.ALong",
710N/A "AThing.AFloat","AThing.ADouble"
710N/A };
710N/A
710N/A public static void check(String attr, MBeanServer server, ObjectName mon,
710N/A ObjectName mbean) throws Exception {
710N/A final Object obj = server.getAttribute(mon, "DerivedGauge");
710N/A check(attr,server,mon,mbean,obj);
710N/A }
710N/A
710N/A public static void check(String attr, MBeanServer server, ObjectName mon,
710N/A ObjectName mbean, Object obj) throws Exception {
710N/A if (obj == null)
710N/A throw new Exception("Derived gauge for: " + attr +
710N/A " ["+mon+", "+mbean+"] is null!");
710N/A if (!Integer.valueOf(0).equals(obj))
710N/A throw new Exception("Derived gauge for: " + attr +
710N/A " ["+mon+", "+mbean+"] is "+obj);
710N/A }
710N/A
710N/A public static void check(String attr, MBeanServer server, ObjectName mon,
710N/A ObjectName mbean, long start) throws Exception {
710N/A final Object obj = server.getAttribute(mon, "DerivedGauge");
710N/A final long now = System.currentTimeMillis();
710N/A final long gran = (Long)server.getAttribute(mon, "GranularityPeriod");
710N/A if (now > start +2*gran) {
710N/A throw new Exception(attr+": Can't verify test case: " +
710N/A "granularity period expired!");
710N/A }
710N/A check(attr,server,mon,mbean,obj);
710N/A }
710N/A
710N/A public static void test(String attr) throws Exception {
710N/A System.err.println("Testing "+ attr);
710N/A final MBeanServer server = MBeanServerFactory.createMBeanServer();
710N/A final ObjectName mbean = new ObjectName("ugly:type=cr.p");
710N/A final My my = new My();
710N/A final GaugeMonitor mon2 = new GaugeMonitor();
710N/A final ObjectName mon2n = new ObjectName("mon1:type=GaugeMonitor");
710N/A final CounterMonitor mon1 = new CounterMonitor();
710N/A final ObjectName mon1n = new ObjectName("mon2:type=CounterMonitor");
710N/A
710N/A server.registerMBean(my, mbean);
710N/A server.registerMBean(mon1, mon1n);
710N/A server.registerMBean(mon2, mon2n);
710N/A
710N/A mon1.addObservedObject(mbean);
710N/A mon1.setGranularityPeriod(60000); // 60 sec...
710N/A mon1.setObservedAttribute(attr);
710N/A mon1.setDifferenceMode(true);
710N/A check(attr,server,mon1n,mbean);
710N/A
710N/A mon2.addObservedObject(mbean);
710N/A mon2.setGranularityPeriod(60000); // 60 sec...
710N/A mon2.setObservedAttribute(attr);
710N/A mon2.setDifferenceMode(true);
710N/A check(attr,server,mon2n,mbean);
710N/A
710N/A final long approxStart = System.currentTimeMillis();
710N/A mon1.start();
710N/A mon2.start();
710N/A
710N/A try {
710N/A check(attr,server,mon1n,mbean,approxStart);
710N/A check(attr,server,mon2n,mbean,approxStart);
710N/A check(attr,server,mon1n,mbean,approxStart);
710N/A check(attr,server,mon2n,mbean,approxStart);
710N/A
710N/A
710N/A mon1.setGranularityPeriod(5);
710N/A mon2.setGranularityPeriod(5);
710N/A
710N/A my.cdl.await(1000, TimeUnit.MILLISECONDS);
710N/A if (my.cdl.getCount() > 0)
710N/A throw new Exception(attr+": Count down not reached!");
710N/A
710N/A // just check that we don't get an exception now...
710N/A System.err.println(attr+": [" + mon1n+
710N/A "] DerivedGauge is now "+
710N/A server.getAttribute(mon1n, "DerivedGauge"));
710N/A System.err.println(attr+": [" + mon2n+
710N/A "] DerivedGauge is now "+
710N/A server.getAttribute(mon2n, "DerivedGauge"));
710N/A } finally {
710N/A try {mon1.stop();} catch (Exception x) {}
710N/A try {mon2.stop();} catch (Exception x) {}
710N/A }
710N/A }
710N/A
710N/A
710N/A
710N/A
710N/A public static void main(String[] args) throws Exception {
710N/A for (String attr:attributes) {
710N/A test(attr);
710N/A }
710N/A for (String attr:things) {
710N/A test(attr);
710N/A }
710N/A }
710N/A
710N/A}