Search in sources :

Example 11 with BundleMock

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

the class BlueprintURLContextTest method setup.

@BeforeClass
public static void setup() {
    bundle = Skeleton.newMock(new BundleMock("aBundle", new Hashtable<String, String>()), Bundle.class);
    BundleContext bc = bundle.getBundleContext();
    new org.apache.aries.jndi.startup.Activator().start(bc);
    Activator a = new Activator();
    a.start(bc);
    a.serviceFound();
    // Register a BlueprintContainer mock that will answer getComponentInstance(String id) calls
    BlueprintContainer bpc = Skeleton.newMock(new BlueprintContainerStub(), BlueprintContainer.class);
    bc.registerService("org.osgi.service.blueprint.container.BlueprintContainer", bpc, new Hashtable<String, String>());
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Bundle(org.osgi.framework.Bundle) BundleMock(org.apache.aries.mocks.BundleMock) BundleContext(org.osgi.framework.BundleContext) BeforeClass(org.junit.BeforeClass)

Example 12 with BundleMock

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

the class FragmentUtilsTest method makeBundleWithExports.

private Bundle makeBundleWithExports(String symbolicName, String version, String exports) {
    Hashtable<String, Object> headers = new Hashtable<String, Object>();
    headers.put(Constants.BUNDLE_VERSION, version);
    headers.put(Constants.EXPORT_PACKAGE, exports);
    Bundle exportBundle = Skeleton.newMock(new BundleMock(symbolicName, headers), Bundle.class);
    return exportBundle;
}
Also used : Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) BundleMock(org.apache.aries.mocks.BundleMock)

Example 13 with BundleMock

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

the class ServiceRegistryContextTest method testLookupWhenServiceHasBeenRemoved.

/**
   * Check that we get a NameNotFoundException if we lookup after the service
   * has been unregistered.
   * 
   * @throws NamingException
   */
@Test(expected = NameNotFoundException.class)
public void testLookupWhenServiceHasBeenRemoved() throws NamingException {
    reg.unregister();
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    ctx.lookup("osgi:service/java.lang.Runnable");
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 14 with BundleMock

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

the class ServiceRegistryContextTest method checkServiceOrderObserved.

@Test
public void checkServiceOrderObserved() 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);
    ServiceRegistration reg2 = 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();
    reg2.unregister();
    Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_RANKING, 55);
    t = Skeleton.newMock(Runnable.class);
    t2 = Skeleton.newMock(Runnable.class);
    bc.registerService(className, t, null);
    bc.registerService(className, t2, props);
    r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
    r.run();
    Skeleton.getSkeleton(t).assertNotCalled(new MethodCall(Runnable.class, "run"));
    Skeleton.getSkeleton(t2).assertCalledExactNumberOfTimes(new MethodCall(Runnable.class, "run"), 1);
}
Also used : Hashtable(java.util.Hashtable) 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 15 with BundleMock

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

the class ServiceRegistryContextTest method checkServiceListLookup.

@Test
public void checkServiceListLookup() 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("java.lang.Thread", new Thread(), null);
    Context ctx2 = (Context) ctx.lookup("osgi:servicelist/java.lang.Runnable");
    Runnable r = (Runnable) ctx2.lookup(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID)));
    r.run();
    Skeleton.getSkeleton(t).assertCalled(new MethodCall(Runnable.class, "run"));
    reg.unregister();
    try {
        r.run();
        fail("Should have received a ServiceException");
    } catch (ServiceException e) {
        assertEquals("service exception has the wrong type", ServiceException.UNREGISTERED, e.getType());
    }
    try {
        ctx2.lookup(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID)));
        fail("Expected a NameNotFoundException");
    } catch (NameNotFoundException e) {
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) ServiceException(org.osgi.framework.ServiceException) NameNotFoundException(javax.naming.NameNotFoundException) 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)

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