KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > mx > mxbean > test > MXBeanInvocationHandlerUnitTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.mx.mxbean.test;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.LinkedHashMap JavaDoc;
30 import java.util.LinkedHashSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.openmbean.OpenMBeanAttributeInfo JavaDoc;
39 import javax.management.openmbean.OpenMBeanInfo JavaDoc;
40 import javax.management.openmbean.OpenType JavaDoc;
41 import javax.management.openmbean.SimpleType JavaDoc;
42
43 import junit.framework.Test;
44 import junit.framework.TestSuite;
45
46 import org.jboss.mx.mxbean.MXBeanFactory;
47 import org.jboss.mx.mxbean.MXBeanUtils;
48 import org.jboss.test.mx.mxbean.support.CollectionsInterface;
49 import org.jboss.test.mx.mxbean.support.CollectionsMXBeanSupport;
50 import org.jboss.test.mx.mxbean.support.CollectionsMXBeanSupportMXBean;
51 import org.jboss.test.mx.mxbean.support.CompositeInterface;
52 import org.jboss.test.mx.mxbean.support.CompositeMXBeanSupport;
53 import org.jboss.test.mx.mxbean.support.CompositeMXBeanSupportMXBean;
54 import org.jboss.test.mx.mxbean.support.SimpleInterface;
55 import org.jboss.test.mx.mxbean.support.SimpleMXBeanSupport;
56 import org.jboss.test.mx.mxbean.support.SimpleMXBeanSupportMXBean;
57 import org.jboss.test.mx.mxbean.support.SimpleObject;
58 import org.jboss.test.mx.mxbean.support.TestEnum;
59
60 /**
61  * MXBeanInvocationHandlerUnitTestCase.
62  *
63  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
64  * @version $Revision: 1.1 $
65  */

