KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > standard > StandardMBeanTEST


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.jmx.compliance.standard;
23
24 import javax.management.MBeanAttributeInfo JavaDoc;
25 import javax.management.MBeanConstructorInfo JavaDoc;
26 import javax.management.MBeanInfo JavaDoc;
27 import javax.management.MBeanOperationInfo JavaDoc;
28 import javax.management.MBeanParameterInfo JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanServerFactory JavaDoc;
31 import javax.management.NotCompliantMBeanException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.StandardMBean JavaDoc;
34
35 import junit.framework.TestCase;
36
37 import org.jboss.test.jmx.compliance.standard.support.ArbitraryInterface;
38 import org.jboss.test.jmx.compliance.standard.support.MBeanRunnable;
39 import org.jboss.test.jmx.compliance.standard.support.MyRunnable;
40 import org.jboss.test.jmx.compliance.standard.support.MyStandardMBean;
41 import org.jboss.test.jmx.compliance.standard.support.NoConstructorsStandardMBean;
42 import org.jboss.test.jmx.compliance.standard.support.Trivial;
43 import org.jboss.test.jmx.compliance.standard.support.TrivialMBean;
44
45
46 /**
47  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
48  */

49
50 public class StandardMBeanTEST
51    extends TestCase
52 {
53    public StandardMBeanTEST(String JavaDoc s)
54    {
55       super(s);
56    }
57
58    public void testOverrideManagementInterface()
59       throws Exception JavaDoc
60    {
61       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
62       ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
63       server.registerMBean(new MBeanRunnable(), name);
64       server.invoke(name, "run", new Object JavaDoc[0], new String JavaDoc[0]);
65    }
66
67    public void testSpecifyManagementInterface()
68       throws Exception JavaDoc
69    {
70       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
71       ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
72       server.registerMBean(new StandardMBean JavaDoc(new MyRunnable(), Runnable JavaDoc.class), name);
73       server.invoke(name, "run", new Object JavaDoc[0], new String JavaDoc[0]);
74    }
75
76    public void testDontSpecifyManagementInterface()
77       throws Exception JavaDoc
78    {
79       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
80       ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
81       server.registerMBean(new StandardMBean JavaDoc(new Trivial(), null), name);
82       server.invoke(name, "doOperation", new Object JavaDoc[] { "arg" }, new String JavaDoc[] { String JavaDoc.class.getName() });
83    }
84
85    public void testGetImplementationImplied()
86       throws Exception JavaDoc
87    {
88       StandardMBean JavaDoc std = new MBeanRunnable();
89       assertTrue("MBeanRunnable is its own implementation", std == std.getImplementation());
90       assertTrue("MBeanRunnable is its own implementation class", std.getClass() == std.getImplementationClass());
91    }
92
93    public void testGetImplementationSpecified()
94       throws Exception JavaDoc
95    {
96       Object JavaDoc obj = new MyRunnable();
97       StandardMBean JavaDoc std = new StandardMBean JavaDoc(obj, Runnable JavaDoc.class);
98       assertTrue("MyRunnable is the implementation", obj == std.getImplementation());
99       assertTrue("MyRunnable is the implementation class", obj.getClass() == std.getImplementationClass());
100    }
101
102    public void testMBeanInterfaceImplied()
103       throws Exception JavaDoc
104    {
105       StandardMBean JavaDoc std = new MBeanRunnable();
106       assertTrue("MBeanRunnable has Runnable as a management interface", Runnable JavaDoc.class == std.getMBeanInterface());
107    }
108
109    public void testMBeanInterfaceSpecified()
110       throws Exception JavaDoc
111    {
112       Object JavaDoc obj = new MyRunnable();
113       StandardMBean JavaDoc std = new StandardMBean JavaDoc(obj, Runnable JavaDoc.class);
114       assertTrue("MyRunnable has Runnable as a management interface", Runnable JavaDoc.class == std.getMBeanInterface());
115    }
116
117    public void testMBeanInterfaceOldStyle()
118       throws Exception JavaDoc
119    {
120       Object JavaDoc obj = new Trivial();
121       StandardMBean JavaDoc std = new StandardMBean JavaDoc(obj, null);
122       assertTrue("Trivial has TrivialMBean as a management interface", TrivialMBean.class == std.getMBeanInterface());
123    }
124
125    public void testMetaData()
126       throws Exception JavaDoc
127    {
128       StandardMBean JavaDoc std = new MyStandardMBean();
129       MBeanInfo JavaDoc info = std.getMBeanInfo();
130       assertEquals(MyStandardMBean.MBEAN_CLASSNAME, info.getClassName());
131       assertEquals(MyStandardMBean.MBEAN_DESCRIPTION, info.getDescription());
132
133       MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
134       assertEquals(attributes.length, 1);
135       assertEquals(MyStandardMBean.MBEAN_ATTRIBUTE_DESCRIPTION + "AnAttribute", attributes[0].getDescription());
136
137       MBeanConstructorInfo JavaDoc[] constructors = info.getConstructors();
138       assertEquals(constructors.length, 2);
139       for (int i = 0; i < 2; i++)
140       {
141          if (constructors[i].getSignature().length == 0)
142             assertEquals(MyStandardMBean.MBEAN_CONSTRUCTOR_DESCRIPTION + "0", constructors[i].getDescription());
143          else
144          {
145             assertEquals(MyStandardMBean.MBEAN_CONSTRUCTOR_DESCRIPTION + "2", constructors[i].getDescription());
146             MBeanParameterInfo JavaDoc[] params = constructors[i].getSignature();
147             assertEquals(params.length, 2);
148             assertEquals(MyStandardMBean.MBEAN_PARAMETER + "0", params[0].getName());
149             assertEquals(MyStandardMBean.MBEAN_PARAMETER + "1", params[1].getName());
150             assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "0", params[0].getDescription());
151             assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "1", params[1].getDescription());
152          }
153       }
154
155       MBeanOperationInfo JavaDoc[] operations = info.getOperations();
156       assertEquals(operations.length, 1);
157       assertEquals(MyStandardMBean.MBEAN_OPERATION_DESCRIPTION + "anOperation", operations[0].getDescription());
158       MBeanParameterInfo JavaDoc[] params = operations[0].getSignature();
159       assertEquals(params.length, 2);
160       assertEquals(MyStandardMBean.MBEAN_PARAMETER + "anOperation0", params[0].getName());
161       assertEquals(MyStandardMBean.MBEAN_PARAMETER + "anOperation1", params[1].getName());
162       assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "anOperation0", params[0].getDescription());
163       assertEquals(MyStandardMBean.MBEAN_PARAMETER_DESCRIPTION + "anOperation1", params[1].getDescription());
164       assertEquals(MBeanOperationInfo.ACTION, operations[0].getImpact());
165    }
166
167    public void testNoConstructorsMetaData()
168       throws Exception JavaDoc
169    {
170       StandardMBean JavaDoc std = new NoConstructorsStandardMBean();
171       MBeanInfo JavaDoc info = std.getMBeanInfo();
172
173       MBeanConstructorInfo JavaDoc[] constructors = info.getConstructors();
174       assertEquals(constructors.length, 0);
175    }
176
177    public void testCaching()
178       throws Exception JavaDoc
179    {
180       StandardMBean JavaDoc std = new MyStandardMBean();
181       MBeanInfo JavaDoc info = std.getMBeanInfo();
182       assertTrue("MBeanInfo should be cached", info == std.getMBeanInfo());
183    }
184
185    public void testErrors()
186       throws Exception JavaDoc
187    {
188       boolean caught = false;
189       try
190       {
191          new StandardMBean JavaDoc(null, Runnable JavaDoc.class);
192       }
193       catch (IllegalArgumentException JavaDoc e)
194       {
195          caught = true;
196       }
197       assertTrue("Expected IllegalArgumentException for null implementation", caught);
198
199       caught = false;
200       try
201       {
202          new StandardMBean JavaDoc(new MyRunnable(), ArbitraryInterface.class);
203       }
204       catch (NotCompliantMBeanException JavaDoc e)
205       {
206          caught = true;
207       }
208       assertTrue("Expected NotCompliantMBeanException for the wrong management interface", caught);
209
210       caught = false;
211       try
212       {
213          new StandardMBean JavaDoc(new MyRunnable(), null);
214       }
215       catch (NotCompliantMBeanException JavaDoc e)
216       {
217          caught = true;
218       }
219       assertTrue("Expected NotCompliantMBeanException for null management interface", caught);
220
221       caught = false;
222       try
223       {
224          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
225          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
226          server.registerMBean(new MBeanRunnable(true), name);
227       }
228       catch (NotCompliantMBeanException JavaDoc e)
229       {
230          caught = true;
231       }
232       assertTrue("Expected NotCompliantMBeanException for wrong management interface", caught);
233
234       caught = false;
235       try
236       {
237          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
238          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
239          server.registerMBean(new MBeanRunnable(0), name);
240       }
241       catch (NotCompliantMBeanException JavaDoc e)
242       {
243          caught = true;
244       }
245       assertTrue("Expected NotCompliantMBeanException for null management interface", caught);
246    }
247 }
248
Popular Tags