KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > junit > MockFactory


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.junit;
10
11 import java.lang.reflect.Proxy JavaDoc;
12 import java.lang.reflect.InvocationHandler JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 /**
17  * This factory creates mock implementations
18  *
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision$
21  */

22 public class MockFactory
23 {
24
25    public static Object JavaDoc wrap(ClassLoader JavaDoc loader, Class JavaDoc[] itfs, Object JavaDoc implementation) throws IllegalArgumentException JavaDoc
26    {
27       if (implementation == null)
28       {
29          throw new IllegalArgumentException JavaDoc("Implementation cannot be null");
30       }
31       InvocationHandler JavaDoc handler = new MockHandler(implementation);
32       Object JavaDoc proxy = Proxy.newProxyInstance(loader, itfs, handler);
33       return proxy;
34    }
35
36    private static class MockHandler implements InvocationHandler JavaDoc
37    {
38
39       private final Object JavaDoc target;
40
41       public MockHandler(Object JavaDoc target)
42       {
43          this.target = target;
44       }
45
46       public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc proxyMethod, Object JavaDoc[] args) throws Throwable JavaDoc
47       {
48          //
49
Method JavaDoc targetMethod = target.getClass().getMethod(proxyMethod.getName(), proxyMethod.getParameterTypes());
50
51          //
52
if (targetMethod == null)
53          {
54             // See if the object has an untyped invoker
55
targetMethod = target.getClass().getMethod("invoke", new Class JavaDoc[]{Object JavaDoc[].class});
56
57             // Adapts the argument
58
if (targetMethod != null)
59             {
60                args = new Object JavaDoc[]{args};
61             }
62          }
63
64          // Nothing found we cant go farther
65
if (targetMethod == null)
66          {
67             throw new UnsupportedOperationException JavaDoc("Method " + proxyMethod + " was not found");
68          }
69
70          // Proceed to invocation
71
try
72          {
73             return targetMethod.invoke(target, args);
74          }
75          catch (Exception JavaDoc e)
76          {
77             if (e instanceof InvocationTargetException JavaDoc)
78             {
79                InvocationTargetException JavaDoc ite = (InvocationTargetException JavaDoc)e;
80                Throwable JavaDoc cause = ite.getCause();
81                throw cause;
82             }
83             else
84             {
85                UnsupportedOperationException JavaDoc ex = new UnsupportedOperationException JavaDoc("Method " + proxyMethod + " threw an unexpected exception");
86                ex.initCause(e);
87                throw ex;
88             }
89          }
90       }
91    }
92 }
93
Popular Tags