KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > compliance > server > MBeanServerTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmx.compliance.server;
23
24 import org.jboss.test.jbossmx.compliance.TestCase;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.Attribute JavaDoc;
30 import javax.management.InstanceNotFoundException JavaDoc;
31 import javax.management.AttributeNotFoundException JavaDoc;
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.RuntimeMBeanException JavaDoc;
34 import javax.management.RuntimeErrorException JavaDoc;
35 import javax.management.InvalidAttributeValueException JavaDoc;
36
37 import org.jboss.test.jbossmx.compliance.server.support.Test;
38 import org.jboss.test.jbossmx.compliance.server.support.TestMBean;
39 import org.jboss.test.jbossmx.compliance.server.support.MyScreamingException;
40 import org.jboss.test.jbossmx.compliance.server.support.ExceptionOnTheRun;
41 import org.jboss.test.jbossmx.compliance.server.support.BabarError;
42
43
44 public class MBeanServerTestCase
45    extends TestCase
46 {
47    public MBeanServerTestCase(String JavaDoc s)
48    {
49       super(s);
50    }
51
52    public void testInvokeWithNonExistantMBean()
53    {
54       try
55       {
56          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
57          server.invoke(new ObjectName JavaDoc(":mbean=doesnotexist"), "noMethod", null, null);
58          
59          // should not reach here
60
fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean.");
61       }
62       catch (InstanceNotFoundException JavaDoc e)
63       {
64          // should get here
65
}
66       catch (Throwable JavaDoc t)
67       {
68          log.debug("failed", t);
69          fail("Unexpected error on server.invoke(NonExistantMBean): " + t.toString());
70       }
71    }
72    
73    public void testInvokeWithBusinessException()
74    {
75       try
76       {
77          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
78          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
79          server.registerMBean(new Test(), name);
80          
81          server.invoke(name, "operationWithException", null, null);
82          
83          // should not get here
84
fail("MBeanException was not thrown.");
85       }
86       catch (MBeanException JavaDoc e)
87       {
88          // this is expected
89
assertTrue(e.getTargetException() instanceof MyScreamingException);
90       }
91       catch (Throwable JavaDoc t)
92       {
93          fail("Unexpected error: " + t.toString());
94       }
95    }
96    
97    
98    public void testGetAttributeWithNonExistingAttribute()
99    {
100       try
101       {
102          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
103          Object JavaDoc foo = server.getAttribute(new ObjectName JavaDoc(MBEAN_SERVER_DELEGATE), "Foo");
104          
105          // should not reach here
106
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
107       }
108       catch (AttributeNotFoundException JavaDoc e)
109       {
110          // Expecting this.
111
}
112       catch (Throwable JavaDoc t)
113       {
114          fail("Unexpected error: " + t.toString());
115       }
116    }
117    
118    public void testGetAttributeWithBusinessException()
119    {
120       try
121       {
122          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
123          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
124          server.registerMBean(new Test(), name);
125          
126          Object JavaDoc foo = server.getAttribute(name, "ThisWillScream");
127          
128          // should not reach here
129
fail("Did not throw the screaming exception");
130       }
131       catch (MBeanException JavaDoc e)
132       {
133          // this is expected
134
// FIXME THS - is this a valid test?
135
//assertTrue(e.getMessage().startsWith("Exception thrown by attribute"));
136
assertTrue(e.getTargetException() instanceof MyScreamingException);
137       }
138       catch (Throwable JavaDoc t)
139       {
140          fail("Unexpected error: " + t.toString());
141       }
142    }
143    
144    public void testGetAttributeWithNonExistingMBean()
145    {
146       try
147       {
148          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
149          ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=DoesNotExist");
150          
151          server.getAttribute(name, "Whatever");
152          
153          // should not reach here
154
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
155       }
156       catch (InstanceNotFoundException JavaDoc e)
157       {
158          // this is expected
159
}
160       catch (Throwable JavaDoc t)
161       {
162          fail("Unexpected error: " + t.toString());
163       }
164    }
165    
166    public void testGetAttributeWithUncheckedException()
167    {
168       try
169       {
170          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
171          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
172          server.registerMBean(new Test(), name);
173          
174          server.getAttribute(name, "ThrowUncheckedException");
175          
176          // should not reach here
177
fail("RuntimeMBeanException was not thrown");
178       }
179       catch (RuntimeMBeanException JavaDoc e)
180       {
181          // this is expected
182
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
183       }
184       catch (Throwable JavaDoc t)
185       {
186          fail("Unexpected err0r: " + t.toString());
187       }
188    }
189    
190    public void testGetAttributeWithError()
191    {
192       try
193       {
194          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
195          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
196          server.registerMBean(new Test(), name);
197          
198          server.getAttribute(name, "Error");
199          
200          // should not reach here
201
fail("Error was not thrown");
202       }
203       catch (RuntimeErrorException JavaDoc e)
204       {
205          // this is expected
206
assertTrue(e.getTargetError() instanceof BabarError);
207       }
208       catch (Throwable JavaDoc t)
209       {
210          fail("Unexpected error: " + t.toString());
211       }
212    }
213    
214    public void testSetAttributeWithNonExistingAttribute()
215    {
216       try
217       {
218          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
219          server.setAttribute(new ObjectName JavaDoc(MBEAN_SERVER_DELEGATE), new Attribute JavaDoc("Foo", "value"));
220          
221          // should not reach here
222
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
223       }
224       catch (AttributeNotFoundException JavaDoc e)
225       {
226          // Expecting this.
227
}
228       catch (Throwable JavaDoc t)
229       {
230          fail("Unexpected error: " + t.toString());
231       }
232    }
233    
234    public void testSetAttributeWithBusinessException()
235    {
236       try
237       {
238          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
239          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
240          server.registerMBean(new Test(), name);
241          
242          server.setAttribute(name, new Attribute JavaDoc("ThisWillScream", "value"));
243          
244          // should not reach here
245
fail("Did not throw the screaming exception");
246       }
247       catch (MBeanException JavaDoc e)
248       {
249          // this is expected
250
// FIXME THS - commented the assertion below: is that really what's required?
251
// assertTrue(e.getMessage().startsWith("Exception thrown by attribute"));
252
assertTrue(e.getTargetException() instanceof MyScreamingException);
253       }
254       catch (Throwable JavaDoc t)
255       {
256          fail("Unexpected error: " + t.toString());
257       }
258    }
259    
260    public void testSetAttributeWithNonExistingMBean()
261    {
262       try
263       {
264          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
265          ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=DoesNotExist");
266          
267          server.setAttribute(name, new Attribute JavaDoc("Whatever", "nothing"));
268          
269          // should not reach here
270
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
271       }
272       catch (InstanceNotFoundException JavaDoc e)
273       {
274          // this is expected
275
}
276       catch (Throwable JavaDoc t)
277       {
278          fail("Unexpected error: " + t.toString());
279       }
280    }
281    
282    public void testSetAttributeWithUncheckedException()
283    {
284       try
285       {
286          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
287          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
288          server.registerMBean(new Test(), name);
289          
290          server.setAttribute(name, new Attribute JavaDoc("ThrowUncheckedException", "value"));
291          
292          // should not reach here
293
fail("RuntimeMBeanException was not thrown");
294       }
295       catch (RuntimeMBeanException JavaDoc e)
296       {
297          // this is expected
298
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
299       }
300       catch (Throwable JavaDoc t)
301       {
302          fail("Unexpected err0r: " + t.toString());
303       }
304    }
305    
306    public void testSetAttributeWithError()
307    {
308       try
309       {
310          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
311          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
312          server.registerMBean(new Test(), name);
313          
314          server.setAttribute(name, new Attribute JavaDoc("Error", "value"));
315          
316          // should not reach here
317
fail("Error was not thrown");
318       }
319       catch (RuntimeErrorException JavaDoc e)
320       {
321          // this is expected
322
assertTrue(e.getTargetError() instanceof BabarError);
323       }
324       catch (Throwable JavaDoc t)
325       {
326          fail("Unexpected error: " + t.toString());
327       }
328    }
329    
330 }
331
Popular Tags