001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.modelmbean;
010:
011: import java.lang.reflect.Method;
012: import javax.management.Descriptor;
013: import javax.management.MBeanOperationInfo;
014: import javax.management.MBeanParameterInfo;
015: import javax.management.RuntimeOperationsException;
016: import javax.management.modelmbean.DescriptorSupport;
017: import javax.management.modelmbean.ModelMBeanOperationInfo;
018:
019: import test.MX4JTestCase;
020:
021: /**
022: * Test case of ModelMBeanOperationInfo. It will try to verify an appropriate
023: * behaviour in particular with respect to the descriptor values
024: *
025: * @version $Revision: 1.5 $
026: * @see
027: */
028:
029: public class ModelMBeanOperationInfoTest extends MX4JTestCase {
030: public static class Surgeon {
031: public boolean appendectomy() {
032: return true;
033: }
034:
035: public boolean tonsillectomy(int tonsils) {
036: return true;
037: }
038: }
039:
040: public ModelMBeanOperationInfoTest(String s) {
041: super (s);
042: }
043:
044: public void setUp() throws Exception {
045: super .setUp();
046: }
047:
048: public void tearDown() throws Exception {
049: super .tearDown();
050: }
051:
052: public void testValidDescriptorFields() throws Exception {
053: // Test that only name and descriptorType are mandatory
054: Descriptor descriptor = new DescriptorSupport(new String[] {
055: "name", "descriptortype", "role", "visibility" },
056: new String[] { "operation1", "operation", "operation",
057: "1" });
058: ModelMBeanOperationInfo operation = new ModelMBeanOperationInfo(
059: "operation1", "An operation", null, "java.lang.String",
060: ModelMBeanOperationInfo.ACTION, descriptor);
061: // in case the descriptor is not valid this should be overriden
062: assertEquals(operation.getDescriptor().getFieldValue(
063: "visibility"), "1");
064: }
065:
066: public void testAddDefaultDisplayName() throws Exception {
067: Method op = ModelMBeanOperationInfoTest.Surgeon.class
068: .getMethod("appendectomy", new Class[0]);
069: String[] fields = { "name", "descriptorType", "role" };
070: String[] values = { op.getName(), "operation", "operation" };
071: DescriptorSupport ds = new DescriptorSupport(fields, values);
072: ModelMBeanOperationInfo info = new ModelMBeanOperationInfo(
073: "Good Appendectomy", op, ds);
074: Descriptor d = info.getDescriptor();
075: String dispname = (String) d.getFieldValue("displayName");
076: assertTrue("Unexpected displayName", dispname.compareTo(op
077: .getName()) == 0);
078: }
079:
080: public void testRoleValidation() throws Exception {
081: Method op = ModelMBeanOperationInfoTest.Surgeon.class
082: .getMethod("appendectomy", new Class[0]);
083: String[] fields = { "name", "descriptorType", "role",
084: "displayName" };
085: String[] values = { op.getName(), "operation", "operation",
086: "appendectomy" };
087: DescriptorSupport ds = new DescriptorSupport(fields, values);
088: ModelMBeanOperationInfo info = new ModelMBeanOperationInfo(
089: "Good Appendectomy", op, ds);
090:
091: try {
092: values = new String[] { op.getName(), "operation",
093: "constructor", "appendectomy" };
094: ds = new DescriptorSupport(fields, values);
095: info = new ModelMBeanOperationInfo("Bad Appendectomy", op,
096: ds);
097: fail("Expecting RuntimeOperationsException");
098: } catch (RuntimeOperationsException x) {
099: assertTrue(true); // success
100: }
101: }
102:
103: public void testCaseInsensitiveDescriptorType() {
104: DescriptorSupport ds = new DescriptorSupport(new String[] {
105: "name=getWineList", "descriptorType=oPERATION",
106: "displayName=Retrieve the list of available wines",
107: "role=getter" });
108: ModelMBeanOperationInfo attrinfo = new ModelMBeanOperationInfo(
109: "getWineList", "Retrieve the list of available wines",
110: new MBeanParameterInfo[0], "String",
111: MBeanOperationInfo.INFO, ds);
112: }
113: }
|