Search in sources :

Example 16 with BundleMock

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

the class ServiceRegistryContextTest method checkServiceListList.

@Test
public void checkServiceListList() 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);
    // we don't want the default service
    reg.unregister();
    ServiceRegistration reg = bc.registerService(className, t, null);
    ServiceRegistration reg2 = bc.registerService(className, new Thread(), null);
    NamingEnumeration<NameClassPair> ne = ctx.list("osgi:servicelist/" + className);
    assertTrue(ne.hasMoreElements());
    NameClassPair ncp = ne.nextElement();
    assertEquals(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID)), ncp.getName());
    assertTrue("Class name not correct. Was: " + ncp.getClassName(), ncp.getClassName().contains("Proxy"));
    assertTrue(ne.hasMoreElements());
    ncp = ne.nextElement();
    assertEquals(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID)), ncp.getName());
    assertEquals("Class name not correct.", Thread.class.getName(), ncp.getClassName());
    assertFalse(ne.hasMoreElements());
}
Also used : NameClassPair(javax.naming.NameClassPair) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 17 with BundleMock

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

the class ServiceRegistryContextTest method testBaseLookup.

@Test
public void testBaseLookup() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    Context ctx2 = (Context) ctx.lookup("osgi:service");
    Runnable r1 = (Runnable) ctx2.lookup("java.lang.Runnable");
    assertNotNull(r1);
    assertTrue("expected proxied service class", r1 != service);
    Runnable r2 = (Runnable) ctx.lookup("aries:services/java.lang.Runnable");
    assertNotNull(r2);
    assertTrue("expected non-proxied service class", r2 == service);
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

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