KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > modelmbean > ModelMBeanOperationInfoTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.modelmbean;
10
11 import java.lang.reflect.Method JavaDoc;
12 import javax.management.Descriptor JavaDoc;
13 import javax.management.MBeanOperationInfo JavaDoc;
14 import javax.management.MBeanParameterInfo JavaDoc;
15 import javax.management.RuntimeOperationsException JavaDoc;
16 import javax.management.modelmbean.DescriptorSupport JavaDoc;
17 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
18
19 import test.MX4JTestCase;
20
21 /**
22  * Test case of ModelMBeanOperationInfo. It will try to verify an appropriate
23  * behaviour in particular with respect to the descriptor values
24  *
25  * @version $Revision: 1.5 $
26  * @see
27  */

28
29 public class ModelMBeanOperationInfoTest extends MX4JTestCase
30 {
31    public static class Surgeon
32    {
33       public boolean appendectomy()
34       {
35          return true;
36       }
37
38       public boolean tonsillectomy(int tonsils)
39       {
40          return true;
41       }
42    }
43
44    public ModelMBeanOperationInfoTest(String JavaDoc s)
45    {
46       super(s);
47    }
48
49    public void setUp() throws Exception JavaDoc
50    {
51       super.setUp();
52    }
53
54    public void tearDown() throws Exception JavaDoc
55    {
56       super.tearDown();
57    }
58
59    public void testValidDescriptorFields() throws Exception JavaDoc
60    {
61       // Test that only name and descriptorType are mandatory
62
Descriptor JavaDoc descriptor = new DescriptorSupport JavaDoc(new String JavaDoc[]{"name", "descriptortype", "role", "visibility"},
63                                                     new String JavaDoc[]{"operation1", "operation", "operation", "1"});
64       ModelMBeanOperationInfo JavaDoc operation
65               = new ModelMBeanOperationInfo JavaDoc("operation1", "An operation", null, "java.lang.String", ModelMBeanOperationInfo.ACTION, descriptor);
66       // in case the descriptor is not valid this should be overriden
67
assertEquals(operation.getDescriptor().getFieldValue("visibility"), "1");
68    }
69
70    public void testAddDefaultDisplayName() throws Exception JavaDoc
71    {
72       Method JavaDoc op =
73               ModelMBeanOperationInfoTest.Surgeon.class.getMethod("appendectomy",
74                                                                   new Class JavaDoc[0]);
75       String JavaDoc[] fields = {"name", "descriptorType", "role"};
76       String JavaDoc[] values =
77               {op.getName(), "operation", "operation"};
78       DescriptorSupport JavaDoc ds = new DescriptorSupport JavaDoc(fields, values);
79       ModelMBeanOperationInfo JavaDoc info =
80               new ModelMBeanOperationInfo JavaDoc("Good Appendectomy", op, ds);
81       Descriptor JavaDoc d = info.getDescriptor();
82       String JavaDoc dispname = (String JavaDoc)d.getFieldValue("displayName");
83       assertTrue("Unexpected displayName",
84                  dispname.compareTo(op.getName()) == 0);
85    }
86
87    public void testRoleValidation() throws Exception JavaDoc
88    {
89       Method JavaDoc op =
90               ModelMBeanOperationInfoTest.Surgeon.class.getMethod("appendectomy",
91                                                                   new Class JavaDoc[0]);
92       String JavaDoc[] fields = {"name", "descriptorType", "role", "displayName"};
93       String JavaDoc[] values =
94               {op.getName(), "operation", "operation", "appendectomy"};
95       DescriptorSupport JavaDoc ds = new DescriptorSupport JavaDoc(fields, values);
96       ModelMBeanOperationInfo JavaDoc info =
97               new ModelMBeanOperationInfo JavaDoc("Good Appendectomy", op, ds);
98
99       try
100       {
101          values =
102          new String JavaDoc[]{
103             op.getName(),
104             "operation",
105             "constructor",
106             "appendectomy"};
107          ds = new DescriptorSupport JavaDoc(fields, values);
108          info = new ModelMBeanOperationInfo JavaDoc("Bad Appendectomy", op, ds);
109          fail("Expecting RuntimeOperationsException");
110       }
111       catch (RuntimeOperationsException JavaDoc x)
112       {
113          assertTrue(true); // success
114
}
115    }
116
117    public void testCaseInsensitiveDescriptorType()
118    {
119       DescriptorSupport JavaDoc ds = new DescriptorSupport JavaDoc(new String JavaDoc[]{
120          "name=getWineList",
121          "descriptorType=oPERATION",
122          "displayName=Retrieve the list of available wines",
123          "role=getter"
124       });
125       ModelMBeanOperationInfo JavaDoc attrinfo =
126               new ModelMBeanOperationInfo JavaDoc("getWineList",
127                                           "Retrieve the list of available wines",
128                                           new MBeanParameterInfo JavaDoc[0],
129                                           "String",
130                                           MBeanOperationInfo.INFO,
131                                           ds);
132    }
133 }
134
Popular Tags