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