1 22 package org.jboss.test.proxycompiler.test; 23 24 import java.lang.reflect.Method ; 25 26 import junit.framework.*; 27 28 import org.jboss.logging.Logger; 29 30 import org.jboss.proxy.compiler.*; 31 32 39 public class ProxyCompilerUnitTestCase 40 extends TestCase 41 { 42 private static final Logger log = Logger.getLogger(ProxyCompilerUnitTestCase.class); 43 44 protected InvocationHandler handler; 45 46 public ProxyCompilerUnitTestCase(String name) 47 { 48 super(name); 49 } 50 51 protected void setUp() throws Exception 52 { 53 super.setUp(); 54 55 handler = new LoggingInvocationHandler(log); 56 } 57 58 62 protected static Object createValue(Class type) 63 { 64 Object rv = null; 65 66 if (type.isPrimitive()) { 67 if (type == Boolean.TYPE) 68 rv = new Boolean (false); 69 70 else if (type == Byte.TYPE) 71 rv = new Byte ((byte)0); 72 73 else if (type == Character.TYPE) 74 rv = new Character ((char)0); 75 76 else if (type == Short.TYPE) 77 rv = new Short ((short)0); 78 79 else if (type == Integer.TYPE) 80 rv = new Integer (0); 81 82 else if (type == Long.TYPE) 83 rv = new Long (0); 84 85 else if (type == Float.TYPE) 86 rv = new Float (0); 87 88 else if (type == Double.TYPE) 89 rv = new Double (0); 90 91 else if (type == Void.TYPE) 92 rv = null; 93 94 else 95 throw new Error ("unreachable"); 96 } 97 98 return rv; 99 } 100 101 104 public static class LoggingInvocationHandler 105 implements InvocationHandler 106 { 107 private Logger log; 108 109 public LoggingInvocationHandler(final Logger log) { 110 this.log = log; 111 } 112 113 public Object invoke(Object dummy, Method method, Object [] args) throws Throwable { 114 log.debug("invoked: " + dummy + "," + method + "," + args); 115 116 log.debug("arguments: "); 117 for (int i=0; i<args.length; i++) { 118 String msg = " arg" + i + ": " + args[i]; 119 if (args[i] != null) msg += ", type=" + args[i].getClass(); 120 log.debug(msg); 121 } 122 123 Object value = createValue(method.getReturnType()); 124 log.debug("return value: " + value); 125 return value; 126 } 127 } 128 129 protected Object createProxy(Class type) throws Exception 130 { 131 Object proxy = Proxy.newProxyInstance(type.getClass().getClassLoader(), 132 new Class [] { type }, 133 handler); 134 135 log.debug("new proxy: " + proxy); 136 137 return proxy; 138 } 139 140 protected void invokeDeclaredMethods(Object obj, Class type) throws Exception 141 { 142 Method [] methods = type.getDeclaredMethods(); 143 144 for (int i=0; i<methods.length; i++) { 145 log.debug("Invoking method: " + methods[i]); 146 147 Class [] pTypes = methods[i].getParameterTypes(); 148 Object [] args = new Object [pTypes.length]; 149 150 for (int j=0; j<args.length; j++) { 152 args[j] = createValue(pTypes[j]); 153 } 154 155 Object rv = methods[i].invoke(obj, args); 156 157 log.debug("Method returned: " + rv); 158 } 159 } 160 161 public static interface EmptyInterface 162 { 163 } 165 166 public void testEmptyInterface() throws Exception 167 { 168 createProxy(EmptyInterface.class); 169 } 170 171 public static interface SimpleInterface 172 { 173 void simple(); 174 } 175 176 public void testSimpleInterface() throws Exception 177 { 178 Object obj = createProxy(SimpleInterface.class); 179 invokeDeclaredMethods(obj, SimpleInterface.class); 180 } 181 182 public static interface ReturnValues 183 { 184 186 void noargs(); 187 188 Object Object_noargs(); 189 190 byte byte__noargs(); 191 192 boolean boolean_noargs(); 193 194 char char_noargs(); 195 196 int int_noargs(); 197 198 short short_noargs(); 199 200 long long_noargs(); 201 202 float float_noargs(); 203 204 double double_noargs(); 205 } 206 207 public void testReturnValues() throws Exception 208 { 209 Object obj = createProxy(ReturnValues.class); 210 invokeDeclaredMethods(obj, ReturnValues.class); 211 } 212 213 public static interface CommonMethodParameters 214 { 215 217 void boolean1(boolean a); 218 219 void boolean2(boolean a, boolean b); 220 221 void boolean3(boolean a, boolean b, boolean c); 222 223 void boolean4(boolean a, boolean b, boolean c, boolean d); 224 225 void byte1(byte a); 226 227 void byte2(byte a, byte b); 228 229 void byte3(byte a, byte b, byte c); 230 231 void byte4(byte a, byte b, byte c, byte d); 232 233 void char1(char a); 234 235 void char2(char a, char b); 236 237 void char3(char a, char b, char c); 238 239 void char4(char a, char b, char c, char d); 240 241 void short1(short a); 242 243 void short2(short a, short b); 244 245 void short3(short a, short b, short c); 246 247 void short4(short a, short b, short c, short d); 248 249 void int1(int a); 250 251 void int2(int a, int b); 252 253 void int3(int a, int b, int c); 254 255 void int4(int a, int b, int c, int d); 256 257 void long1(long a); 258 259 void long2(long a, long b); 260 261 void long3(long a, long b, long c); 262 263 void long4(long a, long b, long c, long d); 264 265 void long5(long a, long b, long c, long d, long e); 266 267 void long6(long a, long b, long c, long d, long e, long f); 268 269 void float1(float a); 270 271 void float2(float a, float b); 272 273 void float3(float a, float b, float c); 274 275 void float4(float a, float b, float c, float d); 276 277 void double1(double a); 278 279 void double2(double a, double b); 280 281 void double3(double a, double b, double c); 282 283 void double4(double a, double b, double c, double d); 284 285 void double5(double a, double b, double c, double d, double e); 286 287 void double6(double a, double b, double c, double d, double e, double f); 288 289 void Object1(Object a); 290 291 void Object2(Object a, Object b); 292 293 void Object3(Object a, Object b, Object c); 294 295 void Object4(Object a, Object b, Object c, Object d); 296 } 297 298 public void testCommonMethodParameters() throws Exception 299 { 300 Object obj = createProxy(CommonMethodParameters.class); 301 invokeDeclaredMethods(obj, CommonMethodParameters.class); 302 } 303 304 public static abstract class SimpleAbstractClass 305 { 306 public abstract void test1(); 307 308 public abstract Object test2(); 309 310 public abstract Object test3(Object obj); 311 312 public abstract Object test4(Object obj) throws Exception ; 313 } 314 315 public void testSimpleAbstractClass() throws Exception 316 { 317 Object obj = createProxy(SimpleAbstractClass.class); 318 invokeDeclaredMethods(obj, SimpleAbstractClass.class); 319 } 320 321 public static interface ComplexInterface 322 extends EmptyInterface, SimpleInterface, CommonMethodParameters 323 { 324 interface NestedInterface 325 extends CommonMethodParameters 326 { 327 } 329 330 abstract class NestedAbstractClass 331 extends SimpleAbstractClass 332 { 333 } 335 336 class ConcreteClass 337 { 338 } 340 341 long complex1(boolean a, byte b, char c, short d, int e, long f, float g, double h, Object i) 342 throws Exception , Error , Throwable ; 343 344 Object [] complex2(boolean[] a, byte[] b, char[] c, short[] d, int[] e, long[] f, float[] g, double[] h, Object [] i) 345 throws Exception , Error , Throwable ; 346 347 Object [][] complex3(boolean[][] a, byte[][] b, char[][] c, short[][] d, int[][] e, long[][] f, float[][] g, double[][] h, Object [][] i) 348 throws Exception , Error , Throwable ; 349 350 Object [][][] complex4(boolean[] a, byte[][] b, char[][][] c, short[][][][] d, int[][][][][] e, long[][][][][][] f, float[][][][][][][] g, double[][][][][][][][] h, Object [][][][][][][][][] i) 351 throws Exception , Error , Throwable ; 352 } 353 354 public void testComplexInterface() throws Exception 355 { 356 Object obj = createProxy(ComplexInterface.class); 357 invokeDeclaredMethods(obj, EmptyInterface.class); 358 invokeDeclaredMethods(obj, SimpleInterface.class); 359 invokeDeclaredMethods(obj, CommonMethodParameters.class); 360 invokeDeclaredMethods(obj, ComplexInterface.class); 361 } 362 } 363 | Popular Tags |