001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jbossmx.compliance.modelmbean;
023:
024: import javax.management.Attribute;
025: import javax.management.Descriptor;
026: import javax.management.MBeanOperationInfo;
027: import javax.management.MBeanParameterInfo;
028: import javax.management.MBeanServer;
029: import javax.management.MBeanServerFactory;
030: import javax.management.ObjectName;
031: import javax.management.modelmbean.DescriptorSupport;
032: import javax.management.modelmbean.ModelMBean;
033: import javax.management.modelmbean.ModelMBeanAttributeInfo;
034: import javax.management.modelmbean.ModelMBeanInfo;
035: import javax.management.modelmbean.ModelMBeanInfoSupport;
036: import javax.management.modelmbean.ModelMBeanOperationInfo;
037: import javax.management.modelmbean.RequiredModelMBean;
038:
039: import org.jboss.test.jbossmx.compliance.TestCase;
040: import org.jboss.test.jbossmx.compliance.modelmbean.support.Resource;
041: import org.jboss.test.jbossmx.compliance.modelmbean.support.Resource2;
042:
043: /**
044: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
045: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
046: * @version $Revision: 59534 $
047: */
048: public class ModelMBeanTestCase extends TestCase {
049: public ModelMBeanTestCase(String s) {
050: super (s);
051: }
052:
053: public void testRequiredModelMBeanConstructors() {
054: try {
055: new RequiredModelMBean();
056: } catch (Throwable t) {
057: log.debug("failed", t);
058: fail("Creating Required ModelMBean instance with default constructor failed: "
059: + t.toString());
060: }
061: }
062:
063: public void testRMMSetManagedResource() {
064: try {
065: ModelMBean modelmbean = new RequiredModelMBean();
066: Resource resource = new Resource();
067: modelmbean.setManagedResource(resource, "ObjectReference");
068: } catch (Throwable t) {
069: log.debug("failed", t);
070: fail("Setting resource object with 'ObjectReference' type failed: "
071: + t.toString());
072: }
073: }
074:
075: public void testRMMSetModelMBeanInfo() {
076: try {
077: ModelMBean modelmbean = new RequiredModelMBean();
078: modelmbean.setModelMBeanInfo(getModelMBeanInfo());
079: } catch (Throwable t) {
080: log.debug("failed", t);
081: fail("Unable to set model mbean info for resource object: "
082: + t.toString());
083: }
084:
085: }
086:
087: public void testRMMInvocation() {
088: try {
089: MBeanServer server = MBeanServerFactory.createMBeanServer();
090:
091: Resource resource = new Resource();
092: ModelMBean modelmbean = new RequiredModelMBean();
093: modelmbean.setModelMBeanInfo(getModelMBeanInfo());
094: modelmbean.setManagedResource(resource, "ObjectReference");
095:
096: ObjectName name = new ObjectName("rmm:invocationTest=true");
097: server.registerMBean(modelmbean, name);
098:
099: assertTrue(((Boolean) server.invoke(name, "isActive",
100: new Object[] {}, new String[] {})).booleanValue());
101: } catch (Throwable t) {
102: log.debug("failed", t);
103: fail("RMMInvocation: " + t.toString());
104: }
105: }
106:
107: /**
108: * A resource that implements an MBean interface at the same time.
109: *
110: * Used to test that the fix for JBAS-1704 doesn't cause a problem
111: * when a target resource *with* an mbean interface, too, registers
112: * through a model mbean and exposes methods/attribute that are not
113: * declared on the mbean interface.
114: */
115: public void testRMMResourceImplementsMBean() {
116: try {
117: MBeanServer server = MBeanServerFactory.createMBeanServer();
118:
119: Resource2 resource = new Resource2();
120: ModelMBean modelmbean = new RequiredModelMBean();
121: modelmbean.setModelMBeanInfo(getModelMBeanInfo2());
122: modelmbean.setManagedResource(resource, "ObjectReference");
123:
124: ObjectName name = new ObjectName(
125: "rmm:resourceImplementsMBean=true");
126: server.registerMBean(modelmbean, name);
127:
128: server.setAttribute(name, new Attribute("pojoAttribute",
129: new Integer(111)));
130:
131: assertEquals((Integer) server.getAttribute(name,
132: "pojoAttribute"), new Integer(111));
133: assertTrue(((Boolean) server.invoke(name, "pojoOperation",
134: new Object[] {}, new String[] {})).booleanValue());
135: } catch (Throwable t) {
136: log.debug("failed", t);
137: fail("testRMMResourceImplementsMBean: " + t.toString());
138: }
139: }
140:
141: private ModelMBeanInfo getModelMBeanInfo() {
142: final boolean READABLE = true;
143: final boolean WRITABLE = true;
144: final boolean BOOLEAN = true;
145:
146: // build 'RoomName' read-write attribute
147: Descriptor descr1 = new DescriptorSupport();
148: descr1.setField("name", "Room");
149: descr1.setField("descriptorType", "attribute");
150: descr1.setField("displayName", "Room Number");
151: descr1.setField("default", "D325");
152:
153: ModelMBeanAttributeInfo roomNameInfo = new ModelMBeanAttributeInfo(
154: "Room", // attribute name
155: String.class.getName(), // attribute type
156: "Room name or number.", // description
157: READABLE, WRITABLE, !BOOLEAN, // read write
158: descr1 // descriptor
159: );
160:
161: // build 'Active' read-only attribute
162: Descriptor descr2 = new DescriptorSupport();
163: descr2.setField("name", "Active");
164: descr2.setField("descriptorType", "attribute");
165: descr2.setField("getMethod", "isActive");
166: descr2.setField("currencyTimeLimit", "10");
167:
168: ModelMBeanAttributeInfo activeInfo = new ModelMBeanAttributeInfo(
169: "Active", boolean.class.getName(), "Printer state.",
170: READABLE, !WRITABLE, !BOOLEAN, descr2);
171:
172: // build 'isActive' getter operation
173: Descriptor descr3 = new DescriptorSupport();
174: descr3.setField("name", "isActive");
175: descr3.setField("descriptorType", "operation");
176: descr3.setField("role", "getter");
177:
178: ModelMBeanOperationInfo isActiveInfo = new ModelMBeanOperationInfo(
179: "isActive", // name & description
180: "Checks if the printer is currently active.", null, // signature
181: boolean.class.getName(), // return type
182: MBeanOperationInfo.INFO, // impact
183: descr3 // descriptor
184: );
185:
186: // MBean descriptor
187: Descriptor descr4 = new DescriptorSupport();
188: descr4.setField("name", RequiredModelMBean.class.getName());
189: descr4.setField("descriptorType", "MBean");
190:
191: // create ModelMBeanInfo
192: ModelMBeanInfo info = new ModelMBeanInfoSupport(
193: RequiredModelMBean.class.getName(), // class name
194: "Printer", // description
195: new ModelMBeanAttributeInfo[] { // attributes
196: roomNameInfo, activeInfo }, null, // constructors
197: new ModelMBeanOperationInfo[] { // operations
198: isActiveInfo }, null, // notifications
199: descr4 // descriptor
200: );
201:
202: return info;
203: }
204:
205: private ModelMBeanInfo getModelMBeanInfo2() {
206: final boolean READABLE = true;
207: final boolean WRITABLE = true;
208: final boolean BOOLEAN = true;
209:
210: // build 'pojoAttribute' read-write attribute
211: Descriptor descr2 = new DescriptorSupport();
212: descr2.setField("name", "pojoAttribute");
213: descr2.setField("descriptorType", "attribute");
214: descr2.setField("getMethod", "getpojoAttribute");
215: descr2.setField("setMethod", "setpojoAttribute");
216:
217: ModelMBeanAttributeInfo pojoAttributeInfo = new ModelMBeanAttributeInfo(
218: "pojoAttribute", int.class.getName(),
219: "A simple integer attribute.", READABLE, WRITABLE,
220: !BOOLEAN, descr2);
221:
222: // JBAS-3614 - the jdk implementation of RequiredModelMBean
223: // requires the attribute to be declared as operation(s), too.
224: // This is not a requirement of the JMX 1.2 spec, though.
225:
226: // build 'getpojoAttribute' operation
227: Descriptor descr21 = new DescriptorSupport();
228: descr21.setField("name", "getpojoAttribute");
229: descr21.setField("descriptorType", "operation");
230:
231: ModelMBeanOperationInfo getpojoAttributeOperation = new ModelMBeanOperationInfo(
232: "getpojoAttribute", // name
233: "A simple operation.", // description
234: null, // signature
235: int.class.getName(), // return type
236: MBeanOperationInfo.INFO, // impact
237: descr21 // descriptor
238: );
239:
240: // build 'setpojoAttribute' operation
241: Descriptor descr22 = new DescriptorSupport();
242: descr22.setField("name", "setpojoAttribute");
243: descr22.setField("descriptorType", "operation");
244:
245: ModelMBeanOperationInfo setpojoAttributeOperation = new ModelMBeanOperationInfo(
246: "setpojoAttribute", // name
247: "A simple operation.", // description
248: new MBeanParameterInfo[] // signature
249: { new MBeanParameterInfo("int", int.class.getName(),
250: "int setter") }, void.class.getName(), // return type
251: MBeanOperationInfo.ACTION, // impact
252: descr22 // descriptor
253: );
254:
255: // build 'pojoOperation' operation
256: Descriptor descr3 = new DescriptorSupport();
257: descr3.setField("name", "pojoOperation");
258: descr3.setField("descriptorType", "operation");
259:
260: ModelMBeanOperationInfo pojoOperationInfo = new ModelMBeanOperationInfo(
261: "pojoOperation", // name & description
262: "A simple operation.", null, // signature
263: boolean.class.getName(), // return type
264: MBeanOperationInfo.ACTION, // impact
265: descr3 // descriptor
266: );
267:
268: // MBean descriptor
269: Descriptor descr4 = new DescriptorSupport();
270: descr4.setField("name", RequiredModelMBean.class.getName());
271: descr4.setField("descriptorType", "MBean");
272:
273: // create ModelMBeanInfo
274: ModelMBeanInfo info = new ModelMBeanInfoSupport(
275: RequiredModelMBean.class.getName(), // class name
276: "POJO", // description
277: new ModelMBeanAttributeInfo[] { // attributes
278: pojoAttributeInfo }, null, // constructors
279: new ModelMBeanOperationInfo[] { // operations
280: getpojoAttributeOperation, setpojoAttributeOperation,
281: pojoOperationInfo }, null, // notifications
282: descr4 // descriptor
283: );
284:
285: return info;
286: }
287: }
|