KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > management > jmx > export > ModelMBeanAssemblerTest


1 package org.objectweb.celtix.bus.management.jmx.export;
2
3
4 import javax.management.Attribute JavaDoc;
5 import javax.management.MBeanAttributeInfo JavaDoc;
6 import javax.management.MBeanNotificationInfo JavaDoc;
7 import javax.management.MBeanOperationInfo JavaDoc;
8 import javax.management.MBeanServer JavaDoc;
9 import javax.management.MBeanServerFactory JavaDoc;
10 import javax.management.MalformedObjectNameException JavaDoc;
11 import javax.management.ObjectName JavaDoc;
12 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
13 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
14 import javax.management.modelmbean.RequiredModelMBean JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import org.objectweb.celtix.bus.management.jmx.export.runtime.ModelMBeanAssembler;
19
20
21
22 public class ModelMBeanAssemblerTest extends TestCase {
23     
24     protected static final String JavaDoc AGE_ATTRIBUTE = "Age";
25
26     protected static final String JavaDoc NAME_ATTRIBUTE = "Name";
27     
28     protected static AnnotationTestInstrumentation ati = new AnnotationTestInstrumentation();
29    
30     protected MBeanServer JavaDoc server;
31     
32     protected ObjectName JavaDoc ton;
33
34     public final void setUp() throws Exception JavaDoc {
35         this.server = MBeanServerFactory.createMBeanServer();
36         try {
37             onSetUp();
38         } catch (Exception JavaDoc e) {
39             releaseServer();
40             throw e;
41         }
42     }
43
44     protected void tearDown() throws Exception JavaDoc {
45         releaseServer();
46         onTearDown();
47     }
48
49     private void releaseServer() {
50         MBeanServerFactory.releaseMBeanServer(this.getServer());
51     }
52
53     protected void onTearDown() throws Exception JavaDoc {
54         // unregister the mbean client
55
server.unregisterMBean(ton);
56     }
57
58     protected void onSetUp() throws Exception JavaDoc {
59         
60         try {
61             ton = new ObjectName JavaDoc("org.objectweb.celtix:Type=testInstrumentation");
62         } catch (MalformedObjectNameException JavaDoc e) {
63             e.printStackTrace();
64         } catch (NullPointerException JavaDoc e) {
65             e.printStackTrace();
66         }
67        
68         
69         //create the mbean and register it
70
ModelMBeanInfo JavaDoc mbi = getMBeanInfoFromAssembler();
71         
72         RequiredModelMBean JavaDoc rtMBean;
73         
74         rtMBean = (RequiredModelMBean JavaDoc)server.instantiate(
75             "javax.management.modelmbean.RequiredModelMBean");
76                        
77         rtMBean.setModelMBeanInfo(mbi);
78     
79         rtMBean.setManagedResource(ati, "ObjectReference");
80                            
81         server.registerMBean(rtMBean, ton);
82     }
83
84     public MBeanServer JavaDoc getServer() {
85         return server;
86     }
87     
88     //the client get and set the ModelObject and setup the ManagerBean
89

90     public void testRegisterOperations() throws Exception JavaDoc {
91         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
92         assertEquals("Incorrect number of operations registered",
93                       10, info.getOperations().length);
94     }
95        
96     public void testGetMBeanInfo() throws Exception JavaDoc {
97         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
98         assertNotNull("MBeanInfo should not be null", info);
99     }
100
101     public void testGetMBeanAttributeInfo() throws Exception JavaDoc {
102         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
103         MBeanAttributeInfo JavaDoc[] inf = info.getAttributes();
104         assertEquals("Invalid number of Attributes returned",
105                        4, inf.length);
106
107         for (int x = 0; x < inf.length; x++) {
108             assertNotNull("MBeanAttributeInfo should not be null", inf[x]);
109             assertNotNull("Description for MBeanAttributeInfo should not be null",
110                             inf[x].getDescription());
111         }
112     }
113
114     public void testGetMBeanOperationInfo() throws Exception JavaDoc {
115         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
116         MBeanOperationInfo JavaDoc[] inf = info.getOperations();
117         assertEquals("Invalid number of Operations returned",
118                                10, inf.length);
119
120         for (int x = 0; x < inf.length; x++) {
121             assertNotNull("MBeanOperationInfo should not be null", inf[x]);
122             assertNotNull("Description for MBeanOperationInfo should not be null",
123                            inf[x].getDescription());
124         }
125     }
126
127     public void testDescriptionNotNull() throws Exception JavaDoc {
128         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
129         assertNotNull("The MBean description should not be null",
130                                 info.getDescription());
131     }
132
133     
134     public void testSetAttribute() throws Exception JavaDoc {
135         getServer().setAttribute(ton, new Attribute JavaDoc(AGE_ATTRIBUTE, 12));
136         assertEquals("The Age should be ", 12, ati.getAge());
137         getServer().setAttribute(ton, new Attribute JavaDoc(NAME_ATTRIBUTE, "Rob Harrop"));
138         assertEquals("The name should be ", "Rob Harrop", ati.getName());
139     }
140
141     public void testGetAttribute() throws Exception JavaDoc {
142         ati.setName("John Smith");
143         Object JavaDoc val = getServer().getAttribute(ton, NAME_ATTRIBUTE);
144         assertEquals("Incorrect result", "John Smith", val);
145     }
146
147     public void testOperationInvocation() throws Exception JavaDoc {
148         Object JavaDoc result = getServer().invoke(ton, "add",
149                                 new Object JavaDoc[] {new Integer JavaDoc(20), new Integer JavaDoc(30)}, new String JavaDoc[] {"int", "int"});
150         assertEquals("Incorrect result", new Integer JavaDoc(50), result);
151     }
152     
153     public void testAttributeHasCorrespondingOperations() throws Exception JavaDoc {
154         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
155
156         ModelMBeanOperationInfo JavaDoc myOperation = info.getOperation("myOperation");
157         assertNotNull("get operation should not be null", myOperation);
158         assertEquals("Incorrect myOperation return type", "long", myOperation.getReturnType());
159                 
160         ModelMBeanOperationInfo JavaDoc add = info.getOperation("add");
161         assertNotNull("set operation should not be null", add);
162         assertEquals("Incorrect add method description", "Add Two Numbers Together", add.getDescription());
163                 
164     }
165
166     public void testNotificationMetadata() throws Exception JavaDoc {
167         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
168         MBeanNotificationInfo JavaDoc[] notifications = info.getNotifications();
169         assertEquals("Incorrect number of notifications", 1, notifications.length);
170         assertEquals("Incorrect notification name", "My Notification", notifications[0].getName());
171
172         String JavaDoc[] notifTypes = notifications[0].getNotifTypes();
173
174         assertEquals("Incorrect number of notification types", 2, notifTypes.length);
175         assertEquals("Notification type.foo not found", "type.foo", notifTypes[0]);
176         assertEquals("Notification type.bar not found", "type.bar", notifTypes[1]);
177     }
178
179     protected ModelMBeanInfo JavaDoc getMBeanInfoFromAssembler() {
180         ModelMBeanAssembler assembler = new ModelMBeanAssembler();
181         return assembler.getModelMbeanInfo(AnnotationTestInstrumentation.class);
182     }
183      
184         
185   
186
187 }
188
Popular Tags