Search in sources :

Example 1 with Skeleton

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

the class EJBLocatorTest method assertAnnotation.

private void assertAnnotation(boolean b) {
    Skeleton s = Skeleton.getSkeleton(registry);
    MethodCall mc = new MethodCall(EJBRegistry.class, "addEJBView", "Annotated", "STATELESS", "test.ejbs.StatelessSessionBean", false);
    if (b)
        s.assertCalledExactNumberOfTimes(mc, 1);
    else
        s.assertNotCalled(mc);
    mc = new MethodCall(EJBRegistry.class, "addEJBView", String.class, "STATEFUL", String.class, boolean.class);
    if (b)
        s.assertCalledExactNumberOfTimes(mc, 1);
    else
        s.assertNotCalled(mc);
}
Also used : EJBRegistry(org.apache.aries.ejb.modelling.EJBRegistry) Skeleton(org.apache.aries.unittest.mocks.Skeleton) MethodCall(org.apache.aries.unittest.mocks.MethodCall)

Example 2 with Skeleton

use of org.apache.aries.unittest.mocks.Skeleton 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)

Example 3 with Skeleton

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

the class ServiceRegistryContextTest method registerService.

/**
 * Register a service in our map.
 *
 * @param service2 The service to register.
 */
private void registerService(Runnable service2) {
    ServiceFactory factory = Skeleton.newMock(ServiceFactory.class);
    Skeleton skel = Skeleton.getSkeleton(factory);
    skel.setReturnValue(new MethodCall(ServiceFactory.class, "getService", Bundle.class, ServiceRegistration.class), service2);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put("rubbish", "smelly");
    reg = bc.registerService(new String[] { "java.lang.Runnable" }, factory, props);
}
Also used : ServiceFactory(org.osgi.framework.ServiceFactory) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Skeleton(org.apache.aries.unittest.mocks.Skeleton) MethodCall(org.apache.aries.unittest.mocks.MethodCall) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 4 with Skeleton

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

the class ServiceRegistryContextTest method simpleJNDILookup.

/**
 * This test does a simple JNDI lookup to prove that works.
 * @throws NamingException
 */
@Test
public void simpleJNDILookup() throws NamingException {
    System.setProperty(Context.URL_PKG_PREFIXES, "helloMatey");
    InitialContext ctx = new InitialContext(new Hashtable<Object, Object>());
    BundleMock mock = new BundleMock("scooby.doo.1", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    Runnable s = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
    assertNotNull("We didn't get a service back from our lookup :(", s);
    s.run();
    Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton skel = Skeleton.getSkeleton(mock.getBundleContext());
    skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null));
    ctx = new InitialContext(new Hashtable<Object, Object>());
    mock = new BundleMock("scooby.doo.2", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    s = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
    // Check we have the packages set correctly
    String packages = System.getProperty(Context.URL_PKG_PREFIXES, null);
    assertTrue(ctx.getEnvironment().containsValue(packages));
    assertNotNull("We didn't get a service back from our lookup :(", s);
    s.run();
    Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 2);
    skel = Skeleton.getSkeleton(mock.getBundleContext());
    skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null));
}
Also used : Hashtable(java.util.Hashtable) BundleMock(org.apache.aries.mocks.BundleMock) Skeleton(org.apache.aries.unittest.mocks.Skeleton) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 5 with Skeleton

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

the class ObjectFactoryTest method testObjectFactoryThatThrowsIsIgnored.

@Test
public void testObjectFactoryThatThrowsIsIgnored() throws Exception {
    final ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    bc.registerService(ObjectFactory.class.getName(), factory, null);
    final Skeleton skeleton = Skeleton.getSkeleton(factory);
    final MethodCall getObjectInstance = new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class);
    skeleton.setThrows(getObjectInstance, new Exception());
    Object original = new Object() {
    };
    Object processed = NamingManager.getObjectInstance(original, null, null, env);
    skeleton.assertCalled(getObjectInstance);
    assertEquals("The original object should be returned despite the object factory throwing an exception", original, processed);
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) Skeleton(org.apache.aries.unittest.mocks.Skeleton) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Test(org.junit.Test)

Aggregations

MethodCall (org.apache.aries.unittest.mocks.MethodCall)7 Skeleton (org.apache.aries.unittest.mocks.Skeleton)7 Hashtable (java.util.Hashtable)2 EJBRegistry (org.apache.aries.ejb.modelling.EJBRegistry)2 Test (org.junit.Test)2 Bundle (org.osgi.framework.Bundle)2 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 Properties (java.util.Properties)1 Callable (java.util.concurrent.Callable)1 InitialContext (javax.naming.InitialContext)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 ObjectFactory (javax.naming.spi.ObjectFactory)1 BundleMock (org.apache.aries.mocks.BundleMock)1 ProxyManager (org.apache.aries.proxy.ProxyManager)1 MethodCallHandler (org.apache.aries.unittest.mocks.MethodCallHandler)1