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());
}
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");
}
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);
}
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)"));
}
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());
}
Aggregations