001: package org.objectweb.celtix.bus.management.jmx.export;
002:
003: import javax.management.Attribute;
004: import javax.management.MBeanAttributeInfo;
005: import javax.management.MBeanNotificationInfo;
006: import javax.management.MBeanOperationInfo;
007: import javax.management.MBeanServer;
008: import javax.management.MBeanServerFactory;
009: import javax.management.MalformedObjectNameException;
010: import javax.management.ObjectName;
011: import javax.management.modelmbean.ModelMBeanInfo;
012: import javax.management.modelmbean.ModelMBeanOperationInfo;
013: import javax.management.modelmbean.RequiredModelMBean;
014:
015: import junit.framework.TestCase;
016:
017: import org.objectweb.celtix.bus.management.jmx.export.runtime.ModelMBeanAssembler;
018:
019: public class ModelMBeanAssemblerTest extends TestCase {
020:
021: protected static final String AGE_ATTRIBUTE = "Age";
022:
023: protected static final String NAME_ATTRIBUTE = "Name";
024:
025: protected static AnnotationTestInstrumentation ati = new AnnotationTestInstrumentation();
026:
027: protected MBeanServer server;
028:
029: protected ObjectName ton;
030:
031: public final void setUp() throws Exception {
032: this .server = MBeanServerFactory.createMBeanServer();
033: try {
034: onSetUp();
035: } catch (Exception e) {
036: releaseServer();
037: throw e;
038: }
039: }
040:
041: protected void tearDown() throws Exception {
042: releaseServer();
043: onTearDown();
044: }
045:
046: private void releaseServer() {
047: MBeanServerFactory.releaseMBeanServer(this .getServer());
048: }
049:
050: protected void onTearDown() throws Exception {
051: // unregister the mbean client
052: server.unregisterMBean(ton);
053: }
054:
055: protected void onSetUp() throws Exception {
056:
057: try {
058: ton = new ObjectName(
059: "org.objectweb.celtix:Type=testInstrumentation");
060: } catch (MalformedObjectNameException e) {
061: e.printStackTrace();
062: } catch (NullPointerException e) {
063: e.printStackTrace();
064: }
065:
066: //create the mbean and register it
067: ModelMBeanInfo mbi = getMBeanInfoFromAssembler();
068:
069: RequiredModelMBean rtMBean;
070:
071: rtMBean = (RequiredModelMBean) server
072: .instantiate("javax.management.modelmbean.RequiredModelMBean");
073:
074: rtMBean.setModelMBeanInfo(mbi);
075:
076: rtMBean.setManagedResource(ati, "ObjectReference");
077:
078: server.registerMBean(rtMBean, ton);
079: }
080:
081: public MBeanServer getServer() {
082: return server;
083: }
084:
085: //the client get and set the ModelObject and setup the ManagerBean
086:
087: public void testRegisterOperations() throws Exception {
088: ModelMBeanInfo info = getMBeanInfoFromAssembler();
089: assertEquals("Incorrect number of operations registered", 10,
090: info.getOperations().length);
091: }
092:
093: public void testGetMBeanInfo() throws Exception {
094: ModelMBeanInfo info = getMBeanInfoFromAssembler();
095: assertNotNull("MBeanInfo should not be null", info);
096: }
097:
098: public void testGetMBeanAttributeInfo() throws Exception {
099: ModelMBeanInfo info = getMBeanInfoFromAssembler();
100: MBeanAttributeInfo[] inf = info.getAttributes();
101: assertEquals("Invalid number of Attributes returned", 4,
102: inf.length);
103:
104: for (int x = 0; x < inf.length; x++) {
105: assertNotNull("MBeanAttributeInfo should not be null",
106: inf[x]);
107: assertNotNull(
108: "Description for MBeanAttributeInfo should not be null",
109: inf[x].getDescription());
110: }
111: }
112:
113: public void testGetMBeanOperationInfo() throws Exception {
114: ModelMBeanInfo info = getMBeanInfoFromAssembler();
115: MBeanOperationInfo[] inf = info.getOperations();
116: assertEquals("Invalid number of Operations returned", 10,
117: inf.length);
118:
119: for (int x = 0; x < inf.length; x++) {
120: assertNotNull("MBeanOperationInfo should not be null",
121: inf[x]);
122: assertNotNull(
123: "Description for MBeanOperationInfo should not be null",
124: inf[x].getDescription());
125: }
126: }
127:
128: public void testDescriptionNotNull() throws Exception {
129: ModelMBeanInfo info = getMBeanInfoFromAssembler();
130: assertNotNull("The MBean description should not be null", info
131: .getDescription());
132: }
133:
134: public void testSetAttribute() throws Exception {
135: getServer().setAttribute(ton, new Attribute(AGE_ATTRIBUTE, 12));
136: assertEquals("The Age should be ", 12, ati.getAge());
137: getServer().setAttribute(ton,
138: new Attribute(NAME_ATTRIBUTE, "Rob Harrop"));
139: assertEquals("The name should be ", "Rob Harrop", ati.getName());
140: }
141:
142: public void testGetAttribute() throws Exception {
143: ati.setName("John Smith");
144: Object val = getServer().getAttribute(ton, NAME_ATTRIBUTE);
145: assertEquals("Incorrect result", "John Smith", val);
146: }
147:
148: public void testOperationInvocation() throws Exception {
149: Object result = getServer().invoke(ton, "add",
150: new Object[] { new Integer(20), new Integer(30) },
151: new String[] { "int", "int" });
152: assertEquals("Incorrect result", new Integer(50), result);
153: }
154:
155: public void testAttributeHasCorrespondingOperations()
156: throws Exception {
157: ModelMBeanInfo info = getMBeanInfoFromAssembler();
158:
159: ModelMBeanOperationInfo myOperation = info
160: .getOperation("myOperation");
161: assertNotNull("get operation should not be null", myOperation);
162: assertEquals("Incorrect myOperation return type", "long",
163: myOperation.getReturnType());
164:
165: ModelMBeanOperationInfo add = info.getOperation("add");
166: assertNotNull("set operation should not be null", add);
167: assertEquals("Incorrect add method description",
168: "Add Two Numbers Together", add.getDescription());
169:
170: }
171:
172: public void testNotificationMetadata() throws Exception {
173: ModelMBeanInfo info = getMBeanInfoFromAssembler();
174: MBeanNotificationInfo[] notifications = info.getNotifications();
175: assertEquals("Incorrect number of notifications", 1,
176: notifications.length);
177: assertEquals("Incorrect notification name", "My Notification",
178: notifications[0].getName());
179:
180: String[] notifTypes = notifications[0].getNotifTypes();
181:
182: assertEquals("Incorrect number of notification types", 2,
183: notifTypes.length);
184: assertEquals("Notification type.foo not found", "type.foo",
185: notifTypes[0]);
186: assertEquals("Notification type.bar not found", "type.bar",
187: notifTypes[1]);
188: }
189:
190: protected ModelMBeanInfo getMBeanInfoFromAssembler() {
191: ModelMBeanAssembler assembler = new ModelMBeanAssembler();
192: return assembler
193: .getModelMbeanInfo(AnnotationTestInstrumentation.class);
194: }
195:
196: }
|