0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6222961
0N/A * @summary Test that the counter/gauge/string monitors
0N/A * support attributes of arbitrary data types.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean AttributeArbitraryDataTypeTest
0N/A * @run build AttributeArbitraryDataTypeTest
0N/A * @run main AttributeArbitraryDataTypeTest
0N/A */
0N/A
1790N/Aimport java.beans.BeanInfo;
0N/Aimport java.beans.IntrospectionException;
0N/Aimport java.beans.PropertyDescriptor;
0N/Aimport java.beans.SimpleBeanInfo;
1790N/Aimport java.lang.reflect.Array;
1790N/Aimport java.lang.reflect.InvocationTargetException;
1790N/Aimport javax.management.AttributeNotFoundException;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.Notification;
0N/Aimport javax.management.NotificationListener;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.monitor.CounterMonitor;
0N/Aimport javax.management.monitor.GaugeMonitor;
0N/Aimport javax.management.monitor.MonitorNotification;
0N/Aimport javax.management.monitor.StringMonitor;
0N/Aimport javax.management.openmbean.CompositeData;
0N/Aimport javax.management.openmbean.CompositeDataSupport;
0N/Aimport javax.management.openmbean.CompositeType;
0N/Aimport javax.management.openmbean.OpenDataException;
0N/Aimport javax.management.openmbean.OpenType;
0N/Aimport javax.management.openmbean.SimpleType;
0N/A
0N/Apublic class AttributeArbitraryDataTypeTest implements NotificationListener {
0N/A
0N/A // Flag to notify that a message has been received
0N/A private boolean counterMessageReceived = false;
0N/A private boolean gaugeMessageReceived = false;
0N/A private boolean stringMessageReceived = false;
0N/A
0N/A // Match enum
0N/A public enum Match { do_not_match_0,
0N/A do_not_match_1,
0N/A do_not_match_2,
0N/A do_match_now };
0N/A
0N/A // MatchBeanInfo class
0N/A public static class MatchBeanInfo extends SimpleBeanInfo {
0N/A public PropertyDescriptor[] getPropertyDescriptors() {
0N/A try {
0N/A return new PropertyDescriptor[] {
0N/A new PropertyDescriptor("name", Match.class, "name", null) };
0N/A } catch (IntrospectionException e ) {
0N/A e.printStackTrace();
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // ComplexAttribute class
0N/A public class ComplexAttribute {
0N/A
0N/A public Integer getIntegerAttribute() {
0N/A return i;
0N/A }
0N/A
0N/A public void setIntegerAttribute(Integer i) {
0N/A this.i = i;
0N/A }
0N/A
0N/A public Double getDoubleAttribute() {
0N/A return d;
0N/A }
0N/A
0N/A public void setDoubleAttribute(Double d) {
0N/A this.d = d;
0N/A }
0N/A
0N/A public String getStringAttribute() {
0N/A return s;
0N/A }
0N/A
0N/A public void setStringAttribute(String s) {
0N/A this.s = s;
0N/A }
0N/A
0N/A public Integer[] getArrayAttribute() {
0N/A return a;
0N/A }
0N/A
0N/A public void setArrayAttribute(Integer[] a) {
0N/A this.a = a;
0N/A }
0N/A
0N/A public Match getEnumAttribute() {
0N/A return e;
0N/A }
0N/A
0N/A public void setEnumAttribute(Match e) {
0N/A this.e = e;
0N/A }
0N/A
0N/A private Integer i;
0N/A private Double d;
0N/A private String s;
0N/A private Integer[] a;
0N/A private Match e;
0N/A }
0N/A
0N/A // MBean class
0N/A public class ObservedObject implements ObservedObjectMBean {
0N/A
0N/A // Simple type buried in complex getter
0N/A //
0N/A public ComplexAttribute getComplexAttribute() {
0N/A return ca;
0N/A }
0N/A
0N/A public void setComplexAttribute(ComplexAttribute ca) {
0N/A this.ca = ca;
0N/A }
0N/A
0N/A private ComplexAttribute ca = null;
0N/A
0N/A // Simple type buried in CompositeData
0N/A //
0N/A public CompositeData getCompositeDataAttribute()
0N/A throws OpenDataException {
0N/A CompositeType ct = new CompositeType("CompositeDataAttribute",
0N/A "Composite Data Attribute",
0N/A itemNames,
0N/A itemDescriptions,
0N/A itemTypes);
0N/A Object itemValues[] = { ia, da, sa };
0N/A return new CompositeDataSupport(ct, itemNames, itemValues);
0N/A }
0N/A
0N/A public Integer ia;
0N/A public Double da;
0N/A public String sa;
0N/A
0N/A private String itemNames[] = { "IntegerAttribute",
0N/A "DoubleAttribute",
0N/A "StringAttribute" };
0N/A private String itemDescriptions[] = { "Integer Attribute",
0N/A "Double Attribute",
0N/A "String Attribute" };
0N/A private OpenType itemTypes[] = { SimpleType.INTEGER,
0N/A SimpleType.DOUBLE,
0N/A SimpleType.STRING };
0N/A }
0N/A
0N/A // MBean interface
0N/A public interface ObservedObjectMBean {
0N/A public ComplexAttribute getComplexAttribute();
0N/A public void setComplexAttribute(ComplexAttribute ca);
0N/A public CompositeData getCompositeDataAttribute()
0N/A throws OpenDataException;
0N/A }
0N/A
0N/A // Notification handler
0N/A public void handleNotification(Notification notification,
0N/A Object handback) {
0N/A MonitorNotification n = (MonitorNotification) notification;
0N/A echo("\tInside handleNotification...");
0N/A String type = n.getType();
0N/A try {
0N/A if (type.equals(MonitorNotification.
0N/A THRESHOLD_VALUE_EXCEEDED)) {
0N/A echo("\t\t" + n.getObservedAttribute() +
0N/A " has reached or exceeded the threshold");
0N/A echo("\t\tDerived Gauge = " + n.getDerivedGauge());
0N/A echo("\t\tTrigger = " + n.getTrigger());
0N/A counterMessageReceived = true;
0N/A } else if (type.equals(MonitorNotification.
0N/A THRESHOLD_HIGH_VALUE_EXCEEDED)) {
0N/A echo("\t\t" + n.getObservedAttribute() +
0N/A " has reached or exceeded the high threshold");
0N/A echo("\t\tDerived Gauge = " + n.getDerivedGauge());
0N/A echo("\t\tTrigger = " + n.getTrigger());
0N/A gaugeMessageReceived = true;
0N/A } else if (type.equals(MonitorNotification.
0N/A STRING_TO_COMPARE_VALUE_MATCHED)) {
0N/A echo("\t\t" + n.getObservedAttribute() +
0N/A " matches the string-to-compare value");
0N/A echo("\t\tDerived Gauge = " + n.getDerivedGauge());
0N/A echo("\t\tTrigger = " + n.getTrigger());
0N/A stringMessageReceived = true;
0N/A } else {
0N/A echo("\t\tSkipping notification of type: " + type);
0N/A }
0N/A } catch (Exception e) {
0N/A echo("\tError in handleNotification!");
0N/A e.printStackTrace(System.out);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Update the counter and check for notifications
0N/A */
0N/A public int counterMonitorNotification(int testCase)
0N/A throws Exception {
0N/A
0N/A counterMessageReceived = false;
0N/A CounterMonitor counterMonitor = null;
0N/A try {
0N/A MBeanServer server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A String domain = server.getDefaultDomain();
0N/A
0N/A // Create a new CounterMonitor MBean and add it to the MBeanServer.
0N/A //
0N/A echo(">>> CREATE a new CounterMonitor MBean");
0N/A ObjectName counterMonitorName = new ObjectName(
0N/A domain + ":type=" + CounterMonitor.class.getName());
0N/A counterMonitor = new CounterMonitor();
0N/A server.registerMBean(counterMonitor, counterMonitorName);
0N/A
0N/A echo(">>> ADD a listener to the CounterMonitor");
0N/A counterMonitor.addNotificationListener(this, null, null);
0N/A
0N/A //
0N/A // MANAGEMENT OF A STANDARD MBEAN
0N/A //
0N/A
0N/A echo(">>> CREATE a new ObservedObject MBean");
0N/A
0N/A ObjectName obsObjName =
0N/A ObjectName.getInstance(domain + ":type=ObservedObject");
0N/A ObservedObject obsObj = new ObservedObject();
0N/A ComplexAttribute ca = new ComplexAttribute();
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.ia = 0;
0N/A break;
0N/A case 2:
0N/A ca.setIntegerAttribute(0);
0N/A obsObj.setComplexAttribute(ca);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[0]);
0N/A obsObj.setComplexAttribute(ca);
0N/A break;
0N/A }
0N/A server.registerMBean(obsObj, obsObjName);
0N/A
0N/A echo(">>> SET the attributes of the CounterMonitor:");
0N/A
0N/A counterMonitor.addObservedObject(obsObjName);
0N/A echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A counterMonitor.setObservedAttribute(
0N/A "CompositeDataAttribute.IntegerAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "CompositeDataAttribute.IntegerAttribute");
0N/A break;
0N/A case 2:
0N/A counterMonitor.setObservedAttribute(
0N/A "ComplexAttribute.integerAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "ComplexAttribute.integerAttribute");
0N/A break;
0N/A case 3:
0N/A counterMonitor.setObservedAttribute(
0N/A "ComplexAttribute.arrayAttribute.length");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "ComplexAttribute.arrayAttribute.length");
0N/A break;
0N/A }
0N/A
0N/A counterMonitor.setNotify(true);
0N/A echo("\tATTRIBUTE \"NotifyFlag\" = true");
0N/A
0N/A Integer threshold = 2;
0N/A counterMonitor.setInitThreshold(threshold);
0N/A echo("\tATTRIBUTE \"Threshold\" = " + threshold);
0N/A
0N/A int granularityperiod = 500;
0N/A counterMonitor.setGranularityPeriod(granularityperiod);
0N/A echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
0N/A
0N/A echo(">>> START the CounterMonitor");
0N/A counterMonitor.start();
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.ia = 1;
0N/A break;
0N/A case 2:
0N/A ca.setIntegerAttribute(1);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[1]);
0N/A break;
0N/A }
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.ia = 2;
0N/A break;
0N/A case 2:
0N/A ca.setIntegerAttribute(2);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[2]);
0N/A break;
0N/A }
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.ia = 3;
0N/A break;
0N/A case 2:
0N/A ca.setIntegerAttribute(3);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[3]);
0N/A break;
0N/A }
0N/A
0N/A // Check if notification was received
0N/A //
0N/A if (counterMessageReceived) {
0N/A echo("\tOK: CounterMonitor notification received");
0N/A } else {
0N/A echo("\tKO: CounterMonitor notification missed or not emitted");
0N/A return 1;
0N/A }
0N/A } finally {
0N/A if (counterMonitor != null)
0N/A counterMonitor.stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Update the gauge and check for notifications
0N/A */
0N/A public int gaugeMonitorNotification(int testCase)
0N/A throws Exception {
0N/A
0N/A gaugeMessageReceived = false;
0N/A GaugeMonitor gaugeMonitor = null;
0N/A try {
0N/A MBeanServer server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A String domain = server.getDefaultDomain();
0N/A
0N/A // Create a new GaugeMonitor MBean and add it to the MBeanServer.
0N/A //
0N/A echo(">>> CREATE a new GaugeMonitor MBean");
0N/A ObjectName gaugeMonitorName = new ObjectName(
0N/A domain + ":type=" + GaugeMonitor.class.getName());
0N/A gaugeMonitor = new GaugeMonitor();
0N/A server.registerMBean(gaugeMonitor, gaugeMonitorName);
0N/A
0N/A echo(">>> ADD a listener to the GaugeMonitor");
0N/A gaugeMonitor.addNotificationListener(this, null, null);
0N/A
0N/A //
0N/A // MANAGEMENT OF A STANDARD MBEAN
0N/A //
0N/A
0N/A echo(">>> CREATE a new ObservedObject MBean");
0N/A
0N/A ObjectName obsObjName =
0N/A ObjectName.getInstance(domain + ":type=ObservedObject");
0N/A ObservedObject obsObj = new ObservedObject();
0N/A ComplexAttribute ca = new ComplexAttribute();
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.da = 0.0;
0N/A break;
0N/A case 2:
0N/A ca.setDoubleAttribute(0.0);
0N/A obsObj.setComplexAttribute(ca);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[0]);
0N/A obsObj.setComplexAttribute(ca);
0N/A break;
0N/A }
0N/A server.registerMBean(obsObj, obsObjName);
0N/A
0N/A echo(">>> SET the attributes of the GaugeMonitor:");
0N/A
0N/A gaugeMonitor.addObservedObject(obsObjName);
0N/A echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A gaugeMonitor.setObservedAttribute(
0N/A "CompositeDataAttribute.DoubleAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "CompositeDataAttribute.DoubleAttribute");
0N/A break;
0N/A case 2:
0N/A gaugeMonitor.setObservedAttribute(
0N/A "ComplexAttribute.doubleAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "ComplexAttribute.doubleAttribute");
0N/A break;
0N/A case 3:
0N/A gaugeMonitor.setObservedAttribute(
0N/A "ComplexAttribute.arrayAttribute.length");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "ComplexAttribute.arrayAttribute.length");
0N/A break;
0N/A }
0N/A
0N/A gaugeMonitor.setNotifyLow(false);
0N/A gaugeMonitor.setNotifyHigh(true);
0N/A echo("\tATTRIBUTE \"Notify Low Flag\" = false");
0N/A echo("\tATTRIBUTE \"Notify High Flag\" = true");
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A case 2:
0N/A Double highThresholdD = 3.0, lowThresholdD = 2.5;
0N/A gaugeMonitor.setThresholds(highThresholdD, lowThresholdD);
0N/A echo("\tATTRIBUTE \"Low Threshold\" = " + lowThresholdD);
0N/A echo("\tATTRIBUTE \"High Threshold\" = " + highThresholdD);
0N/A break;
0N/A case 3:
0N/A Integer highThreshold = 2, lowThreshold = 1;
0N/A gaugeMonitor.setThresholds(highThreshold, lowThreshold);
0N/A echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);
0N/A echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);
0N/A break;
0N/A }
0N/A
0N/A int granularityperiod = 500;
0N/A gaugeMonitor.setGranularityPeriod(granularityperiod);
0N/A echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
0N/A
0N/A echo(">>> START the GaugeMonitor");
0N/A gaugeMonitor.start();
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.da = 2.0;
0N/A break;
0N/A case 2:
0N/A ca.setDoubleAttribute(2.0);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[2]);
0N/A break;
0N/A }
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.da = 4.0;
0N/A break;
0N/A case 2:
0N/A ca.setDoubleAttribute(4.0);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[4]);
0N/A break;
0N/A }
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.da = 6.0;
0N/A break;
0N/A case 2:
0N/A ca.setDoubleAttribute(6.0);
0N/A break;
0N/A case 3:
0N/A ca.setArrayAttribute(new Integer[6]);
0N/A break;
0N/A }
0N/A
0N/A // Check if notification was received
0N/A //
0N/A if (gaugeMessageReceived) {
0N/A echo("\tOK: GaugeMonitor notification received");
0N/A } else {
0N/A echo("\tKO: GaugeMonitor notification missed or not emitted");
0N/A return 1;
0N/A }
0N/A } finally {
0N/A if (gaugeMonitor != null)
0N/A gaugeMonitor.stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Update the string and check for notifications
0N/A */
0N/A public int stringMonitorNotification(int testCase)
0N/A throws Exception {
0N/A
0N/A stringMessageReceived = false;
0N/A StringMonitor stringMonitor = null;
0N/A try {
0N/A MBeanServer server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A String domain = server.getDefaultDomain();
0N/A
0N/A // Create a new StringMonitor MBean and add it to the MBeanServer.
0N/A //
0N/A echo(">>> CREATE a new StringMonitor MBean");
0N/A ObjectName stringMonitorName = new ObjectName(
0N/A domain + ":type=" + StringMonitor.class.getName());
0N/A stringMonitor = new StringMonitor();
0N/A server.registerMBean(stringMonitor, stringMonitorName);
0N/A
0N/A echo(">>> ADD a listener to the StringMonitor");
0N/A stringMonitor.addNotificationListener(this, null, null);
0N/A
0N/A //
0N/A // MANAGEMENT OF A STANDARD MBEAN
0N/A //
0N/A
0N/A echo(">>> CREATE a new ObservedObject MBean");
0N/A
0N/A ObjectName obsObjName =
0N/A ObjectName.getInstance(domain + ":type=ObservedObject");
0N/A ObservedObject obsObj = new ObservedObject();
0N/A ComplexAttribute ca = new ComplexAttribute();
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.sa = "do_not_match_0";
0N/A break;
0N/A case 2:
0N/A ca.setStringAttribute("do_not_match_0");
0N/A obsObj.setComplexAttribute(ca);
0N/A break;
0N/A case 3:
0N/A ca.setEnumAttribute(Match.do_not_match_0);
0N/A obsObj.setComplexAttribute(ca);
0N/A break;
0N/A }
0N/A server.registerMBean(obsObj, obsObjName);
0N/A
0N/A echo(">>> SET the attributes of the StringMonitor:");
0N/A
0N/A stringMonitor.addObservedObject(obsObjName);
0N/A echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A stringMonitor.setObservedAttribute(
0N/A "CompositeDataAttribute.StringAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "CompositeDataAttribute.StringAttribute");
0N/A break;
0N/A case 2:
0N/A stringMonitor.setObservedAttribute(
0N/A "ComplexAttribute.stringAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "ComplexAttribute.stringAttribute");
0N/A break;
0N/A case 3:
0N/A stringMonitor.setObservedAttribute(
0N/A "ComplexAttribute.enumAttribute.name");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = " +
0N/A "ComplexAttribute.enumAttribute.name");
0N/A break;
0N/A }
0N/A
0N/A stringMonitor.setNotifyMatch(true);
0N/A echo("\tATTRIBUTE \"NotifyMatch\" = true");
0N/A
0N/A stringMonitor.setNotifyDiffer(false);
0N/A echo("\tATTRIBUTE \"NotifyDiffer\" = false");
0N/A
0N/A stringMonitor.setStringToCompare("do_match_now");
0N/A echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\"");
0N/A
0N/A int granularityperiod = 500;
0N/A stringMonitor.setGranularityPeriod(granularityperiod);
0N/A echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
0N/A
0N/A echo(">>> START the StringMonitor");
0N/A stringMonitor.start();
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.sa = "do_not_match_1";
0N/A break;
0N/A case 2:
0N/A ca.setStringAttribute("do_not_match_1");
0N/A break;
0N/A case 3:
0N/A ca.setEnumAttribute(Match.do_not_match_1);
0N/A break;
0N/A }
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.sa = "do_match_now";
0N/A break;
0N/A case 2:
0N/A ca.setStringAttribute("do_match_now");
0N/A break;
0N/A case 3:
0N/A ca.setEnumAttribute(Match.do_match_now);
0N/A break;
0N/A }
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A switch (testCase) {
0N/A case 1:
0N/A obsObj.sa = "do_not_match_2";
0N/A break;
0N/A case 2:
0N/A ca.setStringAttribute("do_not_match_2");
0N/A break;
0N/A case 3:
0N/A ca.setEnumAttribute(Match.do_not_match_2);
0N/A break;
0N/A }
0N/A
0N/A // Check if notification was received
0N/A //
0N/A if (stringMessageReceived) {
0N/A echo("\tOK: StringMonitor notification received");
0N/A } else {
0N/A echo("\tKO: StringMonitor notification missed or not emitted");
0N/A return 1;
0N/A }
0N/A } finally {
0N/A if (stringMonitor != null)
0N/A stringMonitor.stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Test the monitor notifications.
0N/A */
0N/A public int monitorNotifications() throws Exception {
0N/A echo(">>> ----------------------------------------");
0N/A int error = counterMonitorNotification(1);
0N/A echo(">>> ----------------------------------------");
0N/A error += counterMonitorNotification(2);
0N/A echo(">>> ----------------------------------------");
0N/A error += counterMonitorNotification(3);
0N/A echo(">>> ----------------------------------------");
0N/A error += gaugeMonitorNotification(1);
0N/A echo(">>> ----------------------------------------");
0N/A error += gaugeMonitorNotification(2);
0N/A echo(">>> ----------------------------------------");
0N/A error += gaugeMonitorNotification(3);
0N/A echo(">>> ----------------------------------------");
0N/A error += stringMonitorNotification(1);
0N/A echo(">>> ----------------------------------------");
0N/A error += stringMonitorNotification(2);
0N/A echo(">>> ----------------------------------------");
0N/A error += stringMonitorNotification(3);
0N/A echo(">>> ----------------------------------------");
0N/A return error;
0N/A }
0N/A
0N/A /*
0N/A * Print message
0N/A */
0N/A private static void echo(String message) {
0N/A System.out.println(message);
0N/A }
0N/A
1790N/A public static Object elementFromComplex(Object complex, String element)
1790N/A throws AttributeNotFoundException {
1790N/A try {
1790N/A if (complex.getClass().isArray() && element.equals("length")) {
1790N/A return Array.getLength(complex);
1790N/A } else if (complex instanceof CompositeData) {
1790N/A return ((CompositeData) complex).get(element);
1790N/A } else {
1790N/A // Java Beans introspection
1790N/A //
1790N/A BeanInfo bi = java.beans.Introspector.getBeanInfo(complex.getClass());
1790N/A PropertyDescriptor[] pds = bi.getPropertyDescriptors();
1790N/A System.out.println("PDs: " + pds.length);
1790N/A for (PropertyDescriptor pd : pds) {
1790N/A System.out.println("Property: " + pd.getName());
1790N/A if (pd.getName().equals(element))
1790N/A return pd.getReadMethod().invoke(complex);
1790N/A }
1790N/A throw new AttributeNotFoundException(
1790N/A "Could not find the getter method for the property " +
1790N/A element + " using the Java Beans introspector");
1790N/A }
1790N/A } catch (InvocationTargetException e) {
1790N/A throw new IllegalArgumentException(e);
1790N/A } catch (AttributeNotFoundException e) {
1790N/A throw e;
1790N/A } catch (Exception e) {
1790N/A AttributeNotFoundException anfe =
1790N/A new AttributeNotFoundException(e.getMessage());
1790N/A anfe.initCause(e);
1790N/A throw anfe;
1790N/A }
1790N/A }
1790N/A
0N/A /*
0N/A * Standalone entry point.
0N/A *
0N/A * Run the test and report to stdout.
0N/A */
0N/A public static void main (String args[]) throws Exception {
1790N/A Match match = Match.do_match_now;
1790N/A String name = (String) elementFromComplex(match, "name");
1790N/A System.out.println("name: " + name);
0N/A AttributeArbitraryDataTypeTest test =
0N/A new AttributeArbitraryDataTypeTest();
0N/A int error = test.monitorNotifications();
0N/A if (error > 0) {
0N/A echo(">>> Unhappy Bye, Bye!");
0N/A throw new IllegalStateException("Test FAILED: Didn't get all " +
0N/A "the notifications that were " +
0N/A "expected by the test!");
0N/A } else {
0N/A echo(">>> Happy Bye, Bye!");
0N/A }
0N/A }
0N/A}