KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > compliance > modelmbean > ModelMBeanTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.jbossmx.compliance.modelmbean;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.Descriptor JavaDoc;
26 import javax.management.MBeanOperationInfo JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.modelmbean.DescriptorSupport JavaDoc;
31 import javax.management.modelmbean.ModelMBean JavaDoc;
32 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
33 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
34 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
35 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
36 import javax.management.modelmbean.RequiredModelMBean JavaDoc;
37
38 import org.jboss.test.jbossmx.compliance.TestCase;
39 import org.jboss.test.jbossmx.compliance.modelmbean.support.Resource;
40 import org.jboss.test.jbossmx.compliance.modelmbean.support.Resource2;
41
42 /**
43  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
44  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
45  * @version $Revision: 43642 $
46  */

47 public class ModelMBeanTestCase extends TestCase
48 {
49    public ModelMBeanTestCase(String JavaDoc s)
50    {
51       super(s);
52    }
53
54    public void testRequiredModelMBeanConstructors()
55    {
56       try
57       {
58          new RequiredModelMBean JavaDoc();
59       }
60       catch (Throwable JavaDoc t)
61       {
62          log.debug("failed", t);
63          fail("Creating Required ModelMBean instance with default constructor failed: " + t.toString());
64       }
65    }
66    
67    public void testRMMSetManagedResource()
68    {
69       try
70       {
71          ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
72          Resource resource = new Resource();
73          modelmbean.setManagedResource(resource, "ObjectReference");
74       }
75       catch (Throwable JavaDoc t)
76       {
77          log.debug("failed", t);
78          fail("Setting resource object with 'ObjectReference' type failed: " + t.toString());
79       }
80    }
81    
82    public void testRMMSetModelMBeanInfo()
83    {
84       try
85       {
86          ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
87          modelmbean.setModelMBeanInfo(getModelMBeanInfo());
88       }
89       catch (Throwable JavaDoc t)
90       {
91          log.debug("failed", t);
92          fail("Unable to set model mbean info for resource object: " + t.toString());
93       }
94
95    }
96
97    public void testRMMInvocation()
98    {
99       try
100       {
101          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
102          
103          Resource resource = new Resource();
104          ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
105          modelmbean.setModelMBeanInfo(getModelMBeanInfo());
106          modelmbean.setManagedResource(resource, "ObjectReference");
107          
108          ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:invocationTest=true");
109          server.registerMBean(modelmbean, name);
110          
111          assertTrue(((Boolean JavaDoc)server.invoke(name, "isActive", new Object JavaDoc[] {}, new String JavaDoc[] {})).booleanValue());
112       }
113       catch (Throwable JavaDoc t)
114       {
115          log.debug("failed", t);
116          fail("RMMInvocation: " + t.toString());
117       }
118    }
119    
120    /**
121     * A resource that implements an MBean interface at the same time.
122     *
123     * Used to test that the fix for JBAS-1704 doesn't cause a problem
124     * when a target resource *with* an mbean interface, too, registers
125     * through a model mbean and exposes methods/attribute that are not
126     * declared on the mbean interface.
127     */

128    public void testRMMResourceImplementsMBean()
129    {
130       try
131       {
132          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
133          
134          Resource2 resource = new Resource2();
135          ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
136          modelmbean.setModelMBeanInfo(getModelMBeanInfo2());
137          modelmbean.setManagedResource(resource, "ObjectReference");
138          
139          ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:resourceImplementsMBean=true");
140          server.registerMBean(modelmbean, name);
141          
142          server.setAttribute(name, new Attribute JavaDoc("pojoAttribute", new Integer JavaDoc(111)));
143          
144          assertEquals((Integer JavaDoc)server.getAttribute(name, "pojoAttribute"), new Integer JavaDoc(111));
145          assertTrue(((Boolean JavaDoc)server.invoke(name, "pojoOperation", new Object JavaDoc[] {}, new String JavaDoc[] {})).booleanValue());
146       }
147       catch (Throwable JavaDoc t)
148       {
149          log.debug("failed", t);
150          fail("testRMMResourceImplementsMBean: " + t.toString());
151       }
152    }
153    
154    private ModelMBeanInfo JavaDoc getModelMBeanInfo()
155    {
156       final boolean READABLE = true;
157       final boolean WRITABLE = true;
158       final boolean BOOLEAN = true;
159       
160       // build 'RoomName' read-write attribute
161
Descriptor JavaDoc descr1 = new DescriptorSupport JavaDoc();
162       descr1.setField("name", "Room");
163       descr1.setField("descriptorType", "attribute");
164       descr1.setField("displayName", "Room Number");
165       descr1.setField("default", "D325");
166
167       ModelMBeanAttributeInfo JavaDoc roomNameInfo =
168          new ModelMBeanAttributeInfo JavaDoc(
169             "Room", // attribute name
170
String JavaDoc.class.getName(), // attribute type
171
"Room name or number.", // description
172
READABLE, WRITABLE, !BOOLEAN, // read write
173
descr1 // descriptor
174
);
175
176
177       // build 'Active' read-only attribute
178
Descriptor JavaDoc descr2 = new DescriptorSupport JavaDoc();
179       descr2.setField("name", "Active");
180       descr2.setField("descriptorType", "attribute");
181       descr2.setField("getMethod", "isActive");
182       descr2.setField("currencyTimeLimit", "10");
183
184       ModelMBeanAttributeInfo JavaDoc activeInfo =
185          new ModelMBeanAttributeInfo JavaDoc(
186             "Active",
187             boolean.class.getName(),
188             "Printer state.",
189             READABLE, !WRITABLE, !BOOLEAN,
190             descr2
191          );
192
193       // build 'isActive' getter operation
194
Descriptor JavaDoc descr3 = new DescriptorSupport JavaDoc();
195       descr3.setField("name", "isActive");
196       descr3.setField("descriptorType", "operation");
197       descr3.setField("role", "getter");
198
199       ModelMBeanOperationInfo JavaDoc isActiveInfo =
200          new ModelMBeanOperationInfo JavaDoc(
201             "isActive", // name & description
202
"Checks if the printer is currently active.",
203             null, // signature
204
boolean.class.getName(), // return type
205
MBeanOperationInfo.INFO, // impact
206
descr3 // descriptor
207
);
208
209       // MBean descriptor
210
Descriptor JavaDoc descr4 = new DescriptorSupport JavaDoc();
211       descr4.setField("name", RequiredModelMBean JavaDoc.class.getName());
212       descr4.setField("descriptorType", "MBean");
213
214       // create ModelMBeanInfo
215
ModelMBeanInfo JavaDoc info = new ModelMBeanInfoSupport JavaDoc(
216                                RequiredModelMBean JavaDoc.class.getName(), // class name
217
"Printer", // description
218
new ModelMBeanAttributeInfo JavaDoc[] { // attributes
219
roomNameInfo,
220                                   activeInfo
221                                },
222                                null, // constructors
223
new ModelMBeanOperationInfo JavaDoc[] { // operations
224
isActiveInfo
225                                },
226                                null, // notifications
227
descr4 // descriptor
228
);
229
230       return info;
231    }
232
233    private ModelMBeanInfo JavaDoc getModelMBeanInfo2()
234    {
235       final boolean READABLE = true;
236       final boolean WRITABLE = true;
237       final boolean BOOLEAN = true;
238       
239       // build 'pojoAttribute' read-write attribute
240
Descriptor JavaDoc descr2 = new DescriptorSupport JavaDoc();
241       descr2.setField("name", "pojoAttribute");
242       descr2.setField("descriptorType", "attribute");
243       descr2.setField("getMethod", "getpojoAttribute");
244       descr2.setField("setMethod", "setpojoAttribute");
245
246       ModelMBeanAttributeInfo JavaDoc pojoAttributeInfo =
247          new ModelMBeanAttributeInfo JavaDoc(
248             "pojoAttribute",
249             int.class.getName(),
250             "A simple integer attribute.",
251             READABLE, WRITABLE, !BOOLEAN,
252             descr2
253          );
254
255       // build 'pojoOperation' operation
256
Descriptor JavaDoc descr3 = new DescriptorSupport JavaDoc();
257       descr3.setField("name", "pojoOperation");
258       descr3.setField("descriptorType", "operation");
259
260       ModelMBeanOperationInfo JavaDoc pojoOperationInfo =
261          new ModelMBeanOperationInfo JavaDoc(
262             "pojoOperation", // name & description
263
"A simple operation.",
264             null, // signature
265
boolean.class.getName(), // return type
266
MBeanOperationInfo.ACTION, // impact
267
descr3 // descriptor
268
);
269
270       // MBean descriptor
271
Descriptor JavaDoc descr4 = new DescriptorSupport JavaDoc();
272       descr4.setField("name", RequiredModelMBean JavaDoc.class.getName());
273       descr4.setField("descriptorType", "MBean");
274
275       // create ModelMBeanInfo
276
ModelMBeanInfo JavaDoc info = new ModelMBeanInfoSupport JavaDoc(
277                                RequiredModelMBean JavaDoc.class.getName(), // class name
278
"POJO", // description
279
new ModelMBeanAttributeInfo JavaDoc[] { // attributes
280
pojoAttributeInfo
281                                },
282                                null, // constructors
283
new ModelMBeanOperationInfo JavaDoc[] { // operations
284
pojoOperationInfo
285                                },
286                                null, // notifications
287
descr4 // descriptor
288
);
289
290       return info;
291    }
292 }
293
Popular Tags