Search in sources :

Example 31 with MethodCall

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

Example 32 with MethodCall

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

the class InitialContextTest method testLookupWithICF.

@Test
public void testLookupWithICF() throws NamingException {
    InitialContextFactory icf = Skeleton.newMock(InitialContextFactory.class);
    bc.registerService(new String[] { InitialContextFactory.class.getName(), icf.getClass().getName() }, icf, (Dictionary) new Properties());
    Skeleton.getSkeleton(icf).setReturnValue(new MethodCall(Context.class, "lookup", "/"), Skeleton.newMock(Context.class));
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, icf.getClass().getName());
    props.put(JNDIConstants.BUNDLE_CONTEXT, bc);
    InitialContext ctx = new InitialContext(props);
    Context namingCtx = (Context) ctx.lookup("/");
    assertTrue("Context returned isn't the raw naming context: " + namingCtx, Skeleton.isSkeleton(namingCtx));
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) LdapContext(javax.naming.ldap.LdapContext) Properties(java.util.Properties) InitialContextFactory(javax.naming.spi.InitialContextFactory) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 33 with MethodCall

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

the class InitialContextTest method testLookupWithoutICFButWithURLLookup.

@Test
public void testLookupWithoutICFButWithURLLookup() throws NamingException {
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Context ctx = Skeleton.newMock(Context.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), ctx);
    Skeleton.getSkeleton(ctx).setReturnValue(new MethodCall(Context.class, "lookup", String.class), "someText");
    Properties props = new Properties();
    props.put(JNDIConstants.JNDI_URLSCHEME, "testURL");
    bc.registerService(ObjectFactory.class.getName(), factory, (Dictionary) props);
    props = new Properties();
    props.put(JNDIConstants.BUNDLE_CONTEXT, bc);
    InitialContext initialCtx = new InitialContext(props);
    Object someObject = initialCtx.lookup("testURL:somedata");
    assertEquals("Expected to be given a string, but got something else.", "someText", someObject);
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) LdapContext(javax.naming.ldap.LdapContext) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) Name(javax.naming.Name) Test(org.junit.Test)

Example 34 with MethodCall

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

the class RecursiveBundleTrackerTest method composite.

private CompositeBundle composite(String symbolicName, String version) {
    CompositeBundle cb = Skeleton.newMock(CompositeBundle.class);
    Skeleton cbSkel = Skeleton.getSkeleton(cb);
    cbSkel.setReturnValue(new MethodCall(CompositeBundle.class, "getSymbolicName"), symbolicName);
    cbSkel.setReturnValue(new MethodCall(CompositeBundle.class, "getVersion"), new Version(version));
    return cb;
}
Also used : Version(org.osgi.framework.Version) Skeleton(org.apache.aries.unittest.mocks.Skeleton) CompositeBundle(org.osgi.service.framework.CompositeBundle) MethodCall(org.apache.aries.unittest.mocks.MethodCall)

Example 35 with MethodCall

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

the class RecursiveBundleTrackerTest method setup.

@Before
public void setup() {
    context = Skeleton.newMock(BundleContext.class);
    Skeleton.getSkeleton(context).setReturnValue(new MethodCall(BundleContext.class, "getServiceReference", "org.osgi.service.framework.CompositeBundleFactory"), Skeleton.newMock(ServiceReference.class));
}
Also used : MethodCall(org.apache.aries.unittest.mocks.MethodCall) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Before(org.junit.Before)

Aggregations

MethodCall (org.apache.aries.unittest.mocks.MethodCall)41 Test (org.junit.Test)30 Properties (java.util.Properties)13 BundleContext (org.osgi.framework.BundleContext)13 Hashtable (java.util.Hashtable)12 InitialContext (javax.naming.InitialContext)11 AriesApplication (org.apache.aries.application.management.AriesApplication)10 Context (javax.naming.Context)9 BundleMock (org.apache.aries.mocks.BundleMock)8 Name (javax.naming.Name)7 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)7 ObjectFactory (javax.naming.spi.ObjectFactory)6 Content (org.apache.aries.application.Content)6 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)6 ModelledResource (org.apache.aries.application.modelling.ModelledResource)6 Skeleton (org.apache.aries.unittest.mocks.Skeleton)6 ArrayList (java.util.ArrayList)5 Manifest (java.util.jar.Manifest)5 Reference (javax.naming.Reference)5 ServiceRegistration (org.osgi.framework.ServiceRegistration)5