use of javax.naming.InitialContext 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 javax.naming.InitialContext 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));
}
use of javax.naming.InitialContext in project aries by apache.
the class ServiceRegistryContextTest method listRepositoryContents.
/**
* This test checks that we can list the contents of the repository using the
* list method
*
* @throws NamingException
*/
public void listRepositoryContents() throws NamingException {
InitialContext ctx = new InitialContext();
NamingEnumeration<NameClassPair> serviceList = ctx.list("osgi:service/java.lang.Runnable/(rubbish=smelly)");
checkThreadRetrievedViaListMethod(serviceList);
assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
//Now add a second service
registerService(new Thread());
serviceList = ctx.list("osgi:service/java.lang.Runnable/(rubbish=smelly)");
checkThreadRetrievedViaListMethod(serviceList);
checkThreadRetrievedViaListMethod(serviceList);
assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
}
use of javax.naming.InitialContext 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 javax.naming.InitialContext 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");
}
Aggregations