Search in sources :

Example 6 with BundleMock

use of org.apache.aries.mocks.BundleMock in project aries by apache.

the class ServiceRegistryContextTest method checkServiceListListBindings.

@Test
public void checkServiceListListBindings() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    String className = Runnable.class.getName();
    MethodCall run = new MethodCall(Runnable.class, "run");
    Runnable t = Skeleton.newMock(Runnable.class);
    Runnable t2 = Skeleton.newMock(Runnable.class);
    // we don't want the default service
    reg.unregister();
    ServiceRegistration reg = bc.registerService(className, t, null);
    ServiceRegistration reg2 = bc.registerService(className, t2, null);
    NamingEnumeration<Binding> ne = ctx.listBindings("osgi:servicelist/" + className);
    assertTrue(ne.hasMoreElements());
    Binding bnd = ne.nextElement();
    assertEquals(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID)), bnd.getName());
    assertTrue("Class name not correct. Was: " + bnd.getClassName(), bnd.getClassName().contains("Proxy") || bnd.getClassName().contains("EnhancerByCGLIB"));
    Runnable r = (Runnable) bnd.getObject();
    assertNotNull(r);
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(run, 1);
    Skeleton.getSkeleton(t2).assertNotCalled(run);
    assertTrue(ne.hasMoreElements());
    bnd = ne.nextElement();
    assertEquals(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID)), bnd.getName());
    assertTrue("Class name not correct. Was: " + bnd.getClassName(), bnd.getClassName().contains("Proxy") || bnd.getClassName().contains("EnhancerByCGLIB"));
    r = (Runnable) bnd.getObject();
    assertNotNull(r);
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(run, 1);
    Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(run, 1);
    assertFalse(ne.hasMoreElements());
}
Also used : Binding(javax.naming.Binding) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 7 with BundleMock

use of org.apache.aries.mocks.BundleMock in project aries by apache.

the class ServiceRegistryContextTest method testLookupForServiceWeNeverHad.

/**
   * Check that we get a NameNotFoundException if we lookup something not in the
   * registry.
   * 
   * @throws NamingException
   */
@Test(expected = NameNotFoundException.class)
public void testLookupForServiceWeNeverHad() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    ctx.lookup("osgi:service/java.lang.Integer");
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 8 with BundleMock

use of org.apache.aries.mocks.BundleMock in project aries by apache.

the class ServiceRegistryContextTest method checkProxyDynamism.

@Test
public void checkProxyDynamism() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    String className = Runnable.class.getName();
    Runnable t = Skeleton.newMock(Runnable.class);
    Runnable t2 = Skeleton.newMock(Runnable.class);
    // we don't want the default service
    reg.unregister();
    ServiceRegistration reg = bc.registerService(className, t, null);
    bc.registerService(className, t2, null);
    Runnable r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(t2).assertNotCalled(new MethodCall(Runnable.class, "run"));
    reg.unregister();
    r.run();
    Skeleton.getSkeleton(t).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 9 with BundleMock

use of org.apache.aries.mocks.BundleMock in project aries by apache.

the class ServiceRegistryContextTest method jndiLookupWithFilter.

/**
   * This test checks that we can pass a filter in without things blowing up.
   * Right now our mock service registry does not implement filtering, so the
   * effect is the same as simpleJNDILookup, but we at least know it is not
   * blowing up.
   * 
   * @throws NamingException
   */
@Test
public void jndiLookupWithFilter() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    Object s = ctx.lookup("osgi:service/java.lang.Runnable/(rubbish=smelly)");
    assertNotNull("We didn't get a service back from our lookup :(", s);
    service.run();
    Skeleton.getSkeleton(service).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
    Skeleton.getSkeleton(mock.getBundleContext()).assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", "(rubbish=smelly)"));
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) 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 10 with BundleMock

use of org.apache.aries.mocks.BundleMock in project aries by apache.

the class BlueprintURLContextTest method setupClassLoader.

@Before
public void setupClassLoader() {
    BundleMock mock = new BundleMock("bundle.for.new.initial.context", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) Before(org.junit.Before)

Aggregations

BundleMock (org.apache.aries.mocks.BundleMock)17 Properties (java.util.Properties)14 InitialContext (javax.naming.InitialContext)13 Test (org.junit.Test)13 MethodCall (org.apache.aries.unittest.mocks.MethodCall)8 Hashtable (java.util.Hashtable)6 BundleContext (org.osgi.framework.BundleContext)6 ServiceRegistration (org.osgi.framework.ServiceRegistration)5 Context (javax.naming.Context)3 Bundle (org.osgi.framework.Bundle)3 ServiceException (org.osgi.framework.ServiceException)2 Binding (javax.naming.Binding)1 NameClassPair (javax.naming.NameClassPair)1 NameNotFoundException (javax.naming.NameNotFoundException)1 DataSource (javax.sql.DataSource)1 Skeleton (org.apache.aries.unittest.mocks.Skeleton)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1 BlueprintContainer (org.osgi.service.blueprint.container.BlueprintContainer)1