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.standard;
023:
024: import org.jboss.test.jbossmx.compliance.TestCase;
025: import org.jboss.test.jbossmx.compliance.standard.support.Trivial;
026:
027: import javax.management.InstanceAlreadyExistsException;
028: import javax.management.InstanceNotFoundException;
029: import javax.management.IntrospectionException;
030: import javax.management.MBeanAttributeInfo;
031: import javax.management.MBeanConstructorInfo;
032: import javax.management.MBeanInfo;
033: import javax.management.MBeanNotificationInfo;
034: import javax.management.MBeanOperationInfo;
035: import javax.management.MBeanParameterInfo;
036: import javax.management.MBeanRegistrationException;
037: import javax.management.MBeanServer;
038: import javax.management.MBeanServerFactory;
039: import javax.management.MalformedObjectNameException;
040: import javax.management.NotCompliantMBeanException;
041: import javax.management.ObjectInstance;
042: import javax.management.ObjectName;
043: import javax.management.ReflectionException;
044:
045: /**
046: * @author <a href="mailto:trevor@protocool.com">Trevor Squires</a>.
047: */
048:
049: public class TrivialTestCase extends TestCase {
050: public TrivialTestCase(String s) {
051: super (s);
052: }
053:
054: public void testRegistration() {
055: MBeanServer server = MBeanServerFactory.newMBeanServer();
056: Trivial trivial = new Trivial();
057:
058: ObjectName name = null;
059: try {
060: name = new ObjectName("trivial:key=val");
061: ObjectInstance instance = server.registerMBean(trivial,
062: name);
063: } catch (Exception e) {
064: fail("registration failed: " + e.getMessage());
065: }
066: assertTrue("expected server to report it as registered", server
067: .isRegistered(name));
068: }
069:
070: public void testConstructorInfo() {
071: MBeanInfo info = getTrivialInfo();
072:
073: MBeanConstructorInfo[] constructors = info.getConstructors();
074: assertEquals("constructor list length", 1, constructors.length);
075:
076: // I really don't feel like reflecting to get the name of the constructor,
077: // it should just be the name of the class right?
078: assertEquals("constructor name", Trivial.class.getName(),
079: constructors[0].getName());
080:
081: MBeanParameterInfo[] params = constructors[0].getSignature();
082: assertEquals("constructor signature length", 0, params.length);
083: }
084:
085: public void testAttributeInfo() {
086: MBeanInfo info = getTrivialInfo();
087:
088: MBeanAttributeInfo[] attributes = info.getAttributes();
089: assertEquals("attribute list length", 1, attributes.length);
090: assertEquals("attribute name", "Something", attributes[0]
091: .getName());
092: assertEquals("attribute type", String.class.getName(),
093: attributes[0].getType());
094: assertEquals("attribute readable", true, attributes[0]
095: .isReadable());
096: assertEquals("attribute writable", true, attributes[0]
097: .isWritable());
098: assertEquals("attribute isIs", false, attributes[0].isIs());
099: }
100:
101: public void testOperationInfo() {
102: MBeanInfo info = getTrivialInfo();
103:
104: MBeanOperationInfo[] operations = info.getOperations();
105: assertEquals("operations list length", 1, operations.length);
106: assertEquals("operation name", "doOperation", operations[0]
107: .getName());
108: assertEquals("operation return type", Void.TYPE.getName(),
109: operations[0].getReturnType());
110: assertEquals("operation impact", MBeanOperationInfo.UNKNOWN,
111: operations[0].getImpact());
112:
113: MBeanParameterInfo[] params = operations[0].getSignature();
114: assertEquals("signature length", 1, params.length);
115: assertEquals("parameter type", String.class.getName(),
116: params[0].getType());
117: }
118:
119: public void testNotificationInfo() {
120: MBeanInfo info = getTrivialInfo();
121:
122: MBeanNotificationInfo[] notifications = info.getNotifications();
123: assertEquals("notification list length", 0,
124: notifications.length);
125: }
126:
127: private MBeanInfo getTrivialInfo() {
128: MBeanInfo info = null;
129:
130: try {
131: MBeanServer server = MBeanServerFactory.newMBeanServer();
132: Trivial trivial = new Trivial();
133:
134: ObjectName name = new ObjectName("trivial:key=val");
135: ObjectInstance instance = server.registerMBean(trivial,
136: name);
137: info = server.getMBeanInfo(name);
138: } catch (MalformedObjectNameException e) {
139: fail("got spurious MalformedObjectNameException");
140: } catch (InstanceAlreadyExistsException e) {
141: fail("got spurious InstanceAlreadyExistsException");
142: } catch (MBeanRegistrationException e) {
143: fail("got spurious MBeanRegistrationException");
144: } catch (NotCompliantMBeanException e) {
145: fail("got spurious NotCompliantMBeanException");
146: } catch (InstanceNotFoundException e) {
147: fail("got spurious InstanceNotFoundException");
148: } catch (IntrospectionException e) {
149: fail("got spurious IntrospectionException");
150: } catch (ReflectionException e) {
151: fail("got spurious ReflectionException");
152: }
153:
154: return info;
155: }
156: }
|