Search in sources :

Example 1 with BundleMock

use of org.apache.aries.mocks.BundleMock 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 2 with BundleMock

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

the class BlueprintURLContextTest method bundleMock.

Bundle bundleMock(String bundleSymbolicNameHeader) {
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicNameHeader);
    Bundle result = Skeleton.newMock(new BundleMock("aBundle", props), Bundle.class);
    return result;
}
Also used : Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) BundleMock(org.apache.aries.mocks.BundleMock)

Example 3 with BundleMock

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

the class ServiceRegistryContextTest method checkProxyWhenServiceGoes.

@Test(expected = ServiceException.class)
public void checkProxyWhenServiceGoes() throws ServiceException, NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    Runnable r = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
    r.run();
    Skeleton.getSkeleton(service).assertCalled(new MethodCall(Runnable.class, "run"));
    reg.unregister();
    r.run();
}
Also used : BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 4 with BundleMock

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

the class ServiceRegistryContextTest method testLookupWithPause.

@Test
public void testLookupWithPause() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(JNDIConstants.REBIND_TIMEOUT, 1000);
    InitialContext ctx = new InitialContext(env);
    Context ctx2 = (Context) ctx.lookup("osgi:service");
    Runnable r1 = (Runnable) ctx2.lookup("java.lang.Runnable");
    reg.unregister();
    long startTime = System.currentTimeMillis();
    try {
        r1.run();
        fail("Should have received an exception");
    } catch (ServiceException e) {
        long endTime = System.currentTimeMillis();
        long diff = endTime - startTime;
        assertTrue("The run method did not fail in the expected time (1s): " + diff, diff >= 1000);
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) ServiceException(org.osgi.framework.ServiceException) Hashtable(java.util.Hashtable) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 5 with BundleMock

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

the class ServiceRegistryContextTest method jndiLookupServiceNameTest.

@Test
public void jndiLookupServiceNameTest() throws NamingException, SQLException {
    InitialContext ctx = new InitialContext(new Hashtable<Object, Object>());
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    DataSource first = Skeleton.newMock(DataSource.class);
    DataSource second = Skeleton.newMock(DataSource.class);
    Hashtable<String, String> properties = new Hashtable<String, String>();
    properties.put("osgi.jndi.service.name", "jdbc/myDataSource");
    bc.registerService(DataSource.class.getName(), first, properties);
    properties = new Hashtable<String, String>();
    properties.put("osgi.jndi.service.name", "jdbc/myDataSource2");
    bc.registerService(DataSource.class.getName(), second, properties);
    DataSource s = (DataSource) ctx.lookup("osgi:service/jdbc/myDataSource");
    assertNotNull(s);
    s = (DataSource) ctx.lookup("osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/myDataSource2)");
    assertNotNull(s);
    // don't care about the method, just need to call something.
    s.isWrapperFor(DataSource.class);
    Skeleton.getSkeleton(second).assertCalled(new MethodCall(DataSource.class, "isWrapperFor", Class.class));
}
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) DataSource(javax.sql.DataSource) 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