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.server;
023:
024: import org.jboss.test.jbossmx.compliance.TestCase;
025:
026: import javax.management.MBeanServer;
027: import javax.management.MBeanServerFactory;
028: import javax.management.ObjectName;
029: import javax.management.Attribute;
030: import javax.management.InstanceNotFoundException;
031: import javax.management.AttributeNotFoundException;
032: import javax.management.MBeanException;
033: import javax.management.RuntimeMBeanException;
034: import javax.management.RuntimeErrorException;
035: import javax.management.InvalidAttributeValueException;
036:
037: import org.jboss.test.jbossmx.compliance.server.support.Test;
038: import org.jboss.test.jbossmx.compliance.server.support.TestMBean;
039: import org.jboss.test.jbossmx.compliance.server.support.MyScreamingException;
040: import org.jboss.test.jbossmx.compliance.server.support.ExceptionOnTheRun;
041: import org.jboss.test.jbossmx.compliance.server.support.BabarError;
042:
043: public class MBeanServerTestCase extends TestCase {
044: public MBeanServerTestCase(String s) {
045: super (s);
046: }
047:
048: public void testInvokeWithNonExistantMBean() {
049: try {
050: MBeanServer server = MBeanServerFactory.createMBeanServer();
051: server.invoke(new ObjectName(":mbean=doesnotexist"),
052: "noMethod", null, null);
053:
054: // should not reach here
055: fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean.");
056: } catch (InstanceNotFoundException e) {
057: // should get here
058: } catch (Throwable t) {
059: log.debug("failed", t);
060: fail("Unexpected error on server.invoke(NonExistantMBean): "
061: + t.toString());
062: }
063: }
064:
065: public void testInvokeWithBusinessException() {
066: try {
067: MBeanServer server = MBeanServerFactory.createMBeanServer();
068: ObjectName name = new ObjectName("test:test=test");
069: server.registerMBean(new Test(), name);
070:
071: server.invoke(name, "operationWithException", null, null);
072:
073: // should not get here
074: fail("MBeanException was not thrown.");
075: } catch (MBeanException e) {
076: // this is expected
077: assertTrue(e.getTargetException() instanceof MyScreamingException);
078: } catch (Throwable t) {
079: fail("Unexpected error: " + t.toString());
080: }
081: }
082:
083: public void testGetAttributeWithNonExistingAttribute() {
084: try {
085: MBeanServer server = MBeanServerFactory.createMBeanServer();
086: Object foo = server.getAttribute(new ObjectName(
087: MBEAN_SERVER_DELEGATE), "Foo");
088:
089: // should not reach here
090: fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
091: } catch (AttributeNotFoundException e) {
092: // Expecting this.
093: } catch (Throwable t) {
094: fail("Unexpected error: " + t.toString());
095: }
096: }
097:
098: public void testGetAttributeWithBusinessException() {
099: try {
100: MBeanServer server = MBeanServerFactory.createMBeanServer();
101: ObjectName name = new ObjectName("test:test=test");
102: server.registerMBean(new Test(), name);
103:
104: Object foo = server.getAttribute(name, "ThisWillScream");
105:
106: // should not reach here
107: fail("Did not throw the screaming exception");
108: } catch (MBeanException e) {
109: // this is expected
110: // FIXME THS - is this a valid test?
111: //assertTrue(e.getMessage().startsWith("Exception thrown by attribute"));
112: assertTrue(e.getTargetException() instanceof MyScreamingException);
113: } catch (Throwable t) {
114: fail("Unexpected error: " + t.toString());
115: }
116: }
117:
118: public void testGetAttributeWithNonExistingMBean() {
119: try {
120: MBeanServer server = MBeanServerFactory.createMBeanServer();
121: ObjectName name = new ObjectName("test:name=DoesNotExist");
122:
123: server.getAttribute(name, "Whatever");
124:
125: // should not reach here
126: fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
127: } catch (InstanceNotFoundException e) {
128: // this is expected
129: } catch (Throwable t) {
130: fail("Unexpected error: " + t.toString());
131: }
132: }
133:
134: public void testGetAttributeWithUncheckedException() {
135: try {
136: MBeanServer server = MBeanServerFactory.createMBeanServer();
137: ObjectName name = new ObjectName("test:test=test");
138: server.registerMBean(new Test(), name);
139:
140: server.getAttribute(name, "ThrowUncheckedException");
141:
142: // should not reach here
143: fail("RuntimeMBeanException was not thrown");
144: } catch (RuntimeMBeanException e) {
145: // this is expected
146: assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
147: } catch (Throwable t) {
148: fail("Unexpected err0r: " + t.toString());
149: }
150: }
151:
152: public void testGetAttributeWithError() {
153: try {
154: MBeanServer server = MBeanServerFactory.createMBeanServer();
155: ObjectName name = new ObjectName("test:test=test");
156: server.registerMBean(new Test(), name);
157:
158: server.getAttribute(name, "Error");
159:
160: // should not reach here
161: fail("Error was not thrown");
162: } catch (RuntimeErrorException e) {
163: // this is expected
164: assertTrue(e.getTargetError() instanceof BabarError);
165: } catch (Throwable t) {
166: fail("Unexpected error: " + t.toString());
167: }
168: }
169:
170: public void testSetAttributeWithNonExistingAttribute() {
171: try {
172: MBeanServer server = MBeanServerFactory.createMBeanServer();
173: server.setAttribute(new ObjectName(MBEAN_SERVER_DELEGATE),
174: new Attribute("Foo", "value"));
175:
176: // should not reach here
177: fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
178: } catch (AttributeNotFoundException e) {
179: // Expecting this.
180: } catch (Throwable t) {
181: fail("Unexpected error: " + t.toString());
182: }
183: }
184:
185: public void testSetAttributeWithBusinessException() {
186: try {
187: MBeanServer server = MBeanServerFactory.createMBeanServer();
188: ObjectName name = new ObjectName("test:test=test");
189: server.registerMBean(new Test(), name);
190:
191: server.setAttribute(name, new Attribute("ThisWillScream",
192: "value"));
193:
194: // should not reach here
195: fail("Did not throw the screaming exception");
196: } catch (MBeanException e) {
197: // this is expected
198: // FIXME THS - commented the assertion below: is that really what's required?
199: // assertTrue(e.getMessage().startsWith("Exception thrown by attribute"));
200: assertTrue(e.getTargetException() instanceof MyScreamingException);
201: } catch (Throwable t) {
202: fail("Unexpected error: " + t.toString());
203: }
204: }
205:
206: public void testSetAttributeWithNonExistingMBean() {
207: try {
208: MBeanServer server = MBeanServerFactory.createMBeanServer();
209: ObjectName name = new ObjectName("test:name=DoesNotExist");
210:
211: server.setAttribute(name, new Attribute("Whatever",
212: "nothing"));
213:
214: // should not reach here
215: fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
216: } catch (InstanceNotFoundException e) {
217: // this is expected
218: } catch (Throwable t) {
219: fail("Unexpected error: " + t.toString());
220: }
221: }
222:
223: public void testSetAttributeWithUncheckedException() {
224: try {
225: MBeanServer server = MBeanServerFactory.createMBeanServer();
226: ObjectName name = new ObjectName("test:test=test");
227: server.registerMBean(new Test(), name);
228:
229: server.setAttribute(name, new Attribute(
230: "ThrowUncheckedException", "value"));
231:
232: // should not reach here
233: fail("RuntimeMBeanException was not thrown");
234: } catch (RuntimeMBeanException e) {
235: // this is expected
236: assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
237: } catch (Throwable t) {
238: fail("Unexpected err0r: " + t.toString());
239: }
240: }
241:
242: public void testSetAttributeWithError() {
243: try {
244: MBeanServer server = MBeanServerFactory.createMBeanServer();
245: ObjectName name = new ObjectName("test:test=test");
246: server.registerMBean(new Test(), name);
247:
248: server.setAttribute(name, new Attribute("Error", "value"));
249:
250: // should not reach here
251: fail("Error was not thrown");
252: } catch (RuntimeErrorException e) {
253: // this is expected
254: assertTrue(e.getTargetError() instanceof BabarError);
255: } catch (Throwable t) {
256: fail("Unexpected error: " + t.toString());
257: }
258: }
259:
260: }
|