KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Proxy JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Calendar JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MBeanServerFactory JavaDoc;
33 import javax.management.openmbean.CompositeData JavaDoc;
34 import javax.management.openmbean.CompositeDataSupport JavaDoc;
35 import javax.management.openmbean.CompositeType JavaDoc;
36 import javax.management.openmbean.OpenType JavaDoc;
37 import javax.management.openmbean.SimpleType JavaDoc;
38
39 import junit.framework.AssertionFailedError;
40
41 import org.jboss.logging.Logger;
42 import org.jboss.mx.mxbean.CompositeDataInvocationHandler;
43 import org.jboss.mx.mxbean.MXBeanUtils;
44 import org.jboss.test.BaseTestCase;
45 import org.jboss.test.mx.mxbean.support.InvalidInterface;
46 import org.jboss.test.mx.mxbean.support.SimpleInterface;
47 import org.jboss.util.UnexpectedThrowable;
48
49 /**
50  * AbstractMXBeanTest.
51  *
52  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
53  * @version $Revision: 1.1 $
54  */

55 public abstract class AbstractMXBeanTest extends BaseTestCase
56 {
57    private static Logger staticLog = Logger.getLogger(AbstractMXBeanTest.class);
58
59    public AbstractMXBeanTest(String JavaDoc name)
60    {
61       super(name);
62    }
63    
64    // @fixme move to AbstractTestCase
65
public static void checkThrowableDeep(Class JavaDoc<? extends Throwable JavaDoc> wrapperExpected, Class JavaDoc<? extends Throwable JavaDoc> deepExpected, Throwable JavaDoc throwable) throws Exception JavaDoc
66    {
67       assertNotNull(deepExpected);
68       assertNotNull(throwable);
69       
70       Throwable JavaDoc original = throwable;
71       
72       if (wrapperExpected != null)
73       {
74          if (wrapperExpected.isInstance(original) == false)
75          {
76             if (original instanceof Exception JavaDoc)
77                throw (Exception JavaDoc) original;
78             else if (original instanceof Error JavaDoc)
79                throw (Error JavaDoc) original;
80             else
81                throw new UnexpectedThrowable("UnexpectedThrowable", original);
82          }
83          staticLog.debug("Got expected " + wrapperExpected.getName() + "(" + original + ")");
84       }
85       
86       while (throwable.getCause() != null)
87          throwable = throwable.getCause();
88       
89       if (deepExpected.isInstance(throwable) == false)
90       {
91          if (original instanceof Exception JavaDoc)
92             throw (Exception JavaDoc) original;
93          else if (original instanceof Error JavaDoc)
94             throw (Error JavaDoc) original;
95          else
96             throw new UnexpectedThrowable("UnexpectedThrowable", original);
97       }
98       else
99       {
100          staticLog.debug("Got expected " + deepExpected.getName() + "(" + throwable + ")");
101       }
102    }
103    
104    // @fixme move to AbstractTestCase
105
public static void checkThrowableDeep(Class JavaDoc<? extends Throwable JavaDoc> expected, Throwable JavaDoc throwable) throws Exception JavaDoc
106    {
107       checkThrowableDeep(null, expected, throwable);
108    }
109    
110    // @fixme move to AbstractTestCase
111
public static <T> T assertInstanceOf(Class JavaDoc<T> expected, Object JavaDoc object) throws Exception JavaDoc
112    {
113       if (object == null)
114          return null;
115       assertTrue(object.getClass(). getName() + " is not an instance of " + expected.getName(), expected.isInstance(object));
116       return expected.cast(object);
117    }
118    
119    public static void checkArrayEquals(Object JavaDoc expected, Object JavaDoc actual)
120    {
121       Object JavaDoc[] a1 = (Object JavaDoc[]) expected;
122       Object JavaDoc[] a2 = (Object JavaDoc[]) actual;
123       if (Arrays.deepEquals(a1, a2) == false)
124          throw new AssertionFailedError("Expected: " + a1 + "=" + Arrays.deepToString(a1) + " got " + a2 + "=" + Arrays.deepToString(a2));
125    }
126    
127    @SuppressWarnings JavaDoc("unchecked")
128    public static void checkCompositeDataHandlerEquals(Object JavaDoc expected, Object JavaDoc actual)
129    {
130       if (expected == null)
131       {
132          assertEquals(expected, actual);
133          return;
134       }
135
136       CompositeDataInvocationHandler handler = (CompositeDataInvocationHandler) Proxy.getInvocationHandler(actual);
137       CompositeData JavaDoc data = handler.getCompositeData();
138       CompositeType JavaDoc type = data.getCompositeType();
139       Set JavaDoc<String JavaDoc> names = type.keySet();
140       Class JavaDoc clazz = expected.getClass();
141       for (String JavaDoc name : names)
142       {
143          OpenType JavaDoc itemType = type.getType(name);
144          try
145          {
146             Method JavaDoc method = MXBeanUtils.getCompositeDataMethod(clazz, name, itemType == SimpleType.BOOLEAN);
147             Object JavaDoc expectedValue = method.invoke(expected, null);
148             Object JavaDoc actualValue = handler.invoke(actual, method, null);
149             assertEquals(expectedValue, actualValue);
150          }
151          catch (RuntimeException JavaDoc e)
152          {
153             throw e;
154          }
155          catch (Error JavaDoc e)
156          {
157             throw e;
158          }
159          catch (Throwable JavaDoc t)
160          {
161             throw new RuntimeException JavaDoc(t);
162          }
163       }
164    }
165    
166    public static void checkValueEquals(Object JavaDoc expected, Object JavaDoc actual)
167    {
168       if (actual == null)
169          assertEquals(expected, actual);
170       else if (actual instanceof Proxy JavaDoc)
171          checkCompositeDataHandlerEquals(expected, actual);
172       else if (actual.getClass().isArray())
173          checkArrayEquals(expected, actual);
174       else
175          assertEquals(expected, actual);
176    }
177
178    protected CompositeData JavaDoc createCompositeData(String JavaDoc name, String JavaDoc[] keys, Object JavaDoc[] values) throws Exception JavaDoc
179    {
180       assertNotNullArray("values", values);
181
182       Class JavaDoc[] types = new Class JavaDoc[values.length];
183       for (int i = 0; i < values.length; ++i)
184          types[i] = values[i].getClass();
185       
186       return createCompositeData(name, keys, types, values);
187    }
188    
189    protected CompositeData JavaDoc createCompositeData(String JavaDoc name, String JavaDoc[] keys, Class JavaDoc[] types, Object JavaDoc[] values) throws Exception JavaDoc
190    {
191       CompositeType JavaDoc compositeType = createCompositeType(name, keys, types);
192       return new CompositeDataSupport JavaDoc(compositeType, keys, values);
193    }
194    
195    protected CompositeData JavaDoc createCompositeData(String JavaDoc name, String JavaDoc[] keys, OpenType JavaDoc[] openTypes, Object JavaDoc[] values) throws Exception JavaDoc
196    {
197       CompositeType JavaDoc compositeType = createCompositeType(name, keys, openTypes);
198       return new CompositeDataSupport JavaDoc(compositeType, keys, values);
199    }
200    
201    protected CompositeType JavaDoc createCompositeType(String JavaDoc name, String JavaDoc[] keys, Class JavaDoc[] types) throws Exception JavaDoc
202    {
203       assertNotNull(name);
204       assertNotNullArray("keys", keys);
205       assertNotNullArray("types", types);
206       assertEquals(keys.length, types.length);
207       
208       OpenType JavaDoc[] openTypes = new OpenType JavaDoc[types.length];
209       for (int i = 0; i < types.length; ++i)
210          openTypes[i] = MXBeanUtils.getOpenType(types[i]);
211
212       return new CompositeType JavaDoc(name, name, keys, keys, openTypes);
213    }
214    
215    protected CompositeType JavaDoc createCompositeType(String JavaDoc name, String JavaDoc[] keys, OpenType JavaDoc[] openTypes) throws Exception JavaDoc
216    {
217       assertNotNull(name);
218       assertNotNullArray("keys", keys);
219       assertNotNullArray("types", openTypes);
220       assertEquals(keys.length, openTypes.length);
221       
222       return new CompositeType JavaDoc(name, name, keys, keys, openTypes);
223    }
224    
225    protected <T> T createCompositeDataProxy(Class JavaDoc<T> intf, String JavaDoc[] keys, Object JavaDoc[] values) throws Exception JavaDoc
226    {
227       assertNotNull(intf);
228       return createCompositeDataProxy(intf, intf.getName(), keys, values);
229    }
230    
231    protected <T> T createCompositeDataProxy(Class JavaDoc<T> intf, String JavaDoc name, String JavaDoc[] keys, Object JavaDoc[] values) throws Exception JavaDoc
232    {
233       CompositeData JavaDoc compositeData = createCompositeData(name, keys, values);
234       return createCompositeDataProxy(intf, compositeData);
235    }
236    
237    protected <T> T createCompositeDataProxy(Class JavaDoc<T> intf, String JavaDoc[] keys, Class JavaDoc[] types, Object JavaDoc[] values) throws Exception JavaDoc
238    {
239       assertNotNull(intf);
240       return createCompositeDataProxy(intf, intf.getName(), keys, types, values);
241    }
242    
243    protected <T> T createCompositeDataProxy(Class JavaDoc<T> intf, String JavaDoc name, String JavaDoc[] keys, Class JavaDoc[] types, Object JavaDoc[] values) throws Exception JavaDoc
244    {
245       CompositeData JavaDoc compositeData = createCompositeData(name, keys, types, values);
246       return createCompositeDataProxy(intf, compositeData);
247    }
248    
249    protected <T> T createCompositeDataProxy(Class JavaDoc<T> intf, String JavaDoc name, String JavaDoc[] keys, OpenType JavaDoc[] openTypes, Object JavaDoc[] values) throws Exception JavaDoc
250    {
251       CompositeData JavaDoc compositeData = createCompositeData(name, keys, openTypes, values);
252       return createCompositeDataProxy(intf, compositeData);
253    }
254    
255    protected <T> T createCompositeDataProxy(Class JavaDoc<T> intf, CompositeData JavaDoc compositeData)
256    {
257       return MXBeanUtils.createCompositeDataProxy(intf, compositeData);
258    }
259    
260    protected CompositeData JavaDoc createTestCompositeData() throws Exception JavaDoc
261    {
262       return createCompositeData("Test", SimpleInterface.KEYS, SimpleInterface.VALUES);
263    }
264    
265    protected SimpleInterface createTestCompositeDataProxy() throws Exception JavaDoc
266    {
267       return createCompositeDataProxy(SimpleInterface.class, SimpleInterface.KEYS, SimpleInterface.VALUES);
268    }
269    
270    protected SimpleInterface createNullCompositeDataProxy() throws Exception JavaDoc
271    {
272       return createCompositeDataProxy(SimpleInterface.class, SimpleInterface.KEYS, SimpleInterface.TYPES, SimpleInterface.NULL_VALUES);
273    }
274    
275    protected InvalidInterface createInvalidCompositeDataProxy() throws Exception JavaDoc
276    {
277       return createCompositeDataProxy(InvalidInterface.class, SimpleInterface.KEYS, SimpleInterface.VALUES);
278    }
279    
280    protected SimpleInterface createTestCompositeDataProxy(String JavaDoc name) throws Exception JavaDoc
281    {
282       return createCompositeDataProxy(SimpleInterface.class, name, SimpleInterface.KEYS, SimpleInterface.VALUES);
283    }
284    
285    protected MBeanServer JavaDoc createMBeanServer()
286    {
287       return MBeanServerFactory.newMBeanServer();
288    }
289    
290    protected void assertNotNullArray(String JavaDoc context, Object JavaDoc[] array) throws Exception JavaDoc
291    {
292       assertNotNull(context + " is null ", array);
293       for (int i = 0; i < array.length; ++i)
294          assertNotNull(context + "[" + i + "] is null", array[i]);
295    }
296
297    public static Date JavaDoc createDate(int year, int month, int day)
298    {
299       Calendar JavaDoc calender = Calendar.getInstance();
300       calender.clear();
301       calender.set(year, month-1, day, 0, 0, 0);
302       return calender.getTime();
303    }
304    
305    public static String JavaDoc getUpperName(String JavaDoc name)
306    {
307       return Character.toUpperCase(name.charAt(0)) + name.substring(1);
308    }
309 }
310
Popular Tags