1 22 package org.jboss.test.mx.mxbean.test; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Proxy ; 26 import java.util.Arrays ; 27 import java.util.Calendar ; 28 import java.util.Date ; 29 import java.util.Set ; 30 31 import javax.management.MBeanServer ; 32 import javax.management.MBeanServerFactory ; 33 import javax.management.openmbean.CompositeData ; 34 import javax.management.openmbean.CompositeDataSupport ; 35 import javax.management.openmbean.CompositeType ; 36 import javax.management.openmbean.OpenType ; 37 import javax.management.openmbean.SimpleType ; 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 55 public abstract class AbstractMXBeanTest extends BaseTestCase 56 { 57 private static Logger staticLog = Logger.getLogger(AbstractMXBeanTest.class); 58 59 public AbstractMXBeanTest(String name) 60 { 61 super(name); 62 } 63 64 public static void checkThrowableDeep(Class <? extends Throwable > wrapperExpected, Class <? extends Throwable > deepExpected, Throwable throwable) throws Exception 66 { 67 assertNotNull(deepExpected); 68 assertNotNull(throwable); 69 70 Throwable original = throwable; 71 72 if (wrapperExpected != null) 73 { 74 if (wrapperExpected.isInstance(original) == false) 75 { 76 if (original instanceof Exception ) 77 throw (Exception ) original; 78 else if (original instanceof Error ) 79 throw (Error ) 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 ) 92 throw (Exception ) original; 93 else if (original instanceof Error ) 94 throw (Error ) 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 public static void checkThrowableDeep(Class <? extends Throwable > expected, Throwable throwable) throws Exception 106 { 107 checkThrowableDeep(null, expected, throwable); 108 } 109 110 public static <T> T assertInstanceOf(Class <T> expected, Object object) throws Exception 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 expected, Object actual) 120 { 121 Object [] a1 = (Object []) expected; 122 Object [] a2 = (Object []) 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 ("unchecked") 128 public static void checkCompositeDataHandlerEquals(Object expected, Object actual) 129 { 130 if (expected == null) 131 { 132 assertEquals(expected, actual); 133 return; 134 } 135 136 CompositeDataInvocationHandler handler = (CompositeDataInvocationHandler) Proxy.getInvocationHandler(actual); 137 CompositeData data = handler.getCompositeData(); 138 CompositeType type = data.getCompositeType(); 139 Set <String > names = type.keySet(); 140 Class clazz = expected.getClass(); 141 for (String name : names) 142 { 143 OpenType itemType = type.getType(name); 144 try 145 { 146 Method method = MXBeanUtils.getCompositeDataMethod(clazz, name, itemType == SimpleType.BOOLEAN); 147 Object expectedValue = method.invoke(expected, null); 148 Object actualValue = handler.invoke(actual, method, null); 149 assertEquals(expectedValue, actualValue); 150 } 151 catch (RuntimeException e) 152 { 153 throw e; 154 } 155 catch (Error e) 156 { 157 throw e; 158 } 159 catch (Throwable t) 160 { 161 throw new RuntimeException (t); 162 } 163 } 164 } 165 166 public static void checkValueEquals(Object expected, Object actual) 167 { 168 if (actual == null) 169 assertEquals(expected, actual); 170 else if (actual instanceof Proxy ) 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 createCompositeData(String name, String [] keys, Object [] values) throws Exception 179 { 180 assertNotNullArray("values", values); 181 182 Class [] types = new Class [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 createCompositeData(String name, String [] keys, Class [] types, Object [] values) throws Exception 190 { 191 CompositeType compositeType = createCompositeType(name, keys, types); 192 return new CompositeDataSupport (compositeType, keys, values); 193 } 194 195 protected CompositeData createCompositeData(String name, String [] keys, OpenType [] openTypes, Object [] values) throws Exception 196 { 197 CompositeType compositeType = createCompositeType(name, keys, openTypes); 198 return new CompositeDataSupport (compositeType, keys, values); 199 } 200 201 protected CompositeType createCompositeType(String name, String [] keys, Class [] types) throws Exception 202 { 203 assertNotNull(name); 204 assertNotNullArray("keys", keys); 205 assertNotNullArray("types", types); 206 assertEquals(keys.length, types.length); 207 208 OpenType [] openTypes = new OpenType [types.length]; 209 for (int i = 0; i < types.length; ++i) 210 openTypes[i] = MXBeanUtils.getOpenType(types[i]); 211 212 return new CompositeType (name, name, keys, keys, openTypes); 213 } 214 215 protected CompositeType createCompositeType(String name, String [] keys, OpenType [] openTypes) throws Exception 216 { 217 assertNotNull(name); 218 assertNotNullArray("keys", keys); 219 assertNotNullArray("types", openTypes); 220 assertEquals(keys.length, openTypes.length); 221 222 return new CompositeType (name, name, keys, keys, openTypes); 223 } 224 225 protected <T> T createCompositeDataProxy(Class <T> intf, String [] keys, Object [] values) throws Exception 226 { 227 assertNotNull(intf); 228 return createCompositeDataProxy(intf, intf.getName(), keys, values); 229 } 230 231 protected <T> T createCompositeDataProxy(Class <T> intf, String name, String [] keys, Object [] values) throws Exception 232 { 233 CompositeData compositeData = createCompositeData(name, keys, values); 234 return createCompositeDataProxy(intf, compositeData); 235 } 236 237 protected <T> T createCompositeDataProxy(Class <T> intf, String [] keys, Class [] types, Object [] values) throws Exception 238 { 239 assertNotNull(intf); 240 return createCompositeDataProxy(intf, intf.getName(), keys, types, values); 241 } 242 243 protected <T> T createCompositeDataProxy(Class <T> intf, String name, String [] keys, Class [] types, Object [] values) throws Exception 244 { 245 CompositeData compositeData = createCompositeData(name, keys, types, values); 246 return createCompositeDataProxy(intf, compositeData); 247 } 248 249 protected <T> T createCompositeDataProxy(Class <T> intf, String name, String [] keys, OpenType [] openTypes, Object [] values) throws Exception 250 { 251 CompositeData compositeData = createCompositeData(name, keys, openTypes, values); 252 return createCompositeDataProxy(intf, compositeData); 253 } 254 255 protected <T> T createCompositeDataProxy(Class <T> intf, CompositeData compositeData) 256 { 257 return MXBeanUtils.createCompositeDataProxy(intf, compositeData); 258 } 259 260 protected CompositeData createTestCompositeData() throws Exception 261 { 262 return createCompositeData("Test", SimpleInterface.KEYS, SimpleInterface.VALUES); 263 } 264 265 protected SimpleInterface createTestCompositeDataProxy() throws Exception 266 { 267 return createCompositeDataProxy(SimpleInterface.class, SimpleInterface.KEYS, SimpleInterface.VALUES); 268 } 269 270 protected SimpleInterface createNullCompositeDataProxy() throws Exception 271 { 272 return createCompositeDataProxy(SimpleInterface.class, SimpleInterface.KEYS, SimpleInterface.TYPES, SimpleInterface.NULL_VALUES); 273 } 274 275 protected InvalidInterface createInvalidCompositeDataProxy() throws Exception 276 { 277 return createCompositeDataProxy(InvalidInterface.class, SimpleInterface.KEYS, SimpleInterface.VALUES); 278 } 279 280 protected SimpleInterface createTestCompositeDataProxy(String name) throws Exception 281 { 282 return createCompositeDataProxy(SimpleInterface.class, name, SimpleInterface.KEYS, SimpleInterface.VALUES); 283 } 284 285 protected MBeanServer createMBeanServer() 286 { 287 return MBeanServerFactory.newMBeanServer(); 288 } 289 290 protected void assertNotNullArray(String context, Object [] array) throws Exception 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 createDate(int year, int month, int day) 298 { 299 Calendar 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 getUpperName(String name) 306 { 307 return Character.toUpperCase(name.charAt(0)) + name.substring(1); 308 } 309 } 310 | Popular Tags |