KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > proxycompiler > test > ProxyCompilerUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, 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.proxycompiler.test;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 import junit.framework.*;
27
28 import org.jboss.logging.Logger;
29
30 import org.jboss.proxy.compiler.*;
31
32 /**
33  * Test the ability of the proxy compiler to create proxies for
34  * simple/complex interfaces and classes.
35  *
36  * @version <tt>$Revision: 58115 $</tt>
37  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
38  */

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 JavaDoc name)
47    {
48       super(name);
49    }
50
51    protected void setUp() throws Exception JavaDoc
52    {
53       super.setUp();
54
55       handler = new LoggingInvocationHandler(log);
56    }
57
58    /**
59     * Create a value for the given type. If non-void primitive, then
60     * return a wrapper, else return null.
61     */

62    protected static Object JavaDoc createValue(Class JavaDoc type)
63    {
64       Object JavaDoc rv = null;
65
66       if (type.isPrimitive()) {
67          if (type == Boolean.TYPE)
68             rv = new Boolean JavaDoc(false);
69          
70          else if (type == Byte.TYPE)
71             rv = new Byte JavaDoc((byte)0);
72          
73          else if (type == Character.TYPE)
74             rv = new Character JavaDoc((char)0);
75          
76          else if (type == Short.TYPE)
77             rv = new Short JavaDoc((short)0);
78          
79          else if (type == Integer.TYPE)
80             rv = new Integer JavaDoc(0);
81          
82          else if (type == Long.TYPE)
83             rv = new Long JavaDoc(0);
84          
85          else if (type == Float.TYPE)
86             rv = new Float JavaDoc(0);
87          
88          else if (type == Double.TYPE)
89             rv = new Double JavaDoc(0);
90          
91          else if (type == Void.TYPE)
92             rv = null;
93          
94          else
95             throw new Error JavaDoc("unreachable");
96       }
97
98       return rv;
99    }
100
101    /**
102     * An InvocationHandler which simplly logs all calls
103     */

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 JavaDoc invoke(Object JavaDoc dummy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
114          log.debug("invoked: " + dummy + "," + method + "," + args);
115
116          log.debug("arguments: ");
117          for (int i=0; i<args.length; i++) {
118             String JavaDoc msg = " arg" + i + ": " + args[i];
119             if (args[i] != null) msg += ", type=" + args[i].getClass();
120             log.debug(msg);
121          }
122
123          Object JavaDoc value = createValue(method.getReturnType());
124          log.debug("return value: " + value);
125          return value;
126       }
127    }
128
129    protected Object JavaDoc createProxy(Class JavaDoc type) throws Exception JavaDoc
130    {
131       Object JavaDoc proxy = Proxy.newProxyInstance(type.getClass().getClassLoader(),
132                                             new Class JavaDoc[] { type },
133                                             handler);
134
135       log.debug("new proxy: " + proxy);
136
137       return proxy;
138    }
139
140    protected void invokeDeclaredMethods(Object JavaDoc obj, Class JavaDoc type) throws Exception JavaDoc
141    {
142       Method JavaDoc[] methods = type.getDeclaredMethods();
143
144       for (int i=0; i<methods.length; i++) {
145          log.debug("Invoking method: " + methods[i]);
146
147          Class JavaDoc[] pTypes = methods[i].getParameterTypes();
148          Object JavaDoc[] args = new Object JavaDoc[pTypes.length];
149
150          // create some dummy arg values
151
for (int j=0; j<args.length; j++) {
152             args[j] = createValue(pTypes[j]);
153          }
154
155          Object JavaDoc rv = methods[i].invoke(obj, args);
156
157          log.debug("Method returned: " + rv);
158       }
159    }
160
161    public static interface EmptyInterface
162    {
163       // empty
164
}
165
166    public void testEmptyInterface() throws Exception JavaDoc
167    {
168       createProxy(EmptyInterface.class);
169    }
170
171    public static interface SimpleInterface
172    {
173       void simple();
174    }
175
176    public void testSimpleInterface() throws Exception JavaDoc
177    {
178       Object JavaDoc obj = createProxy(SimpleInterface.class);
179       invokeDeclaredMethods(obj, SimpleInterface.class);
180    }
181
182    public static interface ReturnValues
183    {
184       // returns, no args
185

186       void noargs();
187
188       Object JavaDoc 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 JavaDoc
208    {
209       Object JavaDoc obj = createProxy(ReturnValues.class);
210       invokeDeclaredMethods(obj, ReturnValues.class);
211    }
212
213    public static interface CommonMethodParameters
214    {
215       // no returns, different args
216

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 JavaDoc a);
290
291       void Object2(Object JavaDoc a, Object JavaDoc b);
292
293       void Object3(Object JavaDoc a, Object JavaDoc b, Object JavaDoc c);
294
295       void Object4(Object JavaDoc a, Object JavaDoc b, Object JavaDoc c, Object JavaDoc d);
296    }
297
298    public void testCommonMethodParameters() throws Exception JavaDoc
299    {
300       Object JavaDoc 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 JavaDoc test2();
309
310       public abstract Object JavaDoc test3(Object JavaDoc obj);
311
312       public abstract Object JavaDoc test4(Object JavaDoc obj) throws Exception JavaDoc;
313    }
314
315    public void testSimpleAbstractClass() throws Exception JavaDoc
316    {
317       Object JavaDoc 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          // blah
328
}
329
330       abstract class NestedAbstractClass
331          extends SimpleAbstractClass
332       {
333          // blah
334
}
335
336       class ConcreteClass
337       {
338          // blah
339
}
340
341       long complex1(boolean a, byte b, char c, short d, int e, long f, float g, double h, Object JavaDoc i)
342          throws Exception JavaDoc, Error JavaDoc, Throwable JavaDoc;
343
344       Object JavaDoc[] complex2(boolean[] a, byte[] b, char[] c, short[] d, int[] e, long[] f, float[] g, double[] h, Object JavaDoc[] i)
345          throws Exception JavaDoc, Error JavaDoc, Throwable JavaDoc;
346
347       Object JavaDoc[][] complex3(boolean[][] a, byte[][] b, char[][] c, short[][] d, int[][] e, long[][] f, float[][] g, double[][] h, Object JavaDoc[][] i)
348          throws Exception JavaDoc, Error JavaDoc, Throwable JavaDoc;
349
350       Object JavaDoc[][][] complex4(boolean[] a, byte[][] b, char[][][] c, short[][][][] d, int[][][][][] e, long[][][][][][] f, float[][][][][][][] g, double[][][][][][][][] h, Object JavaDoc[][][][][][][][][] i)
351          throws Exception JavaDoc, Error JavaDoc, Throwable JavaDoc;
352    }
353
354    public void testComplexInterface() throws Exception JavaDoc
355    {
356       Object JavaDoc 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