KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > modelmbean > ModelMBeanTEST


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.jmx.compliance.modelmbean;
23
24 import javax.management.Descriptor JavaDoc;
25 import javax.management.MBeanOperationInfo JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MBeanInfo JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.Notification JavaDoc;
32 import javax.management.modelmbean.DescriptorSupport JavaDoc;
33 import javax.management.modelmbean.ModelMBean JavaDoc;
34 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
35 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
36 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
37 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
38 import javax.management.modelmbean.RequiredModelMBean JavaDoc;
39
40 import org.jboss.test.jmx.compliance.modelmbean.support.Resource;
41
42 import junit.framework.TestCase;
43
44 public class ModelMBeanTEST extends TestCase
45 {
46    static class RMMListener implements NotificationListener JavaDoc
47    {
48       public void handleNotification(Notification JavaDoc msg, Object JavaDoc handback)
49       {
50          System.out.println("handleNotification, msg="+msg);
51       }
52    }
53
54    public ModelMBeanTEST(String JavaDoc s)
55    {
56       super(s);
57    }
58
59    public void testRequiredModelMBeanConstructors()
60    {
61       try
62       {
63          new RequiredModelMBean JavaDoc();
64       }
65       catch (Throwable JavaDoc t)
66       {
67          t.printStackTrace();
68          fail("Creating Required ModelMBean instance with default constructor failed: " + t.toString());
69       }
70    }
71    
72    public void testRMMSetManagedResource()
73    {
74       try
75       {
76          ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
77          Resource resource = new Resource();
78          modelmbean.setManagedResource(resource, "ObjectReference");
79       }
80       catch (Throwable JavaDoc t)
81       {
82          t.printStackTrace();
83          fail("Setting resource object with 'ObjectReference' type failed: " + t.toString());
84       }
85    }
86
87    public void testRMMSetManagedResourceAfterRegister()
88       throws Exception JavaDoc
89    {
90       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
91          
92       Resource resource = new Resource();
93       ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
94       modelmbean.setModelMBeanInfo(getModelMBeanInfo());
95        
96       ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:invocationTest=true");
97       server.registerMBean(modelmbean, name);
98
99       modelmbean.setManagedResource(resource, "ObjectReference");
100
101       MBeanInfo JavaDoc info = server.getMBeanInfo(name);
102       assertTrue("MBeanInfo != null", info != null);
103
104       Object JavaDoc[] args = {};
105       String JavaDoc[] sig = {};
106       Boolean JavaDoc flag = (Boolean JavaDoc) server.invoke(name, "isActive", args, sig);
107       assertTrue("isActive", flag.booleanValue());
108    }
109
110    public void testRMMSetManagedResourceAfterRegisterViaServer()
111       throws Exception JavaDoc
112    {
113       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
114          
115       Resource resource = new Resource();
116       ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
117       modelmbean.setModelMBeanInfo(getModelMBeanInfo());
118        
119       ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:invocationTest=true");
120       server.registerMBean(modelmbean, name);
121
122       Object JavaDoc[] args = {resource, "ObjectReference"};
123       String JavaDoc[] sig = {"java.lang.Object", "java.lang.String"};
124       server.invoke(name, "setManagedResource", args, sig);
125
126       MBeanInfo JavaDoc info = server.getMBeanInfo(name);
127       assertTrue("MBeanInfo != null", info != null);
128
129       args = new Object JavaDoc[]{};
130       sig = new String JavaDoc[]{};
131       Boolean JavaDoc flag = (Boolean JavaDoc) server.invoke(name, "isActive", args, sig);
132       assertTrue("isActive", flag.booleanValue());
133    }
134
135    public void testCreatedRMMSetManagedResourceAfterRegisterViaServer()
136       throws Exception JavaDoc
137    {
138       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
139       ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:invocationTest=true");
140       ModelMBeanInfo JavaDoc info = getModelMBeanInfo();
141       Object JavaDoc[] ctorArgs = { info };
142       String JavaDoc[] ctorSig = { "javax.management.modelmbean.ModelMBeanInfo" };
143       server.createMBean("javax.management.modelmbean.RequiredModelMBean",
144          name, ctorArgs, ctorSig);
145
146       Resource resource = new Resource();
147
148       Object JavaDoc[] args = {resource, "ObjectReference"};
149       String JavaDoc[] sig = {"java.lang.Object", "java.lang.String"};
150       server.invoke(name, "setManagedResource", args, sig);
151
152       info = (ModelMBeanInfo JavaDoc) server.getMBeanInfo(name);
153       assertTrue("MBeanInfo != null", info != null);
154
155       args = new Object JavaDoc[]{};
156       sig = new String JavaDoc[]{};
157       Boolean JavaDoc flag = (Boolean JavaDoc) server.invoke(name, "isActive", args, sig);
158       assertTrue("isActive", flag.booleanValue());
159    }
160
161    public void testCreatedRMMSetManagedResourceAfterRegisterViaServerListener()
162       throws Exception JavaDoc
163    {
164       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
165       ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:invocationTest=true");
166       ModelMBeanInfo JavaDoc info = getModelMBeanInfo();
167       Object JavaDoc[] ctorArgs = { info };
168       String JavaDoc[] ctorSig = { "javax.management.modelmbean.ModelMBeanInfo" };
169       server.createMBean("javax.management.modelmbean.RequiredModelMBean",
170          name, ctorArgs, ctorSig);
171
172       Resource resource = new Resource();
173
174       Object JavaDoc[] args = {resource, "objectReference"};
175       String JavaDoc[] sig = {"java.lang.Object", "java.lang.String"};
176       server.invoke(name, "setManagedResource", args, sig);
177
178       RMMListener listener = new RMMListener();
179       server.addNotificationListener(name, listener, null, null);
180    }
181
182    public void testRMMSetModelMBeanInfo()
183       throws Exception JavaDoc
184    {
185       ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
186       modelmbean.setModelMBeanInfo(getModelMBeanInfo());
187    }
188
189    public void testRMMInvocation()
190       throws Exception JavaDoc
191    {
192       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
193          
194       Resource resource = new Resource();
195       ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
196       modelmbean.setModelMBeanInfo(getModelMBeanInfo());
197       modelmbean.setManagedResource(resource, "ObjectReference");
198        
199       ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:invocationTest=true");
200       server.registerMBean(modelmbean, name);
201
202       MBeanInfo JavaDoc info = server.getMBeanInfo(name);
203       assertTrue("MBeanInfo != null", info != null);
204
205       assertTrue(((Boolean JavaDoc)server.invoke(name, "isActive", new Object JavaDoc[] {}, new String JavaDoc[] {})).booleanValue());
206    }
207    
208    public void testRMMDefault()
209       throws Exception JavaDoc
210    {
211       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
212          
213       Resource resource = new Resource();
214       ModelMBean JavaDoc modelmbean = new RequiredModelMBean JavaDoc();
215       modelmbean.setManagedResource(resource, "ObjectReference");
216       modelmbean.setModelMBeanInfo(getModelMBeanInfo());
217          
218       ObjectName JavaDoc name = new ObjectName JavaDoc("rmm:test=default");
219       server.registerMBean(modelmbean, name);
220          
221       assertTrue("Should be the default room", server.getAttribute(name, "Room").equals("D325"));
222    }
223
224    public ModelMBeanInfo JavaDoc getModelMBeanInfo()
225    {
226       final boolean READABLE = true;
227       final boolean WRITABLE = true;
228       final boolean BOOLEAN = true;
229       
230       // build 'RoomName' read-write attribute
231
Descriptor JavaDoc descr1 = new DescriptorSupport JavaDoc();
232       descr1.setField("name", "Room");
233       descr1.setField("descriptorType", "attribute");
234       descr1.setField("displayName", "Room Number");
235       descr1.setField("default", "D325");
236
237       ModelMBeanAttributeInfo JavaDoc roomNameInfo =
238          new ModelMBeanAttributeInfo JavaDoc(
239             "Room", // attribute name
240
String JavaDoc.class.getName(), // attribute type
241
"Room name or number.", // description
242
READABLE, WRITABLE, !BOOLEAN, // read write
243
descr1 // descriptor
244
);
245
246       // build 'Active' read-only attribute
247
Descriptor JavaDoc descr2 = new DescriptorSupport JavaDoc();
248       descr2.setField("name", "Active");
249       descr2.setField("descriptorType", "attribute");
250       descr2.setField("getMethod", "isActive");
251       descr2.setField("currencyTimeLimit", "10");
252
253       ModelMBeanAttributeInfo JavaDoc activeInfo =
254          new ModelMBeanAttributeInfo JavaDoc(
255             "Active",
256             boolean.class.getName(),
257             "Printer state.",
258             READABLE, !WRITABLE, !BOOLEAN,
259             descr2
260          );
261
262       // build 'isActive' getter operation
263
Descriptor JavaDoc descr3 = new DescriptorSupport JavaDoc();
264       descr3.setField("name", "isActive");
265       descr3.setField("descriptorType", "operation");
266       descr3.setField("role", "getter");
267
268       ModelMBeanOperationInfo JavaDoc isActiveInfo =
269          new ModelMBeanOperationInfo JavaDoc(
270             "isActive", // name & description
271
"Checks if the printer is currently active.",
272             null, // signature
273
boolean.class.getName(), // return type
274
MBeanOperationInfo.INFO, // impact
275
descr3 // descriptor
276
);
277
278       // MBean descriptor
279
Descriptor JavaDoc descr4 = new DescriptorSupport JavaDoc();
280       descr4.setField("name", RequiredModelMBean JavaDoc.class.getName());
281       descr4.setField("descriptorType", "MBean");
282
283       // create ModelMBeanInfo
284
ModelMBeanInfo JavaDoc info = new ModelMBeanInfoSupport JavaDoc(
285                                RequiredModelMBean JavaDoc.class.getName(), // class name
286
"Printer", // description
287
new ModelMBeanAttributeInfo JavaDoc[] { // attributes
288
roomNameInfo,
289                                   activeInfo
290                                },
291                                null, // constructors
292
new ModelMBeanOperationInfo JavaDoc[] { // operations
293
isActiveInfo
294                                },
295                                null, // notifications
296
descr4 // descriptor
297
);
298
299       return info;
300    }
301
302 }
303
Popular Tags