001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.management.jmx.export;
019:
020: import javax.management.Attribute;
021: import javax.management.MBeanAttributeInfo;
022: import javax.management.MBeanNotificationInfo;
023: import javax.management.MBeanOperationInfo;
024: import javax.management.MBeanServer;
025: import javax.management.MBeanServerFactory;
026: import javax.management.MalformedObjectNameException;
027: import javax.management.ObjectName;
028: import javax.management.modelmbean.ModelMBeanInfo;
029: import javax.management.modelmbean.ModelMBeanOperationInfo;
030: import javax.management.modelmbean.RequiredModelMBean;
031:
032: import org.apache.cxf.management.jmx.export.runtime.ModelMBeanAssembler;
033: import org.junit.After;
034: import org.junit.Assert;
035: import org.junit.Before;
036: import org.junit.Test;
037:
038: public class ModelMBeanAssemblerTest extends Assert {
039:
040: protected static final String AGE_ATTRIBUTE = "Age";
041:
042: protected static final String NAME_ATTRIBUTE = "Name";
043:
044: protected static AnnotationTestInstrumentation ati = new AnnotationTestInstrumentation();
045:
046: protected MBeanServer server;
047:
048: protected ObjectName ton;
049:
050: @Before
051: public final void setUp() throws Exception {
052: this .server = MBeanServerFactory.createMBeanServer();
053: try {
054: onSetUp();
055: } catch (Exception e) {
056: releaseServer();
057: throw e;
058: }
059: }
060:
061: @After
062: public void tearDown() throws Exception {
063: releaseServer();
064: onTearDown();
065: }
066:
067: private void releaseServer() {
068: MBeanServerFactory.releaseMBeanServer(this .getServer());
069: }
070:
071: protected void onTearDown() throws Exception {
072: // unregister the mbean client
073: server.unregisterMBean(ton);
074: }
075:
076: protected void onSetUp() throws Exception {
077:
078: try {
079: ton = new ObjectName(
080: "org.apache.cxf:Type=testInstrumentation");
081: } catch (MalformedObjectNameException e) {
082: e.printStackTrace();
083: } catch (NullPointerException e) {
084: e.printStackTrace();
085: }
086:
087: //create the mbean and register it
088: ModelMBeanInfo mbi = getMBeanInfoFromAssembler();
089:
090: RequiredModelMBean rtMBean;
091:
092: rtMBean = (RequiredModelMBean) server
093: .instantiate("javax.management.modelmbean.RequiredModelMBean");
094:
095: rtMBean.setModelMBeanInfo(mbi);
096:
097: rtMBean.setManagedResource(ati, "ObjectReference");
098:
099: server.registerMBean(rtMBean, ton);
100: }
101:
102: public MBeanServer getServer() {
103: return server;
104: }
105:
106: //the client get and set the ModelObject and setup the ManagerBean
107: @Test
108: public void testRegisterOperations() throws Exception {
109: ModelMBeanInfo info = getMBeanInfoFromAssembler();
110: assertEquals("Incorrect number of operations registered", 10,
111: info.getOperations().length);
112: }
113:
114: @Test
115: public void testGetMBeanInfo() throws Exception {
116: ModelMBeanInfo info = getMBeanInfoFromAssembler();
117: assertNotNull("MBeanInfo should not be null", info);
118: }
119:
120: @Test
121: public void testGetMBeanAttributeInfo() throws Exception {
122: ModelMBeanInfo info = getMBeanInfoFromAssembler();
123: MBeanAttributeInfo[] inf = info.getAttributes();
124: assertEquals("Invalid number of Attributes returned", 4,
125: inf.length);
126:
127: for (int x = 0; x < inf.length; x++) {
128: assertNotNull("MBeanAttributeInfo should not be null",
129: inf[x]);
130: assertNotNull(
131: "Description for MBeanAttributeInfo should not be null",
132: inf[x].getDescription());
133: }
134: }
135:
136: @Test
137: public void testGetMBeanOperationInfo() throws Exception {
138: ModelMBeanInfo info = getMBeanInfoFromAssembler();
139: MBeanOperationInfo[] inf = info.getOperations();
140: assertEquals("Invalid number of Operations returned", 10,
141: inf.length);
142:
143: for (int x = 0; x < inf.length; x++) {
144: assertNotNull("MBeanOperationInfo should not be null",
145: inf[x]);
146: assertNotNull(
147: "Description for MBeanOperationInfo should not be null",
148: inf[x].getDescription());
149: }
150: }
151:
152: @Test
153: public void testDescriptionNotNull() throws Exception {
154: ModelMBeanInfo info = getMBeanInfoFromAssembler();
155: assertNotNull("The MBean description should not be null", info
156: .getDescription());
157: }
158:
159: @Test
160: public void testSetAttribute() throws Exception {
161: getServer().setAttribute(ton, new Attribute(AGE_ATTRIBUTE, 12));
162: assertEquals("The Age should be ", 12, ati.getAge());
163: getServer().setAttribute(ton,
164: new Attribute(NAME_ATTRIBUTE, "Rob Harrop"));
165: assertEquals("The name should be ", "Rob Harrop", ati.getName());
166: }
167:
168: @Test
169: public void testGetAttribute() throws Exception {
170: ati.setName("John Smith");
171: Object val = getServer().getAttribute(ton, NAME_ATTRIBUTE);
172: assertEquals("Incorrect result", "John Smith", val);
173: }
174:
175: @Test
176: public void testOperationInvocation() throws Exception {
177: Object result = getServer().invoke(ton, "add",
178: new Object[] { new Integer(20), new Integer(30) },
179: new String[] { "int", "int" });
180: assertEquals("Incorrect result", new Integer(50), result);
181: }
182:
183: @Test
184: public void testAttributeHasCorrespondingOperations()
185: throws Exception {
186: ModelMBeanInfo info = getMBeanInfoFromAssembler();
187:
188: ModelMBeanOperationInfo myOperation = info
189: .getOperation("myOperation");
190: assertNotNull("get operation should not be null", myOperation);
191: assertEquals("Incorrect myOperation return type", "long",
192: myOperation.getReturnType());
193:
194: ModelMBeanOperationInfo add = info.getOperation("add");
195: assertNotNull("set operation should not be null", add);
196: assertEquals("Incorrect add method description",
197: "Add Two Numbers Together", add.getDescription());
198:
199: }
200:
201: @Test
202: public void testNotificationMetadata() throws Exception {
203: ModelMBeanInfo info = getMBeanInfoFromAssembler();
204: MBeanNotificationInfo[] notifications = info.getNotifications();
205: assertEquals("Incorrect number of notifications", 1,
206: notifications.length);
207: assertEquals("Incorrect notification name", "My Notification",
208: notifications[0].getName());
209:
210: String[] notifTypes = notifications[0].getNotifTypes();
211:
212: assertEquals("Incorrect number of notification types", 2,
213: notifTypes.length);
214: assertEquals("Notification type.foo not found", "type.foo",
215: notifTypes[0]);
216: assertEquals("Notification type.bar not found", "type.bar",
217: notifTypes[1]);
218: }
219:
220: protected ModelMBeanInfo getMBeanInfoFromAssembler() {
221: ModelMBeanAssembler assembler = new ModelMBeanAssembler();
222: return assembler
223: .getModelMbeanInfo(AnnotationTestInstrumentation.class);
224: }
225:
226: }
|