KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > standard > StandardMBeanTEST


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package test.compliance.standard;
9
10 import javax.management.MBeanInfo;
11 import javax.management.MBeanAttributeInfo;
12 import javax.management.MBeanConstructorInfo;
13 import javax.management.MBeanOperationInfo;
14 import javax.management.MBeanParameterInfo;
15 import javax.management.MBeanServer;
16 import javax.management.MBeanServerFactory;
17 import javax.management.NotCompliantMBeanException;
18 import javax.management.ObjectName;
19 import javax.management.StandardMBean;
20
21 import junit.framework.TestCase;
22
23 import test.compliance.standard.support.ArbitraryInterface;
24 import test.compliance.standard.support.MBeanRunnable;
25 import test.compliance.standard.support.MyRunnable;
26 import test.compliance.standard.support.MyStandardMBean;
27 import test.compliance.standard.support.NoConstructorsStandardMBean;
28 import test.compliance.standard.support.Trivial;
29 import test.compliance.standard.support.TrivialMBean;
30
31 /**
32  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
33  */

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