KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > modelmbean > ModelMBeanTEST


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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