66 public class MXBeanInvocationHandlerUnitTestCase extends AbstractMXBeanTest
67 {
68    public static Test suite()
69    {
70       return new TestSuite(MXBeanInvocationHandlerUnitTestCase.class);
71    }
72    
73    public MXBeanInvocationHandlerUnitTestCase(String JavaDoc name)
74    {
75       super(name);
76    }
77    
78    public void testGetSimpleAttributes() throws Exception JavaDoc
79    {
80       MBeanServer JavaDoc server = createMBeanServer();
81       SimpleMXBeanSupport support = new SimpleMXBeanSupport();
82       ObjectName JavaDoc objectName = SimpleMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
83       server.registerMBean(support, objectName);
84       SimpleMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, SimpleMXBeanSupportMXBean.class);
85       OpenMBeanInfo JavaDoc info = (OpenMBeanInfo JavaDoc) server.getMBeanInfo(objectName);
86       OpenType JavaDoc[] types = new OpenType JavaDoc[SimpleInterface.TYPES.length];
87       for (int i = 0; i < types.length; ++i)
88          types[i] = MXBeanUtils.getOpenType(SimpleInterface.TYPES[i]);
89       checkAttributes(SimpleMXBeanSupportMXBean.class, proxy, support, SimpleInterface.KEYS, types, SimpleInterface.VALUES, info);
90    }
91    
92    public void testSetSimpleAttributes() throws Exception JavaDoc
93    {
94       MBeanServer JavaDoc server = createMBeanServer();
95       SimpleMXBeanSupport support = new SimpleMXBeanSupport();
96       ObjectName JavaDoc objectName = SimpleMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
97       server.registerMBean(support, objectName);
98       SimpleMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, SimpleMXBeanSupportMXBean.class);
99       OpenMBeanInfo JavaDoc info = (OpenMBeanInfo JavaDoc) server.getMBeanInfo(objectName);
100       OpenType JavaDoc[] types = new OpenType JavaDoc[SimpleInterface.TYPES.length];
101       for (int i = 0; i < types.length; ++i)
102          types[i] = MXBeanUtils.getOpenType(SimpleInterface.TYPES[i]);
103       proxy.setBigDecimal(SimpleInterface.bigDecimalChangedValue);
104       proxy.setBigInteger(SimpleInterface.bigIntegerChangedValue);
105       proxy.setBoolean(SimpleInterface.booleanChangedValue);
106       proxy.setByte(SimpleInterface.byteChangedValue);
107       proxy.setCharacter(SimpleInterface.characterChangedValue);
108       proxy.setDate(SimpleInterface.dateChangedValue);
109       proxy.setDouble(SimpleInterface.doubleChangedValue);
110       proxy.setFloat(SimpleInterface.floatChangedValue);
111       proxy.setInteger(SimpleInterface.integerChangedValue);
112       proxy.setLong(SimpleInterface.longChangedValue);
113       proxy.setObjectName(SimpleInterface.objectNameChangedValue);
114       proxy.setPrimitiveBoolean(SimpleInterface.primitiveBooleanChangedValue);
115       proxy.setPrimitiveByte(SimpleInterface.primitiveByteChangedValue);
116       proxy.setPrimitiveChar(SimpleInterface.primitiveCharChangedValue);
117       proxy.setPrimitiveDouble(SimpleInterface.primitiveDoubleChangedValue);
118       proxy.setPrimitiveFloat(SimpleInterface.primitiveFloatChangedValue);
119       proxy.setPrimitiveInt(SimpleInterface.primitiveIntChangedValue);
120       proxy.setPrimitiveLong(SimpleInterface.primitiveLongChangedValue);
121       proxy.setPrimitiveShort(SimpleInterface.primitiveShortChangedValue);
122       proxy.setShort(SimpleInterface.shortChangedValue);
123       proxy.setString(SimpleInterface.stringChangedValue);
124       checkAttributes(SimpleInterface.class, proxy, support, SimpleInterface.KEYS, types, SimpleInterface.CHANGED_VALUES, info);
125    }
126
127    public void testSimpleInvoke() throws Exception JavaDoc
128    {
129       MBeanServer JavaDoc server = createMBeanServer();
130       SimpleMXBeanSupport support = new SimpleMXBeanSupport();
131       ObjectName JavaDoc objectName = SimpleMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
132       server.registerMBean(support, objectName);
133       SimpleMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, SimpleMXBeanSupportMXBean.class);
134       assertEquals("olleH", proxy.echoReverse("Hello"));
135    }
136    
137    public void testGetCompositeAttributes() throws Exception JavaDoc
138    {
139       MBeanServer JavaDoc server = createMBeanServer();
140       SimpleObject composite = new SimpleObject();
141       CompositeMXBeanSupport support = new CompositeMXBeanSupport("Simple", composite);
142       ObjectName JavaDoc objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
143       server.registerMBean(support, objectName);
144       CompositeMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CompositeMXBeanSupportMXBean.class);
145       OpenMBeanInfo JavaDoc info = (OpenMBeanInfo JavaDoc) server.getMBeanInfo(objectName);
146       Object JavaDoc[] values = { "Simple", composite };
147       checkAttributes(CompositeMXBeanSupportMXBean.class, proxy, support, CompositeInterface.KEYS, CompositeInterface.TYPES, values, info);
148    }
149    
150    public void testSetCompositeAttributes() throws Exception JavaDoc
151    {
152       MBeanServer JavaDoc server = createMBeanServer();
153       SimpleObject composite = new SimpleObject();
154       CompositeMXBeanSupport support = new CompositeMXBeanSupport("Simple", composite);
155       ObjectName JavaDoc objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
156       server.registerMBean(support, objectName);
157       CompositeMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CompositeMXBeanSupportMXBean.class);
158       OpenMBeanInfo JavaDoc info = (OpenMBeanInfo JavaDoc) server.getMBeanInfo(objectName);
159       SimpleObject changed = new SimpleObject();
160       changed.setBigDecimal(SimpleInterface.bigDecimalChangedValue);
161       changed.setBigInteger(SimpleInterface.bigIntegerChangedValue);
162       changed.setBoolean(SimpleInterface.booleanChangedValue);
163       changed.setByte(SimpleInterface.byteChangedValue);
164       changed.setCharacter(SimpleInterface.characterChangedValue);
165       changed.setDate(SimpleInterface.dateChangedValue);
166       changed.setDouble(SimpleInterface.doubleChangedValue);
167       changed.setFloat(SimpleInterface.floatChangedValue);
168       changed.setInteger(SimpleInterface.integerChangedValue);
169       changed.setLong(SimpleInterface.longChangedValue);
170       changed.setObjectName(SimpleInterface.objectNameChangedValue);
171       changed.setPrimitiveBoolean(SimpleInterface.primitiveBooleanChangedValue);
172       changed.setPrimitiveByte(SimpleInterface.primitiveByteChangedValue);
173       changed.setPrimitiveChar(SimpleInterface.primitiveCharChangedValue);
174       changed.setPrimitiveDouble(SimpleInterface.primitiveDoubleChangedValue);
175       changed.setPrimitiveFloat(SimpleInterface.primitiveFloatChangedValue);
176       changed.setPrimitiveInt(SimpleInterface.primitiveIntChangedValue);
177       changed.setPrimitiveLong(SimpleInterface.primitiveLongChangedValue);
178       changed.setPrimitiveShort(SimpleInterface.primitiveShortChangedValue);
179       changed.setShort(SimpleInterface.shortChangedValue);
180       changed.setString(SimpleInterface.stringChangedValue);
181       proxy.setSimple("Changed");
182       proxy.setComposite(changed);
183       Object JavaDoc[] values = new Object JavaDoc[] { "Changed", changed };
184       checkAttributes(CompositeMXBeanSupportMXBean.class, proxy, support, CompositeInterface.KEYS, CompositeInterface.TYPES, values, info);
185    }
186
187    public void testCompositeInvoke() throws Exception JavaDoc
188    {
189       MBeanServer JavaDoc server = createMBeanServer();
190       SimpleObject composite = new SimpleObject();
191       CompositeMXBeanSupport support = new CompositeMXBeanSupport("Simple", composite);
192       ObjectName JavaDoc objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
193       server.registerMBean(support, objectName);
194       CompositeMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CompositeMXBeanSupportMXBean.class);
195       composite = new SimpleObject();
196       composite.setString("hello");
197       SimpleInterface result = proxy.echoReverse(composite);
198       assertEquals("olleh", result.getString());
199    }
200    
201    public void testGetCollectionAttributes() throws Exception JavaDoc
202    {
203       MBeanServer JavaDoc server = createMBeanServer();
204       String JavaDoc[] array = { "array" };
205       Collection JavaDoc<String JavaDoc> collection = new ArrayList JavaDoc<String JavaDoc>();
206       collection.add("collection");
207       Set JavaDoc<String JavaDoc> set = new HashSet JavaDoc<String JavaDoc>();
208       set.add("set");
209       List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
210       list.add("list");
211       Map JavaDoc<String JavaDoc, Integer JavaDoc> map = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
212       map.put("map", 1);
213       TestEnum enumeration = TestEnum.FIRST;
214       CollectionsMXBeanSupport support = new CollectionsMXBeanSupport(array, collection, set, list, map, enumeration);
215       ObjectName JavaDoc objectName = CollectionsMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
216       server.registerMBean(support, objectName);
217       CollectionsMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CollectionsMXBeanSupportMXBean.class);
218       OpenMBeanInfo JavaDoc info = (OpenMBeanInfo JavaDoc) server.getMBeanInfo(objectName);
219       Object JavaDoc[] values = { array, collection, set, list, map, enumeration };
220       checkAttributes(CollectionsMXBeanSupportMXBean.class, proxy, support, CollectionsInterface.KEYS, CollectionsInterface.TYPES, values, info);
221    }
222    
223    public void testSetCollectionAttributes() throws Exception JavaDoc
224    {
225       MBeanServer JavaDoc server = createMBeanServer();
226       String JavaDoc[] array = { "array" };
227       Collection JavaDoc<String JavaDoc> collection = new ArrayList JavaDoc<String JavaDoc>();
228       collection.add("collection");
229       Set JavaDoc<String JavaDoc> set = new LinkedHashSet JavaDoc<String JavaDoc>();
230       set.add("set");
231       List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
232       list.add("list");
233       Map JavaDoc<String JavaDoc, Integer JavaDoc> map = new LinkedHashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
234       map.put("map", 1);
235       TestEnum enumeration = TestEnum.FIRST;
236       CollectionsMXBeanSupport support = new CollectionsMXBeanSupport(array, collection, set, list, map, enumeration);
237       ObjectName JavaDoc objectName = CollectionsMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
238       server.registerMBean(support, objectName);
239       CollectionsMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CollectionsMXBeanSupportMXBean.class);
240       OpenMBeanInfo JavaDoc info = (OpenMBeanInfo JavaDoc) server.getMBeanInfo(objectName);
241
242       String JavaDoc[] changedArray = { "arrayChanged" };
243       Collection JavaDoc<String JavaDoc> changedCollection = new ArrayList JavaDoc<String JavaDoc>();
244       changedCollection.add("collectionChanged");
245       Set JavaDoc<String JavaDoc> changedSet = new LinkedHashSet JavaDoc<String JavaDoc>();
246       changedSet.add("setChanged");
247       List JavaDoc<String JavaDoc> changedList = new ArrayList JavaDoc<String JavaDoc>();
248       changedList.add("listChanged");
249       Map JavaDoc<String JavaDoc, Integer JavaDoc> changedMap = new LinkedHashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
250       changedMap.put("mapChanged", 2);
251       TestEnum changedEnumeration = TestEnum.SECOND;
252       
253       proxy.setArray(changedArray);
254       proxy.setCollection(changedCollection);
255       proxy.setEnum(changedEnumeration);
256       proxy.setList(changedList);
257       proxy.setMap(changedMap);
258       proxy.setSet(changedSet);
259       Object JavaDoc[] changedValues = { changedArray, changedCollection, changedSet, changedList, changedMap, changedEnumeration };
260       checkAttributes(CollectionsMXBeanSupportMXBean.class, proxy, support, CollectionsInterface.KEYS, CollectionsInterface.TYPES, changedValues, info);
261    }
262
263    public void testCollectionInvoke() throws Exception JavaDoc
264    {
265       MBeanServer JavaDoc server = createMBeanServer();
266       CollectionsMXBeanSupport support = new CollectionsMXBeanSupport();
267       ObjectName JavaDoc objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME;
268       server.registerMBean(support, objectName);
269       CollectionsMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CollectionsMXBeanSupportMXBean.class);
270       List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
271       list.add("one");
272       list.add("two");
273       list.add("three");
274       List JavaDoc<String JavaDoc> expected = new ArrayList JavaDoc<String JavaDoc>();
275       expected.add("three");
276       expected.add("two");
277       expected.add("one");
278       List JavaDoc<String JavaDoc> result = proxy.echoReverse(list);
279       assertEquals(expected, result);
280    }
281
282    protected void checkAttributes(Class JavaDoc intf, Object JavaDoc proxy, Object JavaDoc mxbean, String JavaDoc[] keys, OpenType JavaDoc[] types, Object JavaDoc[] values, OpenMBeanInfo JavaDoc info) throws Exception JavaDoc
283    {
284       MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
285
286       assertEquals(keys.length, values.length);
287       assertEquals(keys.length, types.length);
288       assertEquals(keys.length, attributes.length);
289       
290       Map JavaDoc<String JavaDoc, OpenMBeanAttributeInfo JavaDoc> mapping = new HashMap JavaDoc<String JavaDoc, OpenMBeanAttributeInfo JavaDoc>(attributes.length);
291       for (int i = 0; i < attributes.length; ++i)
292       {
293          OpenMBeanAttributeInfo JavaDoc attribute = (OpenMBeanAttributeInfo JavaDoc) attributes[i];
294          String JavaDoc name = attribute.getName();
295          mapping.put(name, attribute);
296       }
297       
298       for (int i = 0; i < keys.length; ++i)
299       {
300          String JavaDoc name = getUpperName(keys[i]);
301          OpenMBeanAttributeInfo JavaDoc attribute = mapping.get(name);
302          assertNotNull("Could not find key " + name + " in " + mapping.keySet(), attribute);
303          Method JavaDoc method = MXBeanUtils.getCompositeDataMethod(intf, keys[i], types[i] == SimpleType.BOOLEAN);
304          Object JavaDoc value = method.invoke(proxy, null);
305          checkValueEquals(values[i], value);
306          value = method.invoke(mxbean, null);
307          checkValueEquals(values[i], value);
308          assertEquals(types[i], attribute.getOpenType());
309       }
310    }
311 }
312
Popular Tags