Search in sources :

Example 1 with MethodCallHandler

use of org.apache.aries.unittest.mocks.MethodCallHandler in project aries by apache.

the class ServiceRegistryContextTest method registerProxyManager.

private void registerProxyManager() {
    ProxyManager mgr = Skeleton.newMock(ProxyManager.class);
    // public Object createDelegatingProxy(Bundle clientBundle, Collection<Class<?>> classes, Callable<Object> dispatcher, Object template) throws UnableToProxyException;
    Skeleton.getSkeleton(mgr).registerMethodCallHandler(new MethodCall(ProxyManager.class, "createDelegatingProxy", Bundle.class, Collection.class, Callable.class, Object.class), new MethodCallHandler() {

        public Object handle(MethodCall methodCall, Skeleton skeleton) throws Exception {
            @SuppressWarnings("unchecked") Collection<Class<?>> interfaceClasses = (Collection<Class<?>>) methodCall.getArguments()[1];
            Class<?>[] classes = new Class<?>[interfaceClasses.size()];
            Iterator<Class<?>> it = interfaceClasses.iterator();
            for (int i = 0; it.hasNext(); i++) {
                classes[i] = it.next();
            }
            @SuppressWarnings("unchecked") final Callable<Object> target = (Callable<Object>) methodCall.getArguments()[2];
            return Proxy.newProxyInstance(this.getClass().getClassLoader(), classes, new InvocationHandler() {

                public Object invoke(Object mock, Method method, Object[] arguments) throws Throwable {
                    return method.invoke(target.call(), arguments);
                }
            });
        }
    });
    bc.registerService(ProxyManager.class.getName(), mgr, null);
}
Also used : MethodCallHandler(org.apache.aries.unittest.mocks.MethodCallHandler) Bundle(org.osgi.framework.Bundle) ProxyManager(org.apache.aries.proxy.ProxyManager) Method(java.lang.reflect.Method) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InvocationHandler(java.lang.reflect.InvocationHandler) Callable(java.util.concurrent.Callable) ServiceException(org.osgi.framework.ServiceException) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) NameNotFoundException(javax.naming.NameNotFoundException) Iterator(java.util.Iterator) Collection(java.util.Collection) Skeleton(org.apache.aries.unittest.mocks.Skeleton)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Callable (java.util.concurrent.Callable)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 ProxyManager (org.apache.aries.proxy.ProxyManager)1 MethodCall (org.apache.aries.unittest.mocks.MethodCall)1 MethodCallHandler (org.apache.aries.unittest.mocks.MethodCallHandler)1 Skeleton (org.apache.aries.unittest.mocks.Skeleton)1 Bundle (org.osgi.framework.Bundle)1 ServiceException (org.osgi.framework.ServiceException)1