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 test.compliance.standard;
023:
024: import javax.management.MBeanInfo;
025: import javax.management.MBeanAttributeInfo;
026: import javax.management.MBeanConstructorInfo;
027: import javax.management.MBeanOperationInfo;
028: import javax.management.MBeanParameterInfo;
029: import javax.management.MBeanServer;
030: import javax.management.MBeanServerFactory;
031: import javax.management.NotCompliantMBeanException;
032: import javax.management.ObjectName;
033: import javax.management.StandardMBean;
034:
035: import junit.framework.TestCase;
036:
037: import test.compliance.standard.support.ArbitraryInterface;
038: import test.compliance.standard.support.MBeanRunnable;
039: import test.compliance.standard.support.MyRunnable;
040: import test.compliance.standard.support.MyStandardMBean;
041: import test.compliance.standard.support.NoConstructorsStandardMBean;
042: import test.compliance.standard.support.Trivial;
043: import test.compliance.standard.support.TrivialMBean;
044:
045: /**
046: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
047: */
048:
049: public class StandardMBeanTEST extends TestCase {
050: public StandardMBeanTEST(String s) {
051: super (s);
052: }
053:
054: public void testOverrideManagementInterface() throws Exception {
055: MBeanServer server = MBeanServerFactory.newMBeanServer();
056: ObjectName name = new ObjectName("test:test=test");
057: server.registerMBean(new MBeanRunnable(), name);
058: server.invoke(name, "run", new Object[0], new String[0]);
059: }
060:
061: public void testSpecifyManagementInterface() throws Exception {
062: MBeanServer server = MBeanServerFactory.newMBeanServer();
063: ObjectName name = new ObjectName("test:test=test");
064: server.registerMBean(new StandardMBean(new MyRunnable(),
065: Runnable.class), name);
066: server.invoke(name, "run", new Object[0], new String[0]);
067: }
068:
069: public void testDontSpecifyManagementInterface() throws Exception {
070: MBeanServer server = MBeanServerFactory.newMBeanServer();
071: ObjectName name = new ObjectName("test:test=test");
072: server.registerMBean(new StandardMBean(new Trivial(), null),
073: name);
074: server.invoke(name, "doOperation", new Object[] { "arg" },
075: new String[] { String.class.getName() });
076: }
077:
078: public void testGetImplementationImplied() throws Exception {
079: StandardMBean std = new MBeanRunnable();
080: assertTrue("MBeanRunnable is its own implementation",
081: std == std.getImplementation());
082: assertTrue("MBeanRunnable is its own implementation class", std
083: .getClass() == std.getImplementationClass());
084: }
085:
086: public void testGetImplementationSpecified() throws Exception {
087: Object obj = new MyRunnable();
088: StandardMBean std = new StandardMBean(obj, Runnable.class);
089: assertTrue("MyRunnable is the implementation", obj == std
090: .getImplementation());
091: assertTrue("MyRunnable is the implementation class", obj
092: .getClass() == std.getImplementationClass());
093: }
094:
095: public void testMBeanInterfaceImplied() throws Exception {
096: StandardMBean std = new MBeanRunnable();
097: assertTrue(
098: "MBeanRunnable has Runnable as a management interface",
099: Runnable.class == std.getMBeanInterface());
100: }
101:
102: public void testMBeanInterfaceSpecified() throws Exception {
103: Object obj = new MyRunnable();
104: StandardMBean std = new StandardMBean(obj, Runnable.class);
105: assertTrue("MyRunnable has Runnable as a management interface",
106: Runnable.class == std.getMBeanInterface());
107: }
108:
109: public void testMBeanInterfaceOldStyle() throws Exception {
110: Object obj = new Trivial();
111: StandardMBean std = new StandardMBean(obj, null);
112: assertTrue(
113: "Trivial has TrivialMBean as a management interface",
114: TrivialMBean.class == std.getMBeanInterface());
115: }
116:
117: public void testMetaData() throws Exception {
118: StandardMBean std = new MyStandardMBean();
119: MBeanInfo info = std.getMBeanInfo();
120: assertEquals(MyStandardMBean.MBEAN_CLASSNAME, info
121: .getClassName());
122: assertEquals(MyStandardMBean.MBEAN_DESCRIPTION, info
123: .getDescription());
124:
125: MBeanAttributeInfo[] attributes = info.getAttributes();
126: assertEquals(attributes.length, 1);
127: assertEquals(MyStandardMBean.MBEAN_ATTRIBUTE_DESCRIPTION
128: + "AnAttribute", attributes[0].getDescription());
129:
130: MBeanConstructorInfo[] constructors = info.getConstructors();
131: assertEquals(constructors.length, 2);
132: for (int i = 0; i < 2; i++) {
133: if (constructors[i].getSignature().length == 0)
134: assertEquals(
135: MyStandardMBean.MBEAN_CONSTRUCTOR_DESCRIPTION
136: + "0", constructors[i].getDescription());
137: else {
138: assertEquals(
139: MyStandardMBean.MBEAN_CONSTRUCTOR_DESCRIPTION
140: + "2", constructors[i].getDescription());
141: MBeanParameterInfo[] params = constructors[i]
142: .getSignature();
143: assertEquals(params.length, 2);
144: assertEquals(MyStandardMBean.MBEAN_PARAMETER + "0",
145: params[0].getName());
146: assertEquals(MyStandardMBean.MBEAN_PARAMETER + "1",
147: params[1].getName());
148: assertEquals(
149: MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION
150: + "0", params[0].getDescription());
151: assertEquals(
152: MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION
153: + "1", params[1].getDescription());
154: }
155: }
156:
157: MBeanOperationInfo[] operations = info.getOperations();
158: assertEquals(operations.length, 1);
159: assertEquals(MyStandardMBean.MBEAN_OPERATION_DESCRIPTION
160: + "anOperation", operations[0].getDescription());
161: MBeanParameterInfo[] params = operations[0].getSignature();
162: assertEquals(params.length, 2);
163: assertEquals(MyStandardMBean.MBEAN_PARAMETER + "anOperation0",
164: params[0].getName());
165: assertEquals(MyStandardMBean.MBEAN_PARAMETER + "anOperation1",
166: params[1].getName());
167: assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION
168: + "anOperation0", params[0].getDescription());
169: assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION
170: + "anOperation1", params[1].getDescription());
171: assertEquals(MBeanOperationInfo.ACTION, operations[0]
172: .getImpact());
173: }
174:
175: public void testNoConstructorsMetaData() throws Exception {
176: StandardMBean std = new NoConstructorsStandardMBean();
177: MBeanInfo info = std.getMBeanInfo();
178:
179: MBeanConstructorInfo[] constructors = info.getConstructors();
180: assertEquals(constructors.length, 0);
181: }
182:
183: public void testCaching() throws Exception {
184: StandardMBean std = new MyStandardMBean();
185: MBeanInfo info = std.getMBeanInfo();
186: assertTrue("MBeanInfo should be cached", info == std
187: .getMBeanInfo());
188: }
189:
190: public void testErrors() throws Exception {
191: boolean caught = false;
192: try {
193: new StandardMBean(null, Runnable.class);
194: } catch (IllegalArgumentException e) {
195: caught = true;
196: }
197: assertTrue(
198: "Expected IllegalArgumentException for null implementation",
199: caught);
200:
201: caught = false;
202: try {
203: new StandardMBean(new MyRunnable(),
204: ArbitraryInterface.class);
205: } catch (NotCompliantMBeanException e) {
206: caught = true;
207: }
208: assertTrue(
209: "Expected NotCompliantMBeanException for the wrong management interface",
210: caught);
211:
212: caught = false;
213: try {
214: new StandardMBean(new MyRunnable(), null);
215: } catch (NotCompliantMBeanException e) {
216: caught = true;
217: }
218: assertTrue(
219: "Expected NotCompliantMBeanException for null management interface",
220: caught);
221:
222: caught = false;
223: try {
224: MBeanServer server = MBeanServerFactory.newMBeanServer();
225: ObjectName name = new ObjectName("test:test=test");
226: server.registerMBean(new MBeanRunnable(true), name);
227: } catch (NotCompliantMBeanException e) {
228: caught = true;
229: }
230: assertTrue(
231: "Expected NotCompliantMBeanException for wrong management interface",
232: caught);
233:
234: caught = false;
235: try {
236: MBeanServer server = MBeanServerFactory.newMBeanServer();
237: ObjectName name = new ObjectName("test:test=test");
238: server.registerMBean(new MBeanRunnable(0), name);
239: } catch (NotCompliantMBeanException e) {
240: caught = true;
241: }
242: assertTrue(
243: "Expected NotCompliantMBeanException for null management interface",
244: caught);
245: }
246: }
